rc3.org Strong opinions weakly held

Mass SQL Injection attack

I’ve been reading up on the mass SQL injection attack launched against servers running IIS and MS SQL Server last week. That article is a bit out of date, as the problem turned out not to be a security hole in IIS but rather security holes in the Web applications themselves. The script that compromised the sites was clever enough to probe for forms that didn’t prevent SQL injection, and injected SQL specifically designed to work with MS SQL.

The article linked to also says that the attacks were first reported on April 17, but I found an incident report from April 13 describing the attack.

The end goal of these attacks is to infect client PCs with malware. The attacks appends database fields with markup that includes JavaScript from a third party domain, and then when any page that displays the field is viewed by a user, the JavaScript is downloaded and executed, any unpatched client PCs will be infected by the malware.

IIS.net has a post that explains exactly how the attack works. The script finds a form on the site that does not properly escape user input and injects SQL that finds all of the text columns in the database and appends the HTML that loads the malware to each of them. You can read more about it here. The main innovation in this attack is the cleverly written SQL injected that’s ejected. A similar attack could be launched against any application that does not protect against SQL injection. MySQL is somewhat protected in that most sites are running MySQL 4.x, which doesn’t provide support for stored procedures.

There are several takeaways from these attacks:


4 Comments

Sanitizing user input, while essential, is the incorrect way to avoid SQL injection in my opinion. SQL injection is caused by gluing strings together to create SQL queries. In this day and age there is absolutely no excuse for doing this – you should be using a database library that takes a SQL statement with placeholders and the values for those placeholders separately (some environments call these prepared statements, but you can simulate them with anything just by writing a simple function).

PHP originally escaped input by default, and it took YEARS for the community to recover from the mess that it caused!

Posted by Simon Willison on 27 April 2008 @ 2pm

Simon: I agree completely about using placeholders but I think the problem with PHP was that it wasn’t consistent and a lot of code was written which made dangerous assumptions about the magic quotes behavior. It wouldn’t have been perfect but the web would be safer if PHP had default escaping for input and output.

If I could wave a magic wand for PHP6 I’d make a few backwards-incompatible changes:

Posted by Chris Adams on 27 April 2008 @ 3pm

should sanitize all input by default and force programmers to go out of their way to access the original input

sanitize it for what? For output to the web? for usage in sql queries? If the latter, for which databases, and which charactersets should be assumed? My point is there is too many variables to make it automatic.

@Simon:

(some environments call these prepared statements, but you can simulate them with anything just by writing a simple function).

and please, please, people who do that make sure that the function does more than simply glue string togethers without any further checks or escaping, otherwise you’ve only obfuscated the problem in the code, but exploiting is as easy as it was before. (Yes, I’ve seen it often enough…)

Posted by Sencer on 3 May 2008 @ 5am

Php all together needs to be avoided

Posted by http://wasabi.homelinux.com/ on 25 May 2008 @ 3pm

Leave a Comment