我们使用了原始套接字(socket.SOCK_RAW)来发送和接收ICMP消息,也就是通过模拟ICMP协议来进行UDP端口的探测。我们构造了一个简单的ICMP数据包,并将其发送到目标主机的特定端口。然后,我们等待接收目标主机返回的ICMP消息,并判断其类型和代码是否为端口不可达消息。如果是,则推断目标端口关闭;如果不是,则认为目标端口开放。文章来源:https://www.toymoban.com/news/detail-694920.html
import socket
import os
import struct
import time
def udp_port_scan(target_ip, port):
icmp = socket.getprotobyname("icmp")
sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp)
sock.settimeout(1) # 设置超时时间为1秒
# 构造ICMP消息
data = b'abcdefghijklmnopqrstuvwabcdefghi'
icmp_packet = struct.pack("!BBHHH32s", 8, 0, 0, 0, 0, data)
try:
sock.sendto(icmp_packet, (target_ip, port))
start_time = time.time()
while True:
try:
recv_packet, addr = sock.recvfrom(1024)
end_time = time.time()
elapsed_time = (end_time - start_time) * 1000 # 计算往返时间
icmp_header = recv_packet[20:28]
icmp_type, code, checksum, packet_id, sequence = struct.unpack("!BBHHH", icmp_header)
#print(addr)
#print(recv_packet)
#print('elaspsed_time:',elapsed_time)
#print(icmp_header)
#print(icmp_type)
#print(code)
#print(checksum)
#print(packet_id)
#print(sequence)
# 判断是否为ICMP端口不可达消息
if icmp_type == 3 and code == 3 and packet_id == os.getpid() & 0xFFFF:
print(f"Port {port} is closed")
break
# 此时可以认为端口开放
print(f"Port {port} is open")
break
except socket.timeout:
print(f"Port {port} is closed.Timeout!")
break
finally:
sock.close()
target_ip = '10.233.76.44'
target_ip = '115.236.153.177'
target_ip = '8.8.8.8'
ports_to_scan = [80, 443, 22, 53] # 要探测的端口列表
for port in ports_to_scan:
udp_port_scan(target_ip, port)
请注意,在使用原始套接字和ICMP协议进行UDP端口探测时,可能需要使用管理员权限运行脚本。同时,由于涉及到底层协议和操作系统的原因,代码在不同的平台和环境中可能会有所调整。文章来源地址https://www.toymoban.com/news/detail-694920.html
到了这里,关于使用ICMP协议来判断UDP端口的存活状态的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!