TExcelTAD

DNS and ARP Spoofing

Two of the oldest protocols on every network answer a question for you: ARP answers "which MAC address has this IP address on my local network?" and DNS answers "which IP address does this name have?". Neither was designed to check who is answering. Whoever replies convincingly first decides where your traffic goes, which is how an attacker becomes the man in the middle. This lesson shows both mechanisms, finds a spoofed ARP reply in a lab capture with two tools, works out why DNS poisoning became hard, and lists the fixes. How ARP and DNS work normally is in the networking course and its DNS lesson.

ARP spoofing: mechanism

On an Ethernet or Wi-Fi network a host that wants to reach the gateway 10.10.0.1 broadcasts who-has 10.10.0.1?, and the gateway replies 10.10.0.1 is-at 02:00:00:0a:0a:01. The host stores the answer in its ARP cache for a while. The weaknesses:

  1. Any host on the segment can send a reply.
  2. Many operating systems accept a reply even when they never asked (a gratuitous or unsolicited reply), and overwrite the cache entry.
  3. Nothing in ARP proves that the sender owns the address.

So an attacker on the same segment sends the victim "10.10.0.1 is-at my MAC" and the gateway "10.10.0.5 is-at my MAC", forwards the traffic so nobody notices, and reads or alters everything in between. Impact: interception of cleartext traffic, session theft, downgrade attacks, or a simple denial of service if the attacker drops the frames. Limits: it only works inside one broadcast domain (one VLAN), which is one reason segmentation matters.

Finding it in a capture

The lab capture arp.pcap holds a normal request and reply and, five seconds later, a second reply for the same address from a different MAC. Scope: a sample file in your sandbox. -e prints the Ethernet header, so the sender MAC is visible:

bash · try it yourself
tcpdump -e -nn -r $SAMPLES/pcap/arp.pcap
reading from file /opt/sec-samples/pcap/arp.pcap, link-type EN10MB (Ethernet), snapshot length 65535
10:00:00.000000 02:00:00:0a:0a:05 > ff:ff:ff:ff:ff:ff, ethertype ARP (0x0806), length 42: Request who-has 10.10.0.1 tell 10.10.0.5, length 28
10:00:00.000400 02:00:00:0a:0a:01 > 02:00:00:0a:0a:05, ethertype ARP (0x0806), length 42: Reply 10.10.0.1 is-at 02:00:00:0a:0a:01, length 28
10:00:05.000000 02:00:00:0a:0a:66 > 02:00:00:0a:0a:05, ethertype ARP (0x0806), length 42: Reply 10.10.0.1 is-at 02:00:00:0a:0a:66, length 28

The third line is the attack: nobody asked again, yet 02:00:00:0a:0a:66 claims to be the gateway. The same check as a small detector, the logic of tools like arpwatch: remember the first MAC seen for every IP address and alert when a reply contradicts it. It uses scapy, which is installed in the session image, to read the pcap:

bash · try it yourself
python3 - "$SAMPLES/pcap/arp.pcap" <<'PY'
import sys
from scapy.all import rdpcap, ARP
seen = {}
for pkt in rdpcap(sys.argv[1]):
    if ARP in pkt and pkt[ARP].op == 2:                     # op 2 = "is-at" reply
        ip, mac = pkt[ARP].psrc, pkt[ARP].hwsrc
        if ip in seen and seen[ip] != mac:
            print(f"ALERT {ip} changed from {seen[ip]} to {mac}")
        else:
            print(f"learned {ip} is-at {mac}")
        seen.setdefault(ip, mac)
PY
learned 10.10.0.1 is-at 02:00:00:0a:0a:01
ALERT 10.10.0.1 changed from 02:00:00:0a:0a:01 to 02:00:00:0a:0a:66

A change is not always an attack: a replaced network card, a failover pair of routers, a virtual machine moved to another host. Good detectors keep an inventory of expected MACs (from DHCP and asset records) and treat changes for the gateway and servers as high priority.

ARP spoofing: fixes

  • Dynamic ARP Inspection (DAI) on managed switches drops ARP replies whose IP/MAC pair does not match the switch's DHCP snooping table. It is the real fix on wired networks.
  • Port security and 802.1X limit which devices can plug in at all.
  • Client isolation on Wi-Fi stops clients from talking to each other directly.
  • Static ARP entries for a few critical addresses on critical hosts (not scalable, but sometimes right).
  • Segmentation: small VLANs mean fewer neighbours who could spoof.
  • Encryption with authentication on top: TLS, SSH and VPNs make an on-path attacker see only ciphertext and fail certificate checks. This is the defence that still works when the network layer has been fooled.

DNS spoofing: mechanism

DNS spoofing gives a client a false answer for a name. It comes in three main forms:

Form Where the lie is told Example
On-path forgery an attacker on the network answers the query before the real resolver after ARP spoofing, reply to A? www.lab.test with the attacker's address
Cache poisoning a resolver accepts a forged answer and serves it to all its users until the TTL expires race the real authoritative answer with forged packets
Rogue resolver the client is configured to use a resolver the attacker controls a malicious DHCP server, a changed router setting, malware

In the lab capture dns.pcap you can see what a forged answer must match. A response is accepted only if it comes back to the client's source port with the same transaction ID and the same question:

