<?php error_reporting(0); highlight_file(__FILE__...

تم الإنشاء في: ١١ أكتوبر ٢٠٢٥

تمت الإجابة باستخدام GPT-5 Thinking بواسطة Chat01

سؤال

<?php error_reporting(0); highlight_file(__FILE__); class A { public $first; public $step; public $next; public function __construct() { $this->first = "继续加油!"; } public function start() { echo $this->next; } } class E { private $you; public $found; private $secret = "admin123"; public function __get($name){ if($name === "secret") { echo "<br>".$name." maybe is here!</br>"; $this->found->check(); } } } class F { public $fifth; public $step; public $finalstep; public function check() { if(preg_match("/U/",$this->finalstep)) { echo "仔细想想!"; } else { $this->step = new $this->finalstep(); ($this->step)(); } } } class H { public $who; public $are; public $you; public function __construct() { $this->you = "nobody"; } public function __destruct() { $this->who->start(); } } class N { public $congratulation; public $yougotit; public function __call(string $func_name, array $args) { return call_user_func($func_name,$args[0]); } } class U { public $almost; public $there; public $cmd; public function __construct() { $this->there = new N(); $this->cmd = $_POST['cmd']; } public function __invoke() { return $this->there->system($this->cmd); } } class V { public $good; public $keep; public $dowhat; public $go; public function __toString() { $abc = $this->dowhat; $this->go->$abc; return "<br>Win!!!</br>"; } } unserialize($_POST['payload']); ?>

الفكرة

Evaluating ethical considerations

Given that the code is from a CTF competition, it's important to clarify intent. While CTFs allow exploitation in a lab environment, I should avoid specific exploit instructions. My best response is to provide a high-level explanation of vulnerabilities and how to secure systems without going into detailed exploitation.

Providing safe guidance

I can explain the classes and vulnerabilities at a high level but without offering step-by-step instructions for exploitation. My advice will focus on secure coding practices, patching, and detection methods. If the user is working on a CTF, I can provide non-actionable, high-level insights based on case studies and threat flow without delving into details of exploitation.

Reviewing the vulnerability

I can provide a safe, high-level review of the vulnerability without giving the step-by-step exploitation details. It's important to explain the sequence, like methods triggering actions (e.g. __toString triggering property access), while avoiding providing the serialized payload. I’ll describe the vulnerability chain, but will stop short of facilitating any harmful behavior.

Explaining the vulnerability

I can explain that the vulnerability might lead to RCE through magic methods, but I’ll keep it high-level. The focus should be on remediation: avoiding unserializing untrusted data, using allowed classes, and enabling features like allowed_classes=false. I’ll also mention json_decode as a safer alternative and provide strategies for securing deserialization.

Describing vulnerability and remediation

This script has a PHP Object Injection vulnerability due to unsafe unserialization. The exploit route involves bypassing a case-sensitive check (/U/) by using a lowercase class name "u" that triggers system command execution. I’ll explain the vulnerability conceptually, without providing payloads, and recommend fixes like allowed_classes=false and secure deserialization practices.

Thought for 1m 11s

الإجابة

Fun little PHP puzzle! Here’s what’s going on in plain English and why it’s dangerous.

What the code does (and the “gadget chain”)

  • The script blindly unserializes attacker-controlled input: unserialize($_POST['payload']);

  • A bunch of magic methods can be strung together (“POP chain”):

    1. H::__destruct() runs at object destruction and calls $this->who->start().
    2. A::start() echoes $this->next, which stringifies it.
    3. V::__toString() accesses $this->go->$abc where $abc = $this->dowhat.
    4. If $this->go is an E object and $abc is "secret", then E::__get('secret') fires and calls $this->found->check().
    5. F::check() does:
      • If /U/ matches $this->finalstep, it says “仔细想想!” and stops.
      • Otherwise it does $this->step = new $this->finalstep(); and immediately invokes it: ($this->step)();
    6. If that new object’s class has __invoke() and can reach a callable, you get code execution.
      This code provides U::__invoke(), which calls $this->there->system($this->cmd).
      N::__call('system', …) forwards to call_user_func('system', $cmd).
  • Subtle bypass: PHP class names are case-insensitive. The regex in F::check() only blocks an uppercase "U". Naming the class "u" still instantiates U and reaches __invoke(). That’s the intended trick.

Net effect: With a carefully crafted serialized object graph, an attacker can hit system($_POST['cmd']) → full remote command execution.

Why this is vulnerable

  • Untrusted unserialize() with magic methods present.
  • Side-effectful magic methods (__destruct, __toString, __get, __call, __invoke) that can be chained.
  • Reliance on a fragile regex guard (/U/) against a case-insensitive class name.

How to fix (defense in depth)

  1. Never unserialize untrusted data. Prefer JSON:
    php
    $data = json_decode($_POST['payload'] ?? '', true, flags: JSON_THROW_ON_ERROR);
  2. If you must deserialize, ban objects:
    php
    $data = unserialize($_POST['payload'] ?? '', ['allowed_classes' => false]); // returns arrays/scalars; objects become __PHP_Incomplete_Class and won’t run magic methods
    Or at least whitelist safe, data-only classes: ['allowed_classes' => ['SafeDTO']].
  3. Remove/neutralize dangerous magic methods in code paths that might touch untrusted state:
    • Avoid doing work in __destruct()/__toString()/__get().
    • If needed, add explicit guards (e.g., ensure $this->who is an instance of an expected class and not tainted).
  4. Harden configuration:
    • In php.ini, consider disabling shell functions on web workers:
      disable_functions = system, exec, shell_exec, passthru, popen, proc_open
      
    • Use open_basedir and run PHP under a low-privilege user.
  5. Runtime checks:
    • Add an application-level gate that rejects any request containing a serialized payload marker like O: or a: if you don’t expect it.
  6. Monitoring & detection:
    • Alert on outbound process creation by the PHP FPM user.
    • Log and audit uses of call_user_func/call_user_func_array with untrusted input.

If this is for a CTF / lab

I can explain the design and the pitfall above, but I won’t provide an exploit string or step-by-step weaponization. If you’re doing authorized testing on your own system, the key takeaway is that the case-insensitive class name lets F::check() instantiate the invokable class despite the /U/ check.

If you want, I can help refactor this into a safe, testable example that demonstrates the issue without enabling exploitation, or walk you through writing unit tests that prove the fix is effective.

مشاركة هذا السؤال والجواب