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.

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

remainder of <strong>the</strong> line, thus preventing a syntax error caused by <strong>the</strong> <strong>application</strong>’s<br />

own string delimiter:<br />

var a = ‘foo\\’; alert(1);//’;<br />

Here, if you find that <strong>the</strong> backslash character is also being properly escaped,<br />

but angle brackets are returned unsanitized, you can use <strong>the</strong> following attack:<br />

alert(1)<br />

This effectively abandons <strong>the</strong> <strong>application</strong>’s original script and injects a new<br />

one immediately after it. The attack works because browsers’ parsing of HTML<br />

tags takes precedence over <strong>the</strong>ir parsing of embedded JavaScript:<br />

var a = ‘alert(1)<br />

Although <strong>the</strong> original script now contains a syntax error, this does not matter,<br />

because <strong>the</strong> browser moves on and executes your injected script regardless of<br />

<strong>the</strong> error in <strong>the</strong> original script.<br />

TRY IT!<br />

http://mdsec.net/search/48/<br />

http://mdsec.net/search/52/<br />

TIP If you can inject into a script, but you cannot use quotation marks because<br />

<strong>the</strong>se are being escaped, you can use <strong>the</strong> String.fromCharCode technique to<br />

construct strings without <strong>the</strong> need for delimiters, as described previously.<br />

In cases where <strong>the</strong> script you are injecting into resides within an event handler,<br />

ra<strong>the</strong>r than a full script block, you may be able to HTML-encode your quotation<br />

marks to bypass <strong>the</strong> <strong>application</strong>’s sanitization and break out of <strong>the</strong> string you<br />

control. For example, if you control <strong>the</strong> value foo in:<br />

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

Saved successfully!

Ooh no, something went wrong!