Public Key Recovery
Recover an RSA public key from two RS256 signatures, then use it in an algorithm confusion attack.
01
Concept
When a server uses RS256 but doesn't expose its public key, it's still possible to recover the public key mathematically from two JWT signatures sharing the same private key. This uses the GCD (Greatest Common Divisor) of candidate values derived from the RSA-PKCS#1 signature equations.
Once recovered, the public key enables the Algorithm Confusion attack - use it as the HMAC secret with alg: HS256.
Why no browser implementation?
Key recovery requires GCD computation over 4096-bit numbers (RSA-2048 signatures). While possible in JavaScript via BigInt, the multi-step process involving multiple candidate pairs makes a reliable browser implementation impractical compared to the Python tool. This page provides the exact commands to run.
02
Step 1 - Recover the Public Key
1
Obtain two JWT tokens
You need two different JWT tokens signed by the same RS256 private key (same server, different requests with different payloads).
2
Install rsa_sign2n
3
Run the recovery script
4
Extract the public key
The script outputs candidate public keys in PEM format. Multiple candidates may be returned - each corresponds to a possible prime factorization. Test each one in Step 2.
03
Step 2 - Algorithm Confusion with Recovered Key
Once you have the recovered public key, use the Algorithm Confusion page to forge an HS256 token signed with the public key PEM as the HMAC secret.
Open Algorithm Confusion04
References
Practice in the lab
JWT-SecLabs · 8 · Shadow Key - hands-on Docker environment
Open lab