Port and Vulnerability Scanner

Port Scanner

#!/usr/bin/python

from socket import *
import optparse
from threading import *

# We connect to the ports, AF_INET is for ipv4 addresses and SOCK_STREAM for tcp
def connectionScan(targetHost, targetPort):
	try:
		sock = socket(AF_INET, SOCK_STREAM)
		sock.connect((targetHost,targetPort))
		print('[+] %d/tcp open' %targetPort)
	except:
		print ('[-] %d/tcp closed' %targetPort)
	finally:
		sock.close()

# We make sure that with both IP or whatever.com we can analyse it		
def portScan(targetHost,targetPorts):
	try:
		targetIP = gethostbyname(targetHost)
	except:
		print ('Unknow Host %s ' %targetHost)
	try:
		targetName = gethostbyaddr(targetIP)
		print ('[+] Scan Results for: ' + targetName[0])
	except:
		print ('[+] Scan Results for: ' + targetIP)
	setdefaulttimeout(1)
	for targetPort in targetPorts:
		thread = Thread(target=connectionScan, args=(targetHost, int(targetPort))) 
		thread.start()

# We explain the usage and execute the above functions			
def main():
	parser = optparse.OptionParser('Usage of program: ' + '-H <target host> -p <target port>')
	parser.add_option('-H', dest='targetHost', type='string', help='specify target host')
	parser.add_option('-p', dest='targetPort', type='string', help='specify target ports separated by comma')
	(options, args) = parser.parse_args()
	targetHost = options.targetHost
	targetPorts = str(options.targetPort).split(',')
	if (targetHost == None) | (targetPorts[0] == None):
		print (parser.usage)
		exit(0)
	portScan(targetHost,targetPorts)

if __name__ == '__main__':
	main()

Vulnerability scanner

Last updated