编写masscan报告转换脚本

由于nmap扫描比较慢,有时候需要使用masscan对大段ip进行快速扫描。为了后续方便数据处理,往往需要将数据以xls的形式进行统计,但是masscan只支持xml,json,list等格式输出,并不支持直接输出xls格式。最近有正好这个需求,于是写了个小脚本来转换一下。

一、编码

file: masscan-report-converter.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#coding=utf-8
import os
import sys
import time
import argparse
import xml.dom.minidom
import xlsxwriter
from xlsxwriter import Workbook

'''
author: c0ny1
date: 2018-09-28 18:23
'''

def convert_masscan_report(xml_path,xls_path):
workbook = xlsxwriter.Workbook(xls_path)
worksheet = workbook.add_worksheet('Scan info')
worksheet.autofilter("A1:H1") #设置过滤
worksheet.freeze_panes(1, 0) #冻结窗格

worksheet.lastrow = 0
summary_header = ["addr", "port", "state", "protocol", "addrtype", "reason", "reason_ttl", "scan_endtime"]
for idx, item in enumerate(summary_header):
worksheet.write(0, idx, item,workbook.add_format({"bold": True}))
worksheet.lastrow += 1

DOMTree = xml.dom.minidom.parse(xml_path)
data = DOMTree.documentElement
nodelist = data.getElementsByTagName('host')
host_info = {}
for node in nodelist:
scan_endtime = node.getAttribute('endtime')
scan_endtime = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(int(scan_endtime)))
address_node = node.getElementsByTagName('address')
addrtype = address_node[0].getAttribute('addrtype')
addr = address_node[0].getAttribute('addr')
port_node = node.getElementsByTagName('port')
for port in port_node:
protocol = port.getAttribute('protocol')
portid = port.getAttribute('portid')
state_element = port.getElementsByTagName('state')
state = state_element[0].getAttribute('state')
reason = state_element[0].getAttribute('reason')
reason_ttl = state_element[0].getAttribute('reason_ttl')
print '[+] | %s | %s | %s | %s | %s | %s | %s | %s |' % (addr,portid,state,protocol,addrtype,reason,reason_ttl,scan_endtime)
scan_info = [addr,portid,state,protocol,addrtype,reason,reason_ttl,scan_endtime]
for i in range(0,len(scan_info)):
worksheet.write(worksheet.lastrow, i, scan_info[i])
worksheet.lastrow += 1
workbook.close()

if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--input", metavar="XML", help="path to xml input")
parser.add_argument("-o", "--output", metavar="XLS", help="path to xlsx output")

if len(sys.argv) == 1:
sys.argv.append('-h')

args = parser.parse_args()

if args.input:
xml_path = args.input
else :
exit('[*] please use -i set xml path!')

if os.path.lexists(xml_path) == False:
exit('[*] %s does not exist!',xml_path)

if args.output:
xls_path = args.output
else:
xls_path = './masscan_report.xls'

convert_masscan_report(xml_path,xls_path)

目前脚本已经收集到我的WorkScript项目中,地址如下:

https://github.com/c0ny1/WorkScripts/tree/master/masscan-report-converter

二、使用步骤

1.使用masscan进行扫描,扫描结果以xml保存
1
masscan.exe -p21,22,23,80,7001,5900 10.0.0.0/8 --rate=100000 -oX scan_result.xml
2.使用上面写的脚本转换出xls格式的报告
1
masscan-report-converter.py -i scan_result.xml -o scan_result.xls

最终效果如下:

图1-脚本转换后的报告