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.

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

Testing Reflections to Introduce Script<br />

You must manually investigate each instance of reflected input that you have<br />

identified to verify whe<strong>the</strong>r it is actually exploitable. In each location where<br />

data is reflected in <strong>the</strong> response, you need to identify <strong>the</strong> syntactic context of<br />

that data. You must find a way to modify your input such that, when it is copied<br />

into <strong>the</strong> same location in <strong>the</strong> <strong>application</strong>’s response, it results in execution of<br />

arbitrary script. Let’s look at some examples.<br />

Example 1: A Tag Attribute Value<br />

Suppose that <strong>the</strong> returned page contains <strong>the</strong> following:<br />

<br />

One obvious way to craft an XSS exploit is to terminate <strong>the</strong> double quotation<br />

marks that enclose <strong>the</strong> attribute value, close <strong>the</strong> tag, and <strong>the</strong>n<br />

employ some means of introducing JavaScript, such as a tag. For<br />

example:<br />

“>alert(1)<br />

An alternative method in this situation, which may bypass certain input filters,<br />

is to remain within <strong>the</strong> tag itself but inject an event handler containing<br />

JavaScript. For example:<br />

“ onfocus=”alert(1)<br />

Example 2: A JavaScript String<br />

Suppose that <strong>the</strong> returned page contains <strong>the</strong> following:<br />

var a = ‘myxsstestdmqlwp’; var b = 123; ... <br />

Here, <strong>the</strong> input you control is being inserted directly into a quoted string<br />

within an existing script. To craft an exploit, you could terminate <strong>the</strong> single<br />

quotation marks around your string, terminate <strong>the</strong> statement with a semicolon,<br />

and <strong>the</strong>n proceed directly to your desired JavaScript:<br />

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

Note that because you have terminated a quoted string, to prevent errors<br />

from occurring within <strong>the</strong> JavaScript interpreter you must ensure that <strong>the</strong><br />

script continues gracefully with valid syntax after your injected code. In this<br />

example, <strong>the</strong> variable foo is declared, and a second quoted string is opened. It<br />

will be terminated by <strong>the</strong> code that immediately follows your string. Ano<strong>the</strong>r<br />

method that is often effective is to end your input with // to comment out <strong>the</strong><br />

remainder of <strong>the</strong> line.

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

Saved successfully!

Ooh no, something went wrong!