19.09.2017 Views

the-web-application-hackers-handbook

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 9 n Attacking Data Stores 291<br />

Injecting into SQL<br />

Almost every <strong>web</strong> <strong>application</strong> employs a database to store <strong>the</strong> various kinds of<br />

information it needs to operate. For example, a <strong>web</strong> <strong>application</strong> deployed by an<br />

online retailer might use a database to store <strong>the</strong> following information:<br />

n User accounts, credentials, and personal information<br />

n Descriptions and prices of goods for sale<br />

n Orders, account statements, and payment details<br />

n The privileges of each user within <strong>the</strong> <strong>application</strong><br />

The means of accessing information within <strong>the</strong> database is Structured Query<br />

Language (SQL). SQL can be used to read, update, add, and delete information<br />

held within <strong>the</strong> database.<br />

SQL is an interpreted language, and <strong>web</strong> <strong>application</strong>s commonly construct<br />

SQL statements that incorporate user-supplied data. If this is done in an unsafe<br />

way, <strong>the</strong> <strong>application</strong> may be vulnerable to SQL injection. This flaw is one of <strong>the</strong><br />

most notorious vulnerabilities to have afflicted <strong>web</strong> <strong>application</strong>s. In <strong>the</strong> most<br />

serious cases, SQL injection can enable an anonymous attacker to read and<br />

modify all data stored within <strong>the</strong> database, and even take full control of <strong>the</strong><br />

server on which <strong>the</strong> database is running.<br />

As awareness of <strong>web</strong> <strong>application</strong> security has evolved, SQL injection vulnerabilities<br />

have become gradually less widespread and more difficult to detect<br />

and exploit. Many modern <strong>application</strong>s avoid SQL injection by employing APIs<br />

that, if properly used, are inherently safe against SQL injection attacks. In <strong>the</strong>se<br />

circumstances, SQL injection typically occurs in <strong>the</strong> occasional cases where <strong>the</strong>se<br />

defense mechanisms cannot be applied. Finding SQL injection is sometimes a<br />

difficult task, requiring perseverance to locate <strong>the</strong> one or two instances in an<br />

<strong>application</strong> where <strong>the</strong> usual controls have not been applied.<br />

As this trend has developed, methods for finding and exploiting SQL injection<br />

flaws have evolved, using more subtle indicators of vulnerabilities, and more<br />

refined and powerful exploitation techniques. We will begin by examining<br />

<strong>the</strong> most basic cases and <strong>the</strong>n go on to describe <strong>the</strong> latest techniques for blind<br />

detection and exploitation.<br />

A wide range of databases are employed to support <strong>web</strong> <strong>application</strong>s. Although<br />

<strong>the</strong> fundamentals of SQL injection are common to <strong>the</strong> vast majority of <strong>the</strong>se, <strong>the</strong>re<br />

are many differences. These range from minor variations in syntax to significant<br />

divergences in behavior and functionality that can affect <strong>the</strong> types of attacks you<br />

can pursue. For reasons of space and sanity, we will restrict our examples to <strong>the</strong><br />

three most common databases you are likely to encounter — Oracle, MS-SQL,<br />

and MySQL. Wherever applicable, we will draw attention to <strong>the</strong> differences<br />

between <strong>the</strong>se three platforms. Equipped with <strong>the</strong> techniques we describe here,

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

Saved successfully!

Ooh no, something went wrong!