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 11 n Attacking Application Logic 421<br />

The Attack<br />

The SQL injection defense works by doubling any quotation marks appearing<br />

within user input, so that within each pair of quotes, <strong>the</strong> first quote acts as an<br />

escape character to <strong>the</strong> second. However, <strong>the</strong> developers did not consider what<br />

would happen to <strong>the</strong> sanitized input if it was <strong>the</strong>n handed to <strong>the</strong> truncation<br />

function.<br />

Recall <strong>the</strong> SQL injection example in a login function in Chapter 9. Suppose<br />

that <strong>the</strong> <strong>application</strong> doubles any single quotation marks contained in user input<br />

and also <strong>the</strong>n imposes a length limit on <strong>the</strong> data, truncating it to 128 characters.<br />

Supplying this username:<br />

admin’--<br />

now results in <strong>the</strong> following query, which fails to bypass <strong>the</strong> login:<br />

SELECT * FROM users WHERE username = ‘admin’’--’ and password = ‘’<br />

However, if you submit a following username (containing 127 a’s followed<br />

by a single quotation mark):<br />

aaaaaaaa[...]aaaaaaaaaaa’<br />

<strong>the</strong> <strong>application</strong> first doubles up <strong>the</strong> single quotation mark and <strong>the</strong>n truncates <strong>the</strong><br />

string to 128 characters, returning your input to its original value. This results<br />

in a database error, because you have injected an additional single quotation<br />

mark into <strong>the</strong> query without fixing <strong>the</strong> surrounding syntax. If you now also<br />

supply <strong>the</strong> password:<br />

or 1=1--<br />

<strong>the</strong> <strong>application</strong> performs <strong>the</strong> following query, which succeeds in bypassing <strong>the</strong><br />

login:<br />

SELECT * FROM users WHERE username = ‘aaaaaaaa[...]aaaaaaaaaaa’’ and<br />

password = ‘or 1=1--’<br />

The doubled quotation mark at <strong>the</strong> end of <strong>the</strong> string of a’s is interpreted<br />

as an escaped quotation mark and, <strong>the</strong>refore, as part of <strong>the</strong> query data. This<br />

string effectively continues as far as <strong>the</strong> next single quotation mark, which<br />

in <strong>the</strong> original query marked <strong>the</strong> start of <strong>the</strong> user-supplied password value.<br />

Thus, <strong>the</strong> actual username that <strong>the</strong> database understands is <strong>the</strong> literal string<br />

data shown here:<br />

aaaaaaaa[...]aaaaaaaaaaa’and password =<br />

Hence, whatever comes next is interpreted as part of <strong>the</strong> query itself and can<br />

be crafted to interfere with <strong>the</strong> query logic.

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

Saved successfully!

Ooh no, something went wrong!