socket - windows下用python3.3.5 写UDP端口扫描时收到ICMP包但显示不出来怎么办?
怪我咯
怪我咯 2017-04-17 16:09:37
0
0
490

题主想做一个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运行结果如下:

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

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

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

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

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(0)
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template