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