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

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

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

Blind <strong>SQL</strong> <strong>Injection</strong> Exploitation • Chapter 5 229<br />

Let’s now shift our attention to the second character <strong>and</strong> repeat the process, starting at<br />

the letter a <strong>and</strong> moving through the alphabet. As each successive character is found, the<br />

search moves on to the next character. The page queries that reveal the username on our<br />

sample page are as follows:<br />

status=Incubating' AND SUBSTRING(SYSTEM_USER,1,1)='s (True)<br />

status=Incubating' AND SUBSTRING(SYSTEM_USER,2,1)='q (True)<br />

status=Incubating' AND SUBSTRING(SYSTEM_USER,3,1)='l (True)<br />

status=Incubating' AND SUBSTRING(SYSTEM_USER,4,1)='0 (True)<br />

status=Incubating' AND SUBSTRING(SYSTEM_USER,5,1)='5 (True)<br />

Simple, isn’t it? The username is sql05. Unfortunately, though, it’s actually not that simple,<br />

<strong>and</strong> we have skipped over a pretty important question: How do we know when the end of the<br />

username has been reached? If the portion of the username discovered so far is sql05, how can<br />

we be sure that there is not a sixth, seventh, or eighth character? The SUBSTRING( ) function<br />

will not generate an error if you ask it to provide characters past the end of the string; instead,<br />

it returns the empty string “. Therefore, we can include the empty string in our search alphabet,<br />

<strong>and</strong> if it is found we can conclude that the end of the username has been found.<br />

status=Incubating' AND SUBSTRING(SYSTEM_USER,6,1)=' (True)<br />

Hooray! Except that this is not very portable <strong>and</strong> depends on the explicit behavior of a<br />

particular database function. A neater solution would be to determine the length of the<br />

username before extracting it. The advantage of this approach, apart from being applicable<br />

to a wider range of scenarios than the “SUBSTRING( ) returns empty string” approach, is<br />

that it enables the attacker to estimate the maximum time that could possibly be spent<br />

extracting the username. We can find the length of the username with the same technique<br />

we employed to find each character, by testing whether the value is 1, 2, 3, <strong>and</strong> so on until<br />

we find a match:<br />

status=Incubating' AND LEN(SYSTEM_USER)=1-- (False)<br />

status=Incubating' AND LEN(SYSTEM_USER)=2-- (False)<br />

status=Incubating' AND LEN(SYSTEM_USER)=3-- (False)<br />

status=Incubating' AND LEN(SYSTEM_USER)=4-- (False)<br />

status=Incubating' AND LEN(SYSTEM_USER)=5-- (True)<br />

From this sequence of requests it was possible to infer that the length of the username<br />

was 5. Note as well the use of the <strong>SQL</strong> comment (--) that, although not required, makes the<br />

exploit a little simpler.<br />

It is worth reinforcing the point that the inference tool used to determine whether a given<br />

question was TRUE or FALSE was the presence of either an egg count or a message stating<br />

that no eggs matched the given status. The mechanism by which you make an inference<br />

decision is highly dependent on the scenario that faces you <strong>and</strong> can often be substituted with<br />

a number of differing techniques.

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

Saved successfully!

Ooh no, something went wrong!