00->1
Learning pathStep 37 of 62Network attacks and defence ← All writeups

Man-in-the-Middle (MITM) Attacks, Explained

If you are reading this, you have probably seen "MITM" in network courses and in advice about public Wi-Fi, and you want to know what actually happens, how an attacker gets into the middle in the first place, and why HTTPS is the main defence.

By the end you will be able to define a man-in-the-middle (MITM) attack, list the ways an attacker gets between two parties, explain why TLS stops most of them and how it has been defeated, describe two real incidents, and apply the defences. It builds on Spoofing, Handshakes (the TLS section) and Computer and Networking Basics.


1. What a MITM attack is

NIST's glossary gives two wordings (source):

  • "An attack in which an attacker is positioned between two communicating parties in order to intercept and/or alter data traveling between them." (NIST SP 1800-21B)
  • "An attack where the adversary positions himself in between the user and the system so that he can intercept and alter data traveling between them." (NISTIR 7711)

OWASP describes the mechanics: the attacker "splits the original TCP connection into two new connections, one between the client and the attacker and the other between the attacker and the server" (OWASP). Each side thinks it is talking to the other. The attacker relays the messages, and can read or change them on the way. MITRE ATT&CK now calls the technique Adversary-in-the-Middle (T1557).

A picture: a courier delivers letters between two people. A dishonest courier opens each letter, reads it, perhaps rewrites it, reseals it, and delivers it. Neither person notices unless the seal or the handwriting looks wrong. In networking, encryption and authentication are the seal and the handwriting.

What the attacker can do

Capability Example
Eavesdrop (confidentiality) Read passwords, messages, cookies. OWASP's example is capturing a session cookie from an unencrypted HTTP header
Modify (integrity) Change the data in transit. OWASP's example is changing the amount of a money transaction
Inject Add content, such as a fake login prompt or malicious download
Impersonate Reuse stolen credentials or session cookies to act as the victim

2. How the attacker gets "in the middle"

An attacker does not start in the middle. They have to arrange it. ENISA, the EU cybersecurity agency, lists the usual ways in its analysis of the DigiNotar incident: "by using a rogue hotspot, by poisoning the DNS cache or ARP cache, by using malware on the victim's machine, or by accessing the traffic at ISPs directly." MITRE ATT&CK's sub-techniques cover the local-network ones:

Method How it puts the attacker in the path Read more
ARP cache poisoning (T1557.002) Forged ARP replies make devices send traffic for the router's IP address to the attacker's MAC address. ARP "does not require authentication" Spoofing
DHCP spoofing (T1557.003) A fake DHCP server hands out the attacker's address as the default gateway or DNS server Spoofing
LLMNR / NBT-NS / mDNS poisoning and SMB relay (T1557.001) On Windows networks, devices ask the whole local network to resolve a name. The attacker answers "that is me", then captures or relays the authentication hashes that follow
Evil twin (T1557.004) A fake Wi-Fi access point with the same name as a trusted one Evil Twin
DNS poisoning or DNS hijacking The name resolves to the attacker's address DNS Poisoning
Malware on the victim device ENISA lists malware on the victim's machine as one route. Software that intercepts traffic on the device can see it before it is encrypted, or after it is decrypted How Malware Works
A position on the path An ISP, a router that has been compromised, or anyone who can tap the traffic

Two more meanings of "in the middle" appear in the news:

  • Adversary-in-the-middle phishing. A fake login page relays what you type to the real site in real time and captures your password and your session. Covered in Phishing. The defence there is phishing-resistant multi-factor authentication (see Authentication vs. Authorization).
  • Manipulator-in-the-middle and adversary-in-the-middle. These are newer names for the same idea. OWASP's page title uses "manipulator", MITRE uses "adversary". Exams and older documents still say "man-in-the-middle."

3. Why TLS stops it, and how TLS has been defeated

HTTPS gives encryption, integrity and authentication of the server (see Handshakes). If an attacker sits in the middle of an HTTPS connection, they can see encrypted bytes, but they cannot read or alter them. To get further, they must make your browser believe that they are the server, and for that they need a certificate your browser trusts for that name.

So the attacker's real options are:

