Skip to main content

SQL Injection in search field

Earlier I had written about performing SQL injection in search field and how to do a DoS attack and privilege escalation using 'Like' operators. Now another SQLi exploitation I came across recently. That too in the search field. This becomes important as lots of people don't pay much attention on the search forms/ fields in the application. My aim is to show that a search form can also be exploited with SQL Injection. The following queries are based on a real world exploitation. The steps and data are for just illustration purpose only.
Suppose, the search form provides the details of users who have accessed the application some time and their login time details etc, we just need to provide their name in the search box provided. All the data were being going as Post request.
So, to just fingerprint the database, I provide, 'nil'+'esh' in the search field and it successfully gives me the results. That means the database behind the application is concatenating the name and made it a single string as 'nilesh'. This behavior shows it's a MS SQL server behind the scene. However,that also gets confirmed when the application straightaway throws an error about MS SQL server. But the above trick is another way of knowing about the database. For example: 'nil'| |'esh' could be Oracle and 'nil' 'esh' could be MySQL.

Now, next step is to extract some more information from the database, and Union query is a great way of doing that.
I supply, nilesh' UNION SELECT NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL-- which gives me no. of exact columns. As we can see, the no. of NULLs above is 8 so the table has 8 nos of columns. Anything less than 8 don't produce any error and anything above than 8 produces error. So, that way we can determine that the no. of columns are 8. Another way of doing that is executing ' ORDER BY query. Start by 'ORDER BY 1 till you get error and the no. before you get error is no. of the columns. So, supplying 'ORDER BY 9 gave error, so the no. is 8.

Next step is to know the datatype of the columns. The query ' UNION SELECT 'a',NULL,NULL,NULL,NULL,NULL,NULL,NULL--  which gives me error as 'conversion failed....data type int' so the first column is int. Similarly we can keep trying to replace each NULL in above query to know the datatypes of each column. Another example, ' UNION SELECT NULL,'a',NULL,NULL,NULL,NULL,NULL,NULL--  doesn't produce any error, so string datatype, so- forth, so-on.

Now more queries can be executed:
' UNION SELECT @@version,NULL,NULL,NULL,NULL,NULL,NULL,NULL--                /*version info*/

' UNION SELECT NULL,name,NULL,NULL,NULL,NULL,NULL,NULL from sysobjects where xtype='U'--                                                                            /*To retrieve only the user-defined objects*/
It gives the names of tables in database. One of them was, let's say, 'UserTable'. Now in order to see retrieve names of columns in the table, we run the following query:
' UNION SELECT NULL,b.name,NULL,NULL,NULL,NULL,NULL,NULL from sysobjects a,syscolumns b where a.id=b.id and a.name='UserTable'--     /*To discover the names of columns within the  table*/

Now when we have all in hand, we can run the following query to extract data from 'UserTable':
' UNION select ,.....from UserTable--
and that gives us the entire data from that table. That could be username and passwords also depending on the scenarios.




Comments

Vezance Xocobs said…
Good post. My blog once got hacked by SQL injection. I was flabbergasted at that time. My jaw almost dropped when I realized such a thing was even possible. This article went a long way in explaining how it could have happened.

Popular posts from this blog

Using an AirPcap device in Windows with Wireshark

Capturing wireless traffic in a Windows environment is unfortunately not as easy as a setting change. As with most Windows-based software, drivers in Windows are often not open source and do not allow for configuration change into monitor mode. With this in mind, we must use a specialized piece of hardware known as an AirPcap device. Once you have obtained an AirPcap device you will be required to install the software on the accompanying CD to your analysis computer. The configurable options include: • Interface - Select the device you are using for your capture here. Some advanced analysis scenarios may require you to use more than one AirPcap device to sniff simultaneously on multiple channels. • Blink LED - Clicking this button will make the LED lights on the AirPcap device blink. This is primarily used to identify the specific adapter you are using if you are using multiple AirPcap devices. • Channel - In this field, you select the channel you want AirPcap to listen on. Extension C...

Anti CSRF header

Recently I came across an application which was preventing crsf attacks using a unique non-traditional approach. In traditional approach the csrf is thwarted by embedding unique random tokens, called nonce, in each sensitive page. But this application, which was making ajax calls and used jQuery, was creating a header to identify the valid and invalid requests altogether. The idea is to generate a custom header, x-session-token in this case, with every request which is considered sensitive and includes any sort of transaction. For example: xhr.setRequestHeader('x-session-token', csrf_token)   At the server level, server checks for this header if found request is fulfilled, otherwise rejected. We need to use xhr calls for making use of this technique, not useful in regular POST and GET requests. Since, I was not aware of this kind of countermeasures, probably, since most of the applications I did were using standard requests. So, I searched a bit and found even Go...

Some one watching where you visited!

Yes... Mozilla has been susceptible to browser-history stealing java script code. Today, Giorgio posted some cool information about the exploit. Mozilla is already working on this. This bug has been reported. Actually they have set up a web site to show the proof-of-concept. Visit www.statrpanic.com in FF,Safari or Netscape and it will tell you which websites have you been already ! But I am not sure it will work in IE or not because my IE is not responding to the website. Clearing history of visited website makes you safe to this attack. I mean this is one way..may be there are other ways to exploit this. But I have found this effective. Try it yourself in FF and then in IE and see the results.