SQL Injection
SQL Injection
by Fabrizio d’Amore
faculty of Ingegneria dell’Informazione
Università di Roma “La Sapienza”
WHAT IT IS, WHAT IT IS NOT
December 2009
¢ capability of giving SQL commands to a database
engine exploiting a pre-existing application
not exclusive to Web applications, but widespread
2
WHAT APPLICATIONS ARE VULNERABLE?
December 2009
¢ in practice, all databases based on SQL
MS SQL Server, Oracle, MySQL, Postgres, DB 2,
Informix etc.
3
HOW IT WORKS
December 2009
¢ client injects SQL code into the input data of an
application
4
EXAMPLE
December 2009
¢ if the following query returns data...
SELECT * FROM users
December 2009
¢ attacker can access database in read/write/admin
depends on the vulnerability of the specific DBMS
6
POSSIBLE HTML FORM
December 2009
¢ from Wikipedia
(http://it.wikipedia.org/wiki/SQL_injection)
7
POSSIBLE LOGIN.PHP FILE
December 2009
<?php
//Prepares query, in a variable
$query = "SELECT * FROM users WHERE
December 2009
¢ if script does not make input analysis and
validation, user can send
9
OTHER (WORSE) CONSEQUENCES
December 2009
¢ symbol ';' is exploited, it allows to concatenate
commands
¢ or
10
LINKS
December 2009
¢ examples
http://www.owasp.org/index.php/SQL_Injection
http://www.unixwiz.net/techtips/sql-injection.html
December 2009
¢ input validation
client side
¢ parameterized queries
based on predefined query strings
December 2009
¢ use scripts, e.g., Javascript
13
PARAMETERIZED QUERIES
December 2009
¢ avoid the traditional dynamic query string, where
pre-defined substrings have to be replaced by user
defined text
14
JAVA PREPARED STATEMENTS
December 2009
¢ see Sun tutorial on JDBC
(http://java.sun.com/docs/books/tutorial/jdbc/basics
/index.html)
December 2009
// define query schema
String selectStatement = "SELECT * FROM User WHERE
userId = ? ";
// execute query
ResultSet rs = prepStmt.executeQuery(); 16
VULNERABILITIES IN PREPARED STATEMENTS
December 2009
¢ Java prepared statements, if not carefully packed,
may be vulnerable to SQL injection
17
STORED PROCEDURES: WHAT AND WHY
December 2009
¢ compiled procedures (subroutines) made available
at server side to build/support batches operating
on DB
18
USE OF STORED PROCEDURES
December 2009
¢ also known as proc, sproc, StoPro or SP, belong to
data dictionary
December 2009
A few controls (partial list)
¢ format (e.g., digits or dates)
¢ types (e.g., if text has been inserted when digits are