0

I have tried this:

s = socket.socket(socket.AF_INET, socket.SOCK_RAW,socket.IPPROTO_IP)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
s.bind(('', 69))
while True
    try :
        databuffer, (raddress, rport) = s.recvfrom(1024)
    except socket.timeout:
        logging.error("Write timeout on socket")
        continue

But the result says nothing was received(no package!) in databuffer. But I used Wireshark that really captured the packets! I have found some solutions: How Do I Use Raw Socket in Python? The last example shows how to write a very simple network sniffer with raw sockets on Windows. The example requires administrator privileges to modify the interface:

import socket
# the public network interface
HOST = socket.gethostbyname(socket.gethostname())

# create a raw socket and bind it to the public interface
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)
s.bind((HOST, 0))

# Include IP headers
s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)

# receive all packages
s.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)

# receive a package
print s.recvfrom(65565)

# disabled promiscuous mode
s.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF) "

Mine:

HOST = socket.gethostbyname(socket.gethostname())
s = socket.socket(socket.AF_INET,socket.SOCK_RAW,socket.IPPROTO_UDP)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
s.bind((HOST, 69))
s.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)

It works! But there is another problem: the local port 69!

s.bind((HOST, 69))

Packets captured are all from port 0 but not 69!

I was wondering if there are even better ways to do that.

On os:windows server 2003 R2 standard edition sp2 -32bits python 2.7.1

Community
  • 1
  • 1
  • `s.bind((HOST, 0))` means only that the port will be chosen on random from 1024-65535 range. Try setting your port value from 69 to one from that range. – matino Jul 09 '15 at 11:44
  • so when s.bind((HOST, 69)) means ?! or i just detect 69 it means range(69,69)! – johnny_wang Jul 10 '15 at 01:53
  • the key point of my question like this:http://stackoverflow.com/questions/14203914/capturing-packets-from-a-specific-client-udp-python " capture packet from specific client port number" on windows platform SOCK_DGRAM doesnt work ,so i have to use SOCK_RAW instead of SOCK_DGRAM. – johnny_wang Jul 10 '15 at 02:41

0 Answers0