Описание
PhpSpreadsheet has SSRF/RCE in IOFactory::load when $filename is user controlled
The usage of is_file, used to verify if the $filename is indeed an actual file, by all(?) Reader implementations (inside the helper function File::assertFile) is php-wrapper aware, for any php wrappers implementing stat().
The 3 wrappers ftp://, phar:// and ssh2.sftp://, all satisfy this requirement - 2 of which are shown in the PoC below.
This results in a SSRF, at "best", and RCE at worse.
This was tested against the latest release - but the issue seems to go back a while from a first quick check (still present in v1.30.2).
PoC
To reproduce the vulnerable behavior, the following scripts were used:
php.ini file, only needed to build the malicious phar, not necessary to exploit on a deployed instance of the library:
make_phar.php to create the malicious file:
test.php showcases the unsafe pattern:
RCE
Run the PoC (for RCE):
The file /tmp/poc.txt should now be present on disk.
Note: the vuln still triggers if the file pointed to inside the phar does not exist/is not supported (html, xlsx, etc...). This means an attacker could "silently" trigger the vuln without leaving any error logs if the file inside the phar exists and is supported instead.
SSRF
Run the PoC (for SSRF):
Observe a connection is made to 127.0.0.1 on port 21.
Root Cause Analysis
Following the API exposed by the library, using IOFactory::load, the code proceeds as follows:
The one obvious gadget that was found is guarded via __unserialize (or __wakeup in older versions) in the XMLWriter class, making it not possible to use the phar deserialization as a standalone attack vector using just this library - it is still viable to create "POP" gadget chains via other classes which may be available in real-world deployment scenarios.
Phpspreadsheet is used as a backbone for many library wrappers, including very widespread ones from packagist like maatwebsite/excel for Laravel, sonata-project/exporter and so on, hence the deserialization vector stays relevant in other contexts.
Suggested mitigations
Use is_file only after making sure the filename does not contain any php wrapper:
or perhaps even just passing it to realpath before calling is_file to ensure it is parsed correctly:
Note:
stream_is_local()would also not be safe here — as it considersphar://to be local and would not block it.
Пакеты
phpoffice/phpspreadsheet
>= 4.0.0, <= 5.5.0
5.6.0
phpoffice/phpspreadsheet
>= 3.3.0, <= 3.10.3
3.10.4
phpoffice/phpspreadsheet
>= 2.2.0, <= 2.4.3
2.4.4
phpoffice/phpspreadsheet
>= 2.0.0, <= 2.1.14
2.1.15
phpoffice/phpspreadsheet
<= 1.30.2
1.30.3
Связанные уязвимости
PhpSpreadsheet is a library for reading and writing spreadsheet files. In versions 1.30.2 and earlier, 2.0.0 through 2.1.14, 2.2.0 through 2.4.3, 3.3.0 through 3.10.3, and 4.0.0 through 5.5.0, when the filename argument to IOFactory::load() is user-controlled, an attacker can supply a PHP stream wrapper path (such as phar://, ftp://, or ssh2.sftp://) that passes the is_file() check in File::assertFile(). The phar:// wrapper triggers deserialization of the PHAR metadata, which can lead to remote code execution if a suitable gadget chain is available in the application. The ftp:// and ssh2.sftp:// wrappers can be used for server-side request forgery. This issue has been fixed in versions 1.30.3, 2.1.15, 2.4.4, 3.10.4, and 5.6.0.
Уязвимость функций File::prohibitWrappers() и IOFactory::load() PHP-библиотеки PhpSpreadsheet, позволяющая нарушителю осуществить SSRF-атаку или выполнить произвольный код