Jef*_*ood 415 html javascript iframe framebusting
假设您不希望其他网站在以下位置"构建"您的网站<iframe>:
<iframe src="http://example.org"></iframe>
Run Code Online (Sandbox Code Playgroud)
因此,您将反框架,帧破坏JavaScript插入到您的所有页面中:
/* break us out of any containing iframes */
if (top != self) { top.location.replace(self.location.href); }
Run Code Online (Sandbox Code Playgroud)
优秀!现在你自动"破解"或突破任何包含iframe.除了一个小问题.
事实证明,您的帧破坏代码可能被破坏,如下所示:
<script type="text/javascript">
var prevent_bust = 0
window.onbeforeunload = function() { prevent_bust++ }
setInterval(function() {
if (prevent_bust > 0) {
prevent_bust -= 2
window.top.location = 'http://example.org/page-which-responds-with-204'
}
}, 1)
</script>
Run Code Online (Sandbox Code Playgroud)
此代码执行以下操作:
window.onbeforeunload事件处理程序离开当前页面时,都会递增计数器setInterval()如果它看到计数器递增,则将当前位置更改为攻击者控制的服务器我的问题是 - 这更像是一个JavaScript拼图,而不是一个实际的问题 - 你怎么能打败破坏框架的破坏者呢?
我有一些想法,但在我的测试中没有任何效果:
onbeforeunload事件onbeforeunload = null没有任何效果alert()停止的进程让用户知道它正在发生,但没有以任何方式干扰代码; 单击"确定"可以正常继续进行破坏setInterval()计时器我不是一个JavaScript程序员,所以这是我对你的挑战:嘿破坏者,你能破坏破坏框架的破坏者吗?
Eri*_*Law 208
FWIW,目前大多数浏览器支持的X框选项:否认指令,当脚本被禁用它也可使用.
IE8:http:
//blogs.msdn.com/ie/archive/2009/01/27/ie8-security-part-vii-clickjacking-defenses.aspx
Firefox(3.6.9)
https://bugzilla.mozilla.org/show_bug.cgi?id=475530
https://developer.mozilla.org/en/The_X-FRAME-OPTIONS_response_header
Chrome/Webkit
http://blog.chromium.org/2010/01/security-in-depth-new-security-features.html
http://trac.webkit.org/changeset/42333
Hug*_*are 148
我不确定这是否可行 - 但如果你不能打破框架,为什么不显示警告.例如,如果您的页面不是"首页",请创建一个尝试中断框架的setInterval方法.如果经过3或4次尝试后您的页面仍然不是首页 - 创建一个div元素,覆盖整个页面(模态框),并带有消息和类似链接...
您正在未经授权的框架窗口中查看此页面 - (Blah blah ...潜在的安全问题)
单击此链接以解决此问题
不是最好的,但我认为他们没有任何方式可以编写出自己的方式.
Dun*_*ter 34
我们在http://seclab.stanford.edu/websec/framebusting/framebust.pdf上的一个网站中使用了以下方法.
<style>
body {
display : none
}
</style>
<script>
if(self == top) {
document.getElementsByTagName("body")[0].style.display = 'block';
}
else{
top.location = self.location;
}
</script>
Run Code Online (Sandbox Code Playgroud)
Jan*_*nen 29
想到这一点,它似乎至少在Firefox和Opera浏览器中起作用.
if(top != self) {
top.onbeforeunload = function() {};
top.location.replace(self.location.href);
}
Run Code Online (Sandbox Code Playgroud)
小智 23
考虑到为iframe引入沙箱的当前HTML5标准,当攻击者使用沙箱时,可以禁用此页面中提供的所有帧破坏代码,因为它限制了以下iframe:
allow-forms: Allow form submissions.
allow-popups: Allow opening popup windows.
allow-pointer-lock: Allow access to pointer movement and pointer lock.
allow-same-origin: Allow access to DOM objects when the iframe loaded form same origin
allow-scripts: Allow executing scripts inside iframe
allow-top-navigation: Allow navigation to top level window
Run Code Online (Sandbox Code Playgroud)
请参阅:http://www.whatwg.org/specs/web-apps/current-work/multipage/the-iframe-element.html#attr-iframe-sandbox
现在,考虑攻击者使用以下代码在iframe中托管您的网站:
<iframe src="URI" sandbox></iframe>
Run Code Online (Sandbox Code Playgroud)
然后,所有JavaScript帧破坏代码都将失败.
检查所有帧总线代码后,只有这种防御适用于所有情况:
<style id="antiClickjack">body{display:none !important;}</style>
<script type="text/javascript">
if (self === top) {
var antiClickjack = document.getElementById("antiClickjack");
antiClickjack.parentNode.removeChild(antiClickjack);
} else {
top.location = self.location;
}
</script>
Run Code Online (Sandbox Code Playgroud)
最初由Gustav Rydstedt,Elie Bursztein,Dan Boneh和Collin Jackson提出(2010)
Jos*_*ola 19
经过一段时间的思考后,我相信这会告诉他们谁是老板......
if(top != self) {
window.open(location.href, '_top');
}
Run Code Online (Sandbox Code Playgroud)
使用_top作为目标参数window.open()将在同一窗口中启动它.
小智 6
if (top != self) {
top.location.replace(location);
location.replace("about:blank"); // want me framed? no way!
}
Run Code Online (Sandbox Code Playgroud)
我会勇敢地把帽子戴在这个戒指的上面(很古老),看看我能收集到多少羽毛.
这是我的尝试,它似乎在我测试过的任何地方都可以使用(Chrome20,IE8和FF14):
(function() {
if (top == self) {
return;
}
setInterval(function() {
top.location.replace(document.location);
setTimeout(function() {
var xhr = new XMLHttpRequest();
xhr.open(
'get',
'http://mysite.tld/page-that-takes-a-while-to-load',
false
);
xhr.send(null);
}, 0);
}, 1);
}());
Run Code Online (Sandbox Code Playgroud)
我把这段代码放在<head>并从中结束调用它<body>以确保我的页面在开始与恶意代码争论之前呈现,不知道这是否是最好的方法,YMMV.
它是如何工作的?
......我听到你问-好诚实的回答是,我并不真的知道.在我测试的任何地方都花了很多时间才能使它工作,并且它的确切效果会根据你运行它的位置而略有不同.
以下是它背后的想法:
对于我http://mysite.tld/page-that-takes-a-while-to-load(XHR的目标),我使用了一个如下所示的PHP脚本:
<?php sleep(5);
Run Code Online (Sandbox Code Playgroud)
怎么了?
你不能避免Chrome和Firefox的等待时间吗?
显然不是.起初我将XHR指向一个返回404的URL - 这在Firefox中不起作用.然后我尝试了sleep(5);我最终为这个答案着手的方法,然后我开始以各种方式玩睡眠长度.我找不到行为的真实模式,但我确实发现,如果它太短,特别是Firefox不会玩球(Chrome和IE似乎表现得相当不错).我不知道实际上"太短"的定义是什么,但每次5秒似乎都有效.
如果任何传递Javascript忍者想要更好地解释发生了什么,为什么这(可能)是错误的,不可靠的,他们见过的最糟糕的代码等我会高兴地听.
小智 5
好的,所以我们知道这是一个框架.所以我们将location.href改为另一个特殊页面,路径为GET变量.我们现在向用户解释发生了什么,并提供了一个带有target ="_ TOP"选项的链接.它很简单,可能会工作(没有测试过),但它需要一些用户交互.也许你可以向用户指出违规网站,并在某个地方为你的网站制作一个羞辱点击夹克的大厅..只是一个想法,但它夜间工作..
小智 5
所有提出的解决方案直接强制改变顶部窗口的位置.如果用户希望框架在那里怎么办?例如,搜索引擎的图像结果中的顶部框架.
我写了一个原型,默认情况下所有输入(链接,表单和输入元素)都被禁用和/或在激活时不执行任何操作.
如果检测到包含框架,则会禁用输入,并在页面顶部显示警告消息.警告消息包含一个链接,该链接将在新窗口中打开页面的安全版本.这可以防止页面用于点击劫持,同时仍允许用户在其他情况下查看内容.
如果未检测到包含帧,则启用输入.
这是代码.您需要将标准HTML属性设置为安全值,并添加包含实际值的附加属性.它可能是不完整的,并且对于完全安全的附加属性(我正在考虑事件处理程序)可能必须以相同的方式处理:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<title></title>
<script><!--
function replaceAttributeValuesWithActualOnes( array, attributeName, actualValueAttributeName, additionalProcessor ) {
for ( var elementIndex = 0; elementIndex < array.length; elementIndex += 1 ) {
var element = array[ elementIndex ];
var actualValue = element.getAttribute( actualValueAttributeName );
if ( actualValue != null ) {
element[ attributeName ] = actualValue;
}
if ( additionalProcessor != null ) {
additionalProcessor( element );
}
}
}
function detectFraming() {
if ( top != self ) {
document.getElementById( "framingWarning" ).style.display = "block";
} else {
replaceAttributeValuesWithActualOnes( document.links, "href", "acme:href" );
replaceAttributeValuesWithActualOnes( document.forms, "action", "acme:action", function ( form ) {
replaceAttributeValuesWithActualOnes( form.elements, "disabled", "acme:disabled" );
});
}
}
// -->
</script>
</head>
<body onload="detectFraming()">
<div id="framingWarning" style="display: none; border-style: solid; border-width: 4px; border-color: #F00; padding: 6px; background-color: #FFF; color: #F00;">
<div>
<b>SECURITY WARNING</b>: Acme App is displayed inside another page.
To make sure your data is safe this page has been disabled.<br>
<a href="framing-detection.html" target="_blank" style="color: #090">Continue working safely in a new tab/window</a>
</div>
</div>
<p>
Content. <a href="#" acme:href="javascript:window.alert( 'Action performed' );">Do something</a>
</p>
<form name="acmeForm" action="#" acme:action="real-action.html">
<p>Name: <input type="text" name="name" value="" disabled="disabled" acme:disabled=""></p>
<p><input type="submit" name="save" value="Save" disabled="disabled" acme:disabled=""></p>
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
截至2015年,您应该使用CSP2的frame-ancestors指令.这是通过HTTP响应头实现的.
例如
Content-Security-Policy: frame-ancestors 'none'
Run Code Online (Sandbox Code Playgroud)
当然,没有多少浏览器支持CSP2,所以包含旧X-Frame-Options标头是明智的:
X-Frame-Options: DENY
Run Code Online (Sandbox Code Playgroud)
无论如何,我建议包括两者,否则你的网站将继续容易受到旧浏览器中的Clickjacking攻击,当然即使没有恶意,你也会得到不受欢迎的框架.大多数浏览器最近会自动更新,但是出于遗留应用程序兼容性原因,您仍然倾向于让企业用户卡在旧版本的Internet Explorer上.