githubEdit

PHP Object Injection

PHP Object Injection is a type of vulnerability that can occur when untrusted user input is deserialized in a PHP application.

Investigation

Below is an example of an index.php in PHP web application.

<?php

class Example {
    public $file = 'example.txt';
    public $msg = 'Hello World';

    public function SomeFunc() {
        // Some code ...
    }

    public function __destruct() {
        file_put_contents(__DIR__ . '/' . $this->file,$this->msg,FILE_APPEND);
    }
}

$data = unserialize($_GET['data']);

// Some code ...

?>

This code adds a text file named example.txt, that contains "Hello World" strings, into the web root directory. If the "data" parameter (/?data=...) is given, this value will be unserialized by unserialize() method. In short, we can inject the serialized malicious code to the value of the data parameter.

Then access to https://example.com/?data=<serialized_code_here> then our arbitrary code will be executed. For example,

Exploitation 1

Assumes the above situation (”index.php” in the “Investigation” section). We generate the serialized data and inject it to the “data” parameter.

1. Generate a Serialized Malicious Object

Create a malicious “Example” class contains __destruct() method in PHP. This script add the “download.php”, which downloads a PHP reverse shell script, into the web root directory. Then prints the URL encoded serialized “Example” class.

Replace <local-ip> with your local ip address.

Then generate a deserialized data.

Copy the output data.

2. Download a Reverse Shell Script.

In local machine, start local web server where the PHP reverse shell script located.

Now access to https://example.com/?data=<serialized_data_here>.

By this, our PHP reverse shell script is download to the target web root (e.g. https://example.com/shell.php).

Exploitation 2

First off, prepare the reverse shell script (shell.php) in local machine and start web server. Then create a maliciou payload as follow.

Now run it in terminal.

Copy the output and paste it to where the payload affects.

Automation

PHPGGCarrow-up-right is a library of PHP unserialize() payloads along with a tool to generate them.

References

Last updated