|
1 | 1 | #!/usr/bin/env python
|
2 | 2 |
|
3 | 3 | import sys
|
4 |
| -from scapy.all import * |
| 4 | +from scapy.all import rdpcap, re, Raw, TCP |
5 | 5 |
|
6 | 6 |
|
7 |
| -if len(sys.argv) != 2: |
8 |
| - print ("I need an input file. Usage ./pcap2curl.py inputfilename") |
9 |
| - exit() |
10 |
| - |
11 |
| -infile = sys.argv[1] |
12 |
| - |
13 |
| -packets=rdpcap(infile) |
14 |
| - |
15 | 7 | def payload2curl(p):
|
16 |
| - lines=re.compile("[\n\r]+").split(p) |
17 |
| - startline=re.search('^([A-Z]+) ([^ ]+) (HTTP\/[0-9\/]+)',lines[0]) |
18 |
| - curl='curl '; |
19 |
| - method=startline.group(1) |
20 |
| - url=startline.group(2) |
21 |
| - version=startline.group(3) |
22 |
| - |
23 |
| - del lines[0] |
24 |
| - headers=[] |
25 |
| - for line in lines: |
26 |
| - if ":" in line: |
27 |
| - headers.append("-H '"+line+"'") |
28 |
| - if "Host:" in line: |
29 |
| - hostheader=re.search("^Host: (.*)",line) |
30 |
| - hostname=hostheader.group(1) |
31 |
| - |
32 |
| - if hostname not in url: |
33 |
| - url='http://'+hostname+'/'+url |
34 |
| - curl=curl+' '+"'"+url+"' \\\n" |
35 |
| - curl=curl+'-X'+method+" \\\n" |
36 |
| - curl=curl+" \\\n".join(headers) |
37 |
| - return curl |
38 |
| - |
39 |
| -for p in packets: |
40 |
| - payload='' |
41 |
| - if p.haslayer(TCP) and p.haslayer(Raw) and p[TCP].dport == 80: |
42 |
| - payload=p[Raw].load |
43 |
| - print (payload2curl(payload)) |
| 8 | + lines = re.compile("[\n\r]+").split(p.decode()) |
| 9 | + start_line = re.search("^([A-Z]+) ([^ ]+) (HTTP\/[0-9\/]+)", lines[0]) |
| 10 | + method = start_line.group(1) |
| 11 | + url = start_line.group(2) |
| 12 | + version = start_line.group(3) # Never used |
| 13 | + |
| 14 | + del lines[0] |
| 15 | + headers = [] |
| 16 | + for line in lines: |
| 17 | + if ":" in line: |
| 18 | + headers.append("-H '{}'".format(line)) |
| 19 | + if "Host:" in line: |
| 20 | + host_header = re.search("^Host: (.*)", line) |
| 21 | + host_name = host_header.group(1) |
| 22 | + |
| 23 | + if host_name not in url: |
| 24 | + url = "http://{}/{}".format(host_name, url) |
| 25 | + curl = "curl '{}' \\\n -X {} \\\n".format(url, method) |
| 26 | + curl += " \\\n".join(headers) |
| 27 | + return curl |
| 28 | + |
| 29 | + |
| 30 | +def main(): |
| 31 | + if len(sys.argv) != 2: |
| 32 | + print ("I need an input file. Usage ./pcap2curl.py inputfilename") |
| 33 | + return |
| 34 | + |
| 35 | + infile = sys.argv[1] |
| 36 | + packets = rdpcap(infile) |
| 37 | + |
| 38 | + for p in packets: |
| 39 | + if p.haslayer(TCP) and p.haslayer(Raw) and p[TCP].dport == 80: |
| 40 | + payload = p[Raw].load |
| 41 | + print(payload2curl(payload)) |
| 42 | + |
| 43 | +if __name__ == "__main__": |
| 44 | + main() |
0 commit comments