@@ -0,0 +1,139 @@
#!/usr/bin/python
import xml .etree .ElementTree as ET
import subprocess
import time
import os
#http://xael.org/pages/python-nmap-en.html
import nmap
def parseMasscanReport (path ):
hostsPorts = {}
listPorts = []
try :
tree = ET .parse (path )
root = tree .getroot ()
for host in root .findall ('host' ):
for ip in host .findall ('address' ):
valueHost = ip .attrib
#Get IP address
addr = valueHost ['addr' ]
for ports in host .findall ('ports' ):
for port in ports .findall ('port' ):
valuePort = port .attrib
#Get port number
portid = int (valuePort ['portid' ])
listPorts .append (portid )
hostsPorts [addr ] = listPorts
return hostsPorts
except :
return hostsPorts
def getReportDirectory (toolName ):
#Get current time %d%m%y-%H%M%S
scanTime = time .strftime ("%d%m%y" ) + "-" + time .strftime ("%H%M%S" )
reportPath = os .getcwd () + "/" + toolName + "-report-" + scanTime + "/"
#Create report directory
os .mkdir (reportPath )
return reportPath
def masscanExecute (host ,path ):
#masscan arguments
ports = "-p0-65535"
rate = "2500"
fileName = path + "masscan-" + host + ".xml"
print "[+] Masscan for host: " + str (host )
#Execute masscan
p1 = subprocess .Popen (['masscan' ,host ,ports ,'--max-rate' ,rate ,'-oX' ,fileName ],stdout = subprocess .PIPE )
output = p1 .communicate ()
def nmapExecute (scanResult ,path ,protocol ):
#nmap arguments
nmapArgs = ""
if protocol == "tcp" :
nmapArgs = "-PN -sS -sV -sC -T4 -p"
elif protocol == "udp" :
nmapArgs = "-PN -sU -sV"
nm = nmap .PortScanner ()
fileNameCSV = path + "nmap-report" + ".csv"
fileCSV = open (fileNameCSV , "wb" )
#Get host and ports
for result in scanResult :
for host ,ports in result .iteritems ():
args = ""
if protocol == "tcp" :
args = nmapArgs + ',' .join (map (str , ports ))
print "[+] nmap tcp scan for " + host + "..."
elif protocol == "udp" :
print "[+] nmap udp scan for " + host + "..."
args = nmapArgs
#Execute nmap
results = nm .scan (host , arguments = args )
#Create CSV nmap report
fileCSV .write (nm .csv ())
fileName = path + "nmap-" + host + ".xml"
fileXML = open (fileName , "wb" )
#Create XML nmap report
fileXML .write (nm .get_nmap_last_output ())
fileXML .close ()
#Create HTML nmap report
subprocess .call (['xsltproc' ,fileName ,'-o' ,fileName [:- 4 ]+ ".html" ])
print "[-] Finish nmap for " + host + "..."
print "[-] " + str (nm .scaninfo ())
fileCSV .close ()
if __name__ == "__main__" :
#List target hosts
fileHosts = "hosts.txt"
#Create directory masscan report
currentPath = getReportDirectory ("masscan" )
file = open (fileHosts ,"r" )
#Read targets
for line in file :
host = line .rstrip ('\n ' )
#Execute masscan
masscanExecute (host ,currentPath )
#List XML masscan reports
dirs = os .listdir (currentPath )
targets = []
for f in dirs :
path = currentPath + f
#Parse XML masscan report
target = parseMasscanReport (path )
if len (target .keys ()) > 0 :
targets .append (target )
currentPath = ""
print targets
#Create directory nmap report for tcp scan
currentPath = getReportDirectory ("nmap-tcp" )
#Execute nmap tcp scan
nmapExecute (targets ,currentPath ,"tcp" )
currentPath = ""
#Create directory nmap report for udp scan
#Execute nmap udp scan
currentPath = getReportDirectory ("nmap-udp" )
nmapExecute (targets ,currentPath ,"udp" )
file .close ()