# How MITM Attacks Work — and How to Stop Them

> Picture this: you're at a coffee shop, connected to the free Wi-Fi. You open your banking app. What you don't know is that someone two tables away is quietly intercepting every packet you send — reading your requests, seeing your responses, potentially changing them. This is a man-in-the-middle attack. Here's how it works, and what actually stops it.
## What is a man-in-the-middle attack?

A man-in-the-middle (MITM) attack is when an attacker secretly positions themselves between two communicating parties — intercepting, and sometimes modifying, the traffic flowing between them. Neither the client nor the server knows the attacker is there. From both sides, the connection appears to be direct and normal.

## ARP poisoning — the most common technique on local networks

On a local network, devices use ARP (Address Resolution Protocol) to translate IP addresses into MAC addresses. If you want to send data to 192.168.1.1 (the router), your device asks the network "who has 192.168.1.1?" and the router replies with its MAC address. Your device caches this in its ARP table.

The problem: ARP has no authentication. Anyone can send an unsolicited ARP reply, and devices will accept it. An attacker exploits this by broadcasting fake ARP replies:

```
Normal situation:
  Victim  ──────────────────────────►  Router  ──►  Internet
          "192.168.1.1 = AA:BB:CC"

After ARP poisoning:
  Victim  ──────►  Attacker  ──────►  Router  ──►  Internet
          "192.168.1.1 = EE:FF:00"  (attacker's MAC)

The attacker now sees ALL traffic before forwarding it on.
```

The attacker sends two fake ARP messages: one to the victim saying "I am the router," and one to the router saying "I am the victim." Now all traffic flows through the attacker's machine, who can read or modify it before passing it on.

## What can an attacker do once they're in the middle?

- **Eavesdrop:** Read any unencrypted traffic — HTTP pages, unencrypted email, DNS queries
- **SSL stripping:** Downgrade HTTPS connections to HTTP, then read them (works on sites that don't enforce HSTS)
- **Session hijacking:** Steal session cookies from HTTP traffic and use them to impersonate you on a website
- **DNS spoofing:** Return fake DNS responses, redirecting you to a cloned phishing site instead of the real one
- **Content injection:** Modify webpage content in transit — inject malicious scripts, change bank account numbers, alter displayed prices

## Why HTTPS largely defeats MITM

If you're on HTTPS, even if an attacker is intercepting your traffic, they only see encrypted data. They can't read it and they can't modify it without breaking the encryption. This is why HTTPS adoption was such a huge security win for the internet.

However, SSL stripping can still work if you navigate to HTTP first, before the redirect to HTTPS. Browsers address this with HSTS — a mechanism that tells your browser to always use HTTPS for a specific domain, even if you type HTTP.

## Defences against ARP poisoning

- **Dynamic ARP Inspection (DAI):** A feature on managed switches that validates ARP packets against a trusted DHCP database — forged ARP replies get dropped automatically
- **Static ARP entries:** Manually set the router's MAC address so it can't be overwritten by a fake ARP reply (impractical at scale, but works for critical devices)
- **Use a VPN on public networks:** Encrypts all your traffic from the device, so even an ARP poisoning MITM only sees encrypted data
- **HTTPS everywhere + HSTS:** Prevents SSL stripping on sites that enforce it
- **Monitor for ARP anomalies:** Multiple ARP replies from unexpected MACs, or a MAC claiming to be multiple IPs, are red flags

> ⚠️ **Lab note**
> You can safely simulate an ARP poisoning attack in your own isolated home lab (two VMs on a host-only network). Never run ARP poisoning tools on any network you don't own — it is illegal and will disrupt legitimate users' traffic.



