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