News for hackers. Stuff that matters.
Re:PHPMailer Exploit – Remote Code Execution by annnnd on Wednesday December 28, 2016 @02:21AM (#13269000)
TL;DR: PHPmailer fails to properly sanitize input. When configured to use CLI sendmail, this can lead to arbitrary command execution.

But then again, why would you send e-mail to just any address someone enters, without validating it for correctness? I know it is tough to validate all addresses according to RFC, but I'd rather block some legitimate users (which btw. probably know more about RFC than me and I'm sure can find a "nicer" e-mail address if they want to) than let some attacker use some vulnerability like this. Always whitelist valid input, never (just) blacklist it.

Also, I am baffled that frameworks I encountered never demanded from developer to specify exactly what kind of input it expects via POST & co.. It is trivial to write a set of functions like this:

    function input_post_email($field_name, $default_value)
    function input_post_string($field_name, $validation_regex, $default_value)
    ...
The point here is that framework should DEMAND from developer to specify format of each and every input var it needs. It should be difficult to bypass these restrictions, to demotivate developers doing it.

This is a first thing I made in every PHP project I started. Combined with Content Security Policy and output filtering it's... well, better than most other solutions. :)

Re:PHPMailer Exploit – Remote Code Execution by thaumaturgy on Wednesday December 28, 2016 @02:39AM (#13269054)
Misguided developers incorrectly "validating" email addresses is the reason that one of my clients couldn't use one of the newer TLDs, ".place", and ended up scrapping it and trying to find something they wanted a lot less in the much more crowded .com space.

It's also why I far-too-often run into forms that won't let me use a "+" in the username part of my email address, which I use to track who's responsible for sending my email account off to third parties (e.g., "rob+paypal@....").

Some kinds of email validation are better than others. Using regular expressions and strictly adhering to the RFC is the one that developers are usually talking about when they say not to do it. filter_var(..., FILTER_VALIDATE_EMAIL) is sort of okay, although there are lots of edge cases that it doesn't handle correctly.

Re:PHPMailer Exploit – Remote Code Execution by annnnd on Wednesday December 28, 2016 @05:30AM (#13269676)
Genuinly curious: does '+' trick work? I would imagine any infringing parties would learn to remove plus sign and all after it before selling the database of addresses by now...

Other than that: true, I hate incorrect validation. But I hate sloppy security practices even more.

Re:PHPMailer Exploit – Remote Code Execution by thaumaturgy on Wednesday December 28, 2016 @05:43AM (#13269713)
Not as much as it used to. You're right, they've caught on to it. I need to stand up another mail server pretty soon, I want to try making it easy to generate random recipient aliases, so e.g. [email protected] is an alias for [email protected] and [email protected] is an alias for [email protected]. That oughtta stump 'em for a while.
Re:PHPMailer Exploit – Remote Code Execution by danieltillett on Wednesday December 28, 2016 @04:45AM (#13269536)
Sometimes not allowing a "+" is an anti-abuse feature. If you have a service tied to a customer providing a unique email address then you don't want them able to use a "+".
Re:PHPMailer Exploit – Remote Code Execution by jackweirdy on Wednesday December 28, 2016 @04:52AM (#13269558)
That seems like a strange place to verify that a user is only allowed to create one account. After all, they could just create a second email account on a free email provider and use that to sign up.

To get around that, the service provider would have to verify identity further down the line, making the email format restriction redundant.

Re:PHPMailer Exploit – Remote Code Execution by danieltillett on Wednesday December 28, 2016 @05:12AM (#13269619)
I have been down this route and you also have to disable the use of free email accounts.

If the service value is not too high then even making people who want to abuse the service go through the process of registering a free email account works. Put a little bit of friction into the process and the script kiddies move onto an easier target.

Re:PHPMailer Exploit – Remote Code Execution by iopq on Wednesday December 28, 2016 @07:24AM (#13270049)
> I have been down this route and you also have to disable the use of free email accounts.

I don't have any other email. Seems like a really bad idea.

Re:PHPMailer Exploit – Remote Code Execution by danieltillett on Thursday December 29, 2016 @04:59AM (#13276531)
It depends on the service. Some services are only aimed at business which have their own domain, but yes I agree this is a pretty drastic step to take.
Re:PHPMailer Exploit – Remote Code Execution by adjkant on Wednesday December 28, 2016 @04:05AM (#13269391)
I somehow went through life not knowing the + trick. Thanks, and commenting to hopefully highlight this for anyone else who didn't know.
Re:PHPMailer Exploit – Remote Code Execution by thaumaturgy on Wednesday December 28, 2016 @04:32AM (#13269483)
The specific character depends on your MTA configuration. In Postfix this is the recipient_delimiter setting. I went with a "+" because that's the same character Gmail uses. Other mail services may not support this feature. Hope this helps!
Re:PHPMailer Exploit – Remote Code Execution by Synchro on Wednesday December 28, 2016 @04:58AM (#13269577)
This is incorrect. Input sanitisation is not the problem here - PHPMailer sanitises and validates email addresses well enough. The problem is that a valid, sanitised email address can also be an attack in an inappropriate shell context. Much as an SQL injection attack string may be harmless in a shell context but lethal in SQL. Also the vuln does not apply to CLI sendmail, only when sending via the PHP mail() function, and it's at least partly due to bugs in PHP itself.
Re:PHPMailer Exploit – Remote Code Execution by annnnd on Wednesday December 28, 2016 @05:24AM (#13269664)
I am not blaming lack of input sanitization, the bug is clearly still there. What I am saying is that input sanitization would prevent this attack because it will only allow valid characters to get to your mailer, which will exclude spaces, tabs, double and single quotes. Thus I fail to see an exploit vector which would bypass proper input sanitization. Do you see it?

In general I agree that PHP mail() function should take a part of blame here for not exposing a sane interface. Spammers have abused many bugs where programmers failed to sanitize "To:" or "From:" addresses and attackers could pass \r and \n characters (which allowed adding BCC fields, effectively sending e-mails to arbitrary addresses).

> Also the vuln does not apply to CLI sendmail...

Sure it does: >> ...will result in the followig list of arguments passed to sendmail program:

EDIT: you probably meant to say that the vulnerability is only triggered when CLI sendmail is called via mail(), which is true. Configuring PHPMail to use sendmail via CLI directly apparently doesn't expose this vulnerability.

Re:PHPMailer Exploit – Remote Code Execution by pilif on Wednesday December 28, 2016 @05:43AM (#13269710)
> What I am saying is that input sanitization would prevent this attack because it will only allow valid characters to get to your mailer,

you have to be careful with the meaning of "valid" though: What's valid in one context might not be in another context. Do you want to limit the character set to the least common denominator of non-special characters valid in all possible contexts your address might be used?

What if you don't exclude some character because it's not special in any of the current contexts but then you later add another thing to the mix that uses in-band signalling? Now you need to update your whitelist and remove even more "invalid" characters.

This won't end well for you.

I would recommend you don't put a restriction on the input (aside of what the RFC defines as valid) and instead correctly escape (or throw if your context doesn't support escaping) when moving the raw data to a new context.

In this case, I think the problem is in PHP's `mail()` function that put stuff in shell context without any escaping.

The fix should be happing in `mail()` (which is internally shelling out and thus switching context), not in the caller and certainly not in the frontend controller when it needs to decide whether a given email address is valid or not.

Re:PHPMailer Exploit – Remote Code Execution by annnnd on Wednesday December 28, 2016 @05:52AM (#13269747)
I think you are taking my argument too far. Valid input doesn't mean it is safe to use in any context. It just means that anything which is invalid at the start shouldn't even have the chance to get in the system.

You should still properly escape data when passed to another context, no doubt about it.

I am also not suggesting "dirty" practices like replacing double quotes (Wordpress) or magically escaping quotes on input (PHP prior to... 5?). But if you know the input should be a number, allow only digits and '.' and clean everything else. And validate the range too! If you need a database ID, validate form and existence. If you need an e-mail address, run it through filter_var. Always. There is no reason not to.

Re:PHPMailer Exploit – Remote Code Execution by DCoder on Wednesday December 28, 2016 @02:51AM (#13269096)
> When configured to use CLI sendmail,

That's not correct. PHPMailer can be configured to send mail through raw SMTP, by directly invoking sendmail, or by calling PHP's mail() function (which is itself a wrapper around sendmail). This vulnerability affects only the last mode, when PHP's mail() calls sendmail. If you have PHPMailer configured to call sendmail directly, this vulnerability does not apply.

Re:PHPMailer Exploit – Remote Code Execution by annnnd on Wednesday December 28, 2016 @05:34AM (#13269685)
I stand corrected, thank you for clarifying.
Re:PHPMailer Exploit – Remote Code Execution by neotek on Wednesday December 28, 2016 @04:35AM (#13269491)
>I know it is tough to validate all addresses according to RFC

Understatement of the year!

If you were to try and actually validate according to the RFC, chances are you'll introduce new vectors of attack in your code simply because of the complexity of what you're trying to achieve.

Honestly, the email address RFC is a litany of insane choices and kludged-together standards to account for the wild west free-for-all that existed before the modern internet emerged, and most of it has no bearing on the reality of how people use and create email addresses today. Better to just check if the address supplied has an @ in it somewhere and be done with it.

Re:PHPMailer Exploit – Remote Code Execution by x0 on Wednesday December 28, 2016 @06:13AM (#13269822)
That's how I do it. Check for an @. Anything past that is sendmail's problem.
Re:PHPMailer Exploit – Remote Code Execution by nkozyra on Wednesday December 28, 2016 @07:21AM (#13270038)
Even though it false positives some valid emails, I've always felt requiring a @ and a . is a good check for public facing validation.
Re:PHPMailer Exploit – Remote Code Execution by Piskvorrr on Thursday December 29, 2016 @05:48AM (#13276669)
Technically valid e-mail addresses - but nonsensical ones. I haven't seen an e-mail without a domain cross a network in decades ("cross a network" excepts "user@localhost", and I'm not old enough to have seen any IP-adressed mails ;)).

So yes, I believe that in 2016, a match on ^[^@]+@([^.@]+\.)+[^.@]+\.?$ does not have any actual false negatives, although allowing false positives. That's not entirely helpful in this CVE, though.

Re:PHPMailer Exploit – Remote Code Execution by systematical on Wednesday December 28, 2016 @02:31AM (#13269025)
This is true, I couldn't figure out why this wasn't working, then realized I had to comment out this validation:

  filter_var($app->request()->post('email'), FILTER_VALIDATE_EMAIL)