Skip to content
RCE Write-up Advanced

MyBB 0day Authenticated Remote code execution

An administrator can set custom sendmail parameters in MyBB's mail settings, and these are passed to PHP's mail function without sanitisation. Because escapeshellcmd blocks command injection but not argument injection, an attacker with admin access could use sendmail's logging flags to write a PHP web shell to a chosen path.

Anna Follow

Published on 0x1337.ninja

Up to the latest version of MyBB is vulnerable to remote code execution due to the allowing admin to set custom mail parameters used in mail function without proper sanitization.

Anna / 416e6e61 (@AnnaViolet20), on 0x1337.ninja

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.

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. 5 min read.

Context

MyBB is a forum package written in PHP. Up to the latest version at the time of writing, it is vulnerable to remote code execution because it lets an administrator set custom mail parameters that are passed to PHP's mail function without proper sanitisation.

The author is careful to set expectations: there is no default way to get code execution in MyBB, such as installing plugins, even for admin users. The bug described here requires administrator access to the forum, and it also depends on the environment. The sendmail binary must exist, and the www-data user must have permission to send mail through it. That is the default configuration of most installations, because it is the easiest way to make MyBB able to send password reset links and similar messages.

Setup

The preconditions are:

  1. Administrator access to the MyBB admin control panel.
  2. A sendmail binary present on the host.
  3. The web server user (www-data) able to invoke sendmail.
  4. PHP safe mode off, which is the default installation of PHP.

The researcher did not report the issue to the vendor. While reading ZDI advisories, they saw that someone had reported a MyBB authenticated RCE to ZDI. They decided to look for another RCE as an admin. The vendor did not accept it because of the high level of admin access required. The researcher asked why the previous report, which required the same privilege, had been accepted. The vendor's response is shown in the screenshot below.

From the original on 0x1337.ninja

Because of that, the researcher published this article instead of reporting it directly to the vendor.

Finding it

PHP's mail function is an interface for sending email. It invokes the sendmail interface installed on the system. According to the official PHP documentation, the function takes five parameters:

Text
mail(
    string $to,
    string $subject,
    string $message,
    array|string $additional_headers = [],
    string $additional_params = ""
): bool

The interesting one is additional_params. PHP's documentation says:

The additional_params parameter can be used to pass additional flags as command line options to the program configured to be used when sending mail, as defined by the sendmail_path configuration setting. For example, this can be used to set the envelope sender address when using sendmail with the -f sendmail option. This parameter is escaped by escapeshellcmd() internally to prevent command execution. escapeshellcmd() prevents command execution, but allows to add additional parameters. For security reasons, it is recommended for the user to sanitise this parameter to avoid adding unwanted parameters to the shell command.

So the parameter lets an attacker control the arguments of the sendmail command. escapeshellcmd is used to prevent command injection, but it does not prevent argument injection. As the expected behaviour of controlling parameters, it should not prevent this.

An attacker can abuse this, for example by passing the -X parameter to define a file to write logs to, and by controlling the file path and extension. If there is a way to inject custom code into the log file, this writes a web shell on the server.

Exploitation

Step 1: Find the document root

To write a web shell, the attacker first needs to know the document root path and find a way to inject PHP code into the log file. MyBB allows an admin to call phpinfo and see its result. A simple request to the following endpoint shows the document root path:

HTTP
admin/index.php?module=tools-php_info&action=phpinfo

Step 2: Write PHP code into a CSS file

MyBB lets an admin add a new stylesheet and write custom CSS files in cache/themes/theme2. The PHP code is written into a CSS file there.

Step 3: Set the mail parameters

The mail parameters are set to:

Text
-C <web_root>/cache/themes/theme2/rce.css -X <web_root>/rce.php

Step 4: Trigger the mail function

A password reset triggers the call to the mail function.

Step 5: The web shell is written

Since rce.css is not a valid config file, sendmail results in an error and writes its content to the log file, which is the web shell.

https://github.com/416e6e61/MyBB-0day-RCE/blob/main/exploit.py From the original on 0x1337.ninja

The exploit script is published at https://github.com/416e6e61/MyBB-0day-RCE/blob/main/exploit.py.

Root cause

MyBB allows the admin to set mail handlers and additional parameters. When these are set, MyBB updates the config file and writes the configuration to settings without sanitisation.

From the original on 0x1337.ninja

When MyBB sends an email, if the admin has set the usage of mail, it calls the send function of the PhpMail class defined in inc/mailhandlers/php.php. If PHP safe mode is off, which is the default installation of PHP, it uses the additional parameters defined by the attacker.

From the original on 0x1337.ninja

Because the additional parameters are meant to be passed as command line options, the escaping does not stop an attacker from adding unwanted flags such as -C and -X.

Impact

An authenticated administrator can achieve remote code execution on the server, provided that sendmail exists, that www-data can send mail through it, and that PHP safe mode is off. The vendor declined the report because of the high level of admin access required. The researcher notes that there is no default way for an admin to get code execution in MyBB, such as installing plugins, so this bug is a path to code execution from an admin account.

Fix

