forked from mrschyte/nmap-converter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
nmap-converter.py
executable file
·96 lines (77 loc) · 4.51 KB
/
nmap-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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python
from libnmap.parser import NmapParser, NmapParserException
from xlsxwriter import Workbook
from datetime import datetime
import os.path
def main(reports, workbook):
summary = workbook.add_worksheet("Summary")
results = workbook.add_worksheet("Results")
row = 1
for reportid, report in enumerate(reports):
fmt_bold = workbook.add_format({"bold": True})
fmt_conf = workbook.add_format()
fmt_conf.set_num_format('0%')
results.autofilter("A1:N1")
results.freeze_panes(1, 0)
results.data_validation("N2:N$1048576", {"validate": "list",
"source": ["Y", "N", "N/A"]})
summary_header = ["Scan", "Command", "Version", "Scan Type", "Started", "Completed", "Hosts Total", "Hosts Up", "Hosts Down"]
summary_body = {"Scan": lambda report: report.basename,
"Command": lambda report: report.commandline,
"Version": lambda report: report.version,
"Scan Type": lambda report: report.scan_type,
"Started": lambda report: datetime.utcfromtimestamp(report.started).strftime("%Y-%m-%d %H:%M:%S (UTC)"),
"Completed": lambda report: datetime.utcfromtimestamp(report.endtime).strftime("%Y-%m-%d %H:%M:%S (UTC)"),
"Hosts Total": lambda report: report.hosts_total,
"Hosts Up": lambda report: report.hosts_up,
"Hosts Down": lambda report: report.hosts_down}
results_header = ["Host", "IP", "Port", "Protocol", "Status", "Service", "Tunnel", "Method", "Confidence", "Reason", "Product", "Version", "Extra", "Flagged", "Notes"]
results_body = {"Host": lambda host, service: next(iter(host.hostnames), ""),
"IP": lambda host, service: host.address,
"Port": lambda host, service: service.port,
"Protocol": lambda host, service: service.protocol,
"Status": lambda host, service: service.state,
"Service": lambda host, service: service.service,
"Tunnel": lambda host, service: service.tunnel,
"Method": lambda host, service: service.service_dict.get("method", ""),
"Confidence": lambda host, service: float(service.service_dict.get("conf", "0")) / 10,
"Reason": lambda host, service: service.reason,
"Host": lambda host, service: next(iter(host.hostnames), ""),
"Product": lambda host, service: service.service_dict.get("product", ""),
"Version": lambda host, service: service.service_dict.get("version", ""),
"Extra": lambda host, service: service.service_dict.get("extrainfo", ""),
"Flagged": lambda host, service: "N/A",
"Notes": lambda host, service: ""}
results_format = {"Confidence": fmt_conf}
print("[+] Processing {}".format(report.summary))
for idx, item in enumerate(summary_header):
summary.write(0, idx, item, fmt_bold)
for idx, item in enumerate(summary_header):
summary.write(1 + reportid, idx, summary_body[item](report))
for idx, item in enumerate(results_header):
results.write(0, idx, item, fmt_bold)
for host in report.hosts:
print("[+] Processing {}".format(host))
for service in host.services:
for idx, item in enumerate(results_header):
results.write(row, idx, results_body[item](host, service), results_format.get(item, None))
row += 1
workbook.close()
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-o", "--output", metavar="XLS", help="path to xlsx output")
parser.add_argument("reports", metavar="XML", nargs="+", help="path to nmap xml report")
args = parser.parse_args()
if args.output == None:
parser.error("Output must be specified")
reports = []
for report in args.reports:
try:
parsed = NmapParser.parse_fromfile(report)
except NmapParserException as ex:
parsed = NmapParser.parse_fromfile(report, incomplete=True)
parsed.basename = os.path.basename(report)
reports.append(parsed)
workbook = Workbook(args.output)
main(reports, workbook)