johnmahugu

python sniffer

Jun 3rd, 2015
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. import socket
  2. import os
  3.  
  4. # host to listen on
  5. host = "192.168.0.196"
  6.  
  7. # create a raw socket and bind it to the public interface
  8. if os.name == "nt":
  9. socket_protocol = socket.IPPROTO_IP
  10. else:
  11. socket_protocol = socket.IPPROTO_ICMP
  12.  
  13. sniffer = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket_protocol)
  14.  
  15. sniffer.bind((host, 0))
  16.  
  17. # we want the IP headers included in the capture
  18. sniffer.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
  19.  
  20. # if we're on Windows we need to send an IOCTL
  21. # to setup promiscuous mode
  22. if os.name == "nt":
  23. sniffer.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)
  24.  
  25. # read in a single packet
  26. print sniffer.recvfrom(65565)
  27.  
  28. # if we're on Windows turn off promiscuous mode
  29. if os.name == "nt":
  30. sniffer.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)
Advertisement
Add Comment
Please, Sign In to add comment