为什么我的扫描仪没有工作?

时间:2016-09-20 09:54:11

标签: python

发送udp包的方法

def udp_sender(subnet,magic_message):
    time.sleep(5)
    sender = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    for ip in IPNetwork(subnet):
        try:
            sender.sendto(magic_message.encode("utf-8"),("%s"%ip,65212))
        except Exception as e:
            print(e)
           # pass

扫描前设置:

if os.name == "nt":
    socket_protocol = socket.IPPROTO_IP
else:
    socket_protocol = socket.IPPROTO_ICMP

sniffer = socket.socket(socket.AF_INET,socket.SOCK_RAW,socket_protocol)

sniffer.bind((host,0))
sniffer.setsockopt(socket.IPPROTO_IP,socket.IP_HDRINCL,1)

if os.name == "nt":
    sniffer.ioctl(socket.SIO_RCVALL,socket.RCVALL_ON)

t = threading.Thread(target=udp_sender,args=(subnet,magic_message))
t.start()

扫描并接收icmp包:

IP和ICMP是我定义的类:

try:
    while True:
        raw_buffer = sniffer.recvfrom(65565)[0]
        ip_header = IP(raw_buffer)
        #print("Protocol: %s %s -> %s"%        (ip_header.protocol,ip_header.src_address,ip_header.dst_address))
        if ip_header.protocol == "ICMP":
        offset = ip_header.ihl*4
        buf = raw_buffer[offset:offset+sizeof(ICMP)]
        icmp_header = ICMP(buf)
       # print("ICMP -> Type:%d Code:%d"%(icmp_header.type,icmp_header.code))
        if icmp_header.code == 3 and icmp_header.type == 3:
            if IPAddress(ip_header.src_address) in IPNetwork(subnet):
                if raw_buffer[len(raw_buffer)-len(magic_message):].decode("utf-8") == magic_message:
                    print("Host up:%s"%ip_header.src_address)
except KeyboardInterrupt:
    if os.name == "nt":
        sniffer.ioctl(socket.SIO_RCVALL,socket.RCVALL_OFF)

但是,它不起作用,只能主持我自己的IP .....

enter image description here

0 个答案:

没有答案