Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.60 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3.  
  4. import sys
  5. import re
  6. from HTMLParser import HTMLParser
  7. from optparse import OptionParser
  8.  
  9. #オプション解析
  10. options = OptionParser(usage = '%prog ', description = 'for mksummary(03 sheets)')
  11. options.add_option('-v', '--verbose', default = False, action='store_true', help = 'Verbose mode.')
  12.  
  13. def get_lhost(ipaddr):
  14. """lhostを計算して返す"""
  15. if ipaddr == None:
  16. return None
  17.  
  18. result = []
  19. for i in ipaddr.split("."):
  20. result += [i.zfill(3)]
  21. return ".".join(result)
  22.  
  23.  
  24. class RetinaSummaryParser(HTMLParser):
  25. def __init__(self):
  26. HTMLParser.__init__(self)
  27.  
  28. # 汎用
  29. self.flg_tr = False
  30. self.flg_td = False
  31. self.flg_th = False
  32. self.flg_div = False
  33. self.flg_h2 = False
  34. self.count = 0
  35.  
  36. # 状態判定用フラグ
  37. self.flg_indata = False # ポートのデータが記録されたテーブルを処理中か判定するフラグ
  38. self.flg_getdata = False # 対象のデータが存在するセルに到達したらセット するフラグ
  39. self.flg_port = False # Portsのレポート内を探索してるかを判定するフラグ
  40. self.flg_ipaddr = False
  41. self.flg_ipaddr2 = False # IPアドレスを探すフラグ
  42. self.flg_end = False # データの回収が終わったことを判定するフラグ
  43.  
  44. #データ判別フラグ
  45. self.flg_portnum = False
  46. self.flg_protocol = False
  47. self.flg_name = False
  48. self.flg_stat = False
  49.  
  50. # データ格納用
  51. self.ipaddr = None
  52. self.port = None
  53. self.prot = None
  54. self.name = None
  55. self.stat = None
  56.  
  57. def handle_starttag(self, tag, attrs):
  58. if tag == 'tr':
  59. self.flg_tr = True
  60. if tag == 'td':
  61. self.flg_td = True
  62. if 'class' in dict(attrs):
  63. if dict(attrs)['class'] == 'h2':
  64. self.flg_h2 = True
  65.  
  66. if tag == 'th':
  67. self.flg_th = True
  68. if tag == 'div':
  69. self.flg_div = True
  70.  
  71. if self.flg_port and self.flg_td:
  72. if 'class' in dict(attrs):
  73. #if dict(attrs)['class'] == 'c5': #データの始まり
  74. # self.flg_ipaddr = True
  75. if dict(attrs)['class'] == 'h4': #ポートの概要
  76. self.flg_portnum = True
  77. self.flg_getdata = True
  78. self.count = 0
  79. if dict(attrs)['class'] == 'h5': #ポートの詳細データ
  80. self.flg_getdata = True
  81. self.count += 1
  82.  
  83. if self.flg_ipaddr and self.flg_div:
  84. if 'class' in dict(attrs):
  85. if dict(attrs)['class'] == 'h4 text-primary':
  86. self.flg_ipaddr = False
  87. self.flg_ipaddr2 = True
  88.  
  89. def handle_endtag(self, tag):
  90. if tag == 'tr':
  91. self.flg_tr = False
  92. if tag == 'table':
  93. if self.flg_indata: # データ回収完了フラグが真なら出力
  94. # lhost shost port prot stat name
  95. print '%s,%s,%05d,%s,%s,%s' % (
  96. get_lhost(self.ipaddr),
  97. self.ipaddr,
  98. int(self.port),
  99. self.prot,
  100. self.stat,
  101. self.name,
  102. )
  103. self.flg_indata = False
  104.  
  105. if tag == 'td':
  106. self.flg_td = False
  107. self.flg_h2 = False
  108.  
  109. if tag == 'th':
  110. self.flg_th = False
  111.  
  112. if tag == 'div':
  113. self.flg_div = False
  114.  
  115. def handle_data(self, data):
  116. # レポート内の「Ports」だけ対象にする
  117.  
  118. #if re.match(r'^ポート$', data) and self.flg_td:
  119. # self.flg_port = True
  120. # 他のレポートの内容はすべて無視する
  121. #if re.match(r'^Services$', data) and self.flg_td:
  122. # self.flg_port = False
  123. #if re.match(r'^Users$', data) and self.flg_td:
  124. # self.flg_port = False
  125. #if re.match(r'^ソフトウェア$', data) and self.flg_td:
  126. # self.flg_port = False
  127. #if re.match(r'^監査$', data) and self.flg_td:
  128. # self.flg_port = False
  129. #if re.match(r'^Shares$', data) and self.flg_td:
  130. # self.flg_port = False
  131. #if re.match(r'^概要$', data) and self.flg_td:
  132. # self.flg_port = False
  133. #if re.match(r'^アラート$', data) and self.flg_td:
  134. # self.flg_port = False
  135.  
  136. if self.flg_h2:
  137. if re.match(r'^ポート$', data):
  138. self.flg_port = True
  139. else:
  140. self.flg_port = False
  141.  
  142. # data判別
  143.  
  144. if re.match(r'^ポートタイプ$', data) and self.flg_th:
  145. self.flg_protocol = True
  146.  
  147. if re.match(r'^ポートの状態$', data) and self.flg_th:
  148. self.flg_stat = True
  149.  
  150. #if re.match(r'^解説$', data) and self.flg_th:
  151. if re.match(r'^検出されたプロトコル$', data) and self.flg_th:
  152. self.flg_name = True
  153.  
  154. # IP addr
  155. if re.match(r'^IP アドレス:$', data) and self.flg_div:
  156. self.flg_ipaddr = True
  157.  
  158. # IPアドレス取得
  159. if self.flg_ipaddr2:
  160. self.ipaddr = data.strip()
  161. self.flg_ipaddr2 = False
  162.  
  163. # ポートデータ取得
  164. if self.flg_getdata:
  165. self.flg_getdata = False
  166. if opts.verbose:
  167. print ">>> [%d] : %s, %s, %s" % (self.count, data, self.getpos(), self.get_starttag_text())
  168. if self.flg_portnum: # ポート番号
  169. self.port = data.strip("UDP:").strip("TCP:").strip()
  170. self.flg_indata = True
  171. self.flg_portnum = False
  172. if self.flg_protocol: # プロトコル
  173. self.prot = data.strip()
  174. self.flg_protocol = False
  175. if self.flg_name: # サービス名
  176. #self.name = data.replace("\n","").lstrip("[REMOTE]").lstrip("[LOCAL]").strip()
  177. self.name = data.strip()
  178. self.flg_name = False
  179. if self.flg_stat: # 状態
  180. self.stat = data.strip()
  181. self.flg_stat = False
  182. else:
  183. self.flg_portnum = False
  184. self.flg_protocol = False
  185. self.flg_name = False
  186. self.flg_stat = False
  187.  
  188. if __name__ == '__main__':
  189. opts, args = options.parse_args()
  190.  
  191. if len(sys.argv) <= 1:
  192. print '[E] Give me Retina export summary data files(html).'
  193. sys.exit(1)
  194.  
  195. for filename in sys.argv[1:]:
  196. try:
  197. f = open(filename).read()
  198. except:
  199. continue
  200.  
  201. f = f.replace(',', ' ') # カンマを空白に置き換え
  202. f = f.replace('<BR>', '') # 不要なタグを事前に削除
  203.  
  204. p = RetinaSummaryParser()
  205. p.feed(f)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement