Authenticated Code Execution through Phar deserialization in CSV Importer as Shop manager in WooCommerce
The WooCommerce CSV importer passed a user-controlled file path to fopen without validating that it was a CSV file, allowing a Shop manager to use a phar:// wrapper to trigger PHP object deserialisation. This led to arbitrary PHP code execution and full server compromise.
Originally disclosed on HackerOne
- Fixed in 49 days
- Paid in 50 days
- Disclosed after 16 months
This vulnerability is based on the following exploitation technique:
https://blog.ripstech.com/2018/new-php-exploitation-technique/
It is easier to explain this vulnerability by having watched the PoC first: https://www.youtube.com/watch?v=mr3bAOIUwd4
Here is what's happening:
- Since a valid phar file needs o be uploaded to the server (the extension doesn't matter) I upload the poc.jpg via the media uploader
- I begin the Import process with a valid CSV file
- The importer asks if I am sure that I want to run the import on these files
- I confirm and modify the POST parameter to my phar:// wrapper and deserialize the file
- The PHP code executes
The source of the vulnerability within the source code lies in the /woocommerce/includes/import/class-wc-product-csv-importer.php:
public function __construct( $file, $params = array() ) {
$default_args = array(
'start_pos' => 0, // File pointer start.
'end_pos' => -1, // File pointer end.
'lines' => -1, // Max lines to read.
'mapping' => array(), // Column mapping. csv_heading => schema_heading.
'parse' => false, // Whether to sanitize and format data.
'update_existing' => false, // Whether to update existing items.
'delimiter' => ',', // CSV delimiter.
'prevent_timeouts' => true, // Check memory and time usage and abort if reaching limit.
'enclosure' => '"', // The character used to wrap text in the CSV.
'escape' => "\0", // PHP uses '\' as the default escape character. This is not RFC-4180 compliant. This disables the escape character.
);
$this->params = wp_parse_args( $params, $default_args );
$this->file = $file;
if ( isset( $this->params['mapping']['from'], $this->params['mapping']['to'] ) ) {
$this->params['mapping'] = array_combine( $this->params['mapping']['from'], $this->params['mapping']['to'] );
}
$this->read_file();
}
/**
* Read file.
*/
protected function read_file() {
$handle = fopen( $this->file, 'r' ); // @codingStandardsIgnoreLine.
if ( false !== $handle ) {
$this->raw_keys = version_compare( PHP_VERSION, '5.3', '>=' ) ? fgetcsv( $handle, 0, $this->params['delimiter'], $this->params['enclosure'], $this->params['escape'] ) : fgetcsv( $handle, 0, $this->params['delimiter'], $this->params['enclosure'] ); // @codingStandardsIgnoreLine
...
As can be seen, the constructor calls read_file, which in turn calls fopen without any checks, which leads to the deserialization of the Phar object.
I recommend to check the file parameter and see if it actually is a CSV file before calling fopen on it.
I have attached the poc.jpg that worked for my PHP version.
Impact
I only displayed the contents of the /etc/passwd file in the PoC video. However, since I can execute arbitrary PHP code, a complete compromise of the WordPress installation is possible. If an attacker can gain access to a Shop manager account, he can easily and without restrictions take over the server.
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
The importer trusted the user-supplied file path and called fopen on it without checking that it was a real CSV file, enabling a phar:// wrapper to trigger deserialisation.
Impact
An attacker with a Shop manager account could execute arbitrary PHP code and completely compromise the WordPress installation and server.
What the attacker needed
A Shop manager account; ability to upload media; PHP version vulnerable to phar deserialisation
How it was found
- Upload a crafted file through the media uploader.
- Start the import process with a valid CSV file.
- Confirm the import prompt.
- Modify the POST parameter to use a phar:// wrapper pointing at the uploaded file.
- The PHP code inside the phar executes.
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.
A shop manager could upload a crafted file (the extension does not matter). When the import feature opened that file via a phar wrapper, PHP deserialised it and ran code, giving control of the server.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.