Brief Notes on Unserialize Vulnerabilities and __toString

Recently I ran into a PHP unserialize vulnerability on a production site (yes those still exist). A base64 encoded cookie was unserialized, leading to a critical vulnerability due to the presence of a large library of useful classes on the site. I was able to verify the presence of this vulnerability because one member of the unserialized object was written to the page. This meant that in addition to being interested in classes with interesting __destruct functions I could also investigate objects with interesting __toString functions. Luckily PHP has a handy one of these built in: Exception. Inserting an Exception object into the serialized string lead to the callstack being printed in the page source. The following snippet demonstrates what this looks like

<?php 
function func()
{
	$exception = unserialize('O:9:"Exception":0:{}');
	echo $exception . "\n";
}

func();
?>

This code results in something like

Exception in /home/alkali/hacking/unserial.php:5
Stack trace:
#0 /home/alkali/hacking/unserial.php(5): unserialize('O:9:"Exception"...')
#1 /home/alkali/hacking/unserial.php(9): func()
#2 {main}

Here it isn’t very thrilling but in the wild in can reveal sensitive filesystem info as well as what types of PHP libraries are available to be leveraged into more dangerous exploits that delete files, perform arbitrary writes, and execute code. Good luck.

The Church of Latter Day Bytes

SURELY the hacking gods are made of the True Bytes, bytes of such elegance and high shannon entropy that mortal men can only stare in wonder at the output of hexdump

SURELY the hacking gods made the first shellcode, the True Shellcode that began the world and fostered the embers of civilization, lighting in men the desire to pwn and to be True Assholes about it

SURELY when the world ends it will be not with a Kernel Panic but with a clean exit(0) at the end of the True ROP Chain that is Reality.

SURELY the prophet Joybubbles will come again to judge who among us was Truly 1337 and the skr1pt k1ds will be cast into the flames

SURELY then the blessed and the clever will hack forever in the Kingdom of Hex upon the earth and no suffering or well-thought-out exploit mitigation will ever blight the Realm of the Gods and all will be as it should. Amen