Jump to content
Byte-ul

Analysis of PHPMailer Remote Code Execution Vulnerability (CVE-2016-10033)

Recommended Posts

PHP is an open source, general-purpose scripting language used for web development that can also be embedded into HTML. It has over 9 million users, and is used by many popular tools, such as WordPress, Drupal, Joomla!, and so on. This week, a high-level security update was released to fix a remote code execution vulnerability (CVE-2016-10033) in PHPMailer, which is an open source PHP library for sending emails from PHP websites.

This critical vulnerability is caused by class.phpmailer.php incorrectly processing user requests. As a result, remote attackers are able to execute code on vulnerable servers.

This vulnerability affects PHPMailer versions before 5.2.18.

Analysis

When sending an email with PHPMailer, the normal process is, 1) PHPMailer gets user requests, 2) PHPMailer validates the user supplied data, 3) PHPMailer sends the data to the PHP mail() function to send the email.

In the validation stage, PHPMailer validates the user supplied data, as shown in the code below.

CVE-2016-10033%20code%201.png

This validator uses standard validation methods, like PHP’s built-in FILTER_VALIDATE_EMAIL, pcres from squiloople.com, and html5 default patterns for email type.

CVE-2016-10033%20code%202.png

For example, the $address with value “attacker -InjectedParam @example.com” will be rejected. But these methods follow RFC3696, which means the email address can contain spaces when quoted with “"”, so an $address with value “"attacker -InjectedParam"@example.com” will pass through the filter.

After the validation stage, PHPMailer will send the email elements, such as receiver address, subject, body, header, and sender address to the PHP mail() function to send the email. The code is shown below.

CVE-2016-10033%20code%203.png

The issue here is that PHPMailer doesn’t sterilize these email values before sending them to the mail() function. It just returns theCVE-2016-10033%20code%204.png and then sends the $result to the mail() function with the same set of parameters. This issue enables the subsequent attacks.

For example, when sending the request with email address:

CVE-2016-10033%20code%206.png

The mail() function will execute /usr/bin/sendmail with 4 arguments, “/usr/bin/sendmail”, “-t”, “-i” and “-fattacker -InjectedParam@example.com”.

However, an attacker could break the fourth argument by injecting a parameter with “\"”. For example, using the email address:

CVE-2016-10033%20code%207.png

This email address will let the sendmail function execute 6 arguments, “/usr/bin/sendmail”, “-t”, “-i”, “-fattacker”, “-InjectedParam1” and “-InjectedParam2"@example.com”.

How to Reproduce

In order to reproduce the attack, I have built a web server, as shown in Figure 1.

CVE-2016-10033%208.png

Figure 1. Testing the PHPMailer server

The source code is:

CVE-2016-10033%20code%209.png

This is a form similar to the “send feedback” function on some online websites. An attacker could send an email with a malicious email address to execute sendmail arguments, such as:

CVE-2016-10033%20code%2010.png

The PHP code can then be added to the Name or Message part of the string. For example,CVE-2016-10033%20code%2011(1).png. This is shown in Figure 2, below.

CVE-2016-10033%2012.png

Figure 2. Sending the attack email

Before sending this email, the files in the web root directory are shown in Figure 3. The index.php is the code of the webpage shown in Figure 1 ,and the directory “vulnerable” contains the PHPMailer files.

CVE-2016-10033%2013.png

Figure 3. Web root directory before sending the attack email

The payload for sending this email is shown in Figure 4, below.

CVE-2016-10033%2014.png

Figure 4. Attack payload

After sending this attack email, a new file “test.php” is created. It contains the RAW data for sending this email, which contains our PHP code “CVE-2016-10033%20code%2015.png”. It’s shown in Figure 5.

CVE-2016-10033%2016.png

Figure 5. Web root directory after sending the attack email

Then we can access our shell PHP file test.php and run commands. For example, we could execute the command “cat /etc/passwd”, as shown in Figure 6.

CVE-2016-10033%2017.png

Figure 6. Execute the command “cat /etc/passwd” on a test web server

In conclusion, the actual attack could be an arbitrary file upload that exploits the vulnerability in the PHPMailer filter for the sender email address. This means that an attacker could send an email with malicious code to the server, use the sendmail arguments to move the log file with the malicious code to the web root directory, and then execute the code remotely.

Solution

All users of PHPMailer should upgrade to the latest version immediately. Additionally, organizations that have deployed Fortinet IPS solutions are already protected from this vulnerability with the signature PHPMailer.Remote.Code.Execution.

Source: http://blog.fortinet.com/2017/01/05/analysis-of-phpmailer-remote-code-execution-vulnerability-cve-2016-10033

Edited by Byte-ul
  • Upvote 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...