Stopping ARP Spoofing on Linux with arptables and nftables: Protecting Your LAN Gateway

Network tutorial - IT technology blog
Network tutorial - IT technology blog

A Real-World Story: When a Tester’s Machine Brought Down the Entire Office

Our office has around 50 staff members alongside an on-premises internal server cluster. That day, one of our QA testers installed a pirated utility downloaded from the internet. Exactly 10 minutes later, the entire company suffered a massive network outage. Anyone trying to access the internal portal was bombarded with browser SSL certificate warning errors.

Opening a terminal to inspect the ARP cache table, I immediately spotted the culprit: the tester machine’s MAC address had hijacked the default gateway’s IP address (192.168.1.1). This is the textbook scenario of ARP Spoofing (ARP Poisoning).

Why is it so easy to trick? The Layer 2 ARP protocol dates back to the early days of networking when hosts inherently trusted each other. Whenever a machine broadcasts an ARP Reply claiming, “I hold IP 192.168.1.1, and my MAC is xx:xx:xx”, the receiving host unhesitatingly updates its cache without even checking whether it sent an ARP Request in the first place.

An attacker can simply position themselves in the middle (Man-in-the-Middle) to harvest unencrypted traffic, hijack session tokens, or cripple the entire network. If your network switches are budget unmanaged devices lacking Dynamic ARP Inspection (DAI), you must protect your servers yourself. Deploying an ARP filter using arptables or nftables directly on the Linux host is the cleanest and most effective solution.

Installing Tools and Gathering Network Parameters

On modern Linux distributions, arptables actually points to the arptables-nft backend, but the command-line syntax remains familiar.

Installation on Debian / Ubuntu:

sudo apt update
sudo apt install -y arptables nftables net-tools

On RHEL / Rocky Linux / AlmaLinux:

sudo dnf install -y iptables-arptables nftables net-tools

Before writing rules, you need to collect three key parameters:

  • The active network interface (e.g., eth0 or ens18).
  • The Default Gateway IP (e.g., 192.168.1.1).
  • The legitimate MAC address of the Gateway (e.g., 54:e6:fc:aa:bb:cc). Obtain this directly from your network administrator or look at the label under your router/core switch.

Quickly retrieve these parameters via CLI:

# 1. List current interfaces and IP addresses
ip -br addr

# 2. View the default gateway IP
ip route show default

# 3. Get the gateway MAC from the current neighbor table
ip neigh show | grep "$(ip route show default | awk '{print $3}')"

Deployment and Configuration: Choosing arptables or nftables?

Depending on your administration preferences, choose one of the two methods below. If you are provisioning a new server, I recommend going straight with nftables.

Method 1: Using arptables (Familiar iptables-Style Syntax)

Assuming the following lab parameters:

  • Interface: eth0
  • Gateway IP: 192.168.1.1
  • Valid MAC: 54:e6:fc:aa:bb:cc
  • Server static IP: 192.168.1.50

The filtering rule is straightforward: Any incoming ARP packet on interface eth0 with source IP 192.168.1.1 whose source MAC is NOT 54:e6:fc:aa:bb:cc must be dropped immediately.

# Flush existing rules
sudo arptables -F
sudo arptables -X

# Drop fake gateway impostors
sudo arptables -A INPUT -i eth0 --source-ip 192.168.1.1 --source-mac ! 54:e6:fc:aa:bb:cc -j DROP

# Block packets spoofing the server's own IP (prevent fake IP conflicts)
sudo arptables -A INPUT -i eth0 --source-ip 192.168.1.50 -j DROP

To persist rules across reboots:

# For Ubuntu / Debian
sudo apt install -y iptables-persistent
sudo arptables-save | sudo tee /etc/arptables.conf

# Create a systemd service to restore rules at boot
sudo tee /etc/systemd/system/arptables-restore.service << 'EOF'
[Unit]
Description=Restore arptables rules
After=network.target

[Service]
Type=oneshot
ExecStart=/sbin/arptables-restore /etc/arptables.conf
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
EOF

sudo systemctl daemon-reload
sudo systemctl enable arptables-restore

Method 2: Using nftables (Recommended for Modern Servers)

nftables processes packets faster in kernel space and consolidates all L2/L3/L4 rules into a single configuration. Open /etc/nftables.conf:

sudo nano /etc/nftables.conf

Paste the following configuration:

#!/usr/sbin/nft -f

flush ruleset

table arp filter {
    chain input {
        type filter hook input priority filter; policy accept;

        # Accept traffic not coming from interface eth0
        iifname != "eth0" accept

        # Drop and count ARP packets claiming to be the gateway with an invalid source MAC
        arp saddr ip 192.168.1.1 ether saddr != 54:e6:fc:aa:bb:cc counter drop

        # Drop packets spoofing this server's own IP
        arp saddr ip 192.168.1.50 counter drop
    }
}

Test the syntax and apply immediately:

# Test syntax first to prevent network lockouts
sudo nft -c -f /etc/nftables.conf

# Load configuration into the kernel
sudo nft -f /etc/nftables.conf

# Enable service to start automatically at boot
sudo systemctl enable --now nftables

Practical Testing and Monitoring Results

In networking, assumptions are never enough. After configuring the firewall, you must simulate an attack to verify that the rules actually work.

1. Simulating Spoofed ARP Packets with dsniff

Set up a test machine on the same LAN subnet (e.g., IP 192.168.1.88). Install dsniff and start sending poisoned ARP packets toward the server (192.168.1.50):

# Run on the simulated attacker machine
sudo apt install -y dsniff
sudo arpspoof -i eth0 -t 192.168.1.50 192.168.1.1

Switch back to the server and run the verification command:

ip neigh show 192.168.1.1

If the MAC address for 192.168.1.1 remains intact as 54:e6:fc:aa:bb:cc, you have succeeded. The cache has not been poisoned.

2. Monitoring Blocked Packet Counters

Check the count of dropped malicious packets using:

# For arptables
sudo arptables -L -n -v

# For nftables
sudo nft list ruleset

Sample output on nftables will look like this:

table arp filter {
    chain input {
        type filter hook input priority filter; policy accept;
        iifname != "eth0" accept
        arp saddr ip 192.168.1.1 ether saddr != 54:e6:fc:aa:bb:cc counter packets 142 bytes 5964 drop
        arp saddr ip 192.168.1.50 counter packets 0 bytes 0 drop
    }
}

The steadily increasing packets 142 counter proves that the attacker is continuously flooding the network, but every single spoofed packet is dropped right at the entrance.

Pinning Static ARP Entries (Permanent Neighbor)

Firewall filtering effectively drops fraudulent packets. However, for 100% certainty, you should permanently lock the Gateway entry into the kernel using the permanent attribute:

sudo ip neigh replace 192.168.1.1 lladdr 54:e6:fc:aa:bb:cc nud permanent dev eth0

The nud permanent flag forces the Linux kernel to lock this IP-to-MAC mapping permanently. The OS will reject any dynamic update requests from the network, creating an exceptionally reliable layer of defense for mission-critical database and web servers.

Share: