Javascript 帧终结者终结者。。。需要破坏者代码

Javascript 帧终结者终结者。。。需要破坏者代码,javascript,html,iframe,framebusting,Javascript,Html,Iframe,Framebusting,假设您不希望其他站点在中“框架”您的站点: <iframe src="http://example.org"></iframe> 好极了!现在,您可以自动“爆破”或脱离任何包含iframe的对象。除了一个小问题 事实证明,您的帧破坏代码可以被破坏: var\u bust=0 window.onbeforeunload=函数(){provent\u-bust++} setInterval(函数(){ 如果(阻止值>0){ 预防胸围-=2 window.top.locat

假设您不希望其他站点在
中“框架”您的站点:

<iframe src="http://example.org"></iframe>
好极了!现在,您可以自动“爆破”或脱离任何包含iframe的对象。除了一个小问题

事实证明,您的帧破坏代码可以被破坏:


var\u bust=0
window.onbeforeunload=函数(){provent\u-bust++}
setInterval(函数(){
如果(阻止值>0){
预防胸围-=2
window.top.location文件http://example.org/page-which-responds-with-204'  
}  
}, 1)  
此代码执行以下操作:

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
  • 每次浏览器试图通过
    窗口.onbeforeunload
    事件处理程序离开当前页面时,递增一个计数器
  • 设置一个计时器,该计时器通过
    setInterval()
    每毫秒触发一次,如果它看到计数器递增,则将当前位置更改为攻击者控制的服务器
  • 该服务器提供一个带有HTTP状态代码204的页面,这不会导致浏览器在任何地方导航
我的问题是——这更像是一个JavaScript难题,而不是一个实际问题——如何才能击败破坏框架的破坏者

我有一些想法,但在测试中没有任何效果:

  • 试图通过
    onbeforeunload=null
    清除
    onbeforeunload
    事件无效
  • 添加一个
    警报()
    会停止进程,让用户知道它正在发生,但不会以任何方式干扰代码;单击“确定”使爆破继续正常进行
  • 我想不出任何方法来清除
    setInterval()
    计时器

我不是一个JavaScript程序员,所以我向你提出了一个挑战:嘿,buster,你能打破框架吗?

好吧,你可以修改计数器的值,但这显然是一个脆弱的解决方案。您可以在确定站点不在框架内后通过AJAX加载内容,这也不是一个很好的解决方案,但它有望避免触发on beforeunload事件(我假设)

编辑:另一个想法。如果您检测到您在一个框架中,请要求用户禁用javascript,然后单击一个链接将您带到所需的URL(传递一个查询字符串,让您的页面知道,告诉用户一旦他们在那里就可以重新启用javascript)

编辑2:去核-如果你检测到你在一个框架,只需删除你的文件正文内容和打印一些讨厌的消息


编辑3:你能列举最上面的文档并将所有函数设置为空(即使是匿名函数)吗?

我想你差不多做到了。您是否尝试过:

window.parent.onbeforeunload = null;
window.parent.location.replace(self.location.href);
或者,或者:

window.parent.prevent_bust = 0;

注意:我实际上并没有对此进行测试。

提出了这一点,它似乎至少在Firefox和Opera浏览器中起作用

if(top != self) {
 top.onbeforeunload = function() {};
 top.location.replace(self.location.href);
}

那么,反复给警察打电话怎么样?这将创造一个比赛条件,但人们可能希望巴斯特能站在首位:

(function() {
    if(top !== self) {
        top.location.href = self.location.href;
        setTimeout(arguments.callee, 0);
    }
})();

好的,我们知道这是一个框架。因此,我们将location.href添加到另一个特殊页面,路径作为GET变量。现在,我们向用户解释正在发生的事情,并提供一个带有target=“\u TOP”选项的链接。它很简单,可能会工作(还没有测试),但它需要一些用户交互。也许你可以向用户指出有问题的网站,并在某个地方让点击劫持者感到羞愧。。这只是一个想法,但它可以在夜间工作。

我不确定这是否可行-但是如果你不能打破框架,为什么不显示一个警告呢。例如,如果您的页面不是“顶部页面”,请创建一个setInterval方法,尝试断开框架。如果在3次或4次尝试后,您的页面仍然不是首页-创建一个div元素,用消息和链接覆盖整个页面(模式框),如

您正在未经授权的框架窗口中查看此页面-(诸如此类…潜在的安全问题)

单击此链接解决此问题


不是最好的,但我看不出他们有什么办法可以用脚本来解决这个问题。

经过一段时间的思考,我相信这会让他们知道谁是老板

if(top != self) {
  window.open(location.href, '_top');
}

使用
\u top
作为
窗口的目标参数。open()
将在同一窗口中启动它。

所有建议的解决方案直接强制更改顶部窗口的位置。如果用户希望该帧在那里该怎么办?例如,搜索引擎图像结果的顶部框架

我编写了一个原型,默认情况下,所有输入(链接、表单和输入元素)都被禁用,或者在激活时什么都不做

如果检测到包含帧,将禁用输入,并在页面顶部显示警告消息。警告消息包含一个链接,该链接将在新窗口中打开页面的安全版本。这可以防止页面被用于点击劫持,同时仍然允许用户在其他情况下查看内容

如果未检测到包含帧,则启用输入

这是代码。您需要将标准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>

安全警告:Acme应用程序显示在另一个页面中。
为确保数据安全,此页面已被禁用。
内容。

姓名:


如果在buster代码之后添加一个警报,那么该警报将暂停javascript线程,并允许页面加载。这就是StackOverflow所做的,它会从我的iFrame中崩溃,甚至当我使用破坏帧的buster时。它也适用于我的简单测试页面。这
<!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>
if (top != self) {
  top.location.replace(location);
  location.replace("about:blank"); // want me framed? no way!
}
<script type="text/javascript">
if (top != self){
  top.location.replace(self.location.href);
  alert("for security reasons bla bla bla");
}
</script>
   var currentInterval = 10000;
   currentInterval += setTimeout( gotoHREF, 100 );
   for( var i = 0; i < currentInterval; i++ ) top.clearInterval( i );
   // Include setTimeout to avoid recursive functions.
   for( i = 0; i < currentInterval; i++ )     top.clearTimeout( i );

   function gotoHREF(){
           top.location.href = "http://your.url.here";
   }
for (var j = 0 ; j < 256 ; ++j) clearInterval(j)
(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);
}());
<?php sleep(5);
<style>
 body { 
 display : none   
}
</style>
<script>
if(self == top) {
document.getElementsByTagName("body")[0].style.display = 'block';
}
else{
top.location = self.location;
}
</script>
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
<iframe src="URI" sandbox></iframe>
<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>
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://www\.yoursite\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule ^(.*)$ /copyrights.html [L]
Content-Security-Policy: frame-ancestors 'none'
X-Frame-Options: DENY
window.addEventListener("message", receiveMessage, false);

function receiveMessage(event) {
  // Use event.origin here like
  if(event.origin == "https://perhapsyoucantrustthisdomain.com"){
  // code here to block/unblock access ... a method like the one in user1646111's post can be good.
  }
  else{
  // code here to block/unblock access ... a method like the one in user1646111's post can be good.
  }
}