Attacker's route Why it can work Defence
Downgrade to HTTP ("SSL stripping") The user types bank.example without https://, and the first request is plain HTTP. The attacker answers on HTTP and talks HTTPS to the real site on the user's behalf HSTS (below)
A certificate your device already trusts A certificate authority is hacked, a trusted root is installed on the device, or a certificate is issued by mistake Browser and OS trust management, Certificate Transparency monitoring, removing rogue roots
The user clicks through a warning The attacker presents an invalid certificate and the user accepts it Never accept certificate warnings. MITRE lists training users to recognise certificate errors as a mitigation
Control of DNS records The attacker changes DNS records, then requests a valid certificate for the domain Multi-factor authentication on DNS accounts, monitoring (below)

HSTS: stopping the downgrade

HTTP Strict Transport Security (RFC 6797) lets a site tell browsers: "only contact me over HTTPS." The server sends a header such as Strict-Transport-Security: max-age=31536000. For that period, the browser turns any http:// link to the site into https:// before connecting, and it ends the connection on any TLS error or warning instead of letting the user click through. That blocks SSL stripping.

The RFC is open about the gap: the very first visit, before the browser has seen the header, is unprotected if the user typed an http address. Browsers close the gap with preload lists of sites that are always HTTPS.

Real case 1: DigiNotar (2011)

DigiNotar was a Dutch certificate authority, a company browsers trusted to vouch for websites. ENISA's report, drawing on the Fox-IT investigation, says the attack began in mid-June 2011 and that for almost two months false certificates were used to spy on email and web browsing in Iran. Attackers created false certificates for hundreds of websites, including a wildcard certificate for Google. According to the Fox-IT report as covered by iTnews, 531 rogue certificates were detected, and 298,140 unique IP addresses were victims of the MITM attack, almost all in Iran. ENISA gives a more cautious reading: the logs showed about 300,000 certificate-status (OCSP) requests from Iranian addresses, which "suggests that around 300.000 Iranian users were victims", but it warns that this is only an indication, because not all browsers make those requests and the requests could have been blocked or faked.

ENISA drew three lessons:

  1. DigiNotar did not report the attack straight away.
  2. HTTPS places default trust in hundreds of authorities, so "the security of HTTPS equates to the security of the weakest CA".
  3. Basic security measures had not been applied.

DigiNotar did not have a record of every rogue certificate, so the only remedy was removing its root from all browsers. Wikipedia records that major browsers removed it by the end of August 2011 and that the company filed for bankruptcy on 20 September 2011.

Real case 2: Lenovo Superfish (2015)

CISA's alert TA15-051A describes adware preinstalled on some Lenovo PCs from September 2014. It installed its own trusted root certificate and then intercepted, decrypted and re-encrypted users' HTTPS traffic, in order to deliver targeted advertisements. That is a MITM on the user's own machine. The same private key could be recovered from the software, so an attacker anywhere on the network could create certificates for any site that these PCs would trust without a warning. CISA's remediation: uninstall the software and manually remove the root certificate, because uninstalling alone does not remove it.

Other cases worth knowing

  • DNS infrastructure hijacking (CISA AA19-024A). Attackers who obtained credentials for accounts that can change DNS records altered address, mail or name-server records to point at their infrastructure. Because they controlled the records, they could also obtain valid certificates, so victims saw no warnings. CISA's mitigations: change passwords, use multi-factor authentication on registrar and DNS accounts, audit public DNS records, and revoke fraudulent certificates. It also recommends monitoring Certificate Transparency logs for certificates you did not request.
  • State-required certificates (Kazakhstan, 2019). According to Wikipedia, the government told ISPs to require users to install a state certificate, which would have enabled interception of HTTPS traffic. On 21 August 2019, Mozilla and Google said Firefox and Chrome would reject it, and Apple made similar commitments. The episode shows that a certificate you install yourself becomes a trusted door for whoever holds its key.

4. How to tell

Sign Meaning
A browser certificate warning you have not seen before on a site you use Possible interception, or an expired or misconfigured certificate. Do not click through
The certificate issuer has changed for a site you use often Can be a corporate proxy (legitimate and visible to IT) or an attacker. Ask your IT team
A second device claims your router's IP address, or the router's MAC address changes ARP poisoning on the local network
An unexpected DHCP server, or unusual gateway or DNS settings DHCP spoofing
A Wi-Fi network you know asks for a login you have never been asked for Possible evil twin
Valid certificates for your domain that you did not request (Certificate Transparency logs) Possible DNS hijack or a mistakenly issued certificate

