点击劫持防御方案

栏目: 编程工具 · 发布时间: 5年前

内容简介:从lemin.i还会使用到lemon.v来实现一些功能,如何在主流浏览器(Firefox、Chrome、Ie、Safari)上达到防御效果?

Clickjacking攻防文中 学习到了很多,但是在业务上解决遇到一点问题。

lemin.i还会使用到lemon.v来实现一些功能,如何在主流浏览器(Firefox、Chrome、Ie、Safari)上达到防御效果?

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Clickjack</title>
    <style>
        iframe {
            width: 1440px;
            height: 900px;

            position: absolute;
            top: -0px;
            left: -0px;
            z-index: 2;

            -moz-opacity: 0.2;
            opacity: 0.2;
            filter: alpha(opacity=0.2);
        }
        button {
            position: absolute;
            z-index: 1;
            width: 120px;
        }
    </style>
</head>
<body>
    <iframe src="http://lemon.i/test/click/index.php" scrolling="no"></iframe>
    <button>Click Here!</button>
</body>
</html>

点击劫持防御方案

防御

js防御

frame bust:

<style>
    body {display: none;}
</style>

<!-- 此段JS放在body加载完成后 -->
<script>
if (self == top) {
    document.getElementsByTagName("body")[0].style.display = 'block';
} else {
    top.location = self.location;
}
</script>

具有局限性,对于业务有其他调用是会出现问题。

X-Frame-Options

DENY:禁止iframe
SAMEORIGIN:只允许相同域名下的网页iframe,同源政策保护
ALLOW-FROM: https://example.com:白名单限制

但这个缺陷就是chrome、Safari是不支持 ALLOW-FROM 语法!

phpcode

header("X-Frame-Options: allow-from http://lemon.v");

点击劫持防御方案

注意在IE下 allow-from 设置需要定义协议等较为严格的限定.

frame-ancestors、frame-src

https://cloud.tencent.com/developer/section/1189865

frame-ancestors影响以下标签: <frame>,<iframe>,<object>,<embed>,或<applet>
需要注意的是: 此指令在 <meta> 元素或Content-Security-policy-Report-Only标题字段中不受支持

语法:
Content-Security-Policy: frame-ancestors <source>;
Content-Security-Policy: frame-ancestors <source> <source>;

其中<source>
1、设置为none,单引号是必须的,则类似deny
Content-Security-Policy: frame-ancestors 'none';
2、*.lemon.v,通配匹配
Content-Security-Policy: frame-ancestors 'none';

frame-src影响以下标签: <frame>,<iframe> ,语法与 frame-ancestors 类似

phpcode

header("Content-Security-Policy: frame-ancestors lemon.v; frame-src lemon.v;");

frame-srcframe-ancestors 都存在的时候,会忽略 frame-src

当时这个的缺陷就是IE不支持CSP!

结合使用

在chrome、firefox下,会因为 frame-ancestors 指令,忽略 X-Frame-Options 的设置

IE本身不支持CSP,所以只受 X-Frame-Options 影响

所以可以综合使用

header('X-Frame-Options: allow-from http://lemon.v');
header("Content-Security-Policy: frame-ancestors lemon.v; frame-src lemon.v;");

这样解决了一种情况:

业务还存在跨域的lemon.v去iframe,在测试的浏览器中能够达到想要的效果(有些版本未测试,可能有偏差).

另外探索了一下综合使用会出现的问题:

header('X-Frame-Options: deny');
header("Content-Security-Policy: frame-ancestors lemon.v; frame-src lemon.v;");

Safari支持CSP,但 X-Frame-Options 设置为 deny ,会受到它的影响,从而覆盖了 frame-ancestors 的设置


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Head First Design Patterns

Head First Design Patterns

Elisabeth Freeman、Eric Freeman、Bert Bates、Kathy Sierra、Elisabeth Robson / O'Reilly Media / 2004-11-1 / USD 49.99

You're not alone. At any given moment, somewhere in the world someone struggles with the same software design problems you have. You know you don't want to reinvent the wheel (or worse, a flat tire),......一起来看看 《Head First Design Patterns》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具