您好,登錄后才能下訂單哦!
一個(gè)用python寫(xiě)的簡(jiǎn)單的端口掃描器,python環(huán)境為 3.7.0,windows系統(tǒng)
掃描給定主機(jī)是否開(kāi)放了指定的端口
TCP連接掃描是利用TCP的三次握手來(lái)確定主機(jī)的端口是否開(kāi)放。確定主機(jī)端口開(kāi)放之后,給端口發(fā)送消息,接收端口返回的消息,然后判斷該端口運(yùn)行的服務(wù)。
使用時(shí),-H?參數(shù)可以提供主機(jī)的域名或者ip地址,-p/-P?寫(xiě)要掃描的端口,多個(gè)端口用逗號(hào)分隔
'''
@Author:yw
參考書(shū)籍:《python絕技:運(yùn)用python成為頂級(jí)***》
'''
import optparse
from socket import *
import threading
threadlock = threading.Lock() #實(shí)例化threadlock對(duì)象
def Conn_scan(Host, Port):
try:
conn = socket(AF_INET,SOCK_STREAM)
conn.connect((Host, Port))
#conn.send('ywboy'.encode('utf-8')) #發(fā)送測(cè)試
#results = conn.recv(100) #接收主機(jī)返回的信息
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) ##獲得對(duì)應(yīng)主機(jī)的ip地址
except:
print("[-] Cannot resolve '%s':Unknow host" % Host)
return
try:
Name = gethostbyaddr(Host) ##獲得對(duì)應(yīng)主機(jī)的信息,返回主機(jī)名、主機(jī)別名列表、主機(jī)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()
上述代碼,因?yàn)槲抑皇亲隽硕丝趻呙?,所以注釋掉的判斷端口掃描服?wù)的代碼
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。