Asynchronous scanning

We can perform asynchronous scans using the PortScannerAsync() class. In this case, when performing the scan we can indicate an additional callback parameter where we define the return function, which would be executed at the end of the scan:

import nmap

nmasync = nmap.PortScannerAsync()

def callback_result(host, scan_result):
print host, scan_result

nmasync.scan(hosts='127.0.0.1', arguments='-sP', callback=callback_result)
while nmasync.still_scanning():
print("Waiting >>>")
nmasync.wait(2)

In this way, we can define a callback function that is executed whenever Nmap has a result for the machine we are analyzing.

The following script allows us to perform a scan with Nmap asynchronously so that the target and port are requested by input parameters. What the script has to do is perform a scan in the MySQL port (3306) asynchronously and execute the Nmap scripts available for the MySQL service.

To test it, we can run it on the virtual machine, Metasploitable2, for which port 3306 is open, in addition to being able to execute Nmap scripts and obtain additional information about the MySQL service that is running on that vm.

You can find the following code in the filename: NmapScannerAsync.py:

import optparse, nmap
import json
import argparse

def callbackMySql(host, result):
try:
script = result['scan'][host]['tcp'][3306]['script']
print "Command line"+ result['nmap']['command_line']
for key, value in script.items():
print 'Script {0} --> {1}'.format(key, value)
except KeyError:
# Key is not present
pass

class NmapScannerAsync:

def __init__(self):
self.nmsync = nmap.PortScanner()
self.nmasync = nmap.PortScannerAsync()

def scanning(self):
while self.nmasync.still_scanning():
self.nmasync.wait(5)

This is the method that checks the port passed as a parameter and launches Nmap scripts related with MySQL in an asynchronous way:

def nmapScan(self, hostname, port):
try:
print "Checking port "+ port +" .........."
self.nmsync.scan(hostname, port)
self.state = self.nmsync[hostname]['tcp'][int(port)]['state']
print " [+] "+ hostname + " tcp/" + port + " " + self.state
#mysql
if (port=='3306') and self.nmsync[hostname]['tcp'][int(port)]['state']=='open':
print 'Checking MYSQL port with nmap scripts......'
#scripts for mysql:3306 open
print 'Checking mysql-audit.nse.....'
self.nmasync.scan(hostname,arguments="-A -sV -p3306 --script mysql-audit.nse",callback=callbackMySql)
self.scanning()

print 'Checking mysql-brute.nse.....'
self.nmasync.scan(hostname,arguments="-A -sV -p3306 --script mysql-brute.nse",callback=callbackMySql)
self.scanning()

print 'Checking mysql-databases.nse.....'
self.nmasync.scan(hostname,arguments="-A -sV -p3306 --script mysql-databases.nse",callback=callbackMySql)
self.scanning()

print 'Checking mysql-databases.nse.....'
self.nmasync.scan(hostname,arguments="-A -sV -p3306 --script mysql-dump-hashes.nse",callback=callbackMySql)
self.scanning()

print 'Checking mysql-dump-hashes.nse.....' self.nmasync.scan(hostname,arguments="-A -sV -p3306 --script mysql-empty-password.nse",callback=callbackMySql)
self.scanning()

print 'Checking mysql-enum.nse.....'
self.nmasync.scan(hostname,arguments="-A -sV -p3306 --script mysql-enum.nse",callback=callbackMySql)
self.scanning()

print 'Checking mysql-info.nse".....'
self.nmasync.scan(hostname,arguments="-A -sV -p3306 --script mysql-info.nse",callback=callbackMySql)
self.scanning()

print 'Checking mysql-query.nse.....'
self.nmasync.scan(hostname,arguments="-A -sV -p3306 --script mysql-query.nse",callback=callbackMySql)
self.scanning()

print 'Checking mysql-users.nse.....'
self.nmasync.scan(hostname,arguments="-A -sV -p3306 --script mysql-users.nse",callback=callbackMySql)
self.scanning()

print 'Checking mysql-variables.nse.....'
self.nmasync.scan(hostname,arguments="-A -sV -p3306 --script mysql-variables.nse",callback=callbackMySql)
self.scanning()

print 'Checking mysql-vuln-cve2012-2122.nse.....'
self.nmasync.scan(hostname,arguments="-A -sV -p3306 --script mysql-vuln-cve2012-2122.nse",callback=callbackMySql)
self.scanning()

except Exception,e:
print str(e)
print "Error to connect with " + hostname + " for port scanning"
pass


This is our main program for requesting targets and ports as parameters, and calling the nmapScan(ip,port) function for each port:

if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Nmap scanner async')
# Main arguments
parser.add_argument("-target", dest="target", help="target IP / domain", required=True)
parser.add_argument("-ports", dest="ports", help="Please, specify the target port(s) separated by comma[80,8080 by default]", default="80,8080")
parsed_args = parser.parse_args()
port_list = parsed_args.ports.split(',')
ip = parsed_args.target
for port in port_list:
NmapScannerAsync().nmapScan(ip, port)

Now we are going to execute NmapScannerAsync with target and ports parameters:


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.139.168