windows下用python3.3.5 写UDP端口扫描时收到ICMP包但显示不出来怎么办?

发布于 2022-09-01 19:18:46 字数 2708 浏览 29 评论 0

题主想做一个udp扫描端口的程序,代码如下:

import socket
import threading
import time
import struct
import queue
import sys

queue = queue.Queue()

def udp_sender(ip,port):
    try:
        ADDR = (ip,port)
        print(ADDR)
        sock_udp = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
        print("sock_udp define OK")
        sock_udp.sendto("abcd...".encode(),ADDR)
        print("sock_udp send OK")
        sock_udp.close()
        print("udp_sender OK")
    except:
        print("udp_sender Failed")
        pass

def icmp_receiver(ip,port):
    icmp = socket.getprotobyname("icmp")
    try:
        sock_icmp = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp)
        print("sock_icmp OK")
    except socket.error(errno, msg) as err:
        if errno == 1:
            # Operation not permitted
            msg = msg + (
                " - Note that ICMP messages can only be sent from processes"
                " running as root."
            )
            raise socket.error(msg)
        raise # raise the original error
    sock_icmp.settimeout(3)
    try:
        recPacket,addr = sock_icmp.recvfrom(64)
        print("recPacket OK")
    except:
        queue.get(True)
        print("recPacket Failed")
        return
    icmpHeader = recPacket[20:28]
    icmpPort = int(recPacket.encode('hex')[100:104],16)
    head_type, code, checksum, packetID, sequence = struct.unpack(
            "bbHHh", icmpHeader
    )
    sock_icmp.close()
    print(head_type," ",code," ",checksum," ",packetID," ",sequence)
    if code == 3 and icmpPort == port and addr[0] == ip:
        '''return "closed"
    elif code == 1 or 2 or 9 or 10 or 13:
        return "Filtered"'''
        queue.get(False)

    return

def checker_udp(ip,port):

    thread_udp = threading.Thread(target=udp_sender,args=(ip,port))
    thread_icmp = threading.Thread(target=icmp_receiver,args=(ip,port))

    thread_udp.daemon= True
    thread_icmp.daemon = True

    thread_icmp.start()
    time.sleep(0.1)
    thread_udp.start()

    thread_icmp.join()
    thread_udp.join()
    return queue.get(False)

if __name__ == '__main__':
    import sys
    print (checker_udp(socket.gethostbyname("www.fudan.edu.cn"),8888))

用cmd运行结果如下:

clipboard.png

程序一直卡在这边,进行不下去。
好像

try:
    recPacket,addr = sock_icmp.recvfrom(64)

然后我同时用wireshark去看,其实收到了ICMP包,但是显示不出来,如下。

clipboard.png

不知道问题出在哪里,希望大神帮忙指点一下。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文