端口扫描之开放端口扫描方式
端口掃描器
參考:https://www.imooc.com/article/286803
常見(jiàn)的端口掃描類(lèi)型
1. TCP 連接掃描
2. TCP SYN 掃描(也稱(chēng)為半開(kāi)放掃描或stealth掃描)
3. TCP 圣誕樹(shù)(Xmas Tree)掃描
4. TCP FIN 掃描
5. TCP 空掃描(Null)
6. TCP ACK 掃描
7. TCP 窗口掃描
8. UDP 掃描
1、TCP連接掃描
若客戶(hù)端想要連接服務(wù)器80端口時(shí),會(huì)先發(fā)送一個(gè)帶有 SYN 標(biāo)識(shí)和端口號(hào)的 TCP 數(shù)據(jù)包給服務(wù)器(本例中為80端口)。如果端口是開(kāi)放的,則服務(wù)器會(huì)接受這個(gè)連接并返回一個(gè)帶有 SYN 和 ACK 標(biāo)識(shí)的數(shù)據(jù)包給客戶(hù)端。隨后客戶(hù)端會(huì)返回帶有 ACK 和 RST 標(biāo)識(shí)的數(shù)據(jù)包,此時(shí)客戶(hù)端與服務(wù)器建立了連接。
如果完成一次三次握手,那么服務(wù)器上對(duì)應(yīng)的端口肯定就是開(kāi)放的。
當(dāng)客戶(hù)端發(fā)送一個(gè)帶有 SYN 標(biāo)識(shí)和端口號(hào)的 TCP 數(shù)據(jù)包給服務(wù)器后,如果服務(wù)器端返回一個(gè)帶 RST 標(biāo)識(shí)的數(shù)據(jù)包,則說(shuō)明端口處于關(guān)閉狀態(tài)
nmap的-sT模式
#! /usr/bin/python
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
from scapy.all import *
dst_ip = "10.0.0.1"
src_port = RandShort()
dst_port=80
tcp_connect_scan_resp = sr1(IP(dst=dst_ip)/TCP(sport=src_port,dport=dst_port,flags="S"),timeout=10)
if(str(type(tcp_connect_scan_resp))==""):
print( "Closed")
elif(tcp_connect_scan_resp.haslayer(TCP)):
if(tcp_connect_scan_resp.getlayer(TCP).flags == 0x12):
send_rst = sr(IP(dst=dst_ip)/TCP(sport=src_port,dport=dst_port,flags="AR"),timeout=10)# 全連接 AR => ACK+RST
print( "Open")
elif (tcp_connect_scan_resp.getlayer(TCP).flags == 0x14):
print( "Closed")
2、TCP SYN 掃描
客戶(hù)端向服務(wù)器發(fā)送一個(gè)帶有 SYN 標(biāo)識(shí)和端口號(hào)的數(shù)據(jù)包,這種技術(shù)主要用于躲避防火墻的檢測(cè)。
如果目標(biāo)端口開(kāi)發(fā),則會(huì)返回帶有 SYN 和 ACK 標(biāo)識(shí)的 TCP 數(shù)據(jù)包。但是,這時(shí)客戶(hù)端不會(huì)返回 RST+ACK 而是返回一個(gè)只帶有 RST 標(biāo)識(shí)的數(shù)據(jù)包。
如果目標(biāo)端口處于關(guān)閉狀態(tài),那么同之前一樣,服務(wù)器會(huì)返回一個(gè) RST 數(shù)據(jù)包
nmap的-sS模式
#! /usr/bin/python
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
from scapy.all import *
dst_ip = "10.0.0.1"
src_port = RandShort()
dst_port=80
stealth_scan_resp = sr1(IP(dst=dst_ip)/TCP(sport=src_port,dport=dst_port,flags="S"),timeout=10)
if(str(type(stealth_scan_resp))==""):
print ("Filtered")
elif(stealth_scan_resp.haslayer(TCP)):
if(stealth_scan_resp.getlayer(TCP).flags == 0x12):
send_rst = sr(IP(dst=dst_ip)/TCP(sport=src_port,dport=dst_port,flags="R"),timeout=10) # 連接 R==>RST
print( "Open")
elif (stealth_scan_resp.getlayer(TCP).flags == 0x14):
print ("Closed")
elif(stealth_scan_resp.haslayer(ICMP)):
if(int(stealth_scan_resp.getlayer(ICMP).type)==3 and int(stealth_scan_resp.getlayer(ICMP).code) in [1,2,3,9,10,13]):
print ("Filtered")
3、TCP 圣誕樹(shù)(Xmas Tree)掃描
在圣誕樹(shù)掃描中,客戶(hù)端會(huì)向服務(wù)器發(fā)送帶有 PSH,FIN,URG 標(biāo)識(shí)和端口號(hào)的數(shù)據(jù)包給服務(wù)器。
如果目標(biāo)端口是開(kāi)放的,那么不會(huì)有任何來(lái)自服務(wù)器的回應(yīng)。
如果服務(wù)器返回了一個(gè)帶有 RST 標(biāo)識(shí)的 TCP 數(shù)據(jù)包,那么說(shuō)明端口處于關(guān)閉狀態(tài)。
如果服務(wù)器返回了一個(gè) ICMP 數(shù)據(jù)包,其中包含 ICMP 目標(biāo)不可達(dá)錯(cuò)誤類(lèi)型3以及 ICMP 狀態(tài)碼為1,2,3,9,10或13,則說(shuō)明目標(biāo)端口被過(guò)濾了無(wú)法確定是否處于開(kāi)放狀態(tài)。
nmap -sX模式
#! /usr/bin/python
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
from scapy.all import *
dst_ip = "10.0.0.1"
src_port = RandShort()
dst_port=80
xmas_scan_resp = sr1(IP(dst=dst_ip)/TCP(dport=dst_port,flags="FPU"),timeout=10)
if (str(type(xmas_scan_resp))==""):
print( "Open|Filtered")
elif(xmas_scan_resp.haslayer(TCP)):
if(xmas_scan_resp.getlayer(TCP).flags == 0x14):
print( "Closed")
elif(xmas_scan_resp.haslayer(ICMP)):
if(int(xmas_scan_resp.getlayer(ICMP).type)==3 and int(xmas_scan_resp.getlayer(ICMP).code) in [1,2,3,9,10,13]):
print ("Filtered")
4、FIN掃描
FIN 掃描會(huì)向服務(wù)器發(fā)送帶有 FIN 標(biāo)識(shí)和端口號(hào)的 TCP 數(shù)據(jù)包。
如果沒(méi)有服務(wù)器端回應(yīng)則說(shuō)明端口開(kāi)放。
如果服務(wù)器返回一個(gè) RST 數(shù)據(jù)包,則說(shuō)明目標(biāo)端口是關(guān)閉的。
如果服務(wù)器返回了一個(gè) ICMP 數(shù)據(jù)包,其中包含 ICMP 目標(biāo)不可達(dá)錯(cuò)誤類(lèi)型3以及 ICMP 代碼為1,2,3,9,10或13,則說(shuō)明目標(biāo)端口被過(guò)濾了無(wú)法確定端口狀態(tài)。
nmap -sF模式
#! /usr/bin/python
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
from scapy.all import *
dst_ip = "10.0.0.1"
src_port = RandShort()
dst_port=80
fin_scan_resp = sr1(IP(dst=dst_ip)/TCP(dport=dst_port,flags="F"),timeout=10)
if (str(type(fin_scan_resp))==""):
print ("Open|Filtered")
elif(fin_scan_resp.haslayer(TCP)):
if(fin_scan_resp.getlayer(TCP).flags == 0x14):
print ("Closed")
elif(fin_scan_resp.haslayer(ICMP)):
if(int(fin_scan_resp.getlayer(ICMP).type)==3 and int(fin_scan_resp.getlayer(ICMP).code) in [1,2,3,9,10,13]):
print ("Filtered")
5、TCP 空掃描(Null)
在空掃描中,客戶(hù)端發(fā)出的 TCP 數(shù)據(jù)包僅僅只會(huì)包含端口號(hào)而不會(huì)有其他任何的標(biāo)識(shí)信息。
如果目標(biāo)端口是開(kāi)放的則不會(huì)回復(fù)任何信息。
如果服務(wù)器返回了一個(gè) RST 數(shù)據(jù)包,則說(shuō)明目標(biāo)端口是關(guān)閉的。
如果返回 ICMP 錯(cuò)誤類(lèi)型3且代碼為1,2,3,9,10或13的數(shù)據(jù)包,則說(shuō)明端口被服務(wù)器過(guò)濾了。
nmap -sN模式
#! /usr/bin/python
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
from scapy.all import *
dst_ip = "10.0.0.1"
src_port = RandShort()
dst_port=80
null_scan_resp = sr1(IP(dst=dst_ip)/TCP(dport=dst_port,flags=""),timeout=10)
if (str(type(null_scan_resp))==""):
print( "Open|Filtered")
elif(null_scan_resp.haslayer(TCP)):
if(null_scan_resp.getlayer(TCP).flags == 0x14):
print ("Closed")
elif(null_scan_resp.haslayer(ICMP)):
if(int(null_scan_resp.getlayer(ICMP).type)==3 and int(null_scan_resp.getlayer(ICMP).code) in [1,2,3,9,10,13]):
print ("Filtered")
6、TCP ACK掃描
ACK 掃描不是用于發(fā)現(xiàn)端口開(kāi)啟或關(guān)閉狀態(tài)的,而是用于發(fā)現(xiàn)服務(wù)器上是否存在有狀態(tài)防火墻的。它的結(jié)果只能說(shuō)明端口是否被過(guò)濾。再次強(qiáng)調(diào),ACK 掃描不能發(fā)現(xiàn)端口是否處于開(kāi)啟或關(guān)閉狀態(tài)。
客戶(hù)端會(huì)發(fā)送一個(gè)帶有 ACK 標(biāo)識(shí)和端口號(hào)的數(shù)據(jù)包給服務(wù)器。如果服務(wù)器返回一個(gè)帶有 RST 標(biāo)識(shí)的 TCP 數(shù)據(jù)包,則說(shuō)明端口沒(méi)有被過(guò)濾,不存在狀態(tài)防火墻。
如果目標(biāo)服務(wù)器沒(méi)有任何回應(yīng)或者返回ICMP 錯(cuò)誤類(lèi)型3且代碼為1,2,3,9,10或13的數(shù)據(jù)包,則說(shuō)明端口被過(guò)濾且存在狀態(tài)防火墻。
nmap -sA模式
#! /usr/bin/python
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
from scapy.all import *
dst_ip = "10.0.0.1"
src_port = RandShort()
dst_port=80
ack_flag_scan_resp = sr1(IP(dst=dst_ip)/TCP(dport=dst_port,flags="A"),timeout=10)
if (str(type(ack_flag_scan_resp))==""):
print ("Stateful firewall presentn(Filtered)")
elif(ack_flag_scan_resp.haslayer(TCP)):
if(ack_flag_scan_resp.getlayer(TCP).flags == 0x4):
print ("No firewalln(Unfiltered)")
elif(ack_flag_scan_resp.haslayer(ICMP)):
if(int(ack_flag_scan_resp.getlayer(ICMP).type)==3 and int(ack_flag_scan_resp.getlayer(ICMP).code) in [1,2,3,9,10,13]):
print ("Stateful firewall presentn(Filtered)")
7、TCP窗口掃描
TCP 窗口掃描的流程同 ACK 掃描類(lèi)似,同樣是客戶(hù)端向服務(wù)器發(fā)送一個(gè)帶有 ACK 標(biāo)識(shí)和端口號(hào)的 TCP 數(shù)據(jù)包,但是這種掃描能夠用于發(fā)現(xiàn)目標(biāo)服務(wù)器端口的狀態(tài)。在 ACK 掃描中返回 RST 表明沒(méi)有被過(guò)濾,但在窗口掃描中,當(dāng)收到返回的 RST 數(shù)據(jù)包后,它會(huì)檢查窗口大小的值。
如果窗口大小的值是個(gè)非零值,則說(shuō)明目標(biāo)端口是開(kāi)放的。
如果返回的 RST 數(shù)據(jù)包中的窗口大小為0,則說(shuō)明目標(biāo)端口是關(guān)閉的。
nmap -sW模式
#! /usr/bin/python
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
from scapy.all import *
dst_ip = "10.0.0.1"
src_port = RandShort()
dst_port=80
window_scan_resp = sr1(IP(dst=dst_ip)/TCP(dport=dst_port,flags="A"),timeout=10)
if (str(type(window_scan_resp))==""):
print( "No response")
elif(window_scan_resp.haslayer(TCP)):
if(window_scan_resp.getlayer(TCP).window == 0):
print( "Closed")
elif(window_scan_resp.getlayer(TCP).window > 0):
print( "Open")
8、UDP掃描
TCP 是面向連接的協(xié)議,而UDP則是無(wú)連接的協(xié)議。
面向連接的協(xié)議會(huì)先在客戶(hù)端和服務(wù)器之間建立通信信道,然后才會(huì)開(kāi)始傳輸數(shù)據(jù)。如果客戶(hù)端和服務(wù)器之間沒(méi)有建立通信信道,則不會(huì)有任何產(chǎn)生任何通信數(shù)據(jù)。
無(wú)連接的協(xié)議則不會(huì)事先建立客戶(hù)端和服務(wù)器之間的通信信道,只要客戶(hù)端到服務(wù)器存在可用信道,就會(huì)假設(shè)目標(biāo)是可達(dá)的然后向?qū)Ψ桨l(fā)送數(shù)據(jù)。
客戶(hù)端會(huì)向服務(wù)器發(fā)送一個(gè)帶有端口號(hào)的 UDP 數(shù)據(jù)包。如果服務(wù)器回復(fù)了 UDP 數(shù)據(jù)包,則目標(biāo)端口是開(kāi)放的。
如果服務(wù)器返回了一個(gè) ICMP 目標(biāo)不可達(dá)的錯(cuò)誤和代碼3,則意味著目標(biāo)端口處于關(guān)閉狀態(tài)。
如果服務(wù)器返回一個(gè) ICMP 錯(cuò)誤類(lèi)型3且代碼為1,2,3,9,10或13的數(shù)據(jù)包,則說(shuō)明目標(biāo)端口被服務(wù)器過(guò)濾了。
如果服務(wù)器沒(méi)有任何相應(yīng)客戶(hù)端的 UDP 請(qǐng)求,則可以斷定目標(biāo)端口可能是開(kāi)放或被過(guò)濾的,無(wú)法判斷端口的最終狀態(tài)。
#! /usr/bin/python
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
from scapy.all import *
dst_ip = "10.0.0.1"
src_port = RandShort()
dst_port=53
dst_timeout=10
def udp_scan(dst_ip,dst_port,dst_timeout):
udp_scan_resp = sr1(IP(dst=dst_ip)/UDP(dport=dst_port),timeout=dst_timeout)
if (str(type(udp_scan_resp))==""):
retrans = []
for count in range(0,3):
retrans.append(sr1(IP(dst=dst_ip)/UDP(dport=dst_port),timeout=dst_timeout))
for item in retrans:
if (str(type(item))!=""):
udp_scan(dst_ip,dst_port,dst_timeout)
return ("Open|Filtered")
elif (udp_scan_resp.haslayer(UDP)):
return( "Open")
elif(udp_scan_resp.haslayer(ICMP)):
if(int(udp_scan_resp.getlayer(ICMP).type)==3 and int(udp_scan_resp.getlayer(ICMP).code)==3):
return( "Closed")
elif(int(udp_scan_resp.getlayer(ICMP).type)==3 and int(udp_scan_resp.getlayer(ICMP).code) in [1,2,9,10,13]):
return( "Filtered")
print udp_scan(dst_ip,dst_port,dst_timeout)
總結(jié)
以上是生活随笔為你收集整理的端口扫描之开放端口扫描方式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 网络是怎样连接的-UDP协议的收发操作
- 下一篇: 手机淘宝账号快速修改密码方法