# JWT Key ID Injector | Simple Python Script To Check Against Hypothetical JWT Vulnerability

**URL:** <https://onehack.st/t/jwt-key-id-injector-simple-python-script-to-check-against-hypothetical-jwt-vulnerability/171262>\
**Category:** Tools & Scripts\
**Tags:** tools, programming\
**Created:** [January 29, 2021, 1:05pm UTC](https://onehack.st/t/jwt-key-id-injector-simple-python-script-to-check-against-hypothetical-jwt-vulnerability/171262 "2021-01-29T13:05:17Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Prometheus](https://onehack.st/user_avatar/onehack.st/prometheus/32/34950_2.png) [@Prometheus](https://onehack.st/u/Prometheus)\
**Post date:** [January 29, 2021, 1:05pm UTC](https://onehack.st/t/jwt-key-id-injector-simple-python-script-to-check-against-hypothetical-jwt-vulnerability/171262/1 "2021-01-29T13:05:17Z")

</div>

![image](https://onehack.st/uploads/default/original/3X/0/8/08a41b78779342a4e889cfb4aac06740b6d57fed.jpeg)

Simple python script to check against hypothetical JWT vulnerability.

Let’s say there is an application that uses JWT tokens signed HS256 algorithm. An example token looks like the follow:

```auto
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.zbgd5BNF1cqQ_prCEqIvBTjSxMS8bDLnJAE_wE-0Cxg

```

Above token can be decoded to the following data:

```auto
{ "alg": "HS256", "typ": "JWT"}{ "sub": "1234567890", "name": "John Doe", "iat": 1516239022}

```

To calculate signature the following secret is used:

```auto
supersecret

```

The following pseudo code is used to calculate signature:

```auto
$alg = "sha256";$data = "...";$key = "supersecret";hmac($alg, $data, $key);

```

But what if unexpected **“kid”:0** field will be injected into the header?

```auto
{ "alg": "HS256", "typ": "JWT", "kid": 0}{ "sub": "1234567890", "name": "John Doe", "iat": 1516239022}

```

**kid** field is a standard way to choose a key. My assumption is that, if **kid** field is not expected, there may be vulnerable implementation that will treat the string $key value as an array:

```auto
hmac($alg, $data, $key[kid]);

```

As results “s” ($key[0]) value will be used as an HMAC secret.

**Usage**

injector.py script takes original JWT token, injects “kid”:0 field into the header and generates tokens signed with the one-letter secrets (ASCII codes: 32 – 126 [{space}, !, “, #, …, x, y, z, {, |, }, ~]):

```auto
python3 injector.py eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.zbgd5BNF1cqQ_prCEqIvBTjSxMS8bDLnJAE_wE-0Cxg

```

As results two files are created – tokens.txt and tokens\_meta.txt. tokens.txt contains generated tokens and can be used as a list of payloads for the Burp Intruder. If any token is valid (what means that application is vulnerable), tokens\_meta.txt file can be used to check what algorithm and secret were used to generate the given token. tokens\_meta.txt file contains the following data:

```auto
token1:algorithm:secret...token{n}:algorithm:secret

```

**Changes**

Please see the [CHANGELOG](https://github.com/dariusztytko/jwt-key-id-injector/blob/master/CHANGELOG)

## GitHub:

> **[GitHub - dariusztytko/jwt-key-id-injector: Simple python script to check against hypothetical...](https://github.com/dariusztytko/jwt-key-id-injector)**
>
> Simple python script to check against hypothetical JWT vulnerability.
