Live Packet Capture in Python with pcapy


/ Published in: Python
Save to your folder(s)

Tried using pypcap but it didn't seem to do anything and I am too lazy to compile python-libpcap.

Requires:
- pcapy python module (http://oss.coresecurity.com/projects/pcapy.html)
- impacket python module (http://oss.coresecurity.com/projects/impacket.html)
- pcap library, for Windows use WinPCap (http://www.winpcap.org/)

Sources:
- http://oss.coresecurity.com/pcapy/doc/pt01.html
- http://www.thescripts.com/forum/post2740081-5.html


Copy this code and paste it in your HTML
  1. import pcapy
  2. from impacket.ImpactDecoder import *
  3.  
  4. # list all the network devices
  5. pcapy.findalldevs()
  6.  
  7. max_bytes = 1024
  8. promiscuous = False
  9. read_timeout = 100 # in milliseconds
  10. pc = pcapy.open_live("name of network device to capture from", max_bytes, promiscuous, read_timeout)
  11.  
  12. pc.setfilter('tcp')
  13.  
  14. # callback for received packets
  15. def recv_pkts(hdr, data):
  16. packet = EthDecoder().decode(data)
  17. print packet
  18.  
  19. packet_limit = -1 # infinite
  20. pc.loop(packet_limit, recv_pkts) # capture packets

URL: http://andrewtrusty.com/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.