Skip to main content
Category: Attack Techniques

Man-in-the-Middle Attack

Also known as: MITM, MitM, MITM attack, on-path attack, man-in-the-middle
Simply put

A man-in-the-middle attack is a type of cyberattack where an attacker secretly positions themselves between two parties who are communicating, such as a user and a website. The attacker can eavesdrop on the conversation, steal sensitive information, or alter the data being exchanged, typically without either party realizing the communication has been compromised.

Formal definition

A man-in-the-middle (MITM) attack is a network-layer or application-layer attack in which an adversary inserts themselves into the communication path between two entities in order to intercept, relay, and potentially alter data in transit. Attackers typically exploit weak or improperly implemented web-based protocols, insufficient certificate validation, or compromised network infrastructure to gain this position. MITM attacks can target communications between a user and an application, between two services, or between any two networked endpoints. Because the attacker relays traffic between the legitimate parties, both sides may believe they are communicating directly with each other. Defenses typically include enforcing strong transport-layer encryption (such as TLS with proper certificate pinning), mutual authentication, and integrity verification of transmitted data, though detection can be difficult in environments where certificate trust chains are not rigorously validated.

Why it matters

Man-in-the-middle attacks pose a serious threat to application security because they undermine the fundamental trust assumption that two communicating parties are exchanging data privately and without tampering. When an attacker successfully positions themselves on the communication path, they can intercept credentials, session tokens, API keys, and other sensitive data in transit. This is particularly dangerous in application environments where services communicate over networks that may not enforce strong transport-layer encryption, or where certificate validation is improperly implemented. The attack can compromise not only end-user sessions but also service-to-service communications within distributed architectures.

Who it's relevant to

Application Developers
Developers must ensure that their applications enforce strong transport-layer encryption (such as TLS), implement proper certificate validation, and where appropriate use certificate pinning. Failing to validate certificates correctly or allowing protocol downgrades can create openings for MITM attacks even when encryption is nominally in place.
Security Engineers and Architects
Security teams are responsible for designing network and application architectures that minimize the opportunity for attackers to insert themselves into communication paths. This includes enforcing mutual authentication between services, configuring HSTS (HTTP Strict Transport Security), and monitoring for anomalous certificate activity or unexpected protocol downgrades.
DevOps and Platform Engineers
Teams managing infrastructure and deployment pipelines need to ensure that service-to-service communication within distributed systems, including microservices and CI/CD pipelines, uses authenticated and encrypted channels. Compromised or misconfigured internal networks can expose inter-service traffic to interception.
Software Supply Chain Practitioners
MITM attacks can target software supply chains by intercepting package downloads, dependency fetches, or update mechanisms that do not use integrity verification and authenticated transport. Ensuring that package managers validate signatures and enforce HTTPS for repository connections is essential to preventing supply chain compromise via interception.

Inside MITM

Interception
The attacker positions themselves between two communicating parties, typically by exploiting network routing, DNS resolution, or ARP tables, to capture traffic that would normally flow directly between the legitimate endpoints.
Eavesdropping
The attacker passively reads the intercepted communication, potentially capturing credentials, session tokens, API keys, or other sensitive data transmitted between the client and server.
Message Alteration
The attacker may actively modify requests or responses in transit, such as injecting malicious content, changing transaction parameters, or redirecting users to attacker-controlled resources, before forwarding the communication to the intended recipient.
Impersonation
The attacker presents themselves as the legitimate server to the client (and optionally as the legitimate client to the server), often by using forged or fraudulently obtained certificates, rogue access points, or spoofed DNS responses.
TLS/SSL Stripping
A specific technique where the attacker downgrades an HTTPS connection to HTTP by intercepting the initial unencrypted request and maintaining a secure connection to the server while communicating with the client over plaintext.
Session Hijacking
After intercepting authentication tokens or session cookies, the attacker may replay or reuse these credentials to assume the identity of the legitimate user without needing to know the underlying password.

Common questions

Answers to the questions practitioners most commonly ask about MITM.

