> For the complete documentation index, see [llms.txt](https://ganesha-hk.gitbook.io/cybersecurity-writeups/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ganesha-hk.gitbook.io/cybersecurity-writeups/port-swigger-insecure-desiralization-labs/lab-7.md).

# lab-7

## Developing a Custom Gadget Chain for PHP Deserialization

**Difficulty:** Expert

### Lab Description

This lab uses a serialization-based session mechanism. By deploying a custom gadget chain, you can exploit insecure deserialization to achieve remote code execution.

**Objective:** Delete the `morale.txt` file from Carlos's home directory.

### Root Cause

The application stores user session data as a PHP serialized object and restores it using `unserialize()` on client-controlled data. During deserialization, PHP automatically invokes the `__wakeup()` magic method of the `CustomTemplate` class.

The `__wakeup()` method calls `build_product()`, which creates a new `Product` object using the `default_desc_type` and `desc` properties from the deserialized object. Because these properties are attacker-controllable, unvalidated data is trusted during object reconstruction.

The vulnerability becomes exploitable because the `Product` constructor performs dynamic property access (`$desc->$default_desc_type`). If the supplied object does not contain the requested property, PHP automatically invokes the `__get()` magic method. In the `DefaultMap` class, `__get()` forwards the requested property name to a callback using `call_user_func()`. This combination of magic methods and dynamic property access forms a custom gadget chain, allowing attacker-controlled data to reach a dangerous function.

### Exploitation

1. Log in as a normal user and observe that the session cookie contains a PHP serialized object.
2. In Burp's site map, find `CustomTemplate.php` and append `~` to the URL to retrieve the editor backup file containing the source code. Analyse the deserialization logic.
3. Identify that `CustomTemplate::__wakeup()` automatically invokes `build_product()` during deserialization.
4. Identify that `DefaultMap::__get()` uses `call_user_func()` to invoke a callback whenever an undefined property is accessed.
5. Craft a malicious serialized object such that:
   * `CustomTemplate` references a `DefaultMap` object.
   * The dynamic property lookup triggers `DefaultMap::__get()`.
   * `call_user_func()` invokes a dangerous function (such as `exec` or `system`) using attacker-controlled input pointing to `/home/carlos/morale.txt`.
6. Encode the payload: Base64-encode → URL-encode, and replace the session cookie.
7. Send the request. When the application calls `unserialize()`, the gadget chain executes automatically, the dangerous callback is invoked, and `morale.txt` is deleted.

### Lesson Learned

* Never use `unserialize()` on data that originates from users or other untrusted sources.
* Avoid storing serialized PHP objects in client-controlled cookies or other user-modifiable locations. Use safer formats such as JSON for session data wherever possible.
* Minimise the use of PHP magic methods (`__wakeup()`, `__get()`, `__destruct()`, etc.) for security-sensitive operations — they can be invoked implicitly during deserialization without the developer's intent.
* Avoid dynamic function invocation mechanisms such as `call_user_func()` with attacker-influenced values.
* Validate all object properties before using them in dynamic property access or callback execution.
* Remove backup and editor-generated files (`.php~`, `.bak`, `.old`) from production servers to prevent source code disclosure — exposed source code makes gadget chain construction significantly easier.
* Apply the principle of least privilege so that, even if deserialization is abused, the application's OS permissions limit the impact of arbitrary code execution.
* Conduct regular code reviews to identify gadget chains where multiple seemingly harmless classes can be combined into a dangerous execution path.


---

# 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://ganesha-hk.gitbook.io/cybersecurity-writeups/port-swigger-insecure-desiralization-labs/lab-7.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.
