views:

950

answers:

3

Hello

I have some pcap files and I want to filter by protocol, i.e., if I want to filter by HTTP protocol, anything but HTTP packets will remain in the pcap file.

There is a tool called openDPI, and it's perfect for what I need, but there is no wrapper for python language.

Does anyone knows any python modules that can do what I need?

Thanks

Edit 1:

HTTP filtering was just an example, there is a lot of protocols that I want to filter.

Edit 2:

I tried Scapy, but I don't figure how to filter correctly. The filter only accepts Berkeley Packet Filter expression, i.e., I can't apply a msn, or HTTP, or another specific filter from upper layer. Can anyone help me?

A: 

Try pylibpcap.

Dave Bacher
But I don't want to parse each packet to check for the protocol that I want, I want a simple solution (like openDPI). Also, I don't want to worry about "magic number" of all protocols that exists. If there is no solution, then I will have to do that. Thanks
coelhudo
A couple thoughts: 1. most of the python pcap libraries allow you to set a BPF filter on the captured packets. HTTP is an easy filter `tcp port 80`. 2. You could use Wireshark or a similar GUI to isolate the packets that you want, save those to a dumpfile and use pylibpcap or another of these libraries to operate on them.
Dave Bacher
There is no way besides "parsing each packet". You can have a program which does it behind the scenes for you, that's all you can hope.
bortzmeyer
+2  A: 

maybe this can help Scapy?

Ib33X
I hadn't seen scapy before. That's pretty powerful. I figured out how to do filters: `"TCP" in pkt and pkt.sport == 80` but couldn't decipher how to get at the payload of the upper layers given an Ethernet packet from a dump file.
Dave Bacher
sorry I really don't have any real experience with it, only tried few thing's from online doc's.
Ib33X
I discovered that protocol filtering it's not so "easy" as I expected, but Scapy will serve for my purposes. Thanks
coelhudo
+1  A: 

Something along the lines of

from pcapy import open_offline
from impacket.ImpactDecoder import EthDecoder
from impacket.ImpactPacket import IP, TCP, UDP, ICMP

decoder = EthDecoder()

def callback(jdr, data):
    packet = decoder.decode(data)
    child = packet.child()
    if isinstance(child, IP):
        child = packet.child()
        if isinstance(child, TCP):
            if child.get_th_dport() == 80:
                print 'HTTP'

pcap = open_offline('net.cap')
pcap.loop(0, callback)

using

http://oss.coresecurity.com/projects/impacket.html

fraca7