Further SQL Help
COPYRIGHT: THIS TUTORIAL CAN BE COPIED AND SHARED ANYWHERE YOU WANT, BUT YOU SHOULD GIVE CREDITS TO ME. THE CONTENTS ALSO SHOULD'NT BE MODIFIED IN ANY WAY. THAT IS ALL..
DISCLAIMER: THIS TUTORIAL IS SOLELY FOR EDUCATIONAL PURPOSES. YOU TAKE FULL RESPONSIBILITY FOR ANY ACTION YOU DO AFTER READING THIS TUTORIAL.
#What is a database?
#What is an SQL injection?
#Accessing secret data
#Checking for vulnerability
#Finding the number of columns
#Addressing vulnerable part
#Finding the MySQL version
#MySQL 5 or above injection
#MySQL 4 injection
#Modifying site content
#Greetz and shoutz
Greetz to all, I am sam207. In this tutorial, I will demonstrate the infamous MySQL injection in the newbie perspective so that all the newbies can soon become successful SQL injectors. Also do not be harsh on me if there are any grammatical errors on the tutorial because English is not my native language (I'm from Nepal). Now lets begin our walkthrough of SQL injection.
What Is A Database?
Just some general info.. The database is the application that stores a collection of data. A database offers various APIs for creating, accessing and managing the data it holds. And database (DB) servers can be integrated with our web development so that we can pick up the things we want from the database without much difficulties. A DB may hold various critical information like usernames, passwords, credit cards, etc. So, the DB needs to be secured but many DB servers running are insecure either because of their vulnerability or because of poor programming skills. To name few DB servers: MySQL (Open source), MSSQL, MS-ACCESS, Oracle, Postgre SQL (open source), SQLite, etc.
What Is An SQL Injection?
An SQL injection is probably the most abundant programming flaw that exists on the internet at present. It is the vulnerability through which an unauthorized person can access various critical and private data. The SQL injection is not a flaw in the web or DB server, but is a result of the poor and inexperienced programming practices. And it is one of the deadliest as well as easiest attack to execute from a remote location.
In SQL injections, we interact with DB server with the various commands and get various data from it. In this tutorial, I would be discussing the 3 aspects of the SQL injection namely bypassing logins, accessing the secret data and modifying the page contents. So lets head forward on our real walkthrough..
Suppose a site has a login form and only the registered users are allowed to enter the site. Now, say you wanted to bypass the login and enter the site as the legitimate user. If the login script is not properly sanitized by the programmer, you may be lucky enough to enter the site. You might be able to log into the site without knowing the real username and real password by just interacting with the DB server. So, isn't that the beauty of SQL injection??
Let's see an example, where the username admin with the password sam207 can log into the site. Suppose the SQL query for this is carried out as below:
If the above SELECT command evaluates true, the user will be given access to the site otherwise disallowed. Think what we could do if the script is not sanitized.. This opens a door for the hackers to gain illegal access to the site.
In this example, the attacker can enter the following user data in the login form:
Username: a' or 1=1--
So, this would make our query as:
Note that -- is the comment operator and anything after it will be ignored as a comment. There is also another comment operator: /* both should be tried. So our above query becomes:
Now this query evaluates true even if there is no user called 'a' because 1=1 is always true and using OR makes the query return true when one of the queries is true. And this gives access to the sites admin panel.
There can also be various other username and password combinations to play with on vulnerable sites. You can create your own new combinations for the sites login. such as:
username: ' or 1='1 password: ' or 1='1
username: ' or '1'='1' password: ' or '1'='1'
And there are many more cheat sheets. Just google it.
That's all about bypassing logins.
Accessing Secret Data
An SQL injection is not done for bypassing logins exclusively but it is also used for accessing the sensitive and secret data in the DB servers. This part is long, so I would be discussing in the subsections.
Sub-section 1: Checking for vulnerability
Suppose, you have a URL like this:
Now to check if it is vulnerable, you would simply add ' in the end i.e. where id variable is assigned. So, it is:
Now if the site is not vulnerable, it will filter and the page will load normally. But if it doesn't filter the query string, it would give an error similar to below:
"MySQL Syntax Error By '5'' In article.php on line 15."
Or an error that tells us to check the correct MySQL version or MySQL Fetch error or sometimes just a blank page. The error may be in any form. But this will confirm that the site is vulnerable.
Sub-section 2: Find the number of columns
So now its time to find the number of columns present. For this purpose, we will be using 'order by' until we get an error. That is, we make our URL query as:
This didn't give an error.
Now, Increase it to 2.
Still no error
So, we need to increase until we get the error. In my example, I got error when I put the value 3 i.e.
This gave me an error.
So, it means there are 2 columns in the current table (3-1=2). This is how we find the number of columns.
Sub-section 3: Addressing the vulnerable part
Now, we need to use the union statement and find the column which we can replace so as to see the secret data on the page. For this we do:
Now we will see the number(s) on the page somewhere. I mean, either 1 or 2 or both 1 & 2 are seen on the page. So, this means we can replace the number with our commands to display the private data the DB holds.
In my example, 1 is seen on the page. This means, I should replace 1 with my things to proceed further. Got it?? So lets move forward.
Sub-section 4: Finding the MySQL version
For our injection, it is necessary to find the MySQL version because if it is 5, our job becomes lot easier (as version 5 and onwards has a lot more supported commands.) To check the version, there are two MYSQL functions, @@version or version().
So what we do is replace one (which is the replaceable part) with @@version i.e. we do as below:
So, this would return the version of the MySQL running on the server. But, sometimes you may get error with above query. If that is the case, make use of the unhex(hex()) function like this:
Remember that if you have to use the unhex(hex()) function here, you will also have to use this function in the injection process.
@@version will give you the version. It may be either 4 or 5 and above. I'm now going to discuss the injection process for version 5 and 4 separately because as I said earlier, version 5 makes it easy for us to perform the injection.
Sub-section 5: MySQL 5 or above injection
Here, I am gonna show you how to access data in a server running MySQL 5 or above.You have got the MySQL version 5.0.27 standard using the @@version in URL parameter. MySQL from version 5 has a useful function called information_schema. This is a table that holds information about the tables and columns present in the DB server. That is, it contains name of all tables and columns of the site.
For getting table list, we use: table_name FROM information_schema.tables
For getting column list, we use: column_name FROM information_schema.columns
So our query for getting the table list in our example would be:
And yeah if you had to use unhex(hex()) while finding version, you will have to do:
This will list all the tables present in the DB. For our purpose, we will be searching for the table containing the user and password information. So we look the probable table with that information. You can even write down the table names for further reference and works. For my example, I would use tbluser as the table that contains the user and password.
Similarly, to get the column list we would make our query:
This returns all the columns present in the DB server. Now from this listing, we will look for the probable columns for username and password. For my injection, there are two columns holding these info. They are username and password respectively. So that's the column I wanted. You have to search and check the columns until you get no error.
Alternatively to find the column in the specific table, you can do something like below:
This would display the columns present in the table tbluser. But this may not always work. Let me show you how I got to know that the above two columns belong to table tbluser. Now let me show how to display the username and password stored in the DB.
There is a function called concat() that allows me to join the two columns and display on the page. Also I will be using : (a colon) in the hex form. Its hex value is 0x3a (that's a zero at beginning not the letter "o") What I do is:
And this gives me the username and password like below:
Here the password is hashed and in this case, its MD5. Now you need to get the hash cracker like John The Ripper (www.openwalls.org), Cain & Able (www.oxid.it) and crack the hash. The hash may be different like SHA1 (Note: SD1 hashes are usually a multiple of 20 characters long whereas an md5 hash is usually 32 characters long) or sometimes plaintext password may be shown on the page. In this case, when I crack I get the password as sam207.
Now you get to the admin login page and login as admin. Then you can do whatever you like. So that's all for the MySQL version 5.
Sub-section 6: MySQL 4 injection
Now say your victim has MySQL version 4. Then you won't be able to get the table name and column name as in MySQL version 5 because it lacks support for information_schema.tables and information_schema.columns.
So now you will have to guess the table name and column name until you do not get any errors. For example, you would do as below:
Here, I guessed the table name as user. But this gave me the error because the table with the name user didn't exist on the DB. Now I kept on guessing for the table name until I didn't get error.
When I put the table name as tbluser, the page loaded normally. So I came to know that the table tbluser exists.
The page loaded normally. Now again you have to guess the column names present in the tbluser table. I do something like below:
This gave me error so there is no column with this name.
It loaded the page normally along with the username from the table.
It produced an error so again the column pass does not exist in the table tbluser.
The page loaded normally with password hash (or plaintext password).
Now you may do this:
And the result is:
On cracking, I got sam207 as a password. Now I just need to login the site and do whatever I want. A few table names you may try are: user(s), table_user(s), tbluser(s), tbladmin(s), admin(s), members, etc.
You may try these methods to get various data such as credit card numbers, social security numbers, etc. Just what you need to do is figure out the columns and get them displayed on the vulnerable page. That's all on the injection for accessing secret data.
Modifying Site Content
Sometimes you find the vulnerable site and get everything needed but maybe the admin login doesn't exist or it is inaccessible for a certain IP range. Even in that context, you can use some kewl SQL commands for modifying the site content. I haven't seen much articles addressing this one so thought to include it here.
Here, I will basically talk about a few SQL commands you may use to change the site content. The commands are the workhorse of MySQL and are deadly when executed. First let me list these commands:
UPDATE: It is used to edit information already in the db without deleting any rows.
DELETE: It is used to delete the contents of one or more fields.
DROP: It is used completely delete a table and all its associated data.
Now, you could have figured out that these commands can be very destructive if the site lets us interact with DB with no sanitization and proper permissions. Command Usage:
Lets say the query is:
Though in reality, we don't know the query as above, we can find the table and column name as discussed earlier.
So we would do:
Or you could alternatively do:
By executing first query, we have set the title value as 'Hacked By sam207' in the table article while in second query, we have updated all three fields title, data, and author in the table article. Sometimes, you may want to change the specific page with id=5. For this you will do:
As already stated, this deletes the content of one or more fields permanently from the DB server.The syntax is:
Or if you want to delete these fields from the id=5, you will do:
This is another deadly command you can use. With this, you can delete a table and all of its associated data. For this, we make our URL as:
This would delete table article and all its contents.
Finally, I want to say little about ; (the semi colon) Though I have not used this in my tutorial, you can use it to end your first query and start another one. This semicolon can be kept at the end of our first query so that we can start new query after it.
There are many security and exploit sites where you can learn new things. A few examples are:
To learn hacking, you need practice and there are sites which offer you a legal platform to learn hacking through hacking wargames. Some are:
GREETZ & SHOUTZ:
Greetz to all at darkmindz. Load of shoutz to pSyChO mOnkee and sToRm (U two guys rock) and all at GNY. Also greet to t0mmy9 (Thanks for always helping me learn things) at www.thisislegal.com
And hi to all my classmates bigyan musa, bhakunde sameer, gainda sandeep, joe haatti, dipesh bhedo, eman bhainsi, milan biralo, nikesh gandeula (Pheretima posthuma) and all my other classmates. Without you guys, I'm having boring days in my biology class. Hope to meet u all guys. And I wish bright future of you guys. Become successful doctors..
With this, my tutorial which was mainly intended for newbies, ends here. I hope you liked my tutorial. I will hopefully write new tutorials in newbie concept after I teach myself all these things. Any comments can be dropped at samar_acharya[at]hotmail.com
And finally, read more and more, ask more and more and that's the best way to learn anything.
Keep Hacking and Enjoy It.
Tutorial by sam207
Online (last 15 mins): guapboys