#import sockect library
import socket
import sys
import os

#get a banner when connecting to a port
def getBanner(ip,port):
	try:
		socket.setdefaulttimeout(2)
		#instantiate socket class
		s=socket.socket()
		#try to connect to the ftp's server
		s.connect((ip,port))
		banner=s.recv(1024)
		return banner
	except:
		return False

#check some vulnerable banners
def checkVulns(banner,fileName):
	#open the banners file in read mode
	file=open(fileName,"r")
	#get all file lines as a list and loop through them
	for line in file.readlines():
		#strip off the newline character and compare with the banner message
       		if line.rstrip("\n") in banner :
			print "[+]looks like this service is vulnerable :)"
	file.close()

#perform some validations for the received file name 
def getFileName():
	#validate file name parameter from command line
	if len( sys.argv ) < 2 :
		print "hey give me the file name for banners"
		sys.exit()

	#validate if the file name is a real file
	if not os.path.isfile( sys.argv[1] ) :
		print "%s doesn't exists" % sys.argv[1]
		sys.exit()

	#check reading permissions
	if not os.access( sys.argv[1], os.R_OK ) :
		print "Can't read that file"
		sys.exit()

	#if pass all validations, return the file name 
	return sys.argv[1]

#program starts here!!
def main():
	#get the file name and perform some validations
	fileName=getFileName()
	#list with some ports
	portList=[ 21,22,23,25,80,445,3306,5432 ]
	#loop through ips range
	for ipNum in range(64,66):
		#concatenate the ip address
		ip="192.168.1."+str(ipNum)
		#loop through each port, and check if we got some info
		for port in portList:
			print ""
			#output the current ip and port for the banner
			print "[+]"+ip+":"+str(port)

			#try to connect to the machine
			banner=getBanner(ip,port)
			#check if we got some banner
			if banner:
				#output the banner message from the service
				print banner
				#check if the current banner corresponds to a vulnerable version 
				checkVulns(banner,fileName)


#auto load main method
if __name__ == '__main__':
	main()