Does HTTPS completely eliminate the risk of man-in-the-middle attacks?
HTTPS significantly reduces the risk but does not completely eliminate it. MitM attacks can still succeed against HTTPS connections when certificate validation is improperly implemented, when users accept invalid certificate warnings, when trusted certificate authorities are compromised, or when an attacker can install a rogue root certificate on the victim's device. TLS stripping attacks may also downgrade connections before encryption is established if HSTS is not enforced.
Are man-in-the-middle attacks only a concern for public Wi-Fi networks?
No. While public Wi-Fi is a commonly cited attack vector, MitM attacks can occur in many other contexts, including compromised routers on private networks, DNS spoofing at the ISP level, BGP hijacking affecting internet routing, compromised or malicious proxy servers in corporate environments, and supply chain attacks targeting software update channels. Any network path between two communicating parties is a potential interception point.
How should applications implement certificate pinning to mitigate MitM attacks, and what are the trade-offs?
Certificate pinning binds an application to a specific certificate or public key, rejecting connections even if an attacker presents a valid certificate from a different CA. Implementation typically involves embedding expected certificate hashes in the application. The primary trade-off is operational complexity: pinned certificates must be rotated before expiration, and a misconfigured pin can render the application unable to connect. Backup pins and a well-planned rotation strategy are essential to avoid outages.
What role does HSTS play in preventing MitM attacks, and how should it be deployed?
HTTP Strict Transport Security (HSTS) instructs browsers to only communicate over HTTPS, preventing TLS stripping attacks where an attacker downgrades a connection to plaintext HTTP. It should be deployed with the Strict-Transport-Security header, including a sufficiently long max-age value, the includeSubDomains directive where appropriate, and ideally submission to the HSTS preload list. Without preloading, the very first connection remains vulnerable since the browser has not yet received the HSTS header.
How can application security testing tools detect vulnerabilities that could enable MitM attacks?
Static analysis tools can typically detect hardcoded disabling of certificate validation, use of deprecated TLS versions, and missing certificate verification callbacks in code. Dynamic analysis and network testing tools can identify weak cipher suites, improper certificate chain handling, and susceptibility to protocol downgrade attacks at runtime. However, static analysis may produce false negatives for cases where TLS configuration is managed externally (such as through infrastructure-as-code or deployment configurations), and neither approach reliably detects all MitM-enabling conditions without also examining the deployment environment.
What mutual authentication mechanisms help prevent MitM attacks in API-to-API communication?
Mutual TLS (mTLS) is a commonly used mechanism where both the client and server present and validate certificates, ensuring that each party verifies the other's identity. This is particularly relevant for service-to-service communication in microservices architectures. Implementation requires managing a certificate lifecycle for all participating services, which is typically handled through a service mesh or internal PKI. Other approaches include pre-shared keys or token-based authentication layered over TLS, though these may offer narrower protection depending on how they are implemented and validated.

Common misconceptions

Man-in-the-middle attacks only happen on public Wi-Fi networks.
While public Wi-Fi is a common attack vector, man-in-the-middle attacks can occur on any network segment, including corporate LANs, through techniques such as ARP spoofing, DNS poisoning, BGP hijacking, or compromised network infrastructure. They can also occur at the application layer through compromised proxies or certificate authorities.
Using HTTPS completely eliminates the risk of man-in-the-middle attacks.
HTTPS significantly raises the difficulty of interception, but it does not eliminate the risk entirely. Attacks may still succeed through improper certificate validation in application code, user acceptance of invalid certificate warnings, compromised or rogue certificate authorities, TLS stripping on initial HTTP requests before redirection, or pinning bypass techniques. Applications that do not enforce HSTS or that have weak TLS configurations remain vulnerable.
Man-in-the-middle attacks are always detectable by the end user.
Sophisticated man-in-the-middle attacks can be largely transparent to users, especially when the attacker uses valid certificates obtained through compromised CAs or when applications fail to perform strict certificate validation. Without technical controls such as certificate pinning, HSTS, or network monitoring, many interception scenarios produce no visible indicators to the user.

Best practices

Enforce TLS 1.2 or higher on all application endpoints and disable older protocol versions (SSLv3, TLS 1.0, TLS 1.1) to prevent protocol downgrade attacks.
Implement HTTP Strict Transport Security (HSTS) headers with appropriate max-age values and consider HSTS preloading to prevent TLS stripping attacks on initial connections.
Validate server certificates rigorously in application code, including hostname verification, certificate chain validation, and, where appropriate, certificate pinning for high-security mobile or API connections.
Use mutual TLS (mTLS) for service-to-service communication in microservices architectures and API integrations where both endpoints need authenticated identity assurance.
Conduct regular network security assessments and monitor for indicators of interception, such as unexpected ARP changes, rogue DNS responses, or anomalous certificate presentations in internal and external network environments.
Avoid transmitting sensitive data such as credentials, tokens, or PII over unencrypted channels, and ensure that application logic does not fall back to plaintext communication if a secure connection cannot be established.