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.

Exploiting <strong>SQL</strong> <strong>Injection</strong> • Chapter 4 205<br />

Figure 4.16 Using the Server’s<br />

File System to Obtain the Password Hash of User sa<br />

If you need to repeat the process several times, you can make things easier by encapsulating<br />

the code in a stored procedure that can be called at will.<br />

This technique works quite well for extracting small amounts of information, but what<br />

about extracting whole tables? The best option in that case is to rely on bcp.exe, a comm<strong>and</strong>line<br />

utility shipped by default with <strong>SQL</strong> Server. As stated on MSDN, “The bcp utility bulk<br />

copies data between an instance of Microsoft <strong>SQL</strong> Server <strong>and</strong> a data file in a user-specified<br />

format” (see http://msdn.microsoft.com/en-us/library/ms162802.aspx). Bcp.exe is a powerful<br />

utility which accepts a large number of parameters. In our case, however, we are interested in<br />

only a few of them, so here’s an example that retrieves the entire sql_logins table:<br />

EXEC xp_cmdshell 'bcp "select * from sys.sql_logins" queryout<br />

c:\inetpub\wwwroot\hashes.txt -T -c'

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

Saved successfully!

Ooh no, something went wrong!