being a warrior is not a simple thing, it is an endless struggle that lasts until the last moment of our lives. No life down is a warrior, like no life down is doomed mediocre, is we let ourselves become such or that!
--Natsume Stone
Design ideas:
Call Optparse. Optionparse () method, build option parser, accept host name (or IP address), scan port list two parameters. Build two functions Portscan and Connscan,portscan resolve the host name to an IP address, and then enumerate each port in the port list using the Connscan function to attempt to connect to the host and print the information for the scanned port.
Main function Code:
def main(): parser = optparse.OptionParser(‘usage %prog -H <target host> -p <target port>‘) parser.add_option(‘-H‘,dest=‘tgtHost‘,type=‘string‘, help=‘specify target host‘) parser.add_option(‘-p‘,dest=‘tgtPort‘,type=‘int‘, help=‘specify target port‘) (options,args) = parser.parse_args() tgtHost = options.tgtHost tgtPort = options.tgtPort args.append(tgtPort) if (tgtHost == None) | (tgtPort == None): print(parser.usage) exit(0) portScan(tgtHost,args)
Portscann function Code:
def portScan(tgtHost,tgtPorts): try: tgtIP = socket.gethostbyname(tgtHost) except: print("[-]Cannot resolve ‘%s‘:Unkown host" % tgtHost) return try: tgtName = socket.gethostbyaddr(tgtIP) print(‘\n[+]Scan Result for:‘+ tgtName[0]) except: print(‘\n[+]Scan Result for:‘+ tgtIP) socket.setdefaulttimeout(1) for tgtPort in tgtPorts: print(‘Scanning port‘ + str(tgtPort)) connScan(tgtHost,int(tgtPort))
Connscan function Code:
def connScan(tgtHost,tgtPort): try: connSkt = socket.socket(socket.AF_INET,socket.SOCK_STREAM) connSkt.connect((tgtHost,tgtPort)) connSkt.send(‘ViolenPython\r\n‘) results = connSkt.recv(100) print(‘[+]%d/tcp open‘ % tgtPort) print(‘[+]‘+str(results)) connSkt.close() except: print(‘[-]%d/tcp closed‘ % tgtPort)
1. Python penetration test-scan port (note)