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.

References • Chapter 10 441<br />

When quote characters are being filtered or sanitized you will need to encode string<br />

values to prevent them from being corrupted by the filter. Table 10.18 lists the alternative<br />

methods for representing the query SELECT ‘ABC’ within each of the most popular<br />

database platforms.<br />

Table 10.18 Representing Strings without Quote Characters<br />

Platform<br />

Microsoft <strong>SQL</strong> Server<br />

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

Oracle<br />

Query<br />

SELECT char(0x41) + char(0x42) + char(0x43);<br />

SELECT char(65,66,67);<br />

SELECT 0x414243;<br />

SELECT chr(65) || chr(66) || chr(67) from dual;<br />

Select concat(chr(65),concat(chr(66),chr(67))) from dual;<br />

Select upper((select substr(banner,3,1)||substr(banner,<br />

12,1)||substr(banner,4,1) from v$version where<br />

rownum=1)) from dual;<br />

Microsoft <strong>SQL</strong> Server also allows you to build your query within a variable <strong>and</strong> then<br />

call EXEC to execute it. In the following example, we have created a variable named @q<br />

<strong>and</strong> placed the query SELECT ‘ABC’ into it via a HEX-encoded string:<br />

DECLARE @q varchar(8000)<br />

SELECT @q=0x53454c454354202741424327<br />

EXEC(@q)<br />

You can adopt this technique to execute any query without submitting any quote<br />

characters to the application. You can use the following Perl script to automatically encode<br />

<strong>SQL</strong> statements using this technique:<br />

#!/usr/bin/perl<br />

print "Enter <strong>SQL</strong> query to encode:";<br />

$teststr=;chomp $teststr;<br />

$hardcoded_sql =<br />

'declare @q varchar(8000) '.<br />

'select @q=0x*** '.<br />

'exec(@q)';<br />

$prepared = encode_sql($teststr);<br />

$hardcoded_sql =∼s/\*\*\*/$prepared/g;<br />

print "\n[*]-Encoded <strong>SQL</strong>:\n\n";<br />

print $hardcoded_sql ."\n";

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

Saved successfully!

Ooh no, something went wrong!