Skip to content

Instantly share code, notes, and snippets.

@scysys
Created August 12, 2023 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scysys/b5eff3cb1703cde07c7cac847266b7d9 to your computer and use it in GitHub Desktop.
Save scysys/b5eff3cb1703cde07c7cac847266b7d9 to your computer and use it in GitHub Desktop.
Packet Payload Modification for Network Traffic
#!/usr/bin/env python3
from netfilterqueue import NetfilterQueue
import scapy.all as scapy
# Configuration variables
original_keyword = b"search"
modified_keyword = b"replace"
def modify_packet(packet):
scapy_packet = scapy.IP(packet.get_payload())
if scapy_packet.haslayer(scapy.Raw):
packet_payload = scapy_packet[scapy.Raw].load
new_payload = packet_payload.replace(original_keyword, modified_keyword)
scapy_packet[scapy.Raw].load = new_payload
packet.set_payload(bytes(scapy_packet))
print("Original payload:", packet_payload)
print("Modified payload:", new_payload)
packet.accept()
nfqueue = NetfilterQueue()
nfqueue.bind(0, modify_packet)
try:
nfqueue.run()
except KeyboardInterrupt:
print("Flushing iptables rules...")
nfqueue.unbind()
@scysys
Copy link
Author

scysys commented Aug 12, 2023

iptables -I INPUT -p tcp --dport 80 -j NFQUEUE --queue-num 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment