点击劫持(ClickJacking)

1)什么是点劫持

点击劫持是一种视觉上的欺骗手段。攻击者使用一个透明的、不可见的iframe,覆盖在一个网页上,然后诱使用户在该网页上进行操作,此时用户将在不知情的情况下点击透明的iframe页面。通过调整iframe页面的位置,可以诱使用户恰好点击在iframe页面的一些功能性按钮上。

看下面这个例子。

在http://www.a.com/test.html页面中插入了一个指向目标网站的iframe,出于演示的目的,我们让这个iframe变成半透明:

<!DOCTYPE html>
<html>
<head>
    <title>CLICK JACK!!!</tilte>
    <systle>
    iframe {
        width: 900px;
        height: 250px;
    
        /* Use absolute positioning to line up update button with fake button */
        posistion: absolute;
            top: -195px;
            left: -740px;
            z-index: 2;
        
        /* Hide from view */
        -moz-opacity: 05;
        opacity: 0.5;
        filter: alpha(opacity=0.5);
    }

    button {
        position: absolute;
        top: 10px;
        left: 10px;
        z-index: 1;
        width: 120px;
    }
    </sytle>
</head>
<body>
    <iframe src="http://www.qidian.com" scrolling="no"></iframe>
    <button>CLICK HERE</button>
</body>
</html>

在这个test.html中有一个button,如果iframe完全透明时,那么用户看到的是:

当iframe半透明时,可以看到,在button上面其实覆盖了另一个网页:

覆盖的网页其实是一个搜索按钮:

当用户试图点击test.html里的button时,实际上却会点击到iframe页面中的搜索按钮。

分析其代码,起到关键作用的是下面这几行:

iframe {
        width: 900px;
        height: 250px;
    
        /* Use absolute positioning to line up update button with fake button */
        posistion: absolute;
            top: -195px;
            left: -740px;
            z-index: 2;
        
        /* Hide from view */
        -moz-opacity: 05;
        opacity: 0.5;
        filter: alpha(opacity=0.5);
    }

通过控制iframe的长、宽,以及调整top、left的位置,可以把iframe页面内的任意部分覆盖到任何地方。同时设置iframe的position为absolute,并将z-index的值设置为最大,以达到让iframe处于页面的最上层。最后,再通过设置opacity来控制iframe页面的透明程度,值为0是完全不可见。

这样,就完成了一次点击劫持的攻击。

点击劫持攻击与CSRF攻击有异曲同工之妙,都是在用户不知情的情况下诱使用户完成一些动作。但是在CSRF攻击的过程中,如果出现用户交互的页面,则攻击可能会无法顺利完成。与之相反的是,点击劫持没有这个顾虑,它利用的就是与用户产生交互的页面。

 

2)防御 ClickJacking

ClickJacking 是一种视觉上的欺骗,那么如何防御它呢?针对传统的ClickJacking,一般是通过禁止跨域的iframe来防范。

 

frame busting

通常可以写一段JavaScript代码,以禁止iframe的嵌套。这种方法叫frame busting。比如下面这段代码:

if (top.location != location){
    top.location = self.location;
}

常见的frame busting有以下这些方式:

if (top != self)
if (top.location != self.location)
if (top.location != location)
if (parent.frame.length >0 )
if (window != top)
if (window.top !== window.self)
if (window.self != window.top)
if (parent && parant != window)
if (parent && parent.frames && parent.frames.length>0)
top.location = self.location
top.location.href = document.location.href
top.location.href = self.location.href
top.location.replace(self.location)
top.location.href = window.location.href
top.location.replace(document.location)
top.location.href = windows.location.href
top.location.href = "URL"
document.wirte(‘’)
top.location = location
top.location.replace(document.location)
top.location.replace('URL')
top.location.href = document.location
top.location.replace(window.location.href)
top.location.href = location.href
self.parent.location = document.location
parent.location.href = self.document.location
top.location.href = self.location
top.location = window.location
top.location.replace(window.location.pathname)
window.top.location = window.self.location
setTimeout(function(){document.body.innerHTML='';},1)
window.self.onload = function(evt){document.body.innerHTML='';}
var url = window.location.href; top.location.replace(url)

但是frame busting 也存在一些缺陷。由于它是用JavaScript写的,控制能力并不是特别强,因此有许多方法可以绕过它。

比如针对parent.location 的 frame busting,就可以采用嵌套多个 iframe 的方法绕过。假设frame busting 代码如下:

if ( top.location != self.location) {
    parent.location = self.location;
}

那么通过以下方式即可绕过上面的保护代码:

Attacker top frame:
<iframe src="attack2 .html">
Attacker sub-frame:
<iframe src="http://www.victim.com">

此外,像HTML5中iframe的sandbox属性、IE中iframe的security属性等,都可以限制iframe页面中的JavaScript脚步执行,从而可以使得frame busting 失效。

 

X-Frame-Options

因为frame busting存在被绕过的可能,所以我们需要寻找其他更好的解决方案。一个比较好的方案是使用一个HTTP头 X-Frame-Options。

X-Frame-Options 可以说是为了解决ClickJacking 而生的,它有三个可选的值:

  • DENY
  • SAMEORIGIN
  • ALLOW-FROM origin

当值为DENY时,浏览器会拒绝当前页面加载任何frame页面;若值为SAMEORIGIN,则frame页面的地址只能为同源域名下的页面;若值为ALLOW-FROM,则可以定义允许frame加载的页面地址。

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值