您好,登錄后才能下訂單哦!
小編給大家分享一下如何實現基于Python socket的端口掃描程序,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
先來看看第一個端口掃描程序代碼,獲取本機的IP和端口號:
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: 掃描網段主機的端口開放情況 # Author: Hao Chen # Python3.4 #------------------------------------------------------------------------------- import socket def main(): ip_start=input('請輸入開始IP:(默認:127.0.0.1)') if ip_start=='': ip_start='127.0.0.1' ip_end='127.0.0.1' else: ip_end=input('請輸入結束IP:') if ip_end=='': ip_end='127.0.0.1' s=input('請輸入目標主機開始端口:(默認掃描常用端口)') if s=='': portList=[21, 22, 23, 25, 80, 135, 137, 139, 445, 1433, 1502, 3306, 3389, 8080, 9015] else: startport=int(s) s=input('請輸入目標主機結束端口:(默認: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() #結果保存在文件中 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('掃描完成,結果保存在IP_Port.txt文件中') if __name__ == '__main__': main()
以上是“如何實現基于Python socket的端口掃描程序”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。