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.

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

The only significant caveat to using this technique is that <strong>the</strong> resulting request<br />

will contain <strong>the</strong> following header:<br />

Content-Type: text/plain<br />

The original request normally would have contained a different Content-Type<br />

header, depending on exactly how it was generated. If <strong>the</strong> <strong>application</strong> tolerates<br />

<strong>the</strong> supplied Content-Type header and processes <strong>the</strong> message body in <strong>the</strong> normal<br />

way, <strong>the</strong> technique can be used successfully when trying to develop a working<br />

XSS exploit. If <strong>the</strong> <strong>application</strong> fails to process <strong>the</strong> request in <strong>the</strong> normal way, on<br />

account of <strong>the</strong> modified Content-Type header, <strong>the</strong>re may be no way to send a<br />

suitable cross-domain request to trigger <strong>the</strong> XSS-like behavior.<br />

TIP If you identify XSS-like behavior in a request that contains nonstandard<br />

content, <strong>the</strong> first thing you should do is quickly verify whe<strong>the</strong>r <strong>the</strong> behavior<br />

remains when you change <strong>the</strong> Content-Type header to text/plain. If it<br />

does not, it may not be worth investing any fur<strong>the</strong>r effort in trying to develop<br />

a working XSS exploit.<br />

Executing JavaScript from Within XML Responses<br />

The second challenge to overcome when attempting to exploit XSS-like behavior<br />

in nonstandard content is to find a way of manipulating <strong>the</strong> response so<br />

that it executes your script when consumed directly by <strong>the</strong> browser. If <strong>the</strong><br />

response contains an inaccurate Content-Type header, or none at all, or if your<br />

input is being reflected right at <strong>the</strong> start of <strong>the</strong> response body, this task may be<br />

straightforward.<br />

Usually, however, <strong>the</strong> response includes a Content-Type header that accurately<br />

describes <strong>the</strong> type of data that <strong>the</strong> <strong>application</strong> returns. Fur<strong>the</strong>rmore, your<br />

input typically is reflected partway through <strong>the</strong> response, and <strong>the</strong> bulk of <strong>the</strong><br />

response before and after this point will contain data that complies with <strong>the</strong><br />

relevant specifications for <strong>the</strong> stated content type. Different browsers take different<br />

approaches to parsing content. Some simply trust <strong>the</strong> Content-Type header,<br />

and o<strong>the</strong>rs inspect <strong>the</strong> content itself and are willing to override <strong>the</strong> stated type<br />

if <strong>the</strong> actual type appears different. In this situation, however, ei<strong>the</strong>r approach<br />

makes it highly unlikely that <strong>the</strong> browser will process <strong>the</strong> response as HTML.<br />

If it is possible to construct a response that does succeed in executing a script, this<br />

normally involves exploiting some particular syntactic feature of <strong>the</strong> type of content<br />

that is being injected into. Fortunately, in <strong>the</strong> case of XML, this can be achieved by<br />

using XML markup to define a new namespace that is mapped to XHTML, causing<br />

<strong>the</strong> browser to parse uses of that namespace as HTML. For example, when Firefox<br />

processes <strong>the</strong> following response, <strong>the</strong> injected script is executed:<br />

HTTP/1.1 200 Ok<br />

Content-Type: text/xml

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

Saved successfully!

Ooh no, something went wrong!