
PHP to Shell, Part 4: Object Injection to Code Execution
Unlike the previous two posts, nothing here was run against my own lab — building a sink with a genuinely exploitable gadget chain, rather than a toy class that proves nothing, is more setup than this round of testing got to. Everything below is sourced to public research, CVE records and the PHPGGC project’s own chain definitions, and I’m saying that plainly rather than borrowing the credibility of the first two posts for a post that didn’t earn it the same way.
The one-sentence version
unserialize() reconstructs an object graph from a string, and PHP invokes certain “magic
methods” automatically as part of doing that — __wakeup(), __destruct(), __toString().
Nothing requires those methods to be safe for an attacker to trigger with attacker-chosen
property values. Chain enough of them, through classes that happen to already be loaded by the
application (not classes you write), and you reach a function call you control.
The gate that decides everything downstream: a controllable string reaching unserialize()
with no allowed_classes restriction. Cookies, cache values, and any API that implicitly
deserializes (some session handlers, some caching layers) are the usual sinks.
Magic methods, and what each is actually good for
| Method | Fires when | Useful for |
|---|---|---|
__wakeup() |
immediately after deserialization | first step of almost every chain |
__destruct() |
object garbage-collected (end of script, or explicitly) | the most common final link — easy to trigger without further interaction |
__toString() |
object used in string context | pivots the chain into a sprintf, a log line, anything that stringifies |
__call() / __get() / __set() |
missing method or property accessed | bridges to a second class with a different capability |
None of these are dangerous in isolation. A chain is several of them in sequence, each one set up by the attacker-controlled property values from the step before it.
Building a chain is finding software, not writing an exploit
This is the part that gets skipped in a lot of write-ups: a PHP Object Injection chain is discovered, almost never authored. You find classes already present in the application’s dependency tree — Monolog, Guzzle, a framework’s own internals — whose destructors or wakeup methods happen to compose into something dangerous, and you chain those, because an attacker cannot define a new class on the target; only classes already autoloadable there can fire. PHPGGC catalogs known chains by library and version for exactly this reason — it’s a database of other people’s code that composes badly, not a generator of new gadgets.
Two real examples where that composition already happened:
- CVE-2018-15133, Laravel’s
X-XSRF-TOKENhandling: Laravel decrypts that cookie value and unserializes the result. With the application’sAPP_KEY— leaked via a debug page, a committed.env, or a backup — an attacker encrypts a PHPGGCLaravel/RCE/3payload and sends it as the header. The entire finding collapses to one precondition: does the attacker have the key. That’s also why this CVE didn’t enter CISA’s Known Exploited Vulnerabilities catalog until 2024, six years after disclosure — every leakedAPP_KEYre-arms the same primitive indefinitely, patch or no patch. - CVE-2024-5932, WordPress’s GiveWP plugin: the donation form passes the donor-supplied
give_titlefield straight intounserialize(), unauthenticated. GiveWP ships TCPDF and Faker as dependencies, and those happen to supply a usable chain on their own — no second plugin required. It’s the cleanest single-plugin, self-contained example in the WordPress ecosystem, which is also why a public Metasploit module exists for it within weeks.
phar:// as a trigger that never calls unserialize()
A second route into the same mechanism, and the reason the phar wrapper from
part 3 matters here too: on PHP before
8.0, almost any filesystem operation against a phar:// path — not just an include, things
like file_exists() or getimagesize() — implicitly deserializes the archive’s metadata. No
call to unserialize() anywhere in the application. If a gadget chain is available, the
precondition is just “something, anywhere, does a filesystem check on an attacker-influenced
path,” which is a much easier bar to clear than finding an explicit unserialize() sink. PHP
8.0 deferred the metadata unserialization specifically to close this as a trigger without
unserialize() — Phar::getMetadata() still deserializes on demand, so the class of bug
survives, just with a narrower doorway.
No chain, no RCE — the honest downgrade
This is the section that decides whether a finding reads “RCE” or “object injection, no demonstrated impact,” and it’s worth stating as a checklist because every item on it has killed a chain that looked fine on paper:
- The class has to exist and be autoloadable on the target. Present in
vendor/but excluded from the autoload map doesn’t count. - Property names have to match exactly. A rename, a visibility change, or adding a type hint to a property breaks the chain silently.
- The method flow has to be unchanged. A security patch that adds one validation check inside a destructor kills the chain while the library’s version string still matches the chain’s declared range.
- Types have to be satisfied. Since PHP 7, a
TypeErroron a typed property aborts mid-chain. - The sink function has to still exist. Chains built on
assert()as a code-execution sink are dead on PHP 8.0;create_function()is gone in 8.0;preg_replace()’s/emodifier has been gone since 7.0.
Read that intersection as library version ∩ PHP version ∩ installed components ∩ autoload map. When it’s empty, the correct write-up is “object injection confirmed, no usable chain
found” — a real, lower-severity finding, not a failed attempt to hide. It’s also worth checking
before writing a finding off entirely: PHPGGC’s gadget types include file write, file read,
and file delete, not just direct code execution, and a file-write gadget paired with an
include() sink from part 3 is an RCE chain built from two primitives that look individually
unremarkable.
What actually closes this category
unserialize($data, ['allowed_classes' => false])— or better, nounserialize()at all;json_decode()for anything that doesn’t need PHP’s object graph.- Never deserialize a cookie, header, or cache value a client can influence.
- PHP 8.0 or later for the phar metadata deferral.
- Keep frameworks current — not because every chain gets patched quickly, but because public chain authors target the versions people are actually still running.
- Trim unused dependencies and their autoloading. A gadget that can’t be autoloaded can’t
fire, regardless of whether the vulnerable code is sitting in
vendor/.
Part 5 turns from mechanism to the public record: which of these three bug classes actually has a named CVE behind it, which of those CVEs have working exploit code in the wild rather than just a paper trail, and which ones are being used against real targets right now.
Related posts
03 ENTRIEShttps://shulkwisec.github.io/php-to-shell/04-object-injection-to-code-execution/
Printed 2026-11-15


