In the last few weeks I've noticed a new attack making the rounds on my CF server. Although it's not an effective attack against a CF server, it does illustrate how spammers are a boil on the butt of humanity. It's called "email injection" and it's actually an attempt to leverage a PHP vulnerability (or perhaps I should say a “bad PHP coding” vulnerability). How do you know if you are being attacked? If you have a web site with a "contact us" form or any other form whose result is a sent email, and you are getting emails "from" your own domain and "to" your own domain - using bogus email addresses you are probably seeing this technique in action. You will also get bounces and if you look in the raw bounce code you will see something like "bcc: *some email address*". That's the tip off. Please note, this technique does NOT work against Coldfusion as far as I know - only PHP seems to be referenced in the various online discussions of the topic. If you are interested read on.
Email injection is an attempt to exploit the file format of an email message. If you've ever looked in the spooler directory of your CF server or had the occasion to examine raw message formats on a mail server you will know that an email is really a file with instructions for delivery. There are a few required parameters (headers) and a number of optional ones. A typical mail message looks like this:
Of course, there's much more to it than that. There are a myriad of required and optional headers that are inserted into the message. These options and headers are interpreted by email clients, servers and other processes. Here's an example of an actual message - with the names changed to protect the less guilty.
As you can see the format can be pretty loose - with lots of stuff in there that may or may not belong - and the message can usually still be delivered.
PHP handles email through an email script. Such a script typically builds this file on the fly from the various inputs. As you can see the "linebreak" is the chief delimiter in the file format. If a user added a linebreak to the "subject" parameter, and then added a bcc header (for example) the output might end up looking like this:
You can see how that might make a bottom feeding spammer salivate. He can send spam messages from you to his victims through your own secure relay. That's sneaky and underhanded - which is why they love it I'm sure.
It get's worse than that. Using this methodology spammers can overwrite the subject, include HTML in the message and obscure text that is intended for the message. This post is one of the best explanations I found (actually more of a how to). It has a much more in-depth discussion of the issue and lots of PHP code samples
As far as I can see there are no CF implications. CF is in control of the file format sent to the spooler. It clearly defines the sections and headers and I doubt seriously that any attack could be constructed that would work against CF in this case. Please note, I have not tested it - I'm only making an educated guess. If the the attacks against the contact forms on my own site are any indication, we will know for sure very shortly. If you have some information on this subject please enlighten us.
There is a follow up post to this topic.
I've already linked to an excellent PHP blog for folks interested in that end of this conversation. This blog is to help CF folks who might be seeing those strange bounces from bots using this technique against their sites and wonder what exposure they might have in their own code. CF is not vulnerable to this specific attack - but it seems there are bots who do not differentiate and ply the technique anyway.
I agree, don't want to start that discussion either, because I think Coldfusion is definitly the right solution in certain cases.
As an experiment, I added CAPTCHA to my own contact and email-a-friend forms and the attempts stopped completely. This, in addition to the fact that log files indicate attempts are made only seconds apart and have all manner of headers filled with suspect data, would indicate to me that there's some manner of automated attempt that's being used.
I passed CAPTCHA off as the solution to this problem until I noticed something that came through today. Instead of the usual failed attempts, I saw a message come through that had succeeded in injecting headers. The reason is that I was using the contact's name and email as the FROM header of my mail. That gave the attacker the necessary access to the headers of the email.
So, the solution? Well, if you're using any user-supplied data in the email headers (subject, from, to, cc, bcc, etc.) be sure to check it for suspect data like "Content-Type: ", "MIME-Version: ", and carriage returns. Probably the most obvious is the carriage returns as that's what's used as the termination characters in email headers.
Thanks for this excellent rundown.
<cfif #form.Email# contains "@clientsite.com">
<cfmail to="webmaster@mysite.com" subject="Spammer" from="#form.Email#" type="html">
Error-Spam
</cfmail>
<cfelse>
<!---put cfmail tag here--->
</cfif>
PHP is vulnerable because it assembles the message directly by concatenating headers and content. But even then it would be only poorly written PHP that is vulnerable.
Your suggestion works fine, but I would add another possibility. If any form element you are using for "to" "from" or "subject" contains linebreaks (chr(10) or chr(13)) you can safely NOT send the message. linebreaks are what is used to differentiate the headers and they are necessary to make this exploit work.
Thanks for adding to my blog.