Skip to main content

Build an ARP Toolkit — Attack and Detection in C

https://github.com/Itsjustme27/arp_toolkit.git

Updated 7 min readMarkdown
Build an ARP Toolkit — Attack and Detection in C

I spend a lot of time looking at alerts that say "possible MITM activity" or "ARP anomaly detected." They show up in the SIEM, get triaged, and usually turn out to be benign — a NIC failover, a misconfigured switch, something. But I never fully understood what the attack actually looked like at the wire level.

So I built it from scratch in C. Two tools in one repo: one that launches the attack, one that catches it.

What ARP actually is

ARP — Address Resolution Protocol — is how devices on a local network map IP addresses to MAC addresses. When your machine wants to talk to 192.168.1.1, it doesn't know the MAC address yet, so it broadcasts a question to the entire network: "who has 192.168.1.1?"

The router hears it and replies: "I do — here's my MAC." Your machine caches that mapping and starts sending traffic.

That's the whole protocol. No handshake. No verification. No authentication of any kind. If someone else replies first — or sends an unsolicited reply claiming to be the router — your machine will cache that instead. It has no way to tell the difference.

That's ARP spoofing. There's no exploit involved. It's just the protocol working exactly as designed, being used for something it wasn't designed for.

Let me insert a little diagram...

The Two Tools

  1. arp_spoof

Sends crafted ARP reply packets at Layer 2 using raw sockets. Every byte — Ethernet header, ARP header, addresses — is written manually into a buffer before being handed to the kernel. No library abstracts this. You see exactly what goes on the wire.

Usage:

sudo ./arp_spoof <target_ip> <spoof_ip>

Example: Tell 192.168.125.56 that you are the gateway

sudo ./arp_spoof wlan0 192.168.1.50 192.168.1.1

2. arp_monitor

Passively captures ARP traffic using libpcap and maintains an in-memory IP-to-MAC table. Every ARP reply gets compared against what's already cached. If a known IP shows up with a new MAC, it logs an alert with the old and new values side by side.

sudo ./arp_monitor

Packet Structure: The Byte Level

To write the spoofer, I had to understand exactly what an ARP frame looks like. This is where the magic (and the vulnerability) happens.

Ethernet Header -- 14 bytes

[dst MAC: 6 bytes][src MAC: 6 bytes][EtherType: 2 bytes]

EtherType for ARP is 0x0806. That's the value that tells anything reading this frame — the kernel, Wireshark, your SIEM's sensor — that what follows is an ARP payload and not something else.

ARP Header -- 28 bytes

[hw_type:    2][proto_type: 2][hw_size: 1][proto_size: 1][opcode: 2]
[sender MAC: 6][sender IP:  4]
[target MAC: 6][target IP:  4]
Field Value
hw_type 0x0001 -- Ethernet
proto_type 0x0800 -- IPv4
opcode 0x0001 = REQUEST, 0x0002 = REPLY

In a spoofed reply, the sender MAC is set to the attacker's MAC while the sender IP is set to the IP being impersonated. That pairing is what the victim's machine caches. The deception is one field.

Total frame size: 42 bytes.


How it Works

The Spoofer

Raw sockets let you write directly at Layer 2 — you construct the entire frame yourself and hand it to the kernel with sendto(). No IP stack involvement. No UDP socket. The frame you write is the frame that goes on the wire.

The spoofer builds the 42-byte buffer in order: Ethernet header first, then the ARP header with the forged sender MAC and IP. It sends this in a loop so the victim's cache keeps getting overwritten before it can expire.

The Monitor (Detection Logic)

The monitor uses libpcap to sniff all ARP frames. For each reply:

  1. Extract sender IP and sender MAC.

  2. Check the in-memory cache:

    • Not seen before -> Add it to the table.

    • Seen before, same MAC -> Legitimate.

    • Seen before, different MAC -> ALERT.

  3. Alert shows the IP, the old MAC, and the new MAC

Normal traffic:

ARP Monitor started...
[REQUEST] b4:d5:bd:c4:60:1c (192.168.125.56) -> who has 192.168.125.54?
[REPLY]   98:43:fa:94:17:c7 (192.168.125.54) -> I am 192.168.125.56

Attack detected:

[REPLY] b4:d5:bd:c4:60:1c (192.168.125.54) -> I am 192.168.125.56

[ALERT] POSSIBLE ARP SPOOF
   IP     : 192.168.125.54
   OLD MAC : 98:43:fa:94:17:c7
   NEW MAC : b4:d5:bd:c4:60:1c
   ACTION  : investigate

The detection logic is simple by design. It's the same logic your OS uses when it caches ARP entries — just with memory and alerting added on top. Tools like Arpwatch do the same thing at their core.

Building and Testing Setup

You'll need libpcap for the monitor:

$ sudo apt install libpcap-dev 
$ git clone https://github.com/Itsjustme27/arp-toolkit cd arp-toolkit

Monitor

$ cd arp_monitor && gcc -o arp_monitor main.c -lpcap

Spoofer

$ cd ../arp_spoof && gcc -o arp_spoof main.c

The "Lab" Test

# Terminal 1 — flush the OS ARP cache
sudo ip neigh flush all

# Terminal 2 — start the monitor
sudo ./arp_monitor

# Terminal 3 — ping the target so the monitor learns its real MAC
ping -c 3 <target_ip>

# Terminal 4 — run the spoofer
sudo ./arp_spoof wlan0 <target_ip> <spoof_ip>

# Watch Terminal 2 for the alert

What This Changed

The next time I saw an ARP-related alert in the SIEM, I knew exactly what I was looking at. Not just "the MAC changed" as a vague observation — I could picture the exact frame that caused it. The 14-byte Ethernet header, the 28-byte ARP payload, the sender MAC field set to something it shouldn't be.

I also understood for the first time why certain detections fire the way they do:

  • An unsolicited ARP reply — a reply with no preceding request — is always worth flagging. There's no legitimate reason a device would broadcast its MAC mapping without being asked.

  • High-frequency ARP replies from a single source isn't normal network behavior. A spoofer has to keep sending to overwrite the cache before the TTL expires.

  • A MAC change on the default gateway or DNS server IP is one of the more reliable signals you can hunt for, because that hardware almost never changes.

The MITRE ATT&CK reference for this technique is T1557.002 — Adversary-in-the-Middle: ARP Cache Poisoning.

Building the attack made the detection logic obvious. I'd recommend that approach to anyone writing detection rules for protocols they're not fully comfortable with yet.

MITRE ATT&CK Reference: T1557.002 — Adversary-in-the-Middle: ARP Cache Poisoning.

What's Next?

A few additions I want to make:

  • Gratuitous ARP tracking — log and flag all replies that had no corresponding request

  • Rate-based alerting — trigger when reply frequency from a single MAC exceeds a threshold

  • Persistent MAC table — write the cache to disk so learned state survives restarts

  • PCAP export — dump suspicious frames to a file for offline analysis

Disclaimer: This toolkit is for educational use and authorized testing only. Only run it on your own network, in a lab environment, or on a network where you have explicit written permission. Unauthorized ARP spoofing is illegal.

About the author

  • Just a SOC analyst doing detection engineering across banking-sector SIEM environments (LogPoint, FortiSIEM, Wazuh), with a habit of digging past the alert to the mechanism underneath. I build automation to cut manual triage overhead and write custom detection rules for endpoint and network anomalies. Outside work: built a DNS resolver from scratch against RFC 1035, an ARP spoofing detector in C , design CTF challenges and I read Linux kernel source for fun. I write about what I find at prayush.hashnode.dev.

No comments yet. Be the first to comment.