Skip to content

Commit 1ffcde6

Browse files
AceLeeWinnieleviding
authored andcommitted
点击劫持攻击 (javascript-tutorial#254)
* 根据初校意见改正 * 根据二校修改 * Update article.md * Update article.md
1 parent 134d616 commit 1ffcde6

File tree

1 file changed

+67
-66
lines changed

1 file changed

+67
-66
lines changed
Lines changed: 67 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,159 +1,160 @@
1-
# The clickjacking attack
1+
# 点击劫持攻击
22

3-
The "clickjacking" attack allows an evil page to click on a "victim site" *on behalf of the visitor*.
3+
“点击劫持” 攻击即允许恶意网页**以用户的名义**点击 “受害站点”。
44

5-
Many sites were hacked this way, including Twitter, Facebook, Paypal and other sites. They are all fixed, of course.
5+
许多站点都被这样攻击过,包括 TwitterFacebookPaypal 等等许多网站。当然,目前它们都已修复这个问题。
66

7-
## The idea
7+
## 原理
88

9-
The idea is very simple.
9+
原理十分简单。
1010

11-
Here's how clickjacking was done with Facebook:
11+
以下以 Facebook 为例解释点击劫持是如何运作的:
1212

13-
1. A visitor is lured to the evil page. It doesn't matter how.
14-
2. The page has a harmless-looking link on it (like "get rich now" or "click here, very funny").
15-
3. Over that link the evil page positions a transparent `<iframe>` with `src` from facebook.com, in such a way that the "Like" button is right above that link. Usually that's done with `z-index`.
16-
4. In attempting to click the link, the visitor in fact clicks the button.
13+
1. 访问者被恶意网页吸引。此处略过如何被吸引的。
14+
2. 页面上存在一个看起来无害的链接(比如:“马上有钱” 或者 “点我,超好玩!”)。
15+
3. 恶意网页在该链接之上放置一个透明 `<iframe>` 标签,其中 `src` 指向 facebook.com,如此一来,“点赞” 按钮恰好在链接上面。通常用 `z-index` 实现。
16+
4. 如果用户试图点击该链接,实际上是点到了 “点赞” 按钮上。
1717

18-
## The demo
18+
## 示例
1919

20-
Here's how the evil page looks. To make things clear, the `<iframe>` is half-transparent (in real evil pages it's fully transparent):
20+
以下是恶意网页的一般代码。为了更好的说明问题,`<iframe>` 标签设置成半透明状态(真正的恶意网页为全透明状态):
2121

2222
```html run height=120 no-beautify
2323
<style>
24-
iframe { /* iframe from the victim site */
24+
iframe { /* 来自受害网站的 iframe */
2525
width: 400px;
2626
height: 100px;
2727
position: absolute;
2828
top:0; left:-20px;
2929
*!*
30-
opacity: 0.5; /* in real opacity:0 */
30+
opacity: 0.5; /* 真实为 opacity:0 */
3131
*/!*
3232
z-index: 1;
3333
}
3434
</style>
3535

36-
<div>Click to get rich now:</div>
36+
<div>马上有钱:</div>
3737

38-
<!-- The url from the victim site -->
38+
<!-- 来自受害网站的 url -->
3939
*!*
4040
<iframe src="/clickjacking/facebook.html"></iframe>
4141

42-
<button>Click here!</button>
42+
<button>点我!点我!</button>
4343
*/!*
4444

45-
<div>...And you're cool (I'm a cool hacker actually)!</div>
45+
<div>...你会变帅(我才是帅黑客😜)!</div>
4646
```
4747

48-
The full demo of the attack:
48+
完整的攻击示例如下:
4949

5050
[codetabs src="clickjacking-visible" height=160]
5151

52-
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.
52+
例子中的半透明 `<iframe src="facebook.html">` 覆盖在按钮之上。点击按钮实际上点击在 iframe 标签上,但由于 iframe 标签透明,这一动作对用户不可见。
5353

54-
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.
5554

56-
Here's the same example, but closer to reality, with `opacity:0` for `<iframe>`:
55+
因此,若访问者曾登陆 Facebook(“记住我” 开关打开),这个动作会使用户在 Facebook 上进行 “Like” 操作。Twitter 上是 “Follow” 操作。
56+
57+
下面的例子相同,但 `iframe` 设置为 `opacity:0` 更符合实际情况:
5758

