Difficulty: Beginner
What is a man-in-the-middle (MITM) attack, how is it carried out, and how do HTTPS and HSTS prevent it?
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.
bank.example to the attacker's server.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.
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.
| Layer | Defence |
|---|---|
| Transport | TLS 1.2 or 1.3 everywhere, including internal service calls |
| Browser | HSTS with preload, secure cookies (Secure attribute) |
| Mobile apps | Strict certificate validation; pinning only for high-risk apps, with a backup pin |
| Service to service | Mutual TLS (mTLS), common in service meshes like Istio |
| Users | Avoid ignoring certificate warnings; use a trusted VPN on public Wi-Fi |
rejectUnauthorized: false in Node.js, verify=False in Python requests, or a trust-all TrustManager in Android. This silently enables MITM in production.Secure flag, so a session cookie leaks on any HTTP request.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.
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.
MITM, ARP Spoofing, SSL Stripping, HSTS, Certificate Validation