Skip to content

Instantly share code, notes, and snippets.

@prongs
Created November 10, 2012 16:20
Show Gist options
  • Save prongs/4051536 to your computer and use it in GitHub Desktop.
Save prongs/4051536 to your computer and use it in GitHub Desktop.
import re
from scapy.all import *
def psize(packet):
#assume 20 from http://stackoverflow.com/questions/6639799/calculate-size-and-start-of-tcp-packet-data-excluding-header
return packet[IP].len-packet[IP].ihl*4-4*packet[TCP].dataofs
def groupPacketsByFlow(packets):
flows={}
print flows
print type(packets)
for i in xrange(len(packets)):
packet=packets[i]
print packet
print type(packet)
print packet[IP]
return
pair = (packet[IP].src, packets[IP].dst)
print pair
if pair in flows:
print "in if"
flows[(packet[IP].src, packets[IP].dst)].append(packet)
else:
flows[(packet[IP].src, packets[IP].dst)] = [packet]
return flows
def analyzePcap(pcap):
packets=rdpcap(pcap)
print len(packets)
tcp_packets=filter(lambda p:p.haslayer(TCP),packets)
print len(tcp_packets)
flows = groupPacketsByFlow(tcp_packets)
if __name__=="__main__":
analyzePcap('proper.pcap')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment