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.

472 Chapter 12 n Attacking Users: Cross-Site Scripting<br />

Suppose that each field has length restrictions, such that no feasible attack<br />

string can be inserted into any of <strong>the</strong>m. Never<strong>the</strong>less, you can still deliver a<br />

working exploit by using <strong>the</strong> following URL to span a script across <strong>the</strong> three<br />

locations you control:<br />

https://myapp.com/account.php?page_id=”>/*&seed=*/alert(document<br />

.cookie);/*&mode=*/<br />

When <strong>the</strong> parameter values from this URL are embedded into <strong>the</strong> page, <strong>the</strong><br />

result is <strong>the</strong> following:<br />

/*”><br />

<br />

<br />

The resulting HTML is valid and is equivalent to only <strong>the</strong> portions in bold.<br />

The chunks of source code in between have effectively become JavaScript comments<br />

(surrounded by <strong>the</strong> /* and */ markers), so <strong>the</strong> browser ignores <strong>the</strong>m.<br />

Hence, your script is executed just as if it had been inserted whole at one location<br />

within <strong>the</strong> page.<br />

TIP The technique of spanning an attack payload across multiple fields can<br />

sometimes be used to beat o<strong>the</strong>r types of defensive filters. It is fairly common<br />

to find different data validation and sanitization being implemented on different<br />

fields within a single page of an <strong>application</strong>. In <strong>the</strong> previous example, suppose<br />

that <strong>the</strong> page_id and mode parameters are subject to a maximum length<br />

of 12 characters. Because <strong>the</strong>se fields are so short, <strong>the</strong> <strong>application</strong>’s developers<br />

did not bo<strong>the</strong>r to implement any XSS filters. The seed parameter, on <strong>the</strong> o<strong>the</strong>r<br />

hand, is unrestricted in length, so rigorous filters were implemented to prevent<br />

<strong>the</strong> injection of <strong>the</strong> characters “ < or >. In this scenario, despite <strong>the</strong> developers’<br />

efforts, it is still possible to insert an arbitrarily long script into <strong>the</strong> seed parameter<br />

without employing any of <strong>the</strong> blocked characters, because <strong>the</strong> JavaScript<br />

context can be created by data injected into <strong>the</strong> surrounding fields.<br />

A third technique for beating length limits, which can be highly effective<br />

in some situations, is to “convert” a reflected XSS flaw into a DOM-based<br />

vulnerability. For example, in <strong>the</strong> original reflected XSS vulnerability, if <strong>the</strong><br />

<strong>application</strong> places a length restriction on <strong>the</strong> message parameter that is copied<br />

into <strong>the</strong> returned page, you can inject <strong>the</strong> following 45-byte script, which<br />

evaluates <strong>the</strong> fragment string in <strong>the</strong> current URL:<br />

eval(location.hash.slice(1))<br />

By injecting this script into <strong>the</strong> parameter that is vulnerable to reflected XSS,<br />

you can effectively induce a DOM-based XSS vulnerability in <strong>the</strong> resulting page

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

Saved successfully!

Ooh no, something went wrong!