您好,登錄后才能下訂單哦!
一個用python寫的簡單的端口掃描器,python環境為 3.7.0,windows系統
掃描給定主機是否開放了指定的端口
TCP連接掃描是利用TCP的三次握手來確定主機的端口是否開放。確定主機端口開放之后,給端口發送消息,接收端口返回的消息,然后判斷該端口運行的服務。
使用時,-H?參數可以提供主機的域名或者ip地址,-p/-P?寫要掃描的端口,多個端口用逗號分隔
'''
@Author:yw
參考書籍:《python絕技:運用python成為頂級***》
'''
import optparse
from socket import *
import threading
threadlock = threading.Lock() #實例化threadlock對象
def Conn_scan(Host, Port):
try:
conn = socket(AF_INET,SOCK_STREAM)
conn.connect((Host, Port))
#conn.send('ywboy'.encode('utf-8')) #發送測試
#results = conn.recv(100) #接收主機返回的信息
threadlock.acquire() #加鎖
print("[+]%d/tcp Open" % Port)
#print('[+]'+results.decode('utf-8'))
conn.close()
except Exception as e:
threadlock.acquire() #釋放鎖
print('[-]%d/Tcp Closed' % Port)
finally:
threadlock.release()
conn.close()
def Port_scan(Host, Ports):
try:
IP = gethostbyname(Host) ##獲得對應主機的ip地址
except:
print("[-] Cannot resolve '%s':Unknow host" % Host)
return
try:
Name = gethostbyaddr(Host) ##獲得對應主機的信息,返回主機名、主機別名列表、主機IP地址列表
print("\n[+] Scan result for:"+Name[0])
except:
print("\n[+] Scan Results for:"+IP)
setdefaulttimeout(1)
for Port in Ports:
print("Scan port:"+Port)
Conn_scan(Host, int(Port))
def main():
usage = "usage %prog -H <target Host> -p/-P <target ports>"
parse = optparse.OptionParser(usage)
parse.add_option('-H', dest='Host', type='string', help='target Host')
parse.add_option('-p','-P', dest='Ports', type='string', help='SCan Port')
(options, args) = parse.parse_args()
Host = options.Host
Ports = str(options.Ports).split(',')
if (Host==None)|(Ports==None):
print(parse.usage)
exit(0)
Port_scan(Host,Ports)
if __name__ == '__main__':
main()
上述代碼,因為我只是做了端口掃描,所以注釋掉的判斷端口掃描服務的代碼
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。