forked from sujit/iprewrite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iprewrite.py
48 lines (40 loc) · 1.54 KB
/
iprewrite.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from pathlib import Path
from subprocess import call, DEVNULL, STDOUT, check_call
import sys
import os
import mimetypes
import argparse
# Init parser
parser = argparse.ArgumentParser(description='Recursive IP Rewrite Tool')
parser.add_argument('--dir', '-d', type=str, help='Source PCAP directory')
parser.add_argument('--file', '-f', type=str, help='Source PCAP file')
args = parser.parse_args()
# Check if required binaries exist, exit otherwise
executables = ['tcpprep', 'tcprewrite']
for each_binary in executables:
try:
check_call([each_binary, '--version'], stdout=DEVNULL, stderr=STDOUT)
except OSError:
print('%s binary missing on path' % (each_binary))
exit(-1)
# Pull PCAPs list
directory = args.dir
pcapfile = args.file
for file in Path(directory).glob('**/*.pcap'):
mime = mimetypes.guess_type(file)[0]
if mime == 'application/vnd.tcpdump.pcap':
print('\n[*]Generating cache: %s..' % (file))
check_call(['tcpprep', '--port', '-i',
str(file), '--cachefile',
str(file) + '.cache'
],
stdout=DEVNULL,
stderr=STDOUT)
print('[*]Rewriting IP: 172.16.1.10 <-> 172.16.1.254')
check_call(['tcprewrite', '-c', str(file) + '.cache',
'--endpoints', '172.16.1.10:172.16.1.254',
'-i', str(file),
'-o', str(file) + '_custom.pcap'])
os.remove(str(file) + '.cache')
else:
print('[-]Ignored non-libpcap file..')