Quantcast
Channel: BinaryTides » Python
Viewing all articles
Browse latest Browse all 10

Code a packet sniffer in python with pcapy extension

$
0
0
Pcapy
In the previous articles we coded packet sniffers in python using raw sockets. Now lets use the libpcap library for the same. Libpcap is the packet capture library for linux and has wrappers for most languages. In python there are multiple libpcap wrappers like pcapy, pypcap etc. In this article we shall use the pcapy python module.

Pcapy is a Python extension module that interfaces with the libpcap packet capture library. Pcapy enables python scripts to capture packets on the network.

Project website :
http://oss.coresecurity.com/projects/pcapy.html
On ubuntu pcapy can be installed directly from synaptic by issuing the following command
$ sudo apt-get install python-pcapy
Code
Lets code our sniffer right away.

'''
Packet sniffer in python using the pcapy python library

Project website
http://oss.coresecurity.com/projects/pcapy.html
'''

import socket
from struct import *
import datetime
import pcapy
import sys

def main(argv):
#list all devices
devices = pcapy.findalldevs()
print devices

#ask user to enter device name to sniff
print "Available devices are :"
for d in devices :
print d

dev = raw_input("Enter device name to sniff : ")

print "Sniffing device " + dev

'''
open device
# Arguments here are:
# device
# snaplen (maximum number of bytes to capture _per_packet_)
# promiscious mode (1 for true)
# timeout (in milliseconds)
'''
cap = pcapy.open_live(dev , 65536 , 1 , 0)

#start sniffing packets
while(1) :
(header, packet) = cap.next()
#print ('%s: captured...

Read full post here
Code a packet sniffer in python with pcapy extension


Viewing all articles
Browse latest Browse all 10

Trending Articles