Course contentsShow
← Security
Lesson 787 of 3,05216. Web Security - Authentication MechanismsFree lesson

Algorithm Confusion Attacks

Switching from RS256 to HS256 to trick servers into using public keys as HMAC secrets.

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:

  1. Obtain the server's public key (often publicly available)
  2. Create a malicious JWT
  3. Change the alg header field from RS256 to HS256
  4. Sign the JWT using the server's public key as the HMAC secret
  5. 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.