This repository has been archived by the owner on Dec 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
mitmpcap.py
148 lines (118 loc) · 4.46 KB
/
mitmpcap.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import os
import shlex
from time import time
from math import modf
from struct import pack
from subprocess import Popen, PIPE
class Exporter:
def __init__(self):
self.sessions = {}
def write(self, data):
raise NotImplementedError()
def flush(self):
raise NotImplementedError()
def close(self):
raise NotImplementedError()
def header(self):
data = pack('<IHHiIII', 0xa1b2c3d4, 2, 4, 0, 0, 0x040000, 1)
self.write(data)
def packet(self, src_host, src_port, dst_host, dst_port, payload):
key = '%s:%d-%s:%d' % (src_host, src_port, dst_host, dst_port)
session = self.sessions.get(key)
if session is None:
session = {'seq': 1}
self.sessions[key] = session
seq = session['seq']
total = len(payload) + 20 + 20
tcp_args = [src_port, dst_port, seq, 0, 0x50, 0x18, 0x0200, 0, 0]
tcp = pack('>HHIIBBHHH', *tcp_args)
ipv4_args = [0x45, 0, total, 0, 0, 0x40, 6, 0]
ipv4_args.extend(map(int, src_host.split('.')))
ipv4_args.extend(map(int, dst_host.split('.')))
ipv4 = pack('>BBHHHBBHBBBBBBBB', *ipv4_args)
link = b'\x00' * 12 + b'\x08\x00'
usec, sec = modf(time())
usec = int(usec * 1000 * 1000)
sec = int(sec)
size = len(link) + len(ipv4) + len(tcp) + len(payload)
head = pack('<IIII', sec, usec, size, size)
self.write(head)
self.write(link)
self.write(ipv4)
self.write(tcp)
self.write(payload)
session['seq'] = seq + len(payload)
def packets(self, src_host, src_port, dst_host, dst_port, payload):
limit = 40960
for i in range(0, len(payload), limit):
self.packet(src_host, src_port,
dst_host, dst_port,
payload[i:i + limit])
class File(Exporter):
def __init__(self, path):
super().__init__()
self.path = path
if os.path.exists(path):
self.file = open(path, 'ab')
else:
self.file = open(path, 'wb')
self.header()
def write(self, data):
self.file.write(data)
def flush(self):
self.file.flush()
def close(self):
self.file.close()
class Pipe(Exporter):
def __init__(self, cmd):
super().__init__()
self.proc = Popen(shlex.split(cmd), stdin=PIPE)
self.header()
def write(self, data):
self.proc.stdin.write(data)
def flush(self):
self.proc.stdin.flush()
def close(self):
self.proc.terminate()
self.proc.poll()
class Addon:
def __init__(self, createf):
self.createf = createf
self.exporter = None
def load(self, entry): # pylint: disable = unused-argument
self.exporter = self.createf()
def done(self):
self.exporter.close()
self.exporter = None
def response(self, flow):
client_addr = list(flow.client_conn.ip_address[:2])
server_addr = list(flow.server_conn.ip_address[:2])
client_addr[0] = client_addr[0].replace('::ffff:', '')
server_addr[0] = server_addr[0].replace('::ffff:', '')
self.export_request(client_addr, server_addr, flow.request)
self.export_response(client_addr, server_addr, flow.response)
self.exporter.flush()
def export_request(self, client_addr, server_addr, r):
proto = '%s %s %s\r\n' % (r.method, r.path, r.http_version)
payload = bytearray()
payload.extend(proto.encode('ascii'))
payload.extend(bytes(r.headers))
payload.extend(b'\r\n')
payload.extend(r.raw_content)
self.exporter.packets(*client_addr, *server_addr, payload)
def export_response(self, client_addr, server_addr, r):
headers = r.headers.copy()
if r.http_version.startswith('HTTP/2'):
headers.setdefault('content-length', str(len(r.raw_content)))
proto = '%s %s\r\n' % (r.http_version, r.status_code)
else:
headers.setdefault('Content-Length', str(len(r.raw_content)))
proto = '%s %s %s\r\n' % (r.http_version, r.status_code, r.reason)
payload = bytearray()
payload.extend(proto.encode('ascii'))
payload.extend(bytes(headers))
payload.extend(b'\r\n')
payload.extend(r.raw_content)
self.exporter.packets(*server_addr, *client_addr, payload)
addons = [Addon(lambda: File('output.pcap'))]
#addons = [Addon(lambda: Pipe('weer -'))]