JWT Algorithm Confusion Lab: Forge an Admin Token With a Public Key

✨ AI Summary
AuthBox signs its JWTs with RS256 — a private key signs, and the public key (which anyone can have) verifies. The trouble is the verifier reads the token's own alg header to decide how to check the signature. Tell it HS256 and it will verify with a symmetric key — and the only key it has is the public one everybody knows.
Lab at a glance: Advanced · 130 XP · about 20 minutes · runs in your browser, no signup or setup. Try it yourself first; the full solution is behind a spoiler below.

What this lab teaches

JWT algorithm confusion exploits a verifier that trusts the token's alg header. RS256 (asymmetric) verifies with a public key; HS256 (symmetric) verifies by re-computing an HMAC with a shared secret. If the verifier lets the token choose the algorithm, an attacker switches it to HS256 and signs with the public key — which, for HS256, is now a secret both sides 'share'.

The lesson: 'verify with this key' means different things for different algorithms. Letting the token pick the algorithm breaks that assumption.

How AuthBox verifies its tokens

AuthBox hands out RS256 tokens and publishes its public key, as RSA systems do. Its verifier, though, reads the alg header and picks the matching routine. Because the public key is known, an attacker can forge an HS256 token whose 'secret' is that public key, and the verifier will accept it.

The attack approach

  • Note the tokens are RS256 and the public key is available, as it is meant to be.
  • Change a token's header to claim HS256 and set the role claim to admin.
  • Sign the modified token with HMAC, using the public key bytes as the HMAC secret.
  • Present it. The verifier follows the alg header, runs HMAC with the public key, and the signature checks out.

Full walkthrough (spoiler)

If the forged token is rejected, the key bytes or the header are not quite right — open the steps.

Show the step-by-step solution
  1. Take a valid token and change its header algorithm from RS256 to HS256.
  2. Set the payload's role claim to admin.
  3. Compute the HMAC signature over the new header and payload, using the server's public key as the HMAC secret.
  4. Send the token. The verifier reads alg=HS256, HMACs with the public key it holds, gets a match, and trusts the admin claim. The lab detects the valid admin token and marks you solved.

Why it works

RS256 and HS256 both 'use a key', but in opposite ways: RS256 verifies with the public half of a key pair, while HS256 verifies with a shared secret. A verifier that obeys the token's alg header lets the attacker reinterpret the well-known public key as the shared HMAC secret. The signature is valid by the rules the attacker chose.

Core principlePin the expected algorithm at the verifier. Never let the token's alg header decide how its signature is checked.

Where this happens in the real world

Algorithm confusion is a well-documented JWT flaw, and several JWT libraries over the years have shipped verifiers permissive enough to allow it. It — together with the related 'none' algorithm bug — is why security guidance insists on hard-coding the accepted algorithm. It maps to the Identification and Authentication Failures category (A07) in the OWASP Top 10 (2021).

How to defend against it

  • Hard-code the accepted algorithm at the verifier and reject tokens whose header says anything else.
  • Use separate, type-specific keys so a public verification key is never usable as an HMAC secret.
  • Prefer libraries that require you to specify the algorithm rather than reading it from the token.
  • Validate all standard claims (issuer, audience, expiry) in addition to the signature.
Mr Elite tip: When testing JWT auth you are authorised to assess, try changing alg to HS256 and, separately, to none. A verifier that accepts either is trusting the token to describe its own security.

Try it, then go further

Reading about it is the easy part. Open the lab, try your own approach before peeking at the spoiler, and see how many attempts it takes.

❓ Frequently asked questions

What is JWT algorithm confusion?
An attack where a verifier trusts the token's alg header, so an attacker switches an RS256 token to HS256 and signs it with the public key — which the verifier then accepts as the HMAC secret.
Why can the public key act as a secret?
HS256 verifies by re-computing an HMAC with a shared secret. If the verifier uses the RSA public key bytes as that secret, and the public key is known, the attacker can produce a matching signature.
How is this different from the none algorithm bug?
The none bug drops the signature entirely; algorithm confusion keeps a valid-looking signature but computed with the wrong key type. Both come from trusting the alg header.
How do I fix it?
Hard-code the accepted algorithm at the verifier, use type-specific keys, and pick libraries that make you specify the algorithm explicitly.
⚡
Join free to earn XP for reading this article Track your progress, build streaks and compete on the leaderboard.
Join Free
Lokesh N. Singh aka Mr Elite
Lokesh N. Singh aka Mr Elite
Founder, Securityelites · AI Red Team Educator
Founder of Securityelites and creator of the SE-ARTCP credential. Working penetration tester focused on AI red team, prompt injection research, and LLM security education.
About Lokesh ->

Leave a Comment

Your email address will not be published. Required fields are marked *