Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 38 additions & 37 deletions pcap2curl.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,44 @@
#!/usr/bin/env python

import sys
from scapy.all import *
from scapy.all import rdpcap, re, Raw, TCP


if len(sys.argv) != 2:
print ("I need an input file. Usage ./pcap2curl.py inputfilename")
exit()

infile = sys.argv[1]

packets=rdpcap(infile)

def payload2curl(p):
lines=re.compile("[\n\r]+").split(p)
startline=re.search('^([A-Z]+) ([^ ]+) (HTTP\/[0-9\/]+)',lines[0])
curl='curl ';
method=startline.group(1)
url=startline.group(2)
version=startline.group(3)

del lines[0]
headers=[]
for line in lines:
if ":" in line:
headers.append("-H '"+line+"'")
if "Host:" in line:
hostheader=re.search("^Host: (.*)",line)
hostname=hostheader.group(1)

if hostname not in url:
url='http://'+hostname+'/'+url
curl=curl+' '+"'"+url+"' \\\n"
curl=curl+'-X'+method+" \\\n"
curl=curl+" \\\n".join(headers)
return curl

for p in packets:
payload=''
if p.haslayer(TCP) and p.haslayer(Raw) and p[TCP].dport == 80:
payload=p[Raw].load
print (payload2curl(payload))
lines = re.compile("[\n\r]+").split(p.decode())
start_line = re.search("^([A-Z]+) ([^ ]+) (HTTP\/[0-9\/]+)", lines[0])
method = start_line.group(1)
url = start_line.group(2)
version = start_line.group(3) # Never used

del lines[0]
headers = []
for line in lines:
if ":" in line:
headers.append("-H '{}'".format(line))
if "Host:" in line:
host_header = re.search("^Host: (.*)", line)
host_name = host_header.group(1)

if host_name not in url:
url = "http://{}/{}".format(host_name, url)
curl = "curl '{}' \\\n -X {} \\\n".format(url, method)
curl += " \\\n".join(headers)
return curl


def main():
if len(sys.argv) != 2:
print ("I need an input file. Usage ./pcap2curl.py inputfilename")
return

infile = sys.argv[1]
packets = rdpcap(infile)

for p in packets:
if p.haslayer(TCP) and p.haslayer(Raw) and p[TCP].dport == 80:
payload = p[Raw].load
print(payload2curl(payload))

if __name__ == "__main__":
main()