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
|
# Simple port scanner which uses multiprocessing
import socket
import argparse
from multiprocessing import Pool, cpu_count
from datetime import datetime
# Command line argument parsing stuff
parser = argparse.ArgumentParser()
parser.add_argument('hosts', help = "Taget host(s) to scan" + \
" if you have more then one host to scan seperate them by spaces", nargs = '+')
parser.add_argument('-T', dest = 'scanType', help = "Type of scan to do", \
choices = ['port', 'banner'], required = True)
parser.add_argument('-P', dest = 'numPorts', help = "Number of ports to scan", \
choices = ['all', 'common'], default = "common")
args = parser.parse_args()
def portScan(host, port):
try:
portSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
portSocket.settimeout(0.5)
portSocket.connect((host, port))
return "[+] Port %d\t= [OPEN]" % port
except:
pass
finally:
portSocket.close()
def bannerScan(host, port):
try:
bannerSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
bannerSocket.settimeout(1)
bannerSocket.connect((host, port))
result = bannerSocket.recv(100)
return "[+] Port %d\t = %s" % (port, str(result))
except:
pass
finally:
bannerSocket.close()
def main():
startTime = datetime.now()
# Determine the type of scan to run
if args.scanType.lower() == "port":
scan = portScan
else:
scan = bannerScan
# Determine what ports to scan
if args.numPorts.lower() == "common":
ports = [i for i in range(1, 1025)]
else:
ports = [i for i in range(1, 65536)]
# Setup the worker pool.
pool = Pool(processes = cpu_count())
# Run the scans on all the entered hosts
print '\n\n[*] Scanning Hosts...\n'
for host in args.hosts:
results = [pool.apply_async(scan, (host, port)) for port in ports]
print '[*] Results for %s' % host
for result in filter(lambda i : i.get() != None, results):
print result.get()
endTime = datetime.now()
print "\nTotal time to scan: " + str(endTime - startTime)
if __name__ == '__main__':
main()
|