5. Defences

For everyone

Control Why
Use HTTPS and check the address bar Encrypts and authenticates the connection
Never click through certificate warnings That is the user-side route for an attacker
Use a VPN or a mobile hotspot on untrusted networks NSA's guidance for public settings, as summarised by Malwarebytes, recommends a corporate or personal hotspot with strong authentication and encryption, and a VPN and HTTPS when secure networks are unavailable
Turn off auto-connect for Wi-Fi AFP's advice after its evil-twin case
Use phishing-resistant MFA Stops a relayed login from working (see Authentication vs. Authorization)
Keep devices updated and remove software you did not choose Superfish shows how a pre-installed program can break HTTPS

For website owners

Control Why
HTTPS everywhere with TLS 1.2 or 1.3 See Handshakes
HSTS, with preload once you are sure Blocks SSL stripping
Redirect HTTP to HTTPS and avoid mixed content Removes the unprotected first request where possible
Protect DNS and registrar accounts with MFA The route in CISA AA19-024A
Monitor Certificate Transparency logs Alerts you to certificates issued for your domain that you did not ask for
Secure session cookies (Secure, HttpOnly, SameSite) A stolen cookie is as good as a password (see XSS and CSRF)

For network administrators

Control Source
Dynamic ARP Inspection and DHCP snooping on switches Cisco and MITRE (T1557.002, T1557.003)
Disable LLMNR, mDNS and NetBIOS name resolution where not needed, and enable SMB signing MITRE T1557.001
Network segmentation MITRE M1030
Wireless intrusion prevention to find rogue access points and evil twins MITRE T1557.004, NIST SP 800-153
Network intrusion detection for MITM patterns MITRE M1031
Encrypt traffic internally too MITRE M1041

6. Common mistakes

Myth The facts
"MITM only happens on public Wi-Fi" Public Wi-Fi is one place. It also happens on office LANs, through DNS and certificate attacks, and on infected devices
"The padlock means nobody is in the middle" It means the connection is encrypted to a certificate your device trusts for that name. If the trust itself is compromised (DigiNotar, Superfish, a hijacked DNS record), the padlock can show for the attacker
"A VPN makes me immune" It protects the path between you and the VPN. The VPN provider and the destination are still trust points
"Every re-signed certificate is an attack" A company can deliberately install its own root certificate on managed devices. It is the same mechanism as Superfish, so it depends entirely on who controls the key and whether the user knows about it
"A certificate warning is probably nothing" It might be an expired or misconfigured certificate, but it is also one of the few signs of interception a user can see. Treat it as a stop
"HTTPS stops ARP spoofing" ARP spoofing still redirects traffic. TLS stops the attacker from reading or changing it
"It is the same as phishing" Phishing tricks a person. MITM attacks the path. Adversary-in-the-middle phishing combines them

Closing

A man-in-the-middle attack puts the attacker between two parties so they can read, change or inject data. They get there by poisoning ARP or DNS, running a fake DHCP server or hotspot, using malware, or controlling a point on the path. TLS defeats most of it, so determined attackers aim at TLS's trust instead: a hacked certificate authority (DigiNotar), a rogue root installed on the device (Superfish), or control of DNS records that lets them obtain real certificates. The defences are HTTPS with HSTS, never accepting certificate warnings, protecting DNS and certificate processes, MFA that resists phishing, and switch-level controls on local networks.

Keep these points:

  • MITM = relay plus reading or changing. Each side thinks it is talking to the other.
  • Getting in the middle takes ARP, DHCP, name-resolution or DNS tricks, a fake hotspot, malware, or control of the path.
  • TLS stops reading and changing unless the attacker also gets a trusted certificate or downgrades the connection.
  • HSTS blocks SSL stripping. The first visit is the gap, which preload lists close.
  • Never click through a certificate warning.
  • DigiNotar and Superfish show that trust can be attacked, not only traffic.
  • On untrusted networks, use a VPN or your own hotspot.