5859
[codetabs src="clickjacking" height=160]
5960

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.
61+
只需要在恶意网页中的链接正上方放置 `<iframe>`,点击按钮就能发起攻击。通常用 CSS 就能实现。
6162

62-
```smart header="Clickjacking is for clicks, not for keyboard"
63-
The attack only affects mouse actions.
63+
```smart header="点击劫持作用于点击事件,而非键盘事件"
64+
此攻击仅影响鼠标操作。
6465
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 he sees on the page, he actually focuses on the input inside the iframe.
66+
从技术上讲,可以用 iframe 中的文本域覆盖原有的文本域实现攻击。所以当访问者试图聚焦网页中的 input 标签时,实际上聚焦的是 iframe 中的 input 标签。
6667
67-
But then there's a problem. Everything that the visitor types will be hidden, because the iframe is not visible.
68+
但是这里有个问题。访问者的所有输入都会被隐藏,因为该 iframe 是不可见的。
6869
69-
People will usually stop typing when they can't see their new characters printing on the screen.
70+
当用户无法在屏幕上看到自己输入的字符时,通常会停止打字。
7071
```
7172

72-
## Old-school defences (weak)
73+
## 传统防御(弱 👎)
7374

74-
The oldest defence is a bit of JavaScript which forbids opening the page in a frame (so-called "framebusting").
75+
最古老的防御是一段禁止在非顶层页面中打开网页的 JavaScript 代码(所谓的 “framebusting”)。
7576

76-
That looks like this:
77+
如下所示:
7778

7879
```js
7980
if (top != window) {
8081
top.location = window.location;
8182
}
8283
```
8384

84-
That is: if the window finds out that it's not on top, then it automatically makes itself the top.
85+
意思是:window 强制置顶,如果没在顶层,自动置顶。
8586

86-
This not a reliable defence, because there are many ways to hack around it. Let's cover a few.
87+
这个方法并不可靠,因为有许多方式可以绕过这个限制。下面就介绍几个。
8788

88-
### Blocking top-navigation
89+
### 阻塞顶层容器
8990

90-
We can block the transition caused by changing `top.location` in the [beforeunload](info:onload-ondomcontentloaded#window.onbeforeunload) event.
91+
[beforeunload](info:onload-ondomcontentloaded#window.onbeforeunload) 事件中阻塞 `top.location` 变更过渡。
9192

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 him whether he wants to leave.
93+
顶层页面(从属于黑客)在 `beforeunload` 上添加一个处理方法:当 `iframe` 试图变更 `top.location` 时,访问者会收到询问是否离开的消息。
9394

94-
Like this:
95+
如下所示:
9596
```js
9697
window.onbeforeunload = function() {
9798
window.onbeforeunload = null;
9899
return "Want to leave without learning all the secrets (he-he)?";
99100
};
100101
```
101102

102-
In most cases the visitor would answer negatively, because he doesn't know about the iframe, all he can see is the top page, leading him to think there is no reason to leave. So `top.location` won't change!
103+
大多数情况下,由于并不知道 iframe 的存在,访问者看到的只是顶层页面,即本来就要访问的页面,由此认为没有必要离开,所以会回答否。则 `top.location` 并不会变化!
103104

104-
In action:
105+
作用如下:
105106

106107
[codetabs src="top-location"]
107108

108-
### Sandbox attribute
109+
### 沙箱属性
109110

110-
One of the things restricted by the `sandbox` attribute is navigation. A sandboxed iframe may not change `top.location`.
111+
一个受 `sandbox` 属性限制的对象是导航。沙箱化的 iframe 不能变更 `top.location`
111112

112-
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.
113+
但可以添加带有 `sandbox="allow-scripts allow-forms"` 的 iframe 标签。从而放开限制,允许脚本和表单在 iframe 中执行。但 `allow-top-navigation` 禁止了 `top.location` 的变更。
113114

114-
Here's the code:
115+
代码如下:
115116

116117
```html
117118
<iframe *!*sandbox="allow-scripts allow-forms"*/!* src="facebook.html"></iframe>
118119
```
119120

120-
There are other ways to work around that simple protection too.
121+
当然还有其他绕过这个弱鸡防御的方法。
121122

122123
## X-Frame-Options
123124

124-
The server-side header `X-Frame-Options` can permit or forbid displaying the page inside a frame.
125+
服务端 header 字段 `X-Frame-Options` 能够允许或禁止 frame 内页面的显示。
125126

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.
127+
这个 header 必须由 **服务端** 发送:若浏览器发现 `<meta>` 标签里有该字段,则会忽略此字段。即,`<meta http-equiv="X-Frame-Options"...>` 不生效。
127128

128-
The header may have 3 values:
129+
header 有三个值:
129130

130131

131132
`DENY`
132-
: Never ever show the page inside a frame.
133+
: 始终禁止 frame 中的页面加载。
133134

134135
`SAMEORIGIN`
135-
: Allow inside a frame if the parent document comes from the same origin.
136+
: 允许和父页面同一来源的 frame 进行页面加载。
136137

137138
`ALLOW-FROM domain`
138-
: Allow inside a frame if the parent document is from the given domain.
139+
: 允许和父页面同一给定域的 frame 进行页面加载。
139140

140-
For instance, Twitter uses `X-Frame-Options: SAMEORIGIN`. Here's the result:
141+
例如,Twitter 使用的是 `X-Frame-Options: SAMEORIGIN`。如下所示:
141142

142143
```html
143144
<iframe src="https://twitter.com"></iframe>
144145
```
145146

146147
<iframe src="https://twitter.com"></iframe>
147148

148-
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.
149+
取决于浏览器行为,以上 `iframe` 要么显示为空,要么提醒你浏览器不允许内部页面加载。
149150

150-
## Showing with disabled functionality
151+
## 显示不可用功能
151152

152-
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.
153+
`X-Frame-Options` 存在副作用。它无差别地禁止合法站点在 frame 中显示我们的网页。
153154

154-
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.
155+
所以还有其他措施...例如,把设置了 `height: 100%; width: 100%;``<div>` “覆盖” 在页面上,这样就能监听所有的点击事件。在 `window == top` 或无需防御的情况下,此 `<div>` 则应该隐藏起来。
155156

156-
Something like this:
157+
代码示例如下:
157158

158159
```html
159160
<style>
@@ -172,27 +173,27 @@ Something like this:
172173
</div>
173174

