text
stringlengths
0
1.99k
------[ 2.2 - Sleepy Cats Catch No Mice
However, if we're really going to talk about the deserialization problem
from a broader view, you'll see developers have also put in a ton of
effort. Besides minimizing entry points to `unserialize()`, they've
invested a huge amount of effort into strengthening commonly used
libraries. That way, even if attackers manage to find deserialization bugs
within applications, they'll struggle to actually exploit them due to the
lack of usable POP chains - ultimately making the entire PHP ecosystem
much safer!
However, building such defenses was not easy at all. At first, developers
relied on simple regular expressions as a safeguard, but the whole
situation quickly turned into a classic cat-and-mouse game due to PHP's
overly loose serialization parser. Developers soon moved their checks to
the `__wakeup()` method, hoping to build a more robust gatekeeper right at
the very start of the deserialization process. But (un)surprisingly, under
certain conditions, PHP itself could even *silently skip calling the
`__wakeup()` method* [29]! This unexpected behavior completely broke every
defense relying on it - eventually leading to a remote code execution
vulnerability in SugarCRM!
=> How SugarCRM attempted to protect against deserialization attacks
---------------------------------------------------------------------------
public function __wakeup() {
// clean all properties
foreach(get_object_vars($this) as $k => $v) {
$this->$k = null;
}
throw new Exception("Not a serializable object");
}
---------------------------------------------------------------------------
Around 2017, we also started seeing various CTF challenges that aimed to
bypass the `__wakeup()` guard. However, most of them were still based on
scenarios where different objects shared a same context - players had to
trigger garbage collection before one object's *delayed* __wakeup(), then
reuse dangerous code inside another object's destructor. What really
caught my eye was the clever "Reference Trick" [30] used by Paul Axe at
WCTF 2019: by pointing dangerous properties elsewhere, he neatly bypassed
the property-cleanup operations. The same idea was later adopted into
several Laravel's POP chains [31], becoming yet another classic of
deserialization bypass!
------[ 2.3 - The "Holy Grail" of Deserialization Attacks
We've talked about plenty of deserialization issues so far, but how much
damage they can actually cause still depends heavily on whether the
application has any vulnerable classes or not. Of course, projects like
PHPGGC [32] have been continuously documenting and polishing generic POP
chains, but how to pull off deserialization attacks without any existing
application code still remains the holy grail for deserialization hackers.
It's true that in the early days, hackers experimented with neat tricks -
like using `SoapClient` to pull off XXE or SSRF [33] - but that was still
quite far from achieving real RCE. Since then, app-level exploitation
seemed to have hit a bottleneck, and lower-level approaches - especially
those focusing on memory corruption - quickly became hot topics.
When it comes to the legendary memory corruption cases in PHP
deserialization, most people might probably think of the amazing work by
@cutz (and others) on PornHub [34]. But, I bet we all first learned how to
craft fake `zval` structures from Stefan Esser's research [35] - going
step-by-step from arbitrary reads, arbitrary writes, all the way to fully
controlling the Program Counter!
And while we're on this topic, there's another name we absolutely can't
miss: Taoguang "Ryat" Chen. He began reporting tons of bugs directly
inside the serialization parser starting around 2015, sparking a new wave
of low-level research on deserialization issues. Even PHP's core
developers also publicly acknowledged that "deserialization had become the
largest source of security bug reports" for them!
I've also personally benefited multiple times from bugs reported by @Ryat
(thanks!). During one of my red team operations, I started from a Type
Juggling 0day, turned that into a Use-After-Free [36], and then spent
several weeks grinding through it before finally achieving full RCE on a
completely unknown and remote environment. Honestly, it's still one of my
proudest hacks to this day, and I even turned the whole process into a CTF
challenge afterward - if you're interested in the full chain, feel free to
check it out right here [37]!
With the official PHP team announcing "they wouldn't treat `unserialize()`
as a security boundary anymore," it seemed like the whole deserialization
journey was coming to an end. But just as everyone thought the era of
deserialization was about to close, another new chapter was already taking
shape - ready to shock the entire Infosec the very next year. We'll dive
into this brand-new technique shortly in the upcoming section, "New
Attacks and Techniques Born in CTFs".
[ -- Spoiler Alert: it's our all-time friend, LFI... and more! ;) ]