您好,登錄后才能下訂單哦!
小編給大家分享一下如何實(shí)現(xiàn)基于Python socket的端口掃描程序,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
先來看看第一個(gè)端口掃描程序代碼,獲取本機(jī)的IP和端口號(hào):
import socket def get_my_ip(): try: csock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) csock.connect(('8.8.8.8', 80)) (addr, port) = csock.getsockname() csock.close() return addr,port except socket.error: return "127.0.0.1" def int_to_ip(int_ip): return socket.inet_ntoa(struct.pack('I', socket.htonl(int_ip))) def ip_to_int(ip): return socket.ntohl(struct.unpack("I", socket.inet_aton(str(ip)))[0]) (ip,port)=get_my_ip() print "ip=%s port=%d" %(ip,port)
PortScan.py
#!/usr/bin/python # -*- coding: utf-8 -*- import optparse from socket import * from threading import * screenLock = Semaphore(value=1) def connScan(tgtHost, tgtPort): try: connSkt = socket(AF_INET, SOCK_STREAM) connSkt.connect((tgtHost, tgtPort)) connSkt.send('ViolentPython\r\n') results = connSkt.recv(100) screenLock.acquire() print '[+] %d/tcp open' % tgtPort print '[+] ' + str(results) except: screenLock.acquire() print '[-] %d/tcp closed' % tgtPort finally: screenLock.release() connSkt.close() def portScan(tgtHost, tgtPorts): try: tgtIP = gethostbyname(tgtHost) except: print "[-] Cannot resolve '%s': Unknown host" %tgtHost return try: tgtName = gethostbyaddr(tgtIP) print '\n[+] Scan Results for: ' + tgtName[0] except: print '\n[+] Scan Results for: ' + tgtIP setdefaulttimeout(1) for tgtPort in tgtPorts: t = Thread(target=connScan,args=(tgtHost,int(tgtPort))) t.start() 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='string',\ help='specify target port[s] separated by comma') (options, args) = parser.parse_args() tgtHost = options.tgtHost tgtPorts = str(options.tgtPort).split(',') if (tgtHost == None) | (tgtPorts[0] == None): print parser.usage exit(0) portScan(tgtHost, tgtPorts) if __name__ == '__main__': main()
基于Python socket的端口掃描程序
#------------------------------------------------------------------------------- # Name: PortScan # Purpose: 掃描網(wǎng)段主機(jī)的端口開放情況 # Author: Hao Chen # Python3.4 #------------------------------------------------------------------------------- import socket def main(): ip_start=input('請(qǐng)輸入開始IP:(默認(rèn):127.0.0.1)') if ip_start=='': ip_start='127.0.0.1' ip_end='127.0.0.1' else: ip_end=input('請(qǐng)輸入結(jié)束IP:') if ip_end=='': ip_end='127.0.0.1' s=input('請(qǐng)輸入目標(biāo)主機(jī)開始端口:(默認(rèn)掃描常用端口)') if s=='': portList=[21, 22, 23, 25, 80, 135, 137, 139, 445, 1433, 1502, 3306, 3389, 8080, 9015] else: startport=int(s) s=input('請(qǐng)輸入目標(biāo)主機(jī)結(jié)束端口:(默認(rèn):65535)') if s=='': endport=65535 else: endport=int(s) portList=[i for i in range(startport,endport+1)] while 1: #ip_start<ip_end x1=ip_start.rfind('.'); x2=ip_end.rfind('.') if int(ip_start[x1+1:])>int(ip_end[x2+1:]): break; #開始掃描端口 for port in portList: print('正在掃描%s :%d' %(ip_start,port)) try: sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sk.settimeout(10) sk.connect((ip_start,port)) sk.settimeout(None) print('Server %s port %d OK!' % (ip_start,port)) sk.close() #結(jié)果保存在文件中 f=open("IP_Port.txt",'a') f.write(ip_start+' : '+str(port)+'\n') f.close() except Exception: print('Server %s port %d is not connected!' % (ip_start,port)) #更新ip_start i=ip_start.rfind('.') x=int(ip_start[i+1:])+1 ip_start=ip_start[:i+1]+str(x) print('掃描完成,結(jié)果保存在IP_Port.txt文件中') if __name__ == '__main__': main()
以上是“如何實(shí)現(xiàn)基于Python socket的端口掃描程序”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。