Roundcube is a widely distributed open-source webmail software used by many organizations and companies around the globe. In this post, we show how a malicious user can remotely execute arbitrary commands on the underlying operating system, simply by writing an email in Roundcube 1.2.2 (>= 1.0). This vulnerability is highly critical because all default installations are affected.
The mirror on SourceForge counts more than 260,000 downloads for Roundcube in the last 12 months1 which is only a small fraction of the actual users. Once Roundcube is installed on a server, it provides a web interface for authenticated users to send and receive emails with their web browser.
Requirements
The vulnerability has the following requirements for exploitation:
- Roundcube must be configured to use PHP’s
mail()
function (by default, if no SMTP was specified) - PHP’s
mail()
function is configured to use sendmail (by default, see sendmail_path) - PHP is configured to have
safe_mode
turned off (by default, see safe_mode) - An attacker must know or guess the absolute path of the webroot
These requirements are not particularly demanding which in turn means that there were a lot of vulnerable systems in the wild.
Description
In Roundcube 1.2.2 and earlier, user-controlled input flows unsanitized into the fifth argument of a call to PHP’s built-in function mail()
which is documented as critical in terms of security. The problem is that the invocation of the mail()
function will cause PHP to execute the sendmail program. The fifth argument allows passing additional parameters to this execution which allows a configuration of Sendmail. Since sendmail offers the -X
option to log all mail traffic in a file, an attacker can abuse this option and spawn a malicious PHP file in the webroot directory of the attacked server. The following code lines trigger the vulnerability.
program/steps/mail/sendmail.inc
Here, the value of the POST parameter _from
is fetched and Roundcube’s deliver_message()
method is invoked with the value used as second argument $from
.
program/lib/Roundcube/rcube.php
This method will then pass the $from
parameter to a call of the mail()
function. The idea is to pass a custom from
header to the sendmail program via the -f
option.
Insufficient Sanitization
An interesting part is that it seems as if the from
e-mail address is filtered beforehand with a regular expression. Basically, the $from
parameter is expected to have no whitespaces which would limit the possibility to attach other parameters behind the -f
parameter. Using whitespace constants such as $IFS
or injecting new shell commands `
does not succeed at this point. However, there is a logical flaw in the application that causes the sanitization to fail.
program/steps/mail/sendmail.inc
In line 105, an email is extracted from the user-controlled variable $from
that contains no whitespaces. However, this extraction only takes place when the rcmail_email_input_format()
function returns a value equivalent to TRUE. In the following, we will examine this function closely.
program/steps/mail/sendmail.inc
The function uses another regular expression in line 863 which requires that the line ends ($
) right after the email match. A payload used by an attacker does not have to match this regex and therefore the array $result
will stay empty after the foreach
loop. In this case, the implode()
function in line 876 will return an empty string (equal to FALSE) and the $from
variable is not altered nor sanitized.
Proof of Concept
When an email is sent with Roundcube, the HTTP request can be intercepted and altered. Here, the _from
parameter can be modified in order to place a malicious PHP file on the file system.
This allows an attacker to spawn a shell file rce.php in the web root directory with the contents of the _subject
parameter that can contain PHP code. After performing the request, a file with the following content is created:
Since the email data is unencoded, the subject parameter will be reflected in plaintext which allows the injection of PHP tags into the shell file.
Timeline
Date | What |
2016/11/21 | First contact with vendor |
2016/11/22 | Vendor fixes vulnerability on GitHub |
2016/11/28 | Vendor agrees to coordinated disclosure |
2016/11/28 | Vendor releases updated version Roundcube 1.2.3 |
Summary
Roundcube 1.2.2 is resistant against many attack vectors and a large community works on the software continuously together securing the application. However, the vulnerability described in this post could slip through and is an edge-case due to its rarity. With the aid of automated testing, it is not only possible to detect such edge-cases, but it allows to save human resources and therefore focus on different aspects in the development process of a secure web application.
We would like to thank the Roundcube team for the very quick fix after just one day, and the new release made available only after one week! This is a very impressive and professional response towards security issues.