You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 3-frames-and-windows/06-clickjacking/article.md
+20-38
Original file line number
Diff line number
Diff line change
@@ -57,12 +57,12 @@ Here's the same example, but closer to reality, with `opacity:0` for `<iframe>`:
57
57
58
58
[codetabs src="clickjacking" height=160]
59
59
60
-
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.
60
+
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. So that when a user clicks the link, they actually click the button. That's usually doable with CSS.
61
61
62
62
```smart header="Clickjacking is for clicks, not for keyboard"
63
-
The attack only affects mouse actions.
63
+
The attack only affects mouse actions (or similar, like taps on mobile).
64
64
65
-
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.
65
+
Keyboard input is much difficult to redirect. 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.
66
66
67
67
But then there's a problem. Everything that the visitor types will be hidden, because the iframe is not visible.
68
68
@@ -87,19 +87,19 @@ This not a reliable defence, because there are many ways to hack around it. Let'
87
87
88
88
### Blocking top-navigation
89
89
90
-
We can block the transition caused by changing `top.location` in the[beforeunload](info:onload-ondomcontentloaded#window.onbeforeunload) event.
90
+
We can block the transition caused by changing `top.location` in [beforeunload](info:onload-ondomcontentloaded#window.onbeforeunload) event handler.
91
91
92
-
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.
92
+
The top page (enclosing one, belonging to the hacker) sets a preventing handler to it, like this:
93
93
94
-
Like this:
95
94
```js
96
95
window.onbeforeunload=function() {
97
-
window.onbeforeunload=null;
98
-
return"Want to leave without learning all the secrets (he-he)?";
96
+
returnfalse;
99
97
};
100
98
```
101
99
102
-
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!
100
+
When the `iframe` tries to change `top.location`, the visitor gets a message asking them whether they want to leave.
101
+
102
+
In most cases the visitor would answer negatively because they don't know about the iframe - all they can see is the top page, there's no reason to leave. So `top.location` won't change!
103
103
104
104
In action:
105
105
@@ -123,7 +123,7 @@ There are other ways to work around that simple protection too.
123
123
124
124
The server-side header `X-Frame-Options` can permit or forbid displaying the page inside a frame.
125
125
126
-
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.
126
+
It must be sent exactly as HTTP-header: the browser will ignore it if found in HTML`<meta>` tag. So, `<meta http-equiv="X-Frame-Options"...>` won't do anything.
127
127
128
128
The header may have 3 values:
129
129
@@ -156,7 +156,7 @@ Depending on your browser, the `iframe` above is either empty or alerting you th
156
156
157
157
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.
158
158
159
-
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.
159
+
So there are other solutions... For instance, we can "cover" the page with a `<div>` with styles `height: 100%; width: 100%;`, so that it will intercept all clicks. That `<div>`is to be removed if `window == top` or if we figure out that we don't need the protection.
160
160
161
161
Something like this:
162
162
@@ -191,43 +191,25 @@ The demo:
191
191
192
192
## Samesite cookie attribute
193
193
194
-
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.
195
-
196
-
The `samesite` attribute can be set using HTTP response headers or JavaScript. Via HTTP, it looks like:
197
-
198
-
`Set-Cookie: demoCookie=demoValue; samesite=lax`
199
-
200
-
or
194
+
The `samesite` cookie attribute can also prevent clickjacking attacks.
A cookie with such attribute is only sent to a website if it's opened directly, not via a frame, or otherwise. More information in the chapter <info:cookie#samesite>.
203
197
204
-
In JavaScript, it is:
198
+
If the site, such as Facebook, had `samesite` attribute on its authentication cookie, like this:
- Form GET submit (<form method="GET" action="...">)
204
+
...Then such cookie wouldn't be sent when Facebook is open in iframe from another site. So the attack would fail.
223
205
224
-
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.
206
+
The `samesite` cookie attribute will not have an effect when cookies are not used. This may allow other websites to easily show our public, unauthenticated pages in iframes.
225
207
226
-
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.
208
+
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.
227
209
228
210
## Summary
229
211
230
-
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.
212
+
Clickjacking is a way to "trick" users into clicking on a victim site without even knowing what's happening. That's dangerous if there are important click-activated actions.
231
213
232
214
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.
0 commit comments