diff --git a/README.md b/README.md index bab3b41..65d8a9a 100644 --- a/README.md +++ b/README.md @@ -1,33 +1,37 @@ -# Nmap-XML-Parser +# Nmap - xml2csv Converts Nmap XML output to csv file, and other useful functions. Ignores hosts that are down and ports that are not open. +As featured in, Advanced Nmap - Scanning Large Scale Networks: + +https://www.youtube.com/watch?v=okCNbKSdmDA + ## Usage ### Convert Nmap output to csv file -`python3 nmap_xml_parser.py -f nmap_scan.xml -csv nmap_scan.csv` +`python3 xml2csv.py -f nmap_scan.xml -csv nmap_scan.csv` ### Display scan information to the terminal -`python3 nmap_xml_parser.py -f nmap_scan.xml -p` +`python3 xml2csv.py -f nmap_scan.xml -p` ### Display only IP addresses -`python3 nmap_xml_parser.py -f nmap_scan.xml -ip` +`python3 xml2csv.py -f nmap_scan.xml -ip` ### Display IP addresses/ports in URL friendly format > Displays in format http(s)://ipaddr:port if port is a possible web port -`python3 nmap_xml_parser.py -f nmap_scan.xml -pw` +`python3 xml2csv.py -f nmap_scan.xml -pw` ### Display least common open ports > Displays the 10 least common open ports -`python3 nmap_xml_parser.py -f nmap_scan.xml -lc 10` +`python3 xml2csv.py -f nmap_scan.xml -lc 10` ### Display most common open ports > Displays the 10 most common open ports -`python3 nmap_xml_parser.py -f nmap_scan.xml -mc 10` +`python3 xml2csv.py -f nmap_scan.xml -mc 10` ### Display only IP addresses with a specified open port > Displays only IP addresses where port 23 is open -`python3 nmap_xml_parser.py -f nmap_scan.xml -fp 23` +`python3 xml2csv.py -f nmap_scan.xml -fp 23` diff --git a/nmap_xml_parser.py b/xml2csv.py similarity index 86% rename from nmap_xml_parser.py rename to xml2csv.py index e2814b8..a447319 100644 --- a/nmap_xml_parser.py +++ b/xml2csv.py @@ -1,5 +1,7 @@ #!/usr/bin/env python + +# Credit where credit is due... __author__ = 'Jake Miller (@LaconicWolf)' __date__ = '20171220' __version__ = '0.01' @@ -17,7 +19,34 @@ from collections import Counter from time import sleep +def saveallscript(array_script): + allscriptid = [] + alltscriptout = [] + size1 = len(array_script) + #print(size1) + for i in range(0,size1,1): + #print(str(i)) + try: + script_id = array_script.findall('script')[i].attrib['id'] + except (IndexError, KeyError): + script_id = '' + try: + script_output = array_script.findall('script')[i].attrib['output'] + except (IndexError, KeyError): + script_output = '' + + #print(script_id) + #print(script_output) + allscriptid.append(script_id)#mio + alltscriptout.append(script_output)#mio + + return allscriptid,alltscriptout + + + + def get_host_data(root): + #print(root.text) """Traverses the xml tree and build lists of scan information and returns a list of lists. """ @@ -55,6 +84,7 @@ def get_host_data(root): try: port_element = host.findall('ports') ports = port_element[0].findall('port') + #print(ports) for port in ports: port_data = [] @@ -78,22 +108,35 @@ def get_host_data(root): servicefp = port.findall('service')[0].attrib['servicefp'] except (IndexError, KeyError): servicefp = '' + """ try: - script_id = port.findall('script')[0].attrib['id'] + #print(len(port.findall('script'))) + saveallscript(port) + for scriptnum in range(0,len(port.findall('script')),1): + script_id = port.findall('script')[scriptnum].attrib['id'] + #scriptidfor.append(script_id)#mio except (IndexError, KeyError): script_id = '' try: script_output = port.findall('script')[0].attrib['output'] + #print(script_output) except (IndexError, KeyError): script_output = '' + """ # Create a list of the port data - port_data.extend((ip_address, host_name, os_name, + #print(scriptfor) + allscriptid,alltscriptout = saveallscript(port) + for i in range(0,len(alltscriptout),1): + host_data.append((ip_address, host_name, os_name, proto, port_id, service, product, - servicefp, script_id, script_output)) + servicefp, allscriptid[i], alltscriptout[i])) + #host_data.append(port_data) + #print(host_data[0]) + # Add the port data to the host data - host_data.append(port_data) + # If no port information, just create a list of host information except IndexError: @@ -108,6 +151,7 @@ def parse_xml(filename): containing the scan data for a host or hosts.""" try: tree = etree.parse(filename) + #print(tree) except Exception as error: print("[-] A an error occurred. The XML may not be well formed. " "Please review the error and try again: {}".format(error)) @@ -327,4 +371,4 @@ def main(): print("\n[-] Please choose an output option. Use -csv, -ip, or -p\n") exit() csv_name = args.csv - main() \ No newline at end of file + main()