9.4 KiB
The clickjacking attack
The "clickjacking" attack allows an evil page to click on a "victim site" on behalf of the visitor.
Many sites were hacked this way, including Twitter, Facebook, Paypal and other sites. They have all been fixed, of course.
The idea
The idea is very simple.
Here's how clickjacking was done with Facebook:
- A visitor is lured to the evil page. It doesn't matter how.
- The page has a harmless-looking link on it (like "get rich now" or "click here, very funny").
- Over that link the evil page positions a transparent
<iframe>
withsrc
from facebook.com, in such a way that the "Like" button is right above that link. Usually that's done withz-index
. - In attempting to click the link, the visitor in fact clicks the button.
The demo
Here's how the evil page looks. To make things clear, the <iframe>
is half-transparent (in real evil pages it's fully transparent):
<style>
iframe { /* iframe from the victim site */
width: 400px;
height: 100px;
position: absolute;
top:0; left:-20px;
*!*
opacity: 0.5; /* in real opacity:0 */
*/!*
z-index: 1;
}
</style>
<div>Click to get rich now:</div>
<!-- The url from the victim site -->
*!*
<iframe src="/clickjacking/facebook.html"></iframe>
<button>Click here!</button>
*/!*
<div>...And you're cool (I'm a cool hacker actually)!</div>
The full demo of the attack:
[codetabs src="clickjacking-visible" height=160]
Here we have a half-transparent <iframe src="facebook.html">
, and in the example we can see it hovering over the button. A click on the button actually clicks on the iframe, but that's not visible to the user, because the iframe is transparent.
As a result, if the visitor is authorized on Facebook ("remember me" is usually turned on), then it adds a "Like". On Twitter that would be a "Follow" button.
Here's the same example, but closer to reality, with opacity:0
for <iframe>
:
[codetabs src="clickjacking" height=160]
All we need to attack -- is to position the <iframe>
on the evil page in such a way that the button is right over the link. That's usually possible with CSS.
The attack only affects mouse actions.
Technically, if we have a text field to hack, then we can position an iframe in such a way that text fields overlap each other. So when a visitor tries to focus on the input they see on the page, they actually focus on the input inside the iframe.
But then there's a problem. Everything that the visitor types will be hidden, because the iframe is not visible.
People will usually stop typing when they can't see their new characters printing on the screen.
Old-school defences (weak)
The oldest defence is a bit of JavaScript which forbids opening the page in a frame (so-called "framebusting").
That looks like this:
if (top != window) {
top.location = window.location;
}
That is: if the window finds out that it's not on top, then it automatically makes itself the top.
This not a reliable defence, because there are many ways to hack around it. Let's cover a few.
Blocking top-navigation
We can block the transition caused by changing top.location
in the beforeunload event.
The top page (belonging to the hacker) sets a handler to it, and when the iframe
tries to change top.location
the visitor gets a message asking them whether they want to leave.
Like this:
window.onbeforeunload = function() {
window.onbeforeunload = null;
return "Want to leave without learning all the secrets (he-he)?";
};
In most cases the visitor would answer negatively because they don't know about the iframe - all they can see is the top page, leading them to think there is no reason to leave. So top.location
won't change!
In action:
[codetabs src="top-location"]
Sandbox attribute
One of the things restricted by the sandbox
attribute is navigation. A sandboxed iframe may not change top.location
.
So we can add the iframe with sandbox="allow-scripts allow-forms"
. That would relax the restrictions, permitting scripts and forms. But we omit allow-top-navigation
so that changing top.location
is forbidden.
Here's the code:
<iframe *!*sandbox="allow-scripts allow-forms"*/!* src="facebook.html"></iframe>
There are other ways to work around that simple protection too.
X-Frame-Options
The server-side header X-Frame-Options
can permit or forbid displaying the page inside a frame.
It must be sent by the server: the browser will ignore it if found in a <meta>
tag. So, <meta http-equiv="X-Frame-Options"...>
won't do anything.
The header may have 3 values:
DENY
- Never ever show the page inside a frame.
SAMEORIGIN
- Allow inside a frame if the parent document comes from the same origin.
ALLOW-FROM domain
- Allow inside a frame if the parent document is from the given domain.
For instance, Twitter uses X-Frame-Options: SAMEORIGIN
.
Here's the result:
```html
<iframe src="https://twitter.com"></iframe>
```
<!-- ebook: prerender/ chrome headless dies and timeouts on this iframe -->
<iframe src="https://twitter.com"></iframe>
Depending on your browser, the `iframe` above is either empty or alerting you that the browser won't permit that page to be navigating in this way.
Showing with disabled functionality
The X-Frame-Options
header has a side-effect. Other sites won't be able to show our page in a frame, even if they have good reasons to do so.
So there are other solutions... For instance, we can "cover" the page with a <div>
with height: 100%; width: 100%;
, so that it intercepts all clicks. That <div>
should disappear if window == top
or if we figure out that we don't need the protection.
Something like this:
<style>
#protector {
height: 100%;
width: 100%;
position: absolute;
left: 0;
top: 0;
z-index: 99999999;
}
</style>
<div id="protector">
<a href="/" target="_blank">Go to the site</a>
</div>
<script>
// there will be an error if top window is from the different origin
// but that's ok here
if (top.document.domain == document.domain) {
protector.remove();
}
</script>
The demo:
[codetabs src="protector"]
Samesite cookie attribute
The samesite
cookie attribute can also prevent clickjacking attacks. The purpose of the attribute is to prevent cookies from being sent to a website when the user doesn't intend to visit the website. It is designed to prevent cross-site request forgery attacks, but also helps with clickjacking because a hijacked click usually results in an unintended request to a different site. When a cookie has the samesite
attribute, whether the value is strict
or lax
, cookies are not sent to a website when it is loaded inside an iframe.
The samesite
attribute can be set using HTTP response headers or JavaScript. Via HTTP, it looks like:
Set-Cookie: demoCookie=demoValue; samesite=lax
or
Set-Cookie: demoCookie=demoValue; samesite=strict
In JavaScript, it is:
document.cookie = "demoCookie=demoValue; SameSite=Lax";
document.cookie = "demoCookie=demoValue; SameSite=Strict";
When the value is lax
, these types of requests are blocked:
- Form POST submit (<form method="POST" action="...">)
- iframe (<iframe src="..."></iframe>)
- AJAX ($.get("..."))
- Image (<img src="...">)
- Script (<script src="..."></script>)
- Stylesheet (<link rel="stylesheet" type="text/css" href="...">)
When the value is strict
, these types of requests are also blocked, in addition to those under lax
:
- Clicking a link (<a href="..."></a>)
- Prerender (<link rel="prerender" href=".."/>)
- Form GET submit (<form method="GET" action="...">)
In this case, we are concerned with iframe requests. A clickjacking attempt would fail because the user is not considered logged into, for example, Facebook, so they can't "Like" anything through the iframe.
The samesite
attribute will not have an effect when cookies are not used. This may allow websites to easily show public, unauthenticated pages in iframes on unaffiliated websites. However, this may also allow clickjacking attacks to work in a few limited cases. An anonymous polling website that prevents duplicate voting by checking IP addresses, for example, would still be vulnerable to clickjacking because it does not authenticate users using cookies.
Summary
Clickjacking is a way to "trick" users into clicking on a malicious site without even knowing what's happening. That's dangerous if there are important click-activated actions.
A hacker can post a link to their evil page in a message, or lure visitors to their page by some other means. There are many variations.
From one perspective -- the attack is "not deep": all a hacker is doing is intercepting a single click. But from another perspective, if the hacker knows that after the click another control will appear, then they may use cunning messages to coerce the user into clicking on them as well.
The attack is quite dangerous, because when we engineer the UI we usually don't anticipate that a hacker may click on behalf of the visitor. So vulnerabilities can be found in totally unexpected places.
- It is recommended to use
X-Frame-Options: SAMEORIGIN
on pages (or whole websites) which are not intended to be viewed inside frames. - Use a covering
<div>
if we want to allow our pages to be shown in iframes, but still stay safe.