I am starting a series of blog posts that detail security related strategies, penetration testing and best practice methodologies. To start our series, I am going to delve into the world of SQL injection techniques and a general overview for those who are looking to learn a little more about this method of injection.
There is already quite a bit of documentation out there regarding this, so I hope this post isn’t too redundant. There are a lot of tools out there to assist in accomplishing this task, or at the very least tools that assist in automating the probing and injection of SQL from publicly facing websites, forms and the like.
That tool is SQLMAP (http://sqlmap.sourceforge.net/). SQLMAP is an “open source penetration testing tool that automates the process of detecting and exploiting SQL injection flaws and taking over of back-end database servers.”
This article does not introduce anything new, SQL injection has been widely written and used in the wild. I thought I’d write this article to document some of the SQL injection methods and hope that it may be of use to some of you out there in cyberspace.
What is SQL injection anyway?
It is a trick to inject SQL query/command as an input possibly via web pages. Many web pages take parameters from web user, and make SQL query to the database. Take for instance when a user login, web page that user name and password and make SQL query to the database to check if a user has valid name and password. With SQL Injection, it is possible for us to send crafted user name and/or password field that will change the SQL query and thus grant us something else.
What do you need?
Technically all you need is a web browser.
What should I look for?
Web forms. Any input area of a website that interacts with their database backend. Could be a login form, search form or anything like that.
You could also look for pages that actually have querystrings in the URL such as :
Testing if its vulnerable
With those query string URLs or web forms, you could do a simple test to see if its vulnerable to injection. Start with the “single quote trick” , something like this :
For example :
If you do that in a login form for example, if it works, then you will be logged in without any password necessary.
Why ‘ or 1=1–?
Let us look at another example why ‘ or 1=1– is important. Other than bypassing login, it is also possible to view extra information that is not normally available. Take an asp page that will link you to another page with the following URL:
In the URL, ‘category’ is the variable name, and ‘food’ is the value assigned to the variable. In order to do that, an ASP might contain the following code (OK, this is the actual code that we created for this exercise):
sqlstr="SELECT * FROM product WHERE PCategory=’" & v_cat & "’"
As we can see, our variable will be wrapped into v_cat and thus the SQL statement should become:
The query should return a resultset containing one or more rows that match the WHERE condition, in this case, ‘food’.
Now, assume that we change the URL into something like this:
Now, our variable v_cat equals to “food’ or 1=1– “, if we substitute this in the SQL query, we will have:
The query now should now select everything from the product table regardless if PCategory is equal to ‘food’ or not. A double dash “–” tell MS SQL server ignore the rest of the query, which will get rid of the last hanging single quote (‘). Sometimes, it may be possible to replace double dash with single hash “#”.
However, if it is not an SQL server, or you simply cannot ignore the rest of the query, you also may try
The SQL query will now become:
It should return the same result.
Depending on the actual SQL query, you may have to try some of these possibilities:
" or 1=1–
‘ or ‘a’=’a
" or "a"="a
‘) or (‘a’=’a
Remote execution with SQL injection
Being able to inject SQL commands usually means we can execute any SQL query at will.Default installation of MS SQL Server is running as SYSTEM, which is equivalent to Administrator access in Windows. We can use stored procedures like master..xp_cmdshell to perform remote execution:
Try using double quote (“) if single quote (‘) is not working.
The semi colon will end the current SQL query and thus allow you to start a new SQL command. To verify that the command executed successfully, you can listen to ICMP packet from 10.10.1.2, check if there is any packet from the server:
If you do not get any ping request from the server, and get error message indicating permission error, it is possible that the administrator has limited Web User access to these stored procedures.
Getting the output of my SQL query
It is possible to use sp_makewebtask to write your query into an HTML:
But the target IP must folder “share” sharing for Everyone.
Hope this helps!