forked from spyr0-sec/helper-scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nmapMerge.py
157 lines (134 loc) · 4.38 KB
/
nmapMerge.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env python3
####################################################################################
#
# nMapMerge.py
#
# Description
# Combine nMap xml files into one XML
#
# Example
# python nMapMerge.py -f nmap_scan.xml
# python nMapMerge.py -f ./nMap/
#
# Author:
# Hue B. Solutions LLC, CBHue
#
#
####################################################################################
import os
import re
import time
import logging
import xml.etree.ElementTree as ET
from argparse import ArgumentParser
from xml.etree.ElementTree import ParseError
def merge_nMap(xmlFile,mf):
HOSTS = 0
with open(mf, mode = 'a', encoding='utf-8') as mergFile:
with open(xmlFile) as f:
nMapXML = ET.parse(f)
for host in nMapXML.findall('host'):
HOSTS = HOSTS + 1
cHost = ET.tostring(host, encoding='unicode', method='xml')
mergFile.write(cHost)
mergFile.flush()
return HOSTS
def addHeader(f):
nMap_Header = '<?xml version="1.0" encoding="UTF-8"?>'
nMap_Header += '<!DOCTYPE nmaprun>'
nMap_Header += '<?xml-stylesheet href="file:///usr/share/nmap/nmap.xsl" type="text/xsl"?>'
nMap_Header += '<!-- Nmap Merged with nMapMergER.py https://github.com/CBHue/nMapMergER -->'
nMap_Header += '<nmaprun scanner="nmap" args="nmap -iL hostList.txt" start="1" startstr="https://github.com/CBHue/nMapMerge/nMapMerge.py" version="7.70" xmloutputversion="1.04">'
nMap_Header += '<scaninfo type="syn" protocol="tcp" numservices="1" services="1"/>'
nMap_Header += '<verbose level="0"/>'
nMap_Header += '<debugging level="0"/>'
mFile = open(f, "w")
mFile.write(nMap_Header)
mFile.close()
def addFooter(f, h):
nMap_Footer = '<runstats><finished time="1" timestr="Wed Sep 0 00:00:00 0000" elapsed="0" summary="Nmap done at Wed Sep 0 00:00:00 0000; ' + str(h) + ' IP address scanned in 0.0 seconds" exit="success"/>'
nMap_Footer += '</runstats>'
nMap_Footer += '</nmaprun>'
mFile = open(f, "a")
mFile.write(nMap_Footer)
mFile.close()
def htmlER(mergeFile):
import os
cmd = '/usr/bin/xsltproc'
if os.path.isfile(cmd):
out = re.sub(r'.xml', '.html', mergeFile)
cmd = cmd + " -o " + out + " " + mergeFile
os.system(cmd)
print ("Output HTML File:", os.path.abspath(out))
else:
print(cmd, "does not exits")
#
# If you want to use this as a module you need to pass a set of nmap xmls
#
# nmapSET = set()
# nmapSET.add('/nmap-Dir/nmap_10.10.10.10.xml')
#
# Then call the main function passing the set:
# main_nMapMerger(nmapSET)
#
def main_nMapMerger(xmlSet):
HOSTS = 0
# Check to ensute we have work to do
if not xmlSet:
print("No XML files were found ... No work to do")
exit()
# Create the Merged filename
from datetime import datetime
dtNow = datetime.now()
dt = re.sub(r"\s+", '-', str(dtNow))
dt = re.sub(r":", '-', str(dt))
mergeFile = "nMap_Merged_" + dt + ".xml"
# Add Header to mergefile
addHeader(mergeFile)
for xml in xmlSet:
if xml.endswith('.xml'):
logging.debug("Parsing: %r", xml)
H = merge_nMap(xml,mergeFile)
HOSTS = HOSTS + H
# Add Footer to mergefile
addFooter(mergeFile, HOSTS)
print('')
print ("Output XML File:", os.path.abspath(mergeFile))
# Convert merged XML to html
htmlER(mergeFile)
if __name__ == "__main__":
import sys
if sys.version_info <= (3, 0):
sys.stdout.write("This script requires Python 3.x\n")
sys.exit(1)
parser = ArgumentParser()
parser.add_argument("-f", "--file", dest="filename", help="parse FILE", metavar="FILE")
parser.add_argument("-d", "--dir", dest="directory", help="Parse all xml in directory", metavar="DIR")
parser.add_argument("-q", "--quiet", dest="verbose", action="store_false", default=True, help="don't print status messages to stdout")
args = parser.parse_args()
s = set()
if args.verbose:
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
print('Debug On')
if args.filename is not None:
f = args.filename
if f.endswith('.xml'):
logging.debug("Adding: %r", f)
s.add(f)
elif args.directory is not None:
if os.path.isdir(args.directory):
path = args.directory
for f in os.listdir(path):
# For now we assume xml is nMap
if f.endswith('.xml'):
fullname = os.path.join(path, f)
logging.debug("Adding: %r", fullname)
s.add(fullname)
else:
logging.warn("Not a directory: %r", args.directory)
else :
print ("usage issues =(")
parser.print_help()
exit()
# Pass set of xml files to main
main_nMapMerger(s)