Man-in-the-Middle (MITM)

Difficulty: Beginner

Question

What is a man-in-the-middle (MITM) attack, how is it carried out, and how do HTTPS and HSTS prevent it?

Answer

A man-in-the-middle attack is when an attacker secretly positions themselves between two parties, such as your browser and your bank, so that traffic flows through them. They can read it (eavesdropping), change it (tampering), or impersonate either side, while both parties believe they are talking directly. TLS with proper certificate validation, enforced by HSTS, is the main defence on the web.

How attackers get in the middle

  • Rogue or open Wi-Fi: a hotspot named "Airport_Free_WiFi" run by the attacker, or an evil twin of a real cafe network. All your traffic passes through their device.
  • ARP spoofing on a local network: the attacker tells devices "I am the router" so packets are sent to them first.
  • DNS spoofing: poisoned DNS answers point bank.example to the attacker's server.
  • Compromised routers or malicious proxies, including browser extensions and malware that installs its own root certificate.
  • BGP hijacking at internet scale, where routes for an IP range are announced by the wrong network.

Why HTTPS stops most MITM attacks

TLS gives three guarantees: confidentiality (encryption), integrity (tampering is detected) and authentication (the server proves it owns the domain using a certificate signed by a trusted Certificate Authority). An attacker in the middle can still see that you connected to an IP and, usually, the hostname via SNI, but cannot read or change the content. If they present their own certificate, the browser shows a full-page warning because it is not valid for that domain.

SSL stripping and HSTS

The weak point is the first request. If a user types bank.example, the browser tries http:// first. An attacker in the middle can keep the victim on HTTP while talking HTTPS to the real server, quietly reading everything. This is SSL stripping.

HSTS fixes it. The header Strict-Transport-Security: max-age=31536000; includeSubDomains; preload tells the browser to only use HTTPS for that domain for a year, convert any http:// link to https:// internally, and not let users click through certificate errors. Submitting the domain to the HSTS preload list protects even the very first visit, since browsers ship with the list built in.

Defences by layer

LayerDefence
TransportTLS 1.2 or 1.3 everywhere, including internal service calls
BrowserHSTS with preload, secure cookies (Secure attribute)
Mobile appsStrict certificate validation; pinning only for high-risk apps, with a backup pin
Service to serviceMutual TLS (mTLS), common in service meshes like Istio
UsersAvoid ignoring certificate warnings; use a trusted VPN on public Wi-Fi

Common mistakes developers make

  • Disabling certificate checks to "make it work" in code, such as rejectUnauthorized: false in Node.js, verify=False in Python requests, or a trust-all TrustManager in Android. This silently enables MITM in production.
  • Serving login over HTTPS but other pages or API calls over HTTP.
  • Mixed content: loading scripts over HTTP on an HTTPS page.
  • Forgetting the Secure flag, so a session cookie leaks on any HTTP request.
SSL stripping without HSTS. The victim stays on HTTP while the attacker speaks HTTPS to the real bank.
  1. Victim browser → Attacker: GET http://bank.example (plain HTTP)
  2. Attacker → Real bank: GET https://bank.example
  3. Real bank → Attacker: HTTPS login page
  4. Attacker → Victim browser: Same page rewritten to HTTP links
  5. Victim browser → Attacker: Username and password in plain text

Code examples

Enabling HSTS and redirecting HTTP to HTTPS (Nginx)

server {
  listen 80;
  server_name example.in www.example.in;
  return 301 https://$host$request_uri;   # never serve content on HTTP
}

server {
  listen 443 ssl;
  http2 on;
  server_name example.in www.example.in;
  ssl_protocols TLSv1.2 TLSv1.3;
  add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
  # ... certificates and locations
}

The redirect handles the first HTTP request; HSTS makes the browser skip HTTP entirely on later visits. Only add preload once every subdomain serves HTTPS.

The dangerous anti-pattern to avoid

import requests

# BAD: disables certificate validation, anyone in the middle can impersonate the API
requests.get("https://api.partner.example/v1/payouts", verify=False)

# GOOD: default verification, or point to a private CA bundle if needed
requests.get("https://api.partner.example/v1/payouts")
requests.get("https://internal.svc/health", verify="/etc/ssl/certs/internal-ca.pem")

Turning off verification keeps the encryption but removes authentication, which is exactly what a MITM attacker needs. Trust a specific CA bundle instead.

Key points

  • MITM lets an attacker read, alter or impersonate traffic between two parties
  • Common vectors: rogue Wi-Fi, ARP spoofing, DNS spoofing, malicious proxies
  • TLS provides encryption, integrity and server authentication
  • HSTS (ideally preloaded) prevents SSL stripping on the first HTTP request
  • Never disable certificate validation in production code

Common follow-up questions

  • How does the TLS handshake authenticate the server?
  • What is certificate pinning and what are its operational risks?
  • How does mTLS differ from regular TLS?
  • Can a corporate proxy inspect HTTPS traffic? How?

Interview tips

  • Name at least two concrete ways an attacker gets in the middle, not just 'intercepts traffic'
  • Explain SSL stripping and HSTS; it shows you know HTTPS alone has a first-request gap
  • Mention verify=False or rejectUnauthorized: false as a real-world developer mistake

Concepts covered

MITM, ARP Spoofing, SSL Stripping, HSTS, Certificate Validation

More Web Security (OWASP, JWT, XSS, CSRF) questions