Selfdefending Databases Hashdays 2012
Selfdefending Databases Hashdays 2012
Alexander Kornbrust,
Red-Database-Security GmbH
Agenda
¡ Introduction
¡ Root Cause
¡ Anatomy of an attack
¡ Detection of an attack
¡ Countermeasures
¡ How to implement
¡ Discussion
Introduction
This presentation shows how databases can defend themselves against
SQL Injection attacks without human interaction.
The majority of attacker are using tools to attack web applications and
to download data that’s why human reaction on these events is
normally to slow.
Introduction
¡ Use of highly automated SQL injection tools, e.g. sqlmap, Havij,...
http://www.imperva.com/download.asp?id=352
https://www.hashdays.ch/slides/2011/bockermann_hashdays11.pdf (
Hashdays 2011 – Protecting
Databases with Trees
The presentation “A syntax-based approach to detect SQL injections”
from Christian Bockermann showed how to use the parse tree to detect
SQL Injection attacks.
This approach is smart but complex (SQL Parser, Training data, …).
Additionally it does not answer the problem what to in case of an SQL
Injection.
https://www.hashdays.ch/slides/2011/bockermann_hashdays11.pdf
Root Cause
Problem:
Solution:
¡ Applications are longer used than expected (sometimes 10+ years)
Solution:
¡ Even with a SIEM system 2 minutes are a challenge for most organizations
¡ Side effects of a stopping a system/service is not documented in most companies
¡ Application
¡ Not without changing the application itself (which is difficult)
¡ Database
False positives are rare. A false positive could occur if a developers are
deploying applications with incorrect SQL statement (e.g. missing
single quote).
Implementation
Oracle:
MySQL:
* http://dev.mysql.com/doc/refman/5.5/en/writing-audit-plugins.html
Detection of SQL Injection
attacks via error messages
• Depending from the used attack method (UNION, extend query,
create error messages to retrieve data, …) a specific error will be
created
e.g.
ORA-01789: query block has incorrect number of result columns
• Or
Microsoft OLE DB Provider for ODBC Drivers error
'80040e07' [Microsoft][ODBC SQL Server Driver][SQL Server]Syntax
error converting the nvarchar value ’mypassword' to a column of
data type int. /Administrator/login.asp, line 27
Typical SQL Injection Attack I
Original SQL command
Injected:
Union null,username from all_users--
Error message:
ERROR at line 1:
ORA-01789: query block has incorrect number of result
columns
Next attempt:
Union null,null,username from all_users—
Typical SQL Injection Attack VI
SQL Injection Error Codes Oracle - I
Error code
Error Message
Typical Command
ORA-00900
invalid SQL statement
ORA-00906
missing left parenthesis
ORA-00907
missing right parenthesis
ORA-00911
invalid character
e.g. PHP MAGIC_QUOTES_GPC
activated and attempt to
inject a single quote
http://www.evilsql.com/main/page2.php
React on errors
• The system could react on the errors caused by SQL Injection attempts
• Detection only / Audit the event
• If the application is blocked from the internet, intranet users can still
work with the application.
END IF;
END LOOP;
--
END after_error;
/
SQL Server
Concept SQL Server
• Event notifications are a special kind of database object that
send information about server and database events to a Service
Broker service.
http://weblogs.sqlteam.com/mladenp/archive/2008/07/18/Immediate-deadlock-notifications-without-changing-existing-code.aspx
http://msdn.microsoft.com/en-us/library/ms189453.aspx
MySQL
(all credits go to Xavier Mertens)
http://blog.rootshell.be/2012/11/01/mysql-attacks-self-detection/
Concept MySQL
Concept MySQL
• Create an UDF to write errors in a log file.
• Then the query results of the modified query will be read by the
LUA scripts and written to a log-file
Pro:
Cons:
Lock the database account and kill all already running processes
Pro:
Cons:
¡ Side effect on the application (Denial-of-Service)
Get the IP address from the web application server and start a
denial-of-service against the IP where the attack was coming from
Pro:
Cons:
or
Data is lost/published?
What happens after lockout?
In this case the string was not part of an attack. In such a case it could
be an option to exclude the string from the detection and re-enable
the service again.
Or
Developer has deployed wrong SQL code (e.g. Single Quote is missing)
SQL Injection Errors from Web Application
Scanner
How can we detect that a webapp scanner was causing the error?
SQL Injection Errors from Web Application
Scanner
Vulnerable URL:
php3.php?ename=test
ERROR:
SQL Statement:
Select * from emp where ename=‘’ and 1=0 union select 1,password from
dba_users where username='SYSTEM’--
php1.php?id=' OR 'ns'='ns
ERROR:
SQL Statement:
OR 17-7=10
+CHAR(95)+CHAR(33)+CHAR(64)
' OR 'ns'='ns
OR 1=1
/**/AND/**/1=/**/
CHAR(95)+CHAR(33)+CHAR(64)+SUBSTRING(CA
ST((SELECT/**/@@version)/**/AS/**/
varchar(3000)),
0,343)+CHAR(95)+CHAR(33)+CHAR(64)
Matrixay – Forensic Traces
Test strings (partial)
AnD 1=1
AnD AsC(1)<65535
AnD user<Chr(0)
AnD (SeLEcT CoUNt(TaBLe_NaME) FrOM
user_tables)>0
AnD AsCIi(DaTAbAsE())=0!
HP Webinspect – Forensic Traces
Test strings (partial)
¡ Using fake data could help to identify attacks which are not triggered by
error messages (e.g. if attacker uses a known exploit for standard
software (e.g. Wordpress, …))
¡ Fake data (Honey data) is data (e.g. Passwords, Credit card numbers,
…) in tables which is never used by the application. If someone from the
web application server is accessing this kind of data this is often part of
the data discovery process of the attacker.
Fake-Data (Honey-Data)
• Creation a table or tables containing unused data with juicy
names (e.g. PASSWORD, CREDITCARD, SALARY). Such interesting
data is often the target of attackers.
• During the attack, attackers are often accessing the view
ALL_TAB_COLUMNS (Oracle) or INFORMATION_SCHEMA.COLUMNS
(MSSQL) to get the column names of interesting data
• Attackers are normally downloading the data of interesting tables
found via the column name in further attacks.
• You could monitor such an access and could react (send email,
lock user, …)
• Oracle can implement this monitoring via Virtual Private Database
(VPD)
Fake-Data (Honey-Data)
-- Create Honeytable
return '1=1';
end;
/
¡ Can also be used during pentests to monitor if SQL Errors were
triggered by Pentesters
Red-Database-Security GmbH
Bliesstr. 16
D-.66538 Neunkirchen
Germany