10.Web Application Security-SQL Injection
10.Web Application Security-SQL Injection
INFT1201
Mohamed Elghazouly
Web Application Security –
Injection Attacks
Agenda*
• Injection Attacks
• SQL Injection Attacks
• Types of SQL Injection
1. In-Band SQL Injection
2. Blind/Inferential SQL Injection
3. Out-of-Band SQL injection
• Countermeasures
* Most of the content in this lecture note is adopted from Certified Ethical Hacker (CEH)
Version 11 [1]
Injection Attacks
Injection Attacks
• Key Idea: Input data from the application is executed as code by the interpreter.
• Examples
• SQL injection
• Command injection
• HTML script injection
• Object injection
• Remote file injection
• Format Specifier Injection
• Shell injection
The Fundamental Cause
• Mixing data and code together is the cause of several types of vulnerabilities and
attacks including SQL Injection attack, XSS attack, attacks on the system() function
and format string attacks.
Interacting with Database in Web Application
• A typical web application consists of three major components: Web Browser, Web
Application, and Database Server.
• Users do not directly interact with the database but through a web server.
• If this channel is not implemented properly, malicious users can attack the
database and cause damage.
Attack Classification: Injection
Image Source:
OWASP
Injection Attacks: SQL
Attack Classification: Injection
Image Source:
XKCD
Structured Query Language (SQL)
• Structured Query Language (SQL) is a textual language used
by a database server.
• Logic: ‘username’=‘admin’
Example: SELECT * FROM `table` WHERE ‘username’=‘admin’
User Input:
$_POST = admin
$_POST = pass ‘ OR ‘1’=‘1
Query Result:
“SELECT id, name FROM users WHERE name=‘admin’ AND password=‘pass’ OR ‘1’=‘1’ “
Vulnerable Code: Username Field Injection
$conn = mysql_connect("localhost","username","password");
User Input:
$_POST = “admin’ OR ‘1’=‘1 ”
$_POST = “pass“
Why does this work
more often?
Query Result:
“SELECT id, name FROM users WHERE name=‘admin’ OR ‘1’=‘1’ AND password=‘pass’ “
Error-based SQL Injection - Example # 2
Stack Based
UNION Select SQL Injection - Example # 5
https://www.example.com/items.php?id=2'
• A heavy query retrieves a massive amount of data, and it will take a long time to
execute on the database engine.
• Example:
• SELECT count(*) FROM all_users A, all_users B,
all_users C
Out-of-Band SQL injection
Out-of-Band SQL injection
• Out-of-Band SQL Injection: Attackers
use different communication channels
(such as database email functionality
or file writing and loading functions) to
perform the attack and obtain the
results.
Countermeasures
How to Defend Against SQL Injection Attacks
• This coding style allows the database to distinguish between code and data,
regardless of what user input is supplied.
• Prepared statements ensure that an attacker is not able to change the intent of a
query, even if SQL commands are inserted by an attacker.
Prepared Statements – Example
$conn = getDB();
• When the execute function $sql = "SELECT name, local, gender
is called, the prepared FROM USER_TABLE
statement is combined WHERE id = $id AND password =’$pwd’ ";
with the parameter values $result = $conn->query($sql))
you specify.
$conn = getDB();
$stmt = $conn->prepare("SELECT name, local, gender
• The parameter values are FROM USER_TABLE
WHERE id = ? and password = ? ");
combined with the
// Bind parameters to the query
compiled statement, not a
$stmt->bind_param("is", $id, $pwd);
SQL string. $stmt->execute();
$stmt->bind_result($bind_name, $bind_local, $bind_gender);
$stmt->fetch();
Why Are Prepared Statements Secure?
• Trusted code is sent via a code channel.
• Untrusted user-provided data is sent via data channel.
• Database clearly knows the boundary between code and data.
• Data received from the data channel is not parsed.
• Attacker can hide code in data, but the code will never be treated as code, so it
will never be executed.
* SQL Injection Myths and Fallacies by Bill Karwin, for the complete presentation refer to https://www.slideshare.net/billkarwin/sql-injection-myths-and-fallacies
Some good sites to learn more
▪ Prevention guide (with sample code in many languages): http://bobby-
tables.com/
▪ Tutorials:
1. (webinar) http://www.percona.com/webinars/2012-07-25-sql-injection-
myths-and-fallacies
2. http://www.unixwiz.net/techtips/sql-injection.html
▪ Cool site that let’s you try out attacks on a sample DB and explains why they work
http://sqlzoo.net/hack/
▪ Research paper on how to retrofit existing websites to combat SQL injection
attacks http://lersse-dl.ece.ubc.ca/record/205/files/paper.pdf
Summary
• Injection Attacks
• SQL Injection Attacks
• Types of SQL Injection
1. In-Band SQL Injection
2. Blind/Inferential SQL Injection
3. Out-of-Band SQL injection
• Countermeasures
Questions?
References
[1] EC-Council. (2020). Certified Ethical Hacker (CEH) Version 11 eBook w/ iLabs
(Volumes 1 through 4). [VitalSource Bookshelf]. Retrieved
from https://bookshelf.vitalsource.com/#/books/9781635675160/
[6] Web Application Security and Scanning: Explanation and Deep Dive
https://www.rapid7.com/fundamentals/web-application-security/