Banner Grabbing with Python

A simple banner grabber in Python:

import optparse
from socket import *
 
def banner(targetHost, targetPort):
	try:
		connsocket = socket(AF_INET, SOCK_STREAM)
		connsocket.connect((targetHost, targetPort))
		connsocket.send('Hi therern')
		results = connsocket.recv(100)
		print '' + str(results)
		connsocket.close()
	except:
		print 'error'
def main():
	options = {}
	parser = optparse.OptionParser('usage %prog -H <target host> -P <target port>')
	parser.add_option('-H', '--host', dest='target_host', type='string', help='specify target host')
	parser.add_option('-P', '--port', dest='target_port', type='int', help='specify target port')
 
	(options, args) = parser.parse_args()
	target_host = options.target_host
	target_port = options.target_port
 
	if(target_host == None) | (target_port == None):
		print parser.usage
		exit(0)
 
	banner(target_host, target_port)
 
if __name__ == '__main__':
    main()

Usage:

python.exe tcp_banner_grabbing.py -H 127.0.0.1 -P 21
Categories: Python Tags: , , , , , ,
%d bloggers like this: