SQL Injection
SQL Injection
INJECTION
CPSC 4670
Topics
DB Server
Web Server
SQL Injection in PHP
$link = mysql_connect($DB_HOST, $DB_USERNAME,
$DB_PASSWORD) or die ("Couldn't connect: " . mysql_error());
mysql_select_db($DB_DATABASE);
$query = "select count(*) from users where username = '$username' and
password = '$password‘ ";
$result = mysql_query($query);
SQL Injection Attack #1
I will not try to get any information, I just wan to bring the
application down.
Beyond Data Retrieval
• Variable interpolation.
• 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.
4. Did you miss any metacharacters?
Different case
SeLecT instead of SELECT or select
Bypass keyword removal filters
SELSELECTECT
URL-encoding
%53%45%4C%45%43%54
SQL comments
SELECT/*foo*/num/*foo*/FROM/**/cc
SEL/*foo*/ECT
String Building
‘us’||’er’
chr(117)||chr(115)||chr(101)||chr(114)
Stored Procedures
$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();
References:http://devzone.zend.com/article/686
http://unixwiz.net/techtips/sql-injection.html
Prepared Queries
Other Injection Types
Shell injection.
Scripting language injection.
File inclusion.
XML injection.
XPath injection.
LDAP injection.
SMTP injection.
SQL injection Conclusion
SQL injection is technique for exploiting
applications that use relational databases as their
back end.
Applications compose SQL statements and send to
database.
SQL injection use the fact that many of these
applications concatenate the fixed part of SQL
statement with user-supplied data that forms
WHERE predicates or additional sub-queries.
SQL injection Conclusion
The technique is based on malformed user-supplied
data
Transform the innocent SQL calls to a malicious call
Cause unauthorized access, deletion of data, or theft
of information
All databases can be a target of SQL injection and
all are vulnerable to this technique.
The vulnerability is in the application layer outside
of the database, and the moment that the application
has a connection into the database.
Project 7: Due on April 25
1. Andres Andreu, Professional Pen Testing for Web Applications, Wrox, 2006.
2. Chris Anley, “Advanced SQL Injection In SQL Server Applications,”
http://www.nextgenss.com/papers/advanced_sql_injection.pdf, 2002.
3. Stephen J. Friedl, “SQL Injection Attacks by Example,” http://www.unixwiz.net/techtips/sql-
injection.html, 2005.
4. Ferruh Mavituna, SQL Injection Cheat Sheet, http://ferruh.mavituna.com/sql-injection-cheatsheet-
oku
5. J.D. Meier, et. al., Improving Web Application Security: Threats and Countermeasures, Microsoft,
http://msdn2.microsoft.com/en-us/library/aa302418.aspx, 2006.
6. Randall Munroe, XKCD, http://xkcd.com/327/
7. OWASP, OWASP Testing Guide v2, http://www.owasp.org/index.php/Testing_for_SQL_Injection,
2007.
8. Joel Scambray, Mike Shema, and Caleb Sima, Hacking Exposed: Web Applications, 2nd edition,
Addison-Wesley, 2006.
9. SEMS, “SQL Injection used to hack Real Estate Web Sites,”
http://www.semspot.com/2007/12/19/sql-injection-used-to-hack-real-estate-websites-extreme-
blackhat/, 2007.
10. Chris Shiflett, Essential PHP Security, O’Reilly, 2005.
11. SK, “SQL Injection Walkthrough,” http://www.securiteam.com/securityreviews/5DP0N1P76E.html,
2002.
12. SPI Labs, “Blind SQL Injection,” http://sqlinjection.com/assets/documents/Blind_SQLInjection.pdf,
2007.
13. Dafydd Stuttard and Marcus Pinto, Web Application Hacker’s Handbook, Wiley, 2007.
14. WASC, “Web Application Incidents Annual Report 2007,”
https://bsn.breach.com/downloads/whid/The%20Web%20Hacking%20Incidents%20Database
%20Annual%20Report%202007.pdf, 2008.