内容简介:从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-src 、 frame-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 的设置
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Building Social Web Applications
Gavin Bell / O'Reilly Media / 2009-10-1 / USD 34.99
Building a social web application that attracts and retains regular visitors, and gets them to interact, isn't easy to do. This book walks you through the tough questions you'll face if you're to crea......一起来看看 《Building Social Web Applications》 这本书的介绍吧!