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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Time Delays<br />

Testing for <strong>SQL</strong> <strong>Injection</strong> • Chapter 2 79<br />

When testing applications for <strong>SQL</strong> injection vulnerabilities you will often find yourself with<br />

a potential vulnerability that is difficult to confirm. This can be due to a number of reasons,<br />

but mainly because the Web application is not showing any errors <strong>and</strong> because you cannot<br />

retrieve any data.<br />

In this kind of situation, it is useful to inject database time delays <strong>and</strong> check whether the<br />

response from the server has also been delayed. Time delays are a very powerful technique as<br />

the Web server can hide errors or data, but cannot avoid waiting for the database to return a<br />

result, <strong>and</strong> therefore you can confirm the existence of <strong>SQL</strong> injection. This technique is<br />

especially useful in blind injection scenarios.<br />

Microsoft <strong>SQL</strong> servers have a built-in comm<strong>and</strong> to introduce delays to queries:<br />

WAITFOR DELAY ‘hours:minutes:seconds’. For example, the following request to the Victim<br />

Inc. Web server takes around five seconds:<br />

http://www.victim.com/basket.aspx?uid=45; waitfor delay '0:0:5';--<br />

The delay in the response from the server assures us that we are injecting <strong>SQL</strong> code into<br />

the back-end database.<br />

My<strong>SQL</strong> databases don’t have an equivalent to the WAITFOR DELAY comm<strong>and</strong>.<br />

However, it is possible to introduce a delay using functions which take a long time to operate.<br />

The BENCHMARK function is a good option. The My<strong>SQL</strong> BENCHMARK function<br />

executes an expression a number of times. It is used to evaluate the speed of My<strong>SQL</strong><br />

executing expressions. The amount of time required by the database varies depending on<br />

the workload of the server <strong>and</strong> the computing resources; however, provided the delay is<br />

noticeable, this technique can be used for identification of vulnerabilities. Let’s have a look<br />

at the following example:<br />

mysql> SELECT BENCHMARK(10000000,ENCODE('hello','mom'));<br />

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

| BENCHMARK(10000000,ENCODE('hello','mom')) |<br />

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

| 0 |<br />

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

1 row in set (3.65 sec)<br />

It took 3.65 seconds to execute the query, <strong>and</strong> therefore if we inject this code into an<br />

<strong>SQL</strong> injection vulnerability it will delay the response from the server. If we want to delay<br />

the response further, we just need to increment the number of iterations. Here is an<br />

example:<br />

http://www.victim.com/display.php?id=32; SELECT<br />

BENCHMARK(10000000,ENCODE('hello','mom'));--

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

Saved successfully!

Ooh no, something went wrong!