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.

514 Chapter 13 n Attacking Users: O<strong>the</strong>r Techniques<br />

Framebusting Defenses<br />

When UI redress attacks were first widely discussed, many high-profile <strong>web</strong><br />

<strong>application</strong>s sought to defend against <strong>the</strong>m using a defensive technique known<br />

as framebusting. In some cases this was already being used to defend against<br />

o<strong>the</strong>r frame-based attacks.<br />

Framebusting can take various forms, but it essentially involves each relevant<br />

page of an <strong>application</strong> running a script to detect if it is being loaded within an<br />

iframe. If so, an attempt is made to “bust” out of <strong>the</strong> iframe, or some o<strong>the</strong>r<br />

defensive action is performed, such as redirecting to an error page or refusing<br />

to display <strong>the</strong> <strong>application</strong>’s own interface.<br />

A Stanford University study in 2010 examined <strong>the</strong> framebusting defenses used<br />

by 500 top <strong>web</strong>sites. It found that in every instance <strong>the</strong>se could be circumvented in<br />

one way or ano<strong>the</strong>r. How this can be done depends on <strong>the</strong> specific details of each<br />

defense, but can be illustrated using a common example of framebusting code:<br />

<br />

if (top.location != self.location)<br />

{ top.location = self.location }<br />

<br />

This code checks whe<strong>the</strong>r <strong>the</strong> URL of <strong>the</strong> page itself matches <strong>the</strong> URL of <strong>the</strong><br />

top frame in <strong>the</strong> browser window. If it doesn’t, <strong>the</strong> page has been loaded within<br />

a child frame. In that case <strong>the</strong> script tries to break out of <strong>the</strong> frame by reloading<br />

itself into <strong>the</strong> top-level frame in <strong>the</strong> window.<br />

An attacker performing a UI redress attack can circumvent this defense to<br />

successfully frame <strong>the</strong> target page in several ways:<br />

n Since <strong>the</strong> attacker’s page controls <strong>the</strong> top-level frame, it can redefine <strong>the</strong><br />

meaning of top.location so that an exception occurs when a child frame<br />

tries to reference it. For example, in Internet Explorer, <strong>the</strong> attacker can<br />

run <strong>the</strong> following code:<br />

var location = ‘foo’;<br />

This redefines location as a local variable in <strong>the</strong> top-level frame so that<br />

code running in a child frame cannot access it.<br />

n The top-level frame can hook <strong>the</strong> window.onbeforeunload event so that<br />

<strong>the</strong> attacker’s event handler is run when <strong>the</strong> framebusting code tries to<br />

set <strong>the</strong> location of <strong>the</strong> top-level frame. The attacker’s code can perform a<br />

fur<strong>the</strong>r redirect to a URL that returns an HTTP 204 (No Content) response.<br />

This causes <strong>the</strong> browser to cancel <strong>the</strong> chain of redirection calls and leaves<br />

<strong>the</strong> URL of <strong>the</strong> top-level frame unchanged.<br />

n The top-level frame can define <strong>the</strong> sandbox attribute when loading <strong>the</strong><br />

target <strong>application</strong> into a child frame. This disables scripting in <strong>the</strong> child<br />

frame while leaving its cookies enabled.

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

Saved successfully!

Ooh no, something went wrong!