The report does not describe a fix or a change made by the vendor. The underlying issue is that administrator-supplied mail parameters reach PHP's mail function as additional_params, where escapeshellcmd allows argument injection.

Variants to test

  1. Other PHP applications that expose a mail parameter setting to administrators and pass it to mail as additional_params. A positive result is that a flag such as -X writes a file to a path you choose.
  2. Applications that write administrator-supplied configuration to a file without sanitisation. A positive result is that your value appears verbatim in the written configuration.
  3. Environments where sendmail is replaced by another mail transport. A positive result is that the additional parameters are still passed as command line options to that program.

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. 9 min read.

What happened, in plain words

MyBB is a free forum package written in PHP. PHP is a programming language used to build websites; a forum package is a ready-made set of files that gives you a discussion board with users, posts, themes and an admin area. An administrator is a user with the highest level of access: they can change settings, install themes and manage other users.

This report describes a way for an administrator to make the MyBB server run code of the administrator's choosing. That is called remote code execution, or RCE: an attacker gets the server to execute commands or code that the attacker supplied, instead of only reading or writing data. Here the attacker is already an administrator, so the question is not "can a stranger get in?" but "can an admin turn their admin access into full control of the machine?"

The author is careful about expectations. There is no default way to get code execution in MyBB, such as installing plugins, even for admin users. So this bug is a path from an admin account to code execution, not a way in from nothing.

The bug lives in a feature every forum needs: sending email. When someone forgets their password, the forum sends them a reset link. To send mail, MyBB uses PHP's mail function, which in turn calls a program on the server called sendmail. MyBB lets an administrator configure extra options that are passed to that program. Those options are not properly cleaned, and some of them let you tell sendmail to write a log file to a path you choose. If you can also get your own text into that log, you have written a file on the server containing your code. That file is a web shell: a script on the server that runs whatever the attacker asks it to.

A few conditions must hold. The sendmail program must exist on the server, and the user the web server runs as (www-data) must be allowed to send mail through it. That is the default on most installations, because it is the easiest way to make MyBB able to send password reset links and similar messages. PHP safe mode must also be off, which is the default for PHP installations.

Why the author published instead of reporting

While reading advisories from ZDI (Zero Day Initiative, a programme that buys and publishes vulnerability reports), the author saw that someone had reported a MyBB authenticated RCE to ZDI. "Authenticated" means the attacker must be logged in; here, logged in as an administrator. The author decided to look for another RCE reachable as an admin.

The vendor did not accept it because of the high level of admin access required. The author asked why the previous report, which required the same privilege, had been accepted. The vendor's response is shown in the screenshot below.

From the original on 0x1337.ninja

Because of that, the author wrote this article instead of reporting it directly to the vendor.

The mail function, and why it matters

PHP's mail function is an interface for sending email. Think of it as a counter clerk: you hand over an envelope, and the clerk passes it to the actual postal service installed on the machine, which here is sendmail. According to the official PHP documentation, the function takes five parameters:

Text
mail(
    string $to,
    string $subject,
    string $message,
    array|string $additional_headers = [],
    string $additional_params = ""
): bool

Line by line:

  • mail( starts the call to the function.
  • string $to is the recipient's email address.
  • string $subject is the subject line.
  • string $message is the body of the email.
  • array|string $additional_headers = [] is extra header information, such as who the mail is from; it can be given as a list or as one string, and defaults to an empty list.
  • string $additional_params = "" is extra text passed to the mail program as command line options; it defaults to an empty string.
  • ): bool means the function returns true or false, depending on whether the mail was accepted for delivery.

The interesting one for an attacker is additional_params. PHP's documentation says:

The additional_params parameter can be used to pass additional flags as command line options to the program configured to be used when sending mail, as defined by the sendmail_path configuration setting. For example, this can be used to set the envelope sender address when using sendmail with the -f sendmail option. This parameter is escaped by escapeshellcmd() internally to prevent command execution. escapeshellcmd() prevents command execution, but allows to add additional parameters. For security reasons, it is recommended for the user to sanitise this parameter to avoid adding unwanted parameters to the shell command.

In plain words: additional_params is a string that ends up on the command line of the sendmail program. A command line is the text you type in a terminal to run a program, and options (also called flags or arguments) are the words starting with a dash that change what the program does. For example, -f sets the sender address.

PHP tries to protect itself with escapeshellcmd(). Escaping means putting special characters into a harmless form so they cannot break out of the intended command. escapeshellcmd stops command injection, which is when an attacker slips a whole extra command into a string, for example by adding ; rm -rf / after the intended one. But it does not stop argument injection: the attacker is still allowed to add extra options to the same command. And that is exactly what this parameter is for, so the escaping does not prevent it.

An attacker can abuse this. For example, the -X option tells sendmail to write a log of what it does to a file, and the attacker can choose the file path and extension. If the attacker can also get custom code into that log file, the result is a web shell on the server.

Root cause

MyBB allows the admin to set mail handlers and additional parameters. When these are set, MyBB updates the config file and writes the configuration to settings without sanitisation. Sanitisation means cleaning input so that dangerous characters or values cannot do harm; here the value is stored as given.

