Nmap扫描结果xml处理

因需要批量对域名的端口进行扫描后进一步渗透,其他扫描工具域名和ip无法对应,测试起来比较麻烦,因此写了个工具将nmap的xml扫结果处理为csv,csv结果文件字段有:主机名,ip,端口,状态,协议,服务,版本,操作系统类型,其他信息。扫描结果清晰易于管理,方便进行下一步渗透测试。

代码如下:

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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
@author:Chuanwei
@file:nmap_to_csv.py
@time:2019/10/06
"""
"""
使用方法:
nmap扫描输出xml文件:nmap -sS -O -sV -iL test.txt -v -T4 -Pn -oX test.xml
单个:python nmap_to_csv.py test.xml
批量:python nmap_to_csv.py 1.xml 2.xml 3.xml
处理结果输出为csv文件,名称和源文件名称一样。
"""
import xml.etree.ElementTree as ET
import sys

def usage():
print ("使用方法: %s 1.xml 2.xml 3.xml ...... " % __file__)
def parseNmap(filename,out_filename):
try:
tree=ET.parse(filename)
root=tree.getroot()
except Exception as e:

print (e)
return {}
with open(out_filename,"w") as f:
f.write("主机名,ip,端口,状态,协议,服务,版本,操作系统类型,其他信息\n")
for host in root.iter('host'):
if host.find('status').get('state') == 'down':
continue
ip=host.find('address').get('addr',None)
hostname = host.find('hostnames').find('hostname').get('name',None)
if not ip and not hostname:
continue
if host.find('ports').find('port') == None:
output = hostname + "," + ip + ",,,,,,," + "\n"
f.write(output)
else:
for ports in host.iter('port'):
port = ports.get('portid','')
status = ports.find('state').get('state','')
protocol = ports.get('protocol','')
service = ports.find('service').get('name','')
product = ports.find('service').get('product','')
version = ports.find('service').get('version','')
ostype = ports.find('service').get('ostype','')
extrainfo = ports.find('service').get('extrainfo','')
output = hostname + "," + ip + "," + port + "," + status + "," + protocol + "," + service + ","+ version + ","+ ostype + ","+ extrainfo + "\n"
f.write(output)
print(out_filename + "文件已生成!")
def main(args):
for xml_file in args[1:]:
print("处理:" + xml_file)
out_filename = xml_file.strip(".xml")+ ".csv"
parseNmap(xml_file,out_filename)
if __name__ == "__main__":
if len(sys.argv) < 2:
sys.exit(usage())
else:
main(sys.argv)

使用方法

1
2
3
4
nmap扫描输出xml文件:nmap -sS -O -sV -iL test.txt  -v -T4 -Pn -oX test.xml
单个:python nmap_to_csv.py test.xml
批量:python nmap_to_csv.py 1.xml 2.xml 3.xml
处理结果输出为csv文件,名称和源文件名称一样。