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.

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

Are You Owned?<br />

Using Hash Functions to<br />

Store Passwords in Your Database<br />

The scenario that was just illustrated, in which a few queries have been enough to<br />

retrieve a list of usernames <strong>and</strong> passwords unencrypted (in clear text), is not as unusual<br />

as you might think. During our penetration tests <strong>and</strong> security assessments, we (the<br />

book’s authors) have come across plenty of cases in which vulnerable applications had<br />

passwords <strong>and</strong> other sensitive data stored in clear text.<br />

The danger of storing users’ passwords in clear text poses other dangers:<br />

Because human beings have the tendency to reuse the same password for several different<br />

online services, a successful attack such as the one described might pose a threat not only<br />

to the users’ accounts on victim.com, but also to other areas of their online identity, such<br />

as online banking <strong>and</strong> private e-mail. And victim.com might even be liable for these<br />

additional break-ins, depending on the specific laws of the country where it resides!<br />

Therefore, if you are responsible for a Web application or a database that h<strong>and</strong>les<br />

the credentials of your users, make sure that such credentials are always stored<br />

using a cryptographic hash function. A cryptographic hash function transforms an<br />

arbitrary value (in our case, the user’s password) into a fixed-length string (called the<br />

hash value). This function has several mathematical properties, but here we are mostly<br />

interested in two of them:<br />

■■<br />

■■<br />

Given a hash value, it is extremely difficult to construct a value that<br />

generates it.<br />

The probability that two different values generate the same hash value is<br />

extremely low.<br />

Storing the hash value of the password instead of the password itself still allows<br />

users to authenticate, because it’s enough to calculate the hash value of the password<br />

they provide <strong>and</strong> compare it with the stored hash value. However, it provides a security<br />

advantage, because if the list of hash values is captured, the attacker would not be able<br />

to convert them back to the original passwords without going through a brute-force<br />

attack.<br />

When choosing the right hashing algorithm, do not rely on MD5, as several weaknesses<br />

have been found over the years. SHA1 provides a far higher level of security<br />

against attacks, <strong>and</strong> its more recent variants (such as SHA256 <strong>and</strong> SHA512) even more<br />

so, thanks to a longer hash value length. Using such an algorithm will not protect you<br />

against <strong>SQL</strong> injection attacks (fear not—we wrote Chapter 8 <strong>and</strong> Chapter 9 for that),<br />

but will greatly protect your customers in case the data falls into the wrong h<strong>and</strong>s.

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

Saved successfully!

Ooh no, something went wrong!