Skip to content
RCE Write-up Intermediate

PHP mail() Command Injection to Remote Code Execution (RCE)

The PHP mail() function's fifth parameter was built from unsanitised user input, letting an attacker inject sendmail options. This allowed writing a PHP web shell to a web-accessible directory and reading arbitrary files.

nullsecurityx Follow

Published on nullsecurityx.medium.com

PHP is one of the most widely used server-side scripting languages for building dynamic web applications. Understanding which built-in functions may introduce security risks gives attackers a significant advantage during security testing and exploitation.

NullSecurityX, on nullsecurityx.medium.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

The application passed user-controlled input directly into the fifth argument of mail(), which PHP appends to the sendmail command line without sanitisation.

Impact

An attacker could execute arbitrary system commands on the server (remote code execution) and read arbitrary files, leading to full compromise.

What the attacker needed

PHP mail() used with user input in 5th argument; sendmail binary available; web-accessible writable directory

How it was found

  1. Submit a contact form with an email address containing extra sendmail options that set the queue directory and log file to a web-accessible path ending in .php.
  2. Put PHP code in the message body so it ends up in the log file.
  3. Access the newly created PHP file through the browser with a command parameter to run system commands.

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.

PHP's mail() function can pass extra options to the sendmail program that actually sends email. If a website puts user input into those options without cleaning it, an attacker can add their own sendmail options. In this case, one option let the attacker write a file containing PHP code into the website folder, which the server then ran as a program.

What is RCE?

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

More by nullsecurityx

View all

6 more disclosed reports, best first.

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.