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 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,,NULL,NULL,NULL,NULL,NULL,NULL from sysobjects a,syscolumns b where and'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.


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

File Upload through Null Byte Injection

Sometimes, during file upload we come across situation wherein there would be check on the file extension at the client side as well as server side too. If the application does allow only .jpeg extension to be uploaded, the client side java script checks for the extension of the file before passing the request. We all know that how easily this can be defeated.
Some applications, checks for the extension at the server side also. That's not easy to bypass. However there are some ways with which it still can be bypassed. Most of server side scripts are written in high level languages such as Php, Java etc who still use some C/C++ libraries to read the file name and contents. That leads to the problem. In C/C++ a line ends with /00 or which is called Null Byte. So whenever the interpreter sees a null byte at the end of the a string, it stops reading thinking it has reached at the end of the string.
This can be used for the bypass. It works for many servers, specially php servers. Th…

Insecure protocols

Some basic insecure protocols and risk associated with them: