Skip to content
RCE Write-up Advanced

PHPMailer Exploit – Remote Code Execution

The patch for CVE-2016-10033 in PHPMailer 5.2.17 escaped the sender address with escapeshellarg(), but mail() then applied escapeshellcmd() to the fifth argument, so a crafted address containing a quote could break out and inject extra sendmail parameters. An unauthenticated attacker could use the -X parameter to write a PHP file and achieve remote code execution as the web server user.

legalhackers.com

Published on legalhackers.com

~~~~~~~~~~~~~ ExploitBox.io ~~~~~~~~~~~~~~~~ Interested in security / vulns / exploits ? ExploitBox.ioA Playground & Labs for security folks into hacking & the art of exploitation

The author, on legalhackers.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the patch used escapeshellarg() on the sender address, but mail() applies escapeshellcmd() to the fifth parameter, and the two escaping schemes clash so a quote survives and breaks out of the argument.

Impact

A successful exploitation could let remote attackers gain access to the target server in the context of the web server account, potentially leading to full compromise of the web application.

What the attacker needed

Vulnerable PHPMailer (<5.2.20); a form that sends email via PHPMailer; writable directory on the server; no authentication needed

The fix

No official solution was available at the time; the vendor was working on a new patch after the private disclosure.

How it was found

  1. Identify a website component that sends email through a vulnerable PHPMailer version, such as a contact form.
  2. Submit a sender address crafted to break out of the escaped argument and inject extra sendmail parameters.
  3. Use the -X sendmail parameter to write a PHP file into a writable web directory.
  4. Request the written PHP file to execute the attacker's code.

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 write-up has not been written yet. The Original tab links to the source.

PHPMailer is a popular PHP library for sending email. It passed a user-supplied sender address to the system's mail program without fully neutralising special characters, so an attacker could sneak in extra command-line options. One of those options let the attacker write a file containing PHP code, which the web server would then run.

What is RCE?

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.

Similar reports

View all

Same bug class and techniques, clearest first.

Remote code execution

The lesson behind this report.

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.