From the original on 0x1337.ninja

When MyBB is going to send an email, if the admin has set the usage of mail, it calls the send function of the PhpMail class defined in inc/mailhandlers/php.php. A class is a piece of code that groups related functions; a function is a named block of code that does one job. If PHP safe mode is off, which is the default installation of PHP, it uses the additional parameters defined by the attacker.

From the original on 0x1337.ninja

Because the additional parameters are meant to be passed as command line options, the escaping does not stop an attacker from adding unwanted flags such as -C and -X.

Exploitation, step by step

To write a web shell, the attacker first needs to know the document root path and find a way to inject PHP code into the log file. The document root is the folder on the server that the website serves to visitors; a file placed there can be requested in a browser. MyBB allows an admin to call phpinfo and see its result. phpinfo is a PHP page that prints a long list of facts about the PHP installation, including paths. A simple request to the following endpoint shows the document root path:

HTTP
admin/index.php?module=tools-php_info&action=phpinfo

Line by line:

  • admin/index.php is the entry page of the MyBB admin control panel.
  • ? starts the query string, the part of a URL that carries parameters.
  • module=tools-php_info selects the tools module and, within it, the PHP info page.
  • & separates parameters.
  • action=phpinfo asks that page to run and display the PHP information.

Why this step: the attacker needs to know where on disk the website lives, so that the file written later lands in a place a browser can reach.

MyBB lets an admin add a new stylesheet and write custom CSS files in cache/themes/theme2. CSS is the language that describes how a web page looks: colours, fonts, spacing. A stylesheet is a file of CSS rules. The attacker writes PHP code into a CSS file there.

The mail parameters are set to:

Text
-C <web_root>/cache/themes/theme2/rce.css -X <web_root>/rce.php

Line by line:

  • <web_root>/cache/themes/theme2/rce.css is the CSS file the attacker wrote, standing in for that configuration file. <web_root> is a placeholder for the document root found in the previous step.
  • <web_root>/rce.php is where that log will be written: a .php file in the web root, so the web server will treat it as code and run it when requested.

A password reset triggers the call to the mail function. A password reset is the normal "forgot my password" flow: the forum sends an email with a link, which means it calls mail, which means it calls sendmail with the attacker's options.

Since rce.css is not a valid config file, sendmail results in an error and writes its content to the log file, which is the web shell.

https://github.com/416e6e61/MyBB-0day-RCE/blob/main/exploit.py From the original on 0x1337.ninja

The exploit script is published at https://github.com/416e6e61/MyBB-0day-RCE/blob/main/exploit.py.

Impact

An authenticated administrator can achieve remote code execution on the server, provided that sendmail exists, that www-data can send mail through it, and that PHP safe mode is off. The vendor declined the report because of the high level of admin access required. The author notes that there is no default way for an admin to get code execution in MyBB, such as installing plugins, so this bug is a path to code execution from an admin account.

Fix

The report does not describe a fix or a change made by the vendor. The underlying issue is that administrator-supplied mail parameters reach PHP's mail function as additional_params, where escapeshellcmd allows argument injection.

Variants to test

  1. Other PHP applications that expose a mail parameter setting to administrators and pass it to mail as additional_params. A positive result is that a flag such as -X writes a file to a path you choose.
  2. Applications that write administrator-supplied configuration to a file without sanitisation. A positive result is that your value appears verbatim in the written configuration.
  3. Environments where sendmail is replaced by another mail transport. A positive result is that the additional parameters are still passed as command line options to that program.

Glossary

  • Administrator: a user with the highest level of access in an application, able to change settings and manage other users.
  • Argument injection: adding extra command line options to a program through input that was meant to be data, without adding a separate command.
  • Authenticated: requiring the attacker to be logged in.
  • Class: a piece of code that groups related functions and data.
  • Command injection: slipping a whole extra command into input that is passed to a shell.
  • Command line: the text used to run a program, including its options.
  • Configuration file: a file that tells a program how to behave.
  • CSS: the language that describes how a web page looks.
  • Document root: the folder on the server that the website serves to visitors.
  • Escaping: converting special characters into a harmless form so they cannot break out of the intended command.
  • Flag / option / argument: a word starting with a dash on a command line that changes what a program does.
  • Function: a named block of code that performs one job.
  • Log file: a file a program writes records of its activity to.
  • MyBB: a free forum package written in PHP.
  • PHP: a programming language used to build websites.
  • phpinfo: a PHP page that prints facts about the PHP installation, including paths.
  • Remote code execution (RCE): getting a server to run code supplied by the attacker.
  • Sanitisation: cleaning input so dangerous characters or values cannot do harm.
  • sendmail: a program on the server that actually sends email.
  • Stylesheet: a file of CSS rules.
  • Web shell: a script placed on a server that runs whatever the attacker asks it to.
  • www-data: the user account the web server runs as on many Linux systems.
  • ZDI: Zero Day Initiative, a programme that buys and publishes vulnerability reports.

More from MyBB

View all

The newest disclosed reports for this program.

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.