How To Own A Web Server By Writing An Email
PHPMailer allows website visitors to send emails to the website's owners or admins. Recently, there was vulnerability discovered that allows remote attackers to execute code.
PHPMailer validates email addresses using RFC 3696. However, according to the specifications, blank spaces and double-quotes are allowed. By crafting an email address string containing blank spaces and double-quotes, an attacker can then inject code that would have PHPMailer's mail() function call /usr/bin/sendmail differently.
For our tests, we crafted a webpage using the following html form:
The HTML code is shown as:
While send_form_email.php is as follows:
Running The Exploit
We attempt to exloit the vulnerability by typing in the following to the web page:
This generates a file named phpcode.php in /var/www/html/cache/. Looking at the file, we will see that one line contains the php code we placed in the comments section:
By entering the URL to the generated file, we will see the following in the browser:
So far, we know the following:
- From Fig. 2, there is a limit to the number of characters we can send in the message.
- From Fig. 5, the injected code is placed in one line in the generated file.
- We cannot yet "execute" random code.
Basically, our next step would then require us to send code that is:
- Short enough to fit the character limit.
- Does not require line breaks.
- Allow us to have a means to "execute" code.
By using libcurl, we can have the web server download other files for us.
The above, when executed, would download a file and save it as backdoor.php. Executing the generated phpcode.php file in the browser, we would see only the following:
However, in the background, the web server is then instructed to download a file and save it as backdoor.php.
Going to backdoor.php, we see the following:
This is basically a PHP backdoor we can use to execute instructions.
A simple regular expression check can be used to verify the email address as follows:
SonicWALL Threat Research Team has researched this vulnerability and have the following signatures in place to protect their customers:
- WAF:9016 - PHP Injection Attack
- WAF:9039 - PHP Injection Attack 2