使用ICMP协议来判断UDP端口的存活状态

        我们使用了原始套接字(socket.SOCK_RAW)来发送和接收ICMP消息,也就是通过模拟ICMP协议来进行UDP端口的探测。我们构造了一个简单的ICMP数据包,并将其发送到目标主机的特定端口。然后,我们等待接收目标主机返回的ICMP消息,并判断其类型和代码是否为端口不可达消息。如果是,则推断目标端口关闭;如果不是,则认为目标端口开放。

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)
                
                # 判断是否为ICMP端口不可达消息
                if 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")
                break
    finally:
        sock.close()

target_ip = '192.168.0.1'
ports_to_scan = [80, 443, 22, 53]  # 要探测的端口列表

for port in ports_to_scan:
    udp_port_scan(target_ip, port)

请注意,在使用原始套接字和ICMP协议进行UDP端口探测时,可能需要使用管理员权限运行脚本。同时,由于涉及到底层协议和操作系统的原因,代码在不同的平台和环境中可能会有所调整。

你可能感兴趣的:(#,Python,udp,网络协议,网络)