SQL Injection
SQL Injection
Bisha University
What is SQL Injection ?
• SQL injection is a code injection technique that might destroy your database.
• SQL injection is the placement of malicious code in SQL statements, via web page
input.
How SQL Injection works?
Impact of SQL Injection
• There are a number of things an attacker can do when exploiting an SQL injection on
a vulnerable website. Usually, it depends on the privileges of the user the web
application uses to connect to the database server. By exploiting an SQL injection
vulnerability, an attacker can:
• Add, delete, edit or read content in the database
• Read source code from files on the database server
• Write files to the database server
Real World Examples
• On August 17, 2009, the United States Justice Department charged an American
citizen Albert Gonzalez and two unnamed Russians with the theft of 130 million
credit card numbers using an SQL injection attack.
1) In-band SQLi
2) Inferential SQLi
3) Out-of-band SQLi
In-band SQLi
• In-band SQL Injection occurs when an attacker is able to use the same
communication channel to both launch the attack and gather results.
• The two most common types of in-band SQL Injection are Error-based
SQLi and Union-based SQLi.
In-band SQLi
Error-based SQLi :
is an in-band SQL Injection technique that relies on error messages thrown by
the database server to obtain information about the structure of the
database.
Union-based SQLi:
is an in-band SQL injection technique that leverages the UNION SQL operator
to combine the results of two or more SELECT statements into a single result
Inferential SQLi
• In an inferential SQLi attack, no data is actually transferred via the web application
and the attacker would not be able to see the result of an attack in-band.
• A hacker might get access to user names and passwords in a database by simply inserting
" OR ""=" into the user name or password text box:
• The code at the server will create a valid SQL statement like this:
• SELECT * FROM Users WHERE Name ="" or ""="" AND Pass ="" or ""=""
• The SQL above is valid and will return all rows from the "Users" table, since OR ""="" is
always TRUE.
Examples
• SQL Injection Based on Batched SQL Statements
• A batch of SQL statements is a group of two or more SQL statements,
separated by semicolons.
• Example:
• SELECT * FROM Users; DROP TABLE Suppliers
• Look at the following example:
• Example
• txtUserId = getRequestString("UserId");
txtSQL = "SELECT * FROM Users WHERE UserId = " + txtUserId;
• And the following input:
• User id: 105; DROP TABLE Suppliers
• The valid SQL statement would look like this:
• SELECT * FROM Users WHERE UserId = 105; DROP TABLE
Suppliers;
Example Website
Example Website
timbo317
cse7330
’ OR ‘a’=‘a
’ OR ‘a’=‘a
SELECT * FROM `login` WHERE `user`=‘’; DROP TABLE `login`; --’ AND
`pass`=‘’
SQL Injection Prevention
• 1. Employ comprehensive data sanitization. Websites must filter all user
input. Ideally, user data should be filtered for context. For example, email
addresses should be filtered to allow only the characters allowed in an e-
mail address, phone numbers should be filtered to allow only the characters
allowed in a phone number, and so on.