Algorithm Confusion Attacks
What you'll learn: How attackers exploit JWT implementation flaws by switching signature algorithms to trick servers into misusing cryptographic keys.
The Core Vulnerability
You've learned that JWTs support multiple signature algorithms like RS256 (asymmetric with RSA keys) and HS256 (symmetric with shared secrets). Algorithm confusion attacks exploit careless JWT validation by changing the algorithm in the JWT header from RS256 to HS256.
Here's the trick: In RS256, the server uses a public key to verify signatures (which anyone can access) and a private key to sign them. In HS256, both signing and verification use the same shared secret.
If a server accepts both algorithms without strict validation, an attacker can:
- Obtain the server's public key (often publicly available)
- Create a malicious JWT
- Change the
algheader field fromRS256toHS256 - Sign the JWT using the server's public key as the HMAC secret
- Send it to the server
The confused server now treats the public key (which the attacker knows) as an HMAC secret and successfully validates the forged token!
Why This Works
Think of it like having two door locks: one needs a special key (private), and another accepts any matching pattern (shared secret). If someone tricks you into using the "pattern matching" lock with a pattern they already know (your public key), they can unlock your door.
The vulnerability exists because the server trusts the JWT's self-declared algorithm instead of enforcing which algorithm it expects.
Key Takeaway: Algorithm confusion attacks succeed when servers blindly trust the JWT header's algorithm field and fail to enforce a strict whitelist, allowing attackers to exploit the difference between asymmetric and symmetric cryptography by treating public keys as HMAC secrets.