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.

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

Stealing the Password Hashes<br />

We briefly talked about hashing functions earlier in this chapter, when we discussed<br />

a successful attack that recovered the passwords of the application users. In this section,<br />

we’ll talk about hashes again, this time regarding the database users. On all common DBMS<br />

technologies, user passwords are stored using a non-reversible hash (the exact algorithm used<br />

varies depending on the DBMS <strong>and</strong> version, as you will see shortly) <strong>and</strong> such hashes are<br />

stored, you guessed it, in a database table. To read the contents of that table you normally<br />

will need to run your queries with administrative privileges, so if your user does not have<br />

such privileges you should probably return to the privilege escalation section.<br />

If you manage to capture the password hashes, various tools can attempt to retrieve the<br />

original passwords that generated the hashes by means of a brute-force attack. This makes<br />

the database password hashes one of the most common targets in any attack: Because users<br />

often reuse the same password for different machines <strong>and</strong> services, being able to obtain the<br />

passwords of all users is usually enough to ensure a relatively easy <strong>and</strong> quick expansion in<br />

the target network.<br />

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

If you are dealing with a Microsoft <strong>SQL</strong> server, things vary quite a lot depending on the<br />

version you are dealing with. In all cases, you need administrative privileges to access the<br />

hashes, but differences start to surface when you actually try to retrieve them <strong>and</strong>, more<br />

importantly, when you try to crack them to obtain the original passwords.<br />

On <strong>SQL</strong> Server 2000, hashes are stored in the sysxlogins table of the master database. You<br />

can retrieve them easily with the following query:<br />

SELECT name,password FROM master.dbo.sysxlogins<br />

Such hashes are generated using pwdencrypt(), an undocumented function that generates a<br />

salted hash, where the salt is a function of the current time. For instance, here is the hash of<br />

the sa password on one of the <strong>SQL</strong> servers that I use in my tests:<br />

0x0100E21F79764287D299F09FD4B7EC97139C7474CA1893815231E9165D257ACEB815111F2A<br />

E98359F40F84F3CF4C<br />

This hash can be split into the following parts:<br />

■■<br />

■■<br />

■■<br />

■■<br />

0x0100 Header<br />

E21F7976 Salt<br />

4287D299F09FD4B7EC97139C7474CA1893815231 Case-sensitive hash<br />

E9165D257ACEB815111F2AE98359F40F84F3CF4C Case-insensitive hash

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

Saved successfully!

Ooh no, something went wrong!