Cracking a weak JWT secret with hashcat, and the fix
A JWT can be attacked entirely offline. The server never sees the attempts, so there is no failed-login count to watch and nothing to lock out. Why HS256 allows it An HS256 token is signed with a single share
A JWT can be attacked entirely offline. The server never sees the attempts, so there is no failed-login count to watch and nothing to lock out.
Why HS256 allows it
An HS256 token is signed with a single shared secret. The same secret that creates the signature also checks it. If you have one valid token, you have a target to test guesses against on your own machine.
A token looks like three base64url parts joined by dots: header, payload, signature. The signature is an HMAC over the first two parts. Guess the key, recompute the HMAC, compare. Match means you found it.
The attack
Save the full token on one line in token.txt, then:
hashcat -m 16500 token.txt wordlist.txt
Mode 16500 is JWT. With a wordlist such as rockyou, a secret that is a common word, a product name or a default like secret comes back quickly. Once recovered, build a new token with the claims you want, for instance "role": "admin", and sign it with the recovered key. The API checks the signature, finds it valid, and trusts the claims.
What makes a secret fall
Hashcat does not do anything clever here. It hashes candidates at high speed and compares. So the risk tracks the size of the space the secret came from. A word from a leaked password list is a handful of seconds of work. A secret made of a company name plus the year is not much better, because rule-based mangling covers those patterns. The only property that helps is real randomness and length, which is why generated keys beat chosen ones every time.
You can also check your own services before an attacker does. Pull a token from a test environment, run it through hashcat with a small list of your own product names and defaults, and see whether anything matches. If it does, rotate the secret first and ask how it was picked second.
The fix
Generate the secret from a secure random source. Thirty-two bytes is a sensible minimum:
openssl rand -base64 32
Do not type it, do not derive it from the product name, and do not commit it. Store it in a secrets manager or the environment, and rotate it when someone who knew it leaves.
If you can change the design, use RS256 or ES256. The signing key stays with the issuer, and every verifying service only gets the public key, so a compromised verifier cannot mint tokens.
A limit worth knowing
A failed crack run does not prove the token is safe. It only suggests the secret is not in your wordlist. Other mistakes, such as accepting alg: none or not checking expiry, need their own tests.
Practise it
Codelivly has a free weak JWT secret lab where you recover a common-word key, forge an admin token and enter the admin console. If you want the wider set of API attacks afterwards, the API hacking book is a 227-page hands-on guide.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.