点击劫持防御方案

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

内容简介:从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 的设置


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

查看所有标签

猜你喜欢:

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

数据库索引设计与优化

数据库索引设计与优化

【美】Tapio Lahdenmaki、【美】Michael Leach / 曹怡倩、赵建伟 / 电子工业出版社 / 2015-6 / 79.00元

《数据库索引设计与优化》提供了一种简单、高效、通用的关系型数据库索引设计方法。作者通过系统的讲解及大量的案例清晰地阐释了关系型数据库的访问路径选择原理,以及表和索引的扫描方式,详尽地讲解了如何快速地估算SQL 运行的CPU 时间及执行时间,帮助读者从原理上理解SQL、表及索引结构、访问方式等对关系型数据库造成的影响,并能够运用量化的方法进行判断和优化,指导关系型数据库的索引设计。 《数据库索......一起来看看 《数据库索引设计与优化》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

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

RGB CMYK 互转工具