Exploiting Blind SQL Injection in the Wild
Ok, as promised here is a full Blind Sql tutorial. It took me awhile to get this done because it does
take a long time to fully Blindly inject a site all the way to credentials. I obviously can’t cycle through
everything here today, but I can give you all of the syntax for every concept used herein. After reading
this you should have more then a passing understanding of Blind Sql Injection. To start we will extract
the current user.
Using binary search methods, it won’t be too hard to extract the user. First I will show you the binary search in action to get the first digit in this full blind Sql injection then we will move forward rather quickly an it will be assumed you grasp the concepts. To show everyone each and every query used would be tiring so we will build off the concept and once its grasped we can move into other methods and syntaxes used.. If you don’t understand a section reread it or you surely won’t get the next section. You can always pm me for help toom but please RTFM first.. So, let’s move on.
So, we have our Blind Injectable sight: http://www.hkdatation.com/
First off I am not going to actually penetrate the site, but I will give you the knowledge needed to do this on your own. I am here just to show you the concept of blind Sql and explain it alittle so we can all get a stronger grasp on just how its done..
What exactly is Blind Sql injection?
Blind Sql injection is a form of Sql vulnerability that exexutes Sql logic on the server, but doesn’t give the Sql output to the screen that we all know and love. BlindSql is the last possible type of Sql to use in a crackers toolbox and as stated its a pretty tedious process.In most situations you would probably be better off writing/using some kind of tool for it. Still, even with tools if you don’t understand BlindSql then a tool probably won’t be much help.
How does it work though?
Findamentally, Blind Sql works by querying the database with yes/no or true/false questions. Seeing as we can’t extract info in a traditional way we have to resort to forming queries that can pull that same info for us one byte at a time. There is another method of using if/else with Sqli, but I won’t have a use for that today. Before I get ahead of myself let me explain the main styles used for Blind SQLi:
1) Error based blind -> in this situation we are receiving some sort of error. It could be a SQL error or a custom error that lets on nothing of its origin or reason.
1a) A subsection of above. I don’t know the name, but it falls under the same class as error based Blind injection. In this scenario there may be a picture out of place, some text missing, or many times a whole different page altogether. The key is to be able to tell when Sql is executing by looking for these changes. After awhile of practicing it becomes fairly routine to find these injections. This is the type of injection were focusing on today.
2) Timed -> With timed injection we have 2 possible options in MySql we have Benchmark which we can call to encrypt something for for us say 10000 times using Sha1 which due to heavy load will cause a time delay basically letting us know the Sql has been executed. MySql also have the sleep() function which is unfortunately not available in all version’s of MySQL to date.. It started in version MySQL 5.0.12, and is will probably be disabled in future versions. Hopefully not right away though. I’m not going to go too deep into Timed attacks. If someone wants a tutorial for time-delay Sql ‘maybe’ I’ll write one after this one.
Aren’t there plenty of Blind tutorials out there? Why is yours special?
Well, in most Blind tutorials we don’t really get more the version() and user() info which is all fine and dandy, but what if we want more then that? I mean if were testing a website for someone and can only retrieve the user and version info then it may be pretty hard to explain to the Web Admin why this attack can be so devastating. I will be using a Live attack for you and we will actually pull data from information_schema(one byte at a time of course) and before were through hopefully you will have a better grasp on just how this attack works.. And if you know more then me maybe you can give me some pointers too. Anywho, let’s move on.
Like most SQL injections at the beginning, before we dive into the database were going to want to find out a few thins. Like for instance the version() and user(). Its funny that where most Blind tuts end is exactly where we will starting. Now, on a lot of Blind Injections you may get an error page, you may not. You may just get a different page then you would’ve gotten had you used a different query or value. You have to be able to decipher all of these things because just because if you notice a bug and one type of SQLi doesn’t appear to be working its good to have other ways to leverage the bug. BlindSqli is a very good last resort believe me, but it takes alot of patience. Lets not get ahead of ourselves though.
First we use the standard test for blind. 1=1 then 1=2. If the page loads the same then its not vulnerable. If the page is different in any sense or errored we would get different responses. This means our target is most likely is vulnerable.
So, we try out injection:
Assuming the page is vulnerable to BlindSql the results will be different and they are. If you load the page with each query you will see a small missing picture and that’s all it takes. Everything else looks the same, but that little anomoly tells us that there’s a good chance we have a decent candidate for Blind Sql.
So now lets get our version info with this query:
Breaking this down were using substring to break up the data into a true/false situation meaning IF
the first digit in the query response is “5” then we know that the version is greater then or equal to version
5. If the page loads an error or in our case without the picture then we would change the value of 5 to 4 and check
the page load:
Ok, so on this query we get the false page, missing the picture which is different then the intended page we started with. (which we already know, but were confirming. Translated,it means were using version 5 so we have access to information_schema(Ya!). If your unfamiliar still take some time to examine the page output from a true query then to a false query. You need to understand this because this is our main means of extracting data.
Before we get down to the nitty gritty we need to check for subselect. Without subselect we cant do much in the way of exctracting tables/columns so we need to check this before we start throwing queries around. Subselect can be checked easily like so:
This is a simple query that doesn’t need much picking apart. Its just saying Select 1 is equal to 1. If we couldn’t select (Select 1)=1 wouldve been false. Most Sql versions above 5 susceptible to Blind, allow subselect.
note-> From here on out a True page load is true and a False pageload is false. True = Image and False != image. It will help with readability.
4) LENGTH USERNAME
Now were going to need some info. First off we need to know who we are(are we root?), but before we start extracting we should know how many characters exist in the name so we don’t waste any time trying to extract info after we have all the output.
So, first the length of the user() we will use the binary search method that we should already be familiar with:
Broken down this query is saying,”Ok the length of the current user is greater then > less then < or equal to = this amount of characters. Simple right?
So, now we know there are 24 characters in the username.(So much for email@example.com) The username obviously includes the IP as well as the user so lets get to finding out who the current user is. We will do this using the Ascii conversion of letters to Ascii digits, then using the Ascii numbers to translate back to letters.If I’m confusing some of you dont worry it wall all be very clear before the tutorial/possible Whitepaper is finished.
So lets see who we, as user() are exactly :
^ Returns true, Indicating that the current user() 1st digit is greater then the ascii representative of 97 which is a lowercase ‘a’.
For ascii help: http://www.asciitable.com/
5) BINARY SEARCH:
Now, lets do a quick extract of the username. Using binary search methods it won’t be too hard to extract the user.
First I will show you the binary search in action to get the first digit:
Note:As stated we can use < > = in our binary searches in Blind Sql Injection
6) WHAT IS MY NAME?
From here we can guess where the actual user() name is heading. Probably some spinoff of the site in the name like hkdatation_user@ip or whatever. Knowing or having an idea on where the data is taking you is the best way to make the Blind exploitation move quicker. To move to the next digit we move the first limit of 1 up to 2.
So enough talk, its pretty apparent were not root here, but still we figure out exectly who we are using Ascii. Note: alot of requests will be snipped for brevity.
firstname.lastname@example.org <- Thats who we are:)
Now that may seem like it would take forever, but it really doesn’t(Though a perl script would work much faster) because once we get an idea of where the data is taking us the binary search methods really help.
Ok, so now that we know our who we are, that the version is 5, and we have access to subselect. This is valuable data to move forward with our attack.
Like most SQL injection attacks to gain the most leverage over the data and possibly system access we need to gain access to user tables, credit card info, or whatever it is we came to get we really need to be able to query information_schema. Unless were lucky this is a foolproof way to overlook the database. Luckily Blind Sql injection does have a method for us to extract table names from information_schema and the main database. It may look a little familiar if you do a lot of Sql injection.
So to grab our tables from the database:
So as you can see forming Blind Sql strings in MySql are very easy and uses the same logic and queries that we’re used to:
So quickly to go through things. I’m going to shoren these up so hopefully they wont fall off the page:
So we got ft_about so far. Hopefully there in alphabetical order: So, as I’m sure you have already guessed (or know) how to get into the next table: