Wednesday, March 28, 2007

SQL injection attack on a PostgreSQL database (t_jiaozhu)

For the first time I have been in a position to realize that a machine was attacked from an outside source in a production enviroment. A web server running Apache 2 and PostgreSQL was successfully attacked using a SQL injection vulnerability. I first noticed there was a new table in one of our PostgreSQL databases named 't_jiaozhu'.



public t_jiaozhu table postgres

The table wasn't something that myself or our developer had created so I immediately went into WTF mode. First I googled for the term 't_jiazhu' and found that there was only one English result that mentioned SQL injection attacks with the previously mentioned table name. At this point we searched the PostgreSQL log files but didn't turn up much but with the advice of a security guru I know (John Sawyer) we checked out the Apache web server log files and found the attack.



# grep t_jiaozhu *fred-access_log:219.153.131.99 - - [25/Mar/2007:11:59:32 -0400] "HEAD /showemploymentopportunity.php?id=38;create%20table%20t_jiaozhu(jiaozhu%20varchar(200)) HTTP/1.1" 200 - "-" "Mozilla/3.0 (compatible; Indy Library)"


Sawyer also came up with a possibility that the IP in which the attack came from may have been a bot using an IDS.



"After the table was created, there were several hits from that IP that had the following user agent "Mozilla/3.0 (compatible; Indy Library)". A little digging shows that it might be a Chinese spambot."


Our developer quickly discovered that we weren't checking varibles that were being passed. A quick addition of code fixed the problem.



if (!is_numeric($id))
$id = 0;
posted by Stephen Reese at

13 Comments:

Blogger Rio Yotto said...

thanks Richard.
It's really usefull for me.
someone do the same thing for my sql server (windows 2003 Server) but it's clean now.

thank you
~Rio Yotto

1/26/2008 10:48:00 AM  
Blogger Stephen Reese said...

Glad to hear that you were able to fix the problem.

1/26/2008 01:03:00 PM  
Blogger Matt Langley said...

I just got hit too, on a SQL Server. Obviously I've fixed the vulnerability now.

Any idea if it has any other payload other than the table creation? I've not been able to find any yet, although, worryingly, my transaction log has grown 10x more today that it has over the previous year!

2/07/2008 12:11:00 PM  
Blogger Stephen Reese said...

I couldn't find anything else besides the table that was created. I searched through the rest of the tables to make sure there was not any other additions. It doesn't seem to produce any type of shell or escalation of privileges. If you find out more then please inform me.

2/07/2008 12:36:00 PM  
Blogger Matt said...

I certainly will let you know if I find anything. So far, I've only found the table, I am pleased to say.

Thanks,

Matt.

2/07/2008 01:12:00 PM  
Blogger Rio Yotto said...

closed any error code that appears when you run query .. and also check check your scripts and also all permisions on your folder.
i've got attacked by c99 shell scripts also .. :) and it's clean now.

2/07/2008 09:48:00 PM  
Blogger Stephen Reese said...

I'm assuming I didn't have a problem with that because I ran on an open-source platform but thank you for the heads up.

2/07/2008 09:59:00 PM  
Blogger Crivens said...

Thanks Richard, Found your page after a google search for jiaozhu. I got hit on the 25th april and 19:00 gmt. All I got was a table created, using this ;create%20table%20t_jiaozhu(jiaozhu%20varchar(200))
appended to the querystring.

Once this had succeeded they used a
%20and(char(94)%2Bdb_name()%2Bchar(94))
to find the database name and then a similar attacks to find the user name and the table names. Everything apart from the create table failed, fortunately.

It took 27 seconds for the attack to complete and around 50 different attempts where tried, all of which used data from the previous error messages. So an intelligent bot would appear to be the culprit.

The IP address used was 58.241.178.219 HTTP/1.1 Internet+Explorer+6.0

I have increased the security and all the attacks now fail (so far).

Fingers crossed.
Paul

5/02/2008 05:43:00 AM  
Blogger Stephen Reese said...

Thanks for the information Paul!

5/02/2008 11:46:00 AM  
Blogger Bjorgen said...

We have been hit many times by this and other SQL Injection hacks, and we have not yet found a way to protect ourselves from it happening again. The damage was much more than just the creation of the t_jiaozhu table...they were able to insert massive scripts. Any ideas on how to keep this from happening?

7/27/2008 01:28:00 PM  
Blogger Stephen Reese said...

Are you allowing your frontend to insert data into the backend (database)? The problem that caused our DB to have the new table to appear; we weren't sanitizing and checking the data that was being inserted correctly. If script are being uploaded and run I would double check your entire system for vulnerable pieces of software especially the DB package.

7/29/2008 10:16:00 AM  
Blogger CDR32 said...

Thanks for this great article.

7/17/2009 05:34:00 AM  
Blogger EB said...

I guess it is still out there over 2 years after this original post. It hit us recently on a SQL Server through ASP and updated several tables. I thought I had fixed all vulnerabilities. I now plan to restrict the account permissions to SELECT only everywhere that I can.

7/22/2009 08:48:00 AM  

Post a Comment

<< Home