JWT Attacks Explained: How Broken Tokens Leak Accounts


Modern web apps rarely store your session on the server. Instead, they hand your browser a JSON Web Token (JWT) — a signed blob that says who you are. The server trusts it because of the signature. But when verification is done badly, that trust becomes the vulnerability: forge the token, and you forge the identity. JWT flaws pay well in bug bounties precisely because they often mean instant account takeover.

How a JWT Works (in 60 Seconds)

A JWT has three parts, separated by dots: header, payload, signature. The header says how it is signed, the payload carries claims like user ID and role, and the signature proves nobody tampered with it. A typical decoded payload looks like this:

{"sub": "1234", "name": "anony", "role": "user", "exp": 1793558400}

The server checks the signature with its secret key, then trusts the payload. Everything that follows depends on that one check being done correctly.

Attack 1: The “None” Algorithm

Some libraries once accepted tokens whose header declared algorithm none — meaning “no signature, trust me.” An attacker decodes their own token, changes “role”: “user” to “role”: “admin”, strips the signature, sets the algorithm to none, and the server lets them in. Modern libraries reject none by default, but old or custom code still falls for it. In a bounty target, always test whether the server accepts an unsigned token.

Attack 2: Weak or Public Secrets

JWTs are often signed with HMAC using a shared secret. If that secret is weak (“secret”, “password”, “123456”) or leaked — in a public repo, a JavaScript bundle, a config file — an attacker can sign any token they like. Tools like hashcat can brute-force weak HMAC secrets offline from a captured token. If you find a secret in a target’s public assets, that is a serious finding on its own.

Attack 3: Algorithm Confusion (RS256 to HS256)

This is the classic. The server expects RSA signatures (RS256, verified with a public key) but the attacker sends an HMAC token (HS256) signed with that same public key — which is public by design. The server, confused, verifies the HMAC signature using the public key as the secret, and it passes. One token, full forgery. The defense is strict: the server must only accept the exact algorithm it expects, never whatever the token’s header claims.

Attack 4: Ignored Expiry and Missing Checks

Some apps never check the exp claim, so a stolen token works forever. Others trust the payload without verifying the signature at all on certain endpoints. Always test: does a modified payload get rejected? Does an expired token still work? Does removing the signature break access? Each “yes, it still works” is a finding.

How to Hunt Them (Legally)

Only on targets in scope, with permission — bug bounty programs or your own apps:

  1. Decode your own token at a JWT debugger and study the header and payload.
  2. Try the none algorithm, a tampered payload, and a stripped signature, one change at a time.
  3. Check for leaked secrets in the target’s public code and assets.
  4. Test expiry: wait out a short-lived token or edit exp and see if it is enforced.
  5. Report impact honestly: “I could access another user’s data” beats “I changed my own role claim” — prove the effect on a test account you control, never on a real user’s account.

How to Defend

If you build apps: use a maintained JWT library with strict algorithm allowlists, strong random secrets stored server-side (never in client code), short expiry times with refresh tokens, and signature verification on every request — no exceptions, no “trusted” endpoints that skip it.

JWTs are not magic and they are not broken by design. They break when developers treat a token as trustworthy without verifying it properly. Learn the four attacks above, and you will spot one of the highest-impact bug classes in modern web security.

Comments

Popular Posts