Say that there was a publicly accessible web page with the following PHP code:
<?php
class NotInteresting
{
public function noExploits() {
echo "Whatever.";
}
}
$unsafe = unserialize($_GET['data']);
$unsafe->noExploits();
?>
The code would expect the data
URL parameter to contain a serialized instance of NotInteresting
, but of course the data
parameter can be manipulated. When unserialize()
is used on user supplied data it often leads to PHP Object Injection.
However, all the examples of PHP Object Injection I have seen so far (1,2,3) have been dangerous for one of two reasons:
- There were some exploitable classes with dangerous methods (which were only meant to be called internally) which were leveraged to execute arbitrary code, often the case for a CMS.
- The version of PHP was old or outdated and vulnerabilities in the PHP code were exploited.
Given that the PHP version is current - that is, no known vulnerabilities exist in the unserialize()
function - and that there are no custom classes defined (just the default ones - Exception
, stdClass
etc.), is it possible to leverage the above code for a successful attack on a default PHP installation?
Extra info:
As far as I know there are only four exploitable magic methods when constructing an arbitrary class from an unserialize()
call: __call()
, __wakeup()
, __destruct()
and __toString()
:
- __wakeup() is called when an object is unserialized.
- __call() is called when invoking inaccessible methods (or non-existent ones).
- __destruct() is always called after no more references to the object exist.
- __toString() is called when an object is treated as a string.
So I wrote a PHP script to quickly generate a list of the classes which contains these magic methods: See here for a pastebin. Some of these look very 'interesting':
- The XML classes (could lead to XXE)
- The
Phar
classes
However I am unable so far to construct an attack just with these: I will need someone more experienced to weigh in.