bash · try it yourself
tcpdump -nn -r $SAMPLES/pcap/dns.pcap
reading from file /opt/sec-samples/pcap/dns.pcap, link-type EN10MB (Ethernet), snapshot length 65535
10:00:00.000000 IP 10.10.0.5.40001 > 10.10.0.53.53: 6699+ A? www.lab.test. (30)
10:00:00.001200 IP 10.10.0.53.53 > 10.10.0.5.40001: 6699* 1/0/0 A 10.10.0.10 (58)
10:00:00.500000 IP 10.10.0.5.40002 > 10.10.0.53.53: 6700+ A? nope.lab.test. (31)
10:00:00.501100 IP 10.10.0.53.53 > 10.10.0.5.40002: 6700 NXDomain* 0/1/0 (105)

The IDs 6699 and 6700 (0x1a2b and 0x1a2c) and the ports 40001 and 40002 are the secrets an off-path attacker has to guess. Before 2008 many resolvers used a fixed source port, so only the 16-bit ID stood in the way, and an attack published that year (the Kaminsky attack) showed how to race it again and again for random names until one forged answer won. The arithmetic shows why source-port randomisation was the emergency fix:

python · try it yourself
import math

# A forged answer is accepted only if it matches the query's transaction ID (16 bits)
# and arrives on the right UDP source port, before the real answer.
forged_per_race = 100          # forged packets the attacker squeezes in before the real answer
for name, ports in [("fixed source port", 1), ("randomised port, Linux default range", 60999 - 32768 + 1)]:
    space = 65536 * ports
    p = forged_per_race / space                       # chance to win one race
    races = math.log(0.5) / math.log(1 - p)           # races needed for a 50 % chance overall
    print(f"{name:37} space {space:>13,}  p(win one race) {p:.2e}  races for 50 %: {races:,.0f}")
fixed source port                     space        65,536  p(win one race) 1.53e-03  races for 50 %: 454
randomised port, Linux default range  space 1,850,212,352  p(win one race) 5.40e-08  races for 50 %: 12,824,694

With 100 forged packets per race, a fixed port falls in about 454 races, which takes seconds. With the port randomised over a range of about 28,000 ports (the size of Linux's default ephemeral range), it needs about 13 million races. Randomisation made blind poisoning impractical but did not make DNS authenticated: an attacker on the path sees the ID and port and needs no guessing at all.

How it is detected

  • ARP: IP-to-MAC changes for gateways and servers, many unsolicited replies, one MAC claiming many IP addresses; switch logs of DAI drops.
  • DNS: answers that differ from what the authoritative servers say, short or odd TTLs, responses from addresses that are not your resolvers, a burst of responses with wrong IDs (a guessing attack in progress), DNSSEC validation failures in resolver logs, and clients configured with unknown resolvers.
  • Downstream: certificate warnings and connections to unknown addresses for well-known internal names.

How DNS spoofing is prevented

  1. DNSSEC: zones are signed and validating resolvers reject forged records, which defeats cache poisoning for signed zones. Sign your zones and validate on your resolvers.
  2. Encrypted transport to the resolver (DNS over TLS, DNS over HTTPS) stops forgery and snooping between client and resolver; it does not authenticate the data itself, which is DNSSEC's job.
  3. Resolver hardening: source-port and ID randomisation, no open recursion for the Internet, DNS cookies (RFC 7873), patched software.
  4. Control the configuration: DHCP snooping against rogue DHCP servers, managed resolver settings on endpoints, alerts on changes.
  5. TLS on top again: a poisoned name still fails certificate validation if the attacker has no valid certificate for it.

Pitfalls

  • Trusting the local network. Office Wi-Fi, a hotel network and a shared lab are all places where a neighbour can answer ARP.
  • Ignoring certificate warnings, the one place where a spoof becomes visible to the user.
  • Confusing DoH with DNSSEC. One protects the channel, the other the data.
  • Alerting on every MAC change. Without an inventory, alerts drown in failovers and DHCP churn.

Practice

1. In arp.pcap, what makes the third packet suspicious even before you compare MACs?

It is an unsolicited reply: there is no matching request just before it, and it is sent directly to 10.10.0.5. Combined with the changed MAC for the gateway address, it is the classic spoofing pattern.

2. Change forged_per_race to 1,000. How many races does each case need for a 50 % chance?

The chance per race grows tenfold, so the number of races falls about tenfold: roughly 45 with a fixed port and roughly 1.3 million with randomised ports. Run the program to see the exact figures.

3. Your switch supports DAI. What must be in place first, and what about hosts with static addresses?

DHCP snooping, which builds the table of valid IP/MAC/port bindings that DAI checks against. Hosts with static addresses are not in that table, so they need static bindings (ARP ACLs) or their replies are dropped.

Test yourself

Why can any host on a LAN spoof ARP?

  1. ARP replies are encrypted with a shared key
  2. ARP has no authentication and hosts accept unsolicited replies
  3. Only routers can send ARP
  4. ARP works across the Internet

Which switch feature drops ARP replies that do not match the DHCP snooping table?

  1. Spanning Tree
  2. Dynamic ARP Inspection
  3. Port mirroring
  4. QoS

What did source-port randomisation add against blind DNS cache poisoning?

  1. Encryption of DNS answers
  2. Another value of about 15 bits the attacker must guess together with the 16-bit ID
  3. Signatures on records
  4. Nothing

Which mechanism lets a resolver reject forged DNS records cryptographically?

  1. DNS over HTTPS
  2. DNSSEC
  3. A longer TTL
  4. ARP inspection