174175
<script>
175-
// there will be an error if top window is from the different origin
176-
// but that's ok here
176+
// 如果顶层 window 来自不同的域,会报错
177+
// 但是此处并没有报错
177178
if (top.document.domain == document.domain) {
178179
protector.remove();
179180
}
180181
</script>
181182
```
182183

183-
The demo:
184+
演示如下:
184185

185186
[codetabs src="protector"]
186187

187-
## Summary
188+
## 总结
188189

189-
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.
190+
点击劫持是一种 “欺骗” 用户在不知情下点击恶意站点的方式。如果是重要的点击操作,这是非常危险的。
190191

191-
A hacker can post a link to his evil page in a message, or lure visitors to his page by some other means. There are many variations.
192+
黑客可以通过信息提交一个链接到他的恶意网页,或者通过某些手段引诱访问者访问他的网页。当然还有许多其他变体。
192193

193-
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 he may use cunning messages to coerce the user into clicking on them as well.
194+
一方面 —— 这种攻击方式是“浅层”的:黑客只需要拦截一次点击。但另一方面,如果被这次点击之后会开启另一个控制开关,那么黑客同样用狡猾的提示强制用户点击这些控制按钮。
194195

195-
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.
196+
这种攻击相当危险,因为在设计交互界面时,通常不会考虑到可能会有黑客代替真正的访问者点击界面。所以许多意想不到的地方可能发现攻击漏洞。
196197

197-
- It is recommended to use `X-Frame-Options: SAMEORIGIN` on pages (or whole websites) which are not intended to be viewed inside frames.
198-
- Use a covering `<div>` if we want to allow our pages to be shown in iframes, but still stay safe.
198+
- 推荐在网页上(或整个站点)使用 `X-Frame-Options: SAMEORIGIN`,这不会被 frame 内部读取。
199+
- 若要允许的页面在 frame 中显示,用一个 `<div>` 遮盖,这样仍然是安全的。

0 commit comments

Comments
 (0)