# 特征
* 用戶親手操作
* 用戶不知情
* ……
# 危害
* 盜取用戶資金(轉賬、消費)
* 獲取用戶敏感信息
* ……
# 原理
目標網站以透明的`iframe`的形式嵌入到攻擊網站中。
# 防御
## Javascript 禁止內嵌
```javascript
// top:攻擊者文檔的window對象;
// window:被嵌入的iframe文檔的window對象
if (top.location !== window.location) {
top.location = window.location;
}
```
但這種辦法并不能完全防御,比如:
```html
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title>csrf demo</title>
</head>
<body style="background:url(clickhijack.png) no-repeat">
<iframe style="opacity:0" src="http://localhost:1521/post/1" sandbox="allow-form" width="800" height="600"></iframe>
</body>
</html>
```
> 當`iframe`標簽中設置了sandbox屬性時,嵌入頁面`http://localhost:1521/post/1`中的腳本會被禁止運行,自然`top.location = window.location`這段防御腳本就沒用了。
> `sandbox="allow-form"`是說禁止包括腳本運行在內的很多功能,但是允許表單提交。
## X-FRAME-OPTIONS 禁止內嵌
對某個網頁設置`http`頭`X-Frame-Options`
> X-Frame-Options: DENY // 該網頁不允許被內嵌
> X-Frame-Options: SAMEORIGIN // 該網頁僅允許被同域名的網頁內嵌
> X-Frame-Options: ALLOW-FROM // 該網頁僅允許被指定的網頁內嵌
## 其他輔助手段
比如:驗證碼