Detecting (and even preventing) SQL Injection Using the Percona Toolkit and Noinject!



Similar documents
CSCI110 Exercise 4: Database - MySQL

5 Percona Toolkit tools that could save your day. Stéphane Combaudon FOSDEM February 3rd, 2013

Check list for web developers

The purpose of this report is to educate our prospective clients about capabilities of Hackers Locked.

Web Application Report

Automating Security Testing. Mark Fallon Senior Release Manager Oracle

Version of this tutorial: 1.06a (this tutorial will going to evolve with versions of NWNX4)

Basic & Advanced Administration for Citrix NetScaler 9.2

Web Applications Security: SQL Injection Attack

Bubble Code Review for Magento

Cyber Security Challenge Australia 2014

Advanced Web Security, Lab

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

Playing with Web Application Firewalls

SQL INJECTION ATTACKS By Zelinski Radu, Technical University of Moldova

Application security testing: Protecting your application and data

D61830GC30. MySQL for Developers. Summary. Introduction. Prerequisites. At Course completion After completing this course, students will be able to:

Still Aren't Doing. Frank Kim

Advanced Administration for Citrix NetScaler 9.0 Platinum Edition

STOPPING LAYER 7 ATTACKS with F5 ASM. Sven Müller Security Solution Architect

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

Using Nessus In Web Application Vulnerability Assessments

Web Application Guidelines

SQL Injection. The ability to inject SQL commands into the database engine through an existing application

MAGENTO HOSTING Progressive Server Performance Improvements

Use Enterprise SSO as the Credential Server for Protected Sites

Benchmarking and monitoring tools

Easy Method: Blind SQL Injection

CS 558 Internet Systems and Technologies

Practical Identification of SQL Injection Vulnerabilities

White Paper. Blindfolded SQL Injection

SQL Injection. SQL Injection. CSCI 4971 Secure Software Principles. Rensselaer Polytechnic Institute. Spring

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

THE SMARTEST WAY TO PROTECT WEBSITES AND WEB APPS FROM ATTACKS

Hardening MySQL. Maciej Dobrzański maciek at

SiteCelerate white paper

ABC LTD EXTERNAL WEBSITE AND INFRASTRUCTURE IT HEALTH CHECK (ITHC) / PENETRATION TEST

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

EVALUATING COMMERCIAL WEB APPLICATION SECURITY. By Aaron Parke

Threat Modeling. Categorizing the nature and severity of system vulnerabilities. John B. Dickson, CISSP

Magento Security and Vulnerabilities. Roman Stepanov

Microsoft Windows PowerShell v2 For Administrators

Guidelines for Web applications protection with dedicated Web Application Firewall

Facebook Twitter YouTube Google Plus Website

External Vulnerability Assessment. -Technical Summary- ABC ORGANIZATION

ArcGIS Server Security Threats & Best Practices David Cordes Michael Young

Using MySQL for Big Data Advantage Integrate for Insight Sastry Vedantam

SQL Injection Attack Lab

Make a folder named Lab3. We will be using Unix redirection commands to create several output files in that folder.

Web and Security 1 / 40

What is Web Security? Motivation

STABLE & SECURE BANK lab writeup. Page 1 of 21

Implementation of Web Application Firewall

Understanding Sql Injection

SQL Injection. By Artem Kazanstev, ITSO and Alex Beutel, Student

LISTSERV LDAP Documentation

CSE598i - Web 2.0 Security OWASP Top 10: The Ten Most Critical Web Application Security Vulnerabilities

FINAL DoIT v.8 APPLICATION SECURITY PROCEDURE

Auditing a Web Application. Brad Ruppert. SANS Technology Institute GWAS Presentation 1

How I hacked PacketStorm ( )

LabVIEW Internet Toolkit User Guide

SQL Injection Vulnerabilities in Desktop Applications

IP Application Security Manager and. VMware vcloud Air

Application Security Testing. Erez Metula (CISSP), Founder Application Security Expert

Hardened Plone. Making Your Plone Site Even More Secure. Presented by: Nathan Van Gheem

Oracle Forms Services Secure Web.Show_Document() calls to Oracle Reports

Securing and Accelerating Databases In Minutes using GreenSQL

PHP on IBM i: What s New with Zend Server 5 for IBM i

Black Hat Briefings USA 2004 Cameron Hotchkies

Perl In Secure Web Development

JOOMLA SECURITY. ireland website design. by Oliver Hummel. ADDRESS Unit 12D, Six Cross Roads Business Park, Waterford City

