SQ L Injection 1
SQ L Injection 1
SQ L Injection 1
SQL Injection
Topic
1. What are injection attacks?
2. How SQL Injection Works
3. Exploiting SQL Injection Bugs
4. Prevent SQL Injection
5. Other Injection Attacks
Injection
Injection attacks trick an application into including unintended commands in
the data send to an interpreter.
Interpreters
Interpret strings as commands.
Ex: SQL, shell (cmd.exe, bash)
Key Idea
Input data from the application is executed as code by the interpreter.
What is SQL Injection
SQL injection (SQLi) is an application security weakness that allows attackers to
control an application’s database
letting them access or delete data, change an application’s data-driven behavior, and
do other undesirable things
by tricking the application into sending unexpected SQL commands
One of the most dangerous issues in terms of data confidentiality and integrity in
web applications is a vulnerability called SQL injection.
SQL Injection
1. App sends form to user.
Form
sqlString = “select USERID from USER where USERID = `” & userId & “` and PWD = `” & pwd & “`”
result = GetQueryResult(sqlString)
Because anything after the -- will be ignore, the injection will work even without
any specific injection into the password predicate.
More Example (5)
User ID: ` ; DROP TABLE USER ; --
Password: `OR ``=`
Select USERID from USER where USERID = `` ; DROP TABLE USER ; -- ` and PWD =
``OR ``=``
I will not try to get any information, I just wan to bring the application down.
Impact of SQL Injection
Leakage of sensitive information.
Reputation decline.
Modification of sensitive information.
Loss of control of db server
Data loss.
Denial of service.
The Cause: String Building
Building a SQL command string with user input in any language is dangerous.
String concatenation with variables
String format functions like sprintf().
String templating with variable replacement.
Mitigating SQL Injection
Ineffective Mitigations
Blacklists
Stored Procedures
Problems:
1. Numeric parameters don’t use quotes.
2. URL escaped metacharacters.
3. Unicode encoded metacharacters.
Though it's easy to point out some dangerous characters, it's harder to point to
all of them.
Stored Procedures
Stored Procedures build strings too:
CREATE PROCEDURE dbo.doQuery(@id nchar(128))
AS
DECLARE @query nchar(256)
SELECT @query = ‘SELECT cc FROM cust WHERE id=‘’’ + @id + ‘’’’
EXEC @query
RETURN It's always possible to write a stored procedure that itself constructs a
query dynamically: this provides no protection against SQL Injection.
It's only proper binding with prepare/execute or direct SQL statements
with bound variables that provide protection.
Whitelist
Reject input that doesn’t match your list of safe characters to accept
$email is the data obtained from the user's form, and it is passed as positional parameter #1 (the first
question mark), and at no point do the contents of this variable have anything to do with SQL
statement parsing. Quotes, semicolons, backslashes, SQL comment notation - none of this has any
impact, because it's "just data". There simply is nothing to subvert, so the application is be largely
immune to SQL injection attacks.
Prepared Queries
Bound parameters in Java
Insecure version
Statement s = connection.createStatement(); ResultSet rs = s.executeQuery("SELECT email FROM
member WHERE name = " + formField);
// *boom*
Secure version
PreparedStatement ps = connection.prepareStatement( "SELECT email FROM member
WHERE name = ?");
ps.setString(1, formField);
ResultSet rs = ps.executeQuery();
Thank You!