Welcome to TechNet Blogs Sign in | Join | Help

Anatomy of a SQL Injection Incident

A number of people are reporting that 10K+ websites have been hacked via a SQL injection attack that injected a link to a malicious .js file into text fields in their database.  For example, here's Avert Labs report.

The reports that I've seen talk about how the .js file tries to compromise clients that connect to the defaced web servers; however, they don't discuss the really interesting part.  How did the servers get hacked in the first place? 

Whenever I see a large number of hosts compromised in the same way, my initial assumption is that they all share a piece of vulnerable code.  If this was the case, I thought that it would be in everybody's interest to figure out what that shared code was and take appropriate action.

Since the CSS Security team here at Microsoft worked with several of these incidents, I was able to look at multiple sets of data and the work that my colleagues had already done.  The first thing I noticed was that the attacks looked, with a few exceptions, identical.  They shared several things in common:

  • Two initial query strings that do some basic injection, apparently as a test.
  • One or more additional queries, specifically to do IS_SRVROLEMEMBER() happen in some cases.
  • Two final queries that DECLARE a variable that CASTs a large hex value into NVARCHAR and then EXEC()'s that string.  The string contains a script to append the link to the .js file onto every string-type column in every table in the database.
  • All of these happen within a very short period of time.  The only lag seems to be the time it takes the final two queries to execute.  (In the case with the largest database, the last query actually failed with a timeout.  I guess that's not surprising since it's essentially doing a find-and-replace on the entire database table.)

The last item makes me think that this is an automated process of some sort, particularly since the user-agent string for the last two queries is different from the user-agent string for the preceding ones.

This is where it gets interesting, though.  In comparing the pages that were compromised in different incidents, I realized that, while they're all vulnerable to SQL injection, they don't appear to share any common code. 

My next question, then, was how did the attacker select these sites?  Given that the whole attack took a very short period of time and the attacker only touched a single ASP page on the server, it was pretty clear that they had some reason to believe that page was vulnerable prior to connecting to the site.  It's possible that they had previously scanned the site but I didn't find any indication that this was true.

The other possibility that occurred to me was that the attacker was using a generalized approach to search Google for vulnerable sites.  As I was thinking this through, I found that I wasn't the first person to have thought about it:

How Prevalent Are SQL Injection Vulnerabilities 

Guard Against SQL Injection Attacks 

SQL Injection Is Surprisingly Easy

So, there you have it -- automated SQL injection attacks in a nutshell.  Next week, I'll try to find some time to go into some of the details and brainstorm on how incident responders can get to the root of SQL injection attacks.

(I should note that many brilliant people on my team provided information and analysis into how the incidents happened.)

(Part 2 is here.)

Published Friday, March 14, 2008 4:19 PM by neilcar

Comments

# Mass SQL injection coming to an IIS + ASP server near you . . .

Saturday, March 15, 2008 5:21 AM by Robert Hensing's Blog

My friend Neil has a pretty good post on the mass SQL injection stuff that was reported in the press

# re: Anatomy of a SQL Injection Incident

Monday, March 17, 2008 7:07 AM by anonymous

This seems to repeat what the Internet Storm Center wrote on Januari 9th, and they linked it back to November 2007.

# re: Anatomy of a SQL Injection Incident

Monday, March 17, 2008 9:20 AM by neilcar

You're quite right -- this looks like another wave of the same attack.  I was aware of the earlier incidents but I hadn't seen any data from them.

# re: Anatomy of a SQL Injection Incident

Monday, March 17, 2008 3:09 PM by eponymous

isn't this the same or similar attack vector to the RIAA hack a few months ago

# Se prémunir des attaques de type "SQL Injection"...

Wednesday, March 19, 2008 4:35 AM by SQL Server, BizTalk Server, le 64 bits et au-delĂ  !...

Pour mieux comprendre les risques auxquels sont exposées vos bases de données, il est intéressant

# re: Anatomy of a SQL Injection Incident

Saturday, March 22, 2008 1:18 PM by Michael Doe

Hi Neil,

I work as System Engineer in a large ISP company and we are hosting a large number of legacy ASP applications which contain SQL Injection flaws. I have been using this tool when clients agree:

http://www.codeplex.com/IIS6SQLInjection

So far the results have been very good and I have not had problems (except that I cannot install in 64 bit). Do you know something about this tool? Is there a way to make it work in 64 bit?

Thanks,

P.S.: I am not using my real name to avoid problem with my clients.

# Anatomy of a SQL Injection Incident

Wednesday, March 26, 2008 3:11 AM by Microsoft Switzerland Security Blog

Blog Posting from Neil Carpenter: "A number of people are reporting that 10K+ Web sites have been hacked

# Mass SQL Injection -- Get Used To It

Friday, April 04, 2008 2:00 PM by Neil Carpenter's Blog

It looks like another wave of the mass SQL injection I talked about last month is going on.  The

# SQLInjectionFinder

Tuesday, May 27, 2008 1:51 PM by Neil Carpenter's Blog

My colleague Greg , who has forgotten more about command line scripting than I will ever know, put together

# SQL Injection General Guidance

Wednesday, May 28, 2008 4:13 AM by Microsoft Switzerland Security Blog

There s a lot of noise arround currently ongoig SQL injection attacks and even if that is quite an "old"

# The latest SQL Injection Attacks

Friday, May 30, 2008 3:41 AM by Roger's Security Blog

Well, there was quite some chatter over the last few weeks with regards to the massive defacements we

# Inyección SQL... esta bajo ataque?

Monday, June 16, 2008 4:55 AM by Todo es posible, nada es seguro

Hay muchos sitios y blogs que hablan sobre el tema de inyección SQL. Puede encontrar toda la información

# SQL Injection attacks

Tuesday, June 24, 2008 8:17 PM by Troubleshooting and Tips - Cindy Gross

<p>This year SQL injection attacks are being stepped up and even automated against SQL Server. While SQL injection attacks can occur against any DBMS, my blog will only address SQL Server.</p ...

# SQL Injection Atacken

Wednesday, June 25, 2008 2:03 AM by Schweizer IT Professional und TechNet Blog

Vielen von Ihnen ist es beretis bekannt, dass es seit einigen Monaten SQL injection Attacken gegen angreifbare

New Comments to this post are disabled
 
Page view tracker