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.

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

JavaScript contained within or loaded by a page returned by that domain only.<br />

Hence, if a script residing on mdattacker.net queries document.cookie, it will<br />

not obtain <strong>the</strong> cookies issued by mdsec.net, and <strong>the</strong> hijacking attack will fail.<br />

The reason why <strong>the</strong> attack that exploits <strong>the</strong> XSS vulnerability is successful is<br />

that, as far as <strong>the</strong> user’s browser is concerned, <strong>the</strong> attacker’s malicious JavaScript<br />

was sent to it by mdsec.net. When <strong>the</strong> user requests <strong>the</strong> attacker’s URL, <strong>the</strong><br />

browser makes a request to http://mdsec.net/error/5/Error.ashx , and <strong>the</strong><br />

<strong>application</strong> returns a page containing some JavaScript. As with any JavaScript<br />

received from mdsec.net, <strong>the</strong> browser executes this script within <strong>the</strong> security<br />

context of <strong>the</strong> user’s relationship with mdsec.net. This is why <strong>the</strong> attacker’s<br />

script, although it actually originates elsewhere, can gain access to <strong>the</strong> cookies<br />

issued by mdsec.net. This is also why <strong>the</strong> vulnerability itself has become<br />

known as cross-site scripting.<br />

Stored XSS Vulnerabilities<br />

A different category of XSS vulnerability is often called stored cross-site scripting.<br />

This version arises when data submitted by one user is stored in <strong>the</strong> <strong>application</strong><br />

(typically in a back-end database) and <strong>the</strong>n is displayed to o<strong>the</strong>r users without<br />

being filtered or sanitized appropriately.<br />

Stored XSS vulnerabilities are common in <strong>application</strong>s that support interaction<br />

between end users, or where administrative staff access user records and<br />

data within <strong>the</strong> same <strong>application</strong>. For example, consider an auction <strong>application</strong><br />

that allows buyers to post questions about specific items and sellers to post<br />

responses. If a user can post a question containing embedded JavaScript, and<br />

<strong>the</strong> <strong>application</strong> does not filter or sanitize this, an attacker can post a crafted<br />

question that causes arbitrary scripts to execute within <strong>the</strong> browser of anyone<br />

who views <strong>the</strong> question, including both <strong>the</strong> seller and o<strong>the</strong>r potential buyers.<br />

In this context, <strong>the</strong> attacker could potentially cause unwitting users to bid on<br />

an item without intending to, or cause a seller to close an auction and accept<br />

<strong>the</strong> attacker’s low bid for an item.<br />

Attacks against stored XSS vulnerabilities typically involve at least two requests<br />

to <strong>the</strong> <strong>application</strong>. In <strong>the</strong> first, <strong>the</strong> attacker posts some crafted data containing<br />

malicious code that <strong>the</strong> <strong>application</strong> stores. In <strong>the</strong> second, a victim views a page<br />

containing <strong>the</strong> attacker’s data, and <strong>the</strong> malicious code is executed when <strong>the</strong><br />

script is executed in <strong>the</strong> victim’s browser. For this reason, <strong>the</strong> vulnerability is<br />

also sometimes called second-order cross-site scripting. (In this instance, “XSS”<br />

is really a misnomer, because <strong>the</strong> attack has no cross-site element. The name is<br />

widely used, however, so we will retain it here.)<br />

Figure 12-4 illustrates how an attacker can exploit a stored XSS vulnerability<br />

to perform <strong>the</strong> same session hijacking attack as was described for reflected XSS.

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

Saved successfully!

Ooh no, something went wrong!