Role Based Access Control. Using PHP Sessions

Chapter 1 Web Application (In)security 1

FREQUENTLY ASKED QUESTIONS

How To Install Amyshelf On Windows 2000 Or Later

Source Code Review Using Static Analysis Tools

Enterprise Application Security Workshop Series

MPP Manager Users Guide

Getting started with OWASP WebGoat 4.0 and SOAPUI.

Oracle Audit Vault and Database Firewall. Morana Kobal Butković Principal Sales Consultant Oracle Hrvatska

Job Reference Guide. SLAMD Distributed Load Generation Engine. Version 1.8.2

WordPress Security Scan Configuration

Evaluation of Penetration Testing Software. Research

Threat Modeling/ Security Testing. Tarun Banga, Adobe 1. Agenda

LockoutGuard v1.2 Documentation

OWASP and OWASP Top 10 (2007 Update) OWASP. The OWASP Foundation. Dave Wichers. The OWASP Foundation. OWASP Conferences Chair

Firewalls, NAT and Intrusion Detection and Prevention Systems (IDS)

Log Analyzer Reference

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

Vulnerability Scan. January 6, 2015

Attack and Penetration Testing 101

UQC103S1 UFCE Systems Development. uqc103s/ufce PHP-mySQL 1

Recon and Mapping Tools and Exploitation Tools in SamuraiWTF Report section Nick Robbins

CHAPTER 5 INTELLIGENT TECHNIQUES TO PREVENT SQL INJECTION ATTACKS

Ruby on Rails Secure Coding Recommendations

G563 Quantitative Paleontology. SQL databases. An introduction. Department of Geological Sciences Indiana University. (c) 2012, P.

Oracle Forms Services Secure Web.Show_Document() calls to Oracle Reports Server 6i

Integrating VoltDB with Hadoop

The release notes provide details of enhancements and features in Cloudera ODBC Driver for Impala , as well as the version history.

Transcription:

Detecting (and even preventing) SQL Injection Using the Percona Toolkit and Noinject! Justin Swanhart Percona Live, April 2013

INTRODUCTION 2

Introduction 3 Who am I? What do I do? Why am I here?

The tools 4 MySQL (5.0+) Percona Toolkit pt-query-digest pt-fingerprint MySQL Proxy (0.8.0+) Apache and PHP 5.3+

WHAT IS SQL INJECTION? 5

What is SQL injection? 6 SQL injection is an attack vector An attacker modifies the SQL queries which will be executed by the server But the attacker does not need to change the code on the server or get access to the server

What is SQL injection interpolation (strings) 7 $username = $_GET[ username ]; $sql = select 1 from users.users where admin_flag=true and username =. $username. ; $ wget http://host/path.php?username=bob SQL injection! $ wget http://host/path.php?user_id= ' or '1'='1 and username = or 1 = 1

Escape strings, or use prepared statements! 8 #escape string values $username = mysqli_real_escape_string($_get[ username ]); $sql = select and username =. $username. ; #prepared statement $username = GET[ username ]; $stmt = mysqli_stmt_init($conn); $sql = select and username =? mysqli_stmt_prepare($stmt, $sql); mysqli_stmt_bind_param($stmt, s, $username); mysqli_stmt_execute($stmt); mysqli_stmt_close($stmt);

What is SQL injection interpolation (ints) 9 $user_id = $_GET[ user_id ]; $sql = select 1 from users.users where admin_flag=true and user_id =. $user_id; SQL injection! $ wget http://host/path.php?user_id=1 $ wget http://host/path.php?user_id= 1 or 1=1

Use type checking, or prepared statements! 10 #check that integers really are integers! $user_id = GET[ user_id ]; if(!is_numeric(user_id)) $user_id = NULL ; $sql = select and user_id =. $user_id; #prepared statement $user_id = GET[ user_id ]; $sql = select and user_id =? mysqli_stmt_bind_param($stmt, i, $user_id); mysqli_stmt_execute($stmt);

When escaping can t help 11 Some parts of a SQL statement can t be manipulated using parameters These include ORDER BY columns Variable number of items in an IN list Adding SQL syntax like DISTINCT

Don t use user input in the query 12 #avoid using user input directly in ANY way $sql = select * from listings where deleted = 0 and sold = 0 and open = 1 ; if(!empty($_get[ ob ])) { } $sql.= ORDER BY. $_GET[ ob ]; wget?ob=post_date Bad! wget?ob= post_date union all (select * from listings) Now we can see all listings

Use whitelisting instead 13 #avoid using user input directly in ANY way $sql = select * from listings where deleted = 0 and sold = 0 and open = 1 ; $allowed = array( post_date, neighborhood, etc ); if(!empty($_get[ ob ]) && is_string($_get[ ob ])) { } if(in_array($_get[ ob ], $allowed)) { } $sql.= ORDER BY. $_GET[ ob ]; in_array() is the keeper of the gate wget?ob=post_date wget?ob= post_date union all (select * from listings)

All that works great for the apps you control 14 BUT If you don t have the source for an app, then you really can t be sure it isn t safe from SQL injection Or maybe you have to support old apps Or apps that were not developed rigorously What do we do in these cases?

15 Out-of-band SQL injection detection SQL INJECTION DETECTION USING PT-QUERY-DIGEST

How to detect SQL injection? 16 Most applications only do a small number of things. Add orders, mark orders as shipped, update addresses, etc. The SQL patterns that identify these behaviors can be collected and whitelisted. Queries that don t match a known fingerprint may be investigated as SQL injection attempts

What is a query fingerprint? 17 A query fingerprinting algorithm transforms a query into a form that allows like queries to be grouped together and identified as a unit In other words, these like queries share a fingerprint Even though the queries differ slightly they still fingerprint to the same value This is a heuristic based approach

Tools that support query fingerprints 18 Percona Toolkit tools pt-query-digest Reads slow query logs and populates the whitelist table. Can also be used to display new queries that have not been marked as allowed. pt-fingerprint Takes a query (or queries) and produces fingerprints. Useful for third party tools that want to use fingerprints.

What is a query fingerprint (cont?) 19 select * from some_table where col = 3 becomes select * from some_table where col =? select * from some_table where col = IN (1,2) becomes select * from some_table where col IN (?)

Query fingerprints expressed as hashes 20 pt-query-digest can provide short hashes of checksums select * from some_table where col =? 982e5737f9747a5d (1631105377) select * from some_table where col = IN (?) 2da8ed487cdfc1c8 (1680229806268) base 10

pt-query-digest 21 Normally used for profiling slow queries Has a SQL review feature for DBAs Designed to mark query fingerprints as having been reviewed This feature can be co-opted to discover new query fingerprints automatically New fingerprints are either new application code or SQL injection attempts

pt-query-digest review feature 22 Need to store the fingerprints in a table Known good fingerprints will be marked as reviewed If pt-query-digest discovers new fingerprints you will be alerted because there will be unreviewed queries in the table

pt-query-digest - review table initialization 23 Need to initialize the table pt-query-digest /path/to/slow.log \ --create-review-table --review h=127.0.0.1,p=3306,u=percona,p=2un1c0rns,d=percona,t=whitelist \ --sample 1 \ --no-report Where to store fingerprints Don t print report Don t waste time on stats

pt-query-digest command-line review 24 pt-query-digest /path/to/slow.log \ --review DSN \ --sample 1 \ --report \ --limit 0 How it knows which queries have already been reviewed Don t collect stats, just sample one of each new fingerprint Display the report of queries Ensure that all unreviewed queries are shown

USING THE WHITELIST WITH SQL 25

Detecting new query fingerprints 26 SELECT count(*) FROM percona.whitelist WHERE reviewed_by IS NULL; Any new queries? percona.whitelist is just an example name, you can use any you like SELECT checksum, sample FROM percona.whitelist WHERE reviewed_by IS NULL; Get a list of the queries

Add a query fingerprint to the whitelist 27 UPDATE percona.whitelist SET reviewed_by = allow, reviewed_on = now() WHERE checksum= 1680229806268;

Blacklist a query fingerprint 28 You might also explicitly blacklist a fingerprint UPDATE percona.whitelist SET reviewed_by = deny, reviewed_on = now() WHERE checksum = 1631105377;

Web interface for whitelist management 29 The Noinject! project (discussed later) has a web interface that can be used to mark queries as reviewed It can be with both the noinject.lua proxy script or with pt-query-digest

30 Out of band detection LIMITATIONS AND CAVEATS

Out-of-band detection 31 Some damage or information leakage may have already happened To limit the extent of the damage send an alert as soon as a new pattern is detected Ensure thorough application pattern detection in a test environment to avoid false positives

Get logs as fast as possible 32 Use tcpdump on a mirrored server port Pipe the output to pt-query-digest Use tcpdump on the database server Adds some additional overhead from running the tools on the same machine Possibly higher packet loss Collect and process slow query logs frequently Adds slow query log overhead to server Longer delay before processing

33 What to do BEFORE a fishy fingerprint appears FINDING THE VULNERABILITY

Prepare for finding a vulnerability 34 Tracking down the vulnerable code fragment can be difficult if you have only the SQL statement Not just a problem with SQL injection since it is usually convenient to see where a SQL statement was generated from

Add tracing comments to queries 35 A good approach is to modify the data access layer (DAL) to add SQL comments Comments are preserved in the slow query log Comments are displayed in SHOW commands SHOW ENGINE INNODB STATUS SHOW PROCESSLIST Make sure your client does not strip comments!

Add tracing information 36 PHP can use debug_backtrace() for example PERL has variables that point to the file and line Investigate the debugging section of your langauge s manual

What to place in the comment 37 Here are some important things to consider placing into the tracing comment session_id (or important cookie info) application file name, and line number important GET, POST, PUT or DELETE contents Any other important information which could be useful for tracking down the vector being used in an attack

Example comments in SQL queries 38 select airport_name, count(*) from dim_airport join ontime_fact on dest_airport_id = airport_id where depdelay > 30 and flightdate_id = 20080101 /* webserver:192.168.1.3,file:show_delays.php,l ine:326,function:get_delayed_flights,user:ju stin,sessionid:7b7n2pcniokcgf */ This comment contains all that you need

Most apps don t do this out of the box 39 You can modify the application If you have the source code (and it uses a DAL) BUT There isn t much you can do if The application is closed source, or you can t change the source There is no DAL (code/query spaghetti) For any other reason it is problematic to inject information into all SQL queries

If I can t change the source? 40 You can t fix the problems when you detect them. Consider using an open source solution Or consider in-band protection

41 In-band SQL injection detection SQL INJECTION PREVENTION

In-band protection 42 Using pt-query-digest to discover new query patterns is useful But it doesn t work in real time It can t block bad queries from actually executing

In-band protection 43 What is needed is a man in the middle that inspects each query to ensure it matches an allowed fingerprint. MySQL proxy can be used for this purpose

MySQL Proxy 44 MySQL Proxy Supports Lua scripting for easy development Adds some latency to all queries Considered alpha quality though for simple scripts it seems stable enough Fingerprinting and checking database also adds latency. 3ms 5ms per query is to be expected

Noinject! The Lua script and PHP interface 45 http://code.google.com/p/noinject-mysql The Lua script for MySQL proxy is pretty much drop-in. Just modify it to point to your database server and specify credentials and other options. PHP script is similarly easy to configure. Drop in a directory on an Apache box Modify the script to set the options.

The Lua proxy script known queries 46 By default the script will retrieve all known good fingerprints and cache them locally when the first query is received from a client Also by default, all queries that fail to pass the known whitelist check are logged in an exception table. Both of these options can be changed easily

The Lua proxy script known queries 47 Each query is fingerprinted If the fingerprint is on the whitelist, the actual query is sent to the server If the query is not on the whitelist the behavior varies depending on the proxy mode

Lua script Proxy mode 48 permissive mode Records the SQL fingerprint into the whitelist table but does not mark it as reviewed Allows the query to proceed restrictive mode Records the SQL fingerprint into the whitelist table Returns an empty set for the query

Why use permissive mode? 49 Permissive mode allows the collection of SQL fingerprints for an application dynamically Just run the application with typical workload and the SQL queries will be recorded automatically Eventually switch to restrictive mode

PHP Web interface 50 1999 mode HTML interface White or black list the fingerprint Query Sample Last action time with note

If you want something prettier 51 This is open source so If you want bug fixes or have feature requests You can engage with Percona for development You can contribute! You can fork your own version

If the proxy overhead is too high 52 You could develop the functionality in MySQL too bad the parser is not pluggable Try mysqlnd plugins fingerprint queries in PHP match them to a whitelist maintained in a serialized PHP array reject queries that aren t approved Improve the proxy lua script fingerprint process could probably be made faster

Percona Training Advantage 53 This presentation and the Noinject! tool were created by Justin Swanhart, one of Percona s expert trainers Check out http://training.percona.com for a list of training events near you Request training directly by Justin or any of our other expert trainers by contacting your Percona sales rep today

Q/A 54