Friday, June 19, 2009

pcap to scapy

Script that generate a python file with the packet generation code that Scapy need to replicate the traffic of a pcap file. I hope it would be useful for someone when testing NIDS features :)


## pcap2scapy.py ##
###################
# Author: Pablo Rincon Crespo
# mail: pablo@ossim.net
# Comments: This script read a pcap and write a .py with the scapy commands needed to replicate the traffic.

from scapy import *
import sys


if len(sys.argv) ==2:
print "Parsing "+str(sys.argv[1])
else:
print "Usage: python "+sys.argv[0]+" file.pcap"
exit(10)

pcap=rdpcap(sys.argv[1])
out=file(sys.argv[1]+".py","w")

out.write("from scapy import *\n\nl=[]\n")
i=0
for p in pcap:
i=i+1
# p.display()
print "*** Scapy packet "+str(i)+": ***"
print p.command()
out.write("p="+p.command()+"\nl.append(p)\n\n")

out.write("\n\n#sendp(l,iface='eth0')\n#wrpcap('/tmp/tmp.pcap',l)")

out.close()
print str(i) +" packets written in "+sys.argv[1]+".py"