1- import io
1+ """
2+
3+ *********************************************************************************************
4+ * NOTICE FROM AUTOSPLOIT DEVELOPERS *
5+ *********************************************************************************************
6+ * this is basically an exact copy of *
7+ * `https://github.com/komand/python-nmap/blob/master/nmap/nmap.py` that has been modified *
8+ * to better fit into autosploits development. There has been very minimal changes to it *
9+ * and it still basically functions the exact same way *
10+ *********************************************************************************************
11+
12+
13+ ORIGINAL INFO:
14+ --------------
15+ nmap.py - version and date, see below
16+ Source code : https://bitbucket.org/xael/python-nmap
17+ Author :
18+ * Alexandre Norman - norman at xael.org
19+ Contributors:
20+ * Steve 'Ashcrow' Milner - steve at gnulinux.net
21+ * Brian Bustin - brian at bustin.us
22+ * old.schepperhand
23+ * Johan Lundberg
24+ * Thomas D. maaaaz
25+ * Robert Bost
26+ * David Peltier
27+ Licence: GPL v3 or any later version for python-nmap
28+ This program is free software: you can redistribute it and/or modify
29+ it under the terms of the GNU General Public License as published by
30+ the Free Software Foundation, either version 3 of the License, or
31+ any later version.
32+ This program is distributed in the hope that it will be useful,
33+ but WITHOUT ANY WARRANTY; without even the implied warranty of
34+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35+ GNU General Public License for more details.
36+ You should have received a copy of the GNU General Public License
37+ along with this program. If not, see <http://www.gnu.org/licenses/>.
38+ **************
39+ IMPORTANT NOTE
40+ **************
41+ The Nmap Security Scanner used by python-nmap is distributed
42+ under it's own licence that you can find at https://svn.nmap.org/nmap/COPYING
43+ Any redistribution of python-nmap along with the Nmap Security Scanner
44+ must conform to the Nmap Security Scanner licence
45+
46+ __author__ = 'Alexandre Norman (norman@xael.org)'
47+ __version__ = '0.6.2'
48+ __last_modification__ = '2017.01.07'
49+ """
50+
251import os
3- import re
4- import csv
5- import sys
652import shlex
753import subprocess
854
955from xml .etree import ElementTree
10- from multiprocessing import Process
1156
1257import lib .jsonize
1358import lib .errors
@@ -45,7 +90,11 @@ def do_scan(host, nmap_path, ports=None, arguments=None):
4590 nmap_path , '-oX' , '-' , host ,
4691 '-p ' + ports if ports is not None else "" ,
4792 ] + arguments_list
48- lib .output .info ("launching nmap scan against {} ({})" .format (host , " " .join (launch_arguments )))
93+ to_launch = []
94+ for item in launch_arguments :
95+ if not item == "" :
96+ to_launch .append (item )
97+ lib .output .info ("launching nmap scan against {} ({})" .format (host , " " .join (to_launch )))
4998 process = subprocess .Popen (
5099 launch_arguments , bufsize = 10000 , stdin = subprocess .PIPE ,
51100 stdout = subprocess .PIPE , stderr = subprocess .PIPE
@@ -127,24 +176,24 @@ def parse_xml_output(output, warnings, error):
127176 results ['nmap_scan' ][host ]['addresses' ] = addresses
128177 results ['nmap_scan' ][host ]['vendors' ] = vendors
129178
130- print results ;exit (1 )
131-
132179 for status in attempted_host .findall ('status' ):
133- results ['nmap_scan' ][attempted_host ]['status' ] = {
180+ results ['nmap_scan' ][host ]['status' ] = {
134181 'state' : status .get ('state' ),
135182 'reason' : status .get ('reason' )
136183 }
137184 for uptime in attempted_host .findall ('uptime' ):
138- results ['nmap_scan' ][attempted_host ]['uptime' ] = {
185+ results ['nmap_scan' ][host ]['uptime' ] = {
139186 'seconds' : uptime .get ('seconds' ),
140187 'lastboot' : uptime .get ('lastboot' )
141188 }
142189 for discovered_port in attempted_host .findall ('ports/port' ):
143190 protocol = discovered_port .get ('protocol' )
144191 port_number = discovered_port .get ('portid' )
145- port_state = discovered_port .find ('state' ).get ('reason' )
192+ port_state = discovered_port .find ('state' ).get ('state' )
193+ port_reason = discovered_port .find ('state' ).get ('reason' )
146194
147- # damn I didn't even know you could do this!
195+ # this is actually a thing!!
196+ name = discovered_config = discovered_version = extra_information = discovered_product = stuff = ""
148197 for discovered_name in discovered_port .findall ('service' ):
149198 name = discovered_name .get ('name' )
150199 if discovered_name .get ('product' ):
@@ -153,4 +202,17 @@ def parse_xml_output(output, warnings, error):
153202 discovered_version = discovered_name .get ('version' )
154203 if discovered_name .get ('extrainfo' ):
155204 extra_information = discovered_name .get ('extrainfo' )
156- print results
205+ if discovered_name .get ('conf' ):
206+ discovered_config = discovered_name .get ('conf' )
207+
208+ for other_stuff in discovered_name .findall ('cpe' ):
209+ stuff = other_stuff .text
210+ if protocol not in results ['nmap_scan' ][host ].keys ():
211+ results ['nmap_scan' ][host ][protocol ] = list ()
212+ results ['nmap_scan' ][host ][protocol ].append ({
213+ 'port' : port_number , 'state' : port_state , 'reason' : port_reason ,
214+ 'name' : name , 'product' : discovered_product , 'version' : discovered_version ,
215+ 'extrainfo' : extra_information , 'conf' : discovered_config , 'cpe' : stuff
216+ })
217+
218+ return results
0 commit comments