代码之家  ›  专栏  ›  技术社区  ›  jbindel

帧中断只跨域而不是来自同一原点的iframe?

  •  3
  • jbindel  · 技术社区  · 14 年前

    这个问题之前 asked and answered 没错,但似乎没有发布解决方案。

    如果一个站点有iframe,并且想要防止这些iframe被另一个域包围在一个框架中,那么简单化的框架破坏是没有用的:

    <script>if (top != self) top.location = location</script>
    

    但是,由于到其他域的跨框架脚本应该会生成异常,因此在iframe中类似这样的东西似乎可以很好地工作:

    <script>
    try {
      if (window.document.domain != top.document.domain) {   // throws exception
        throw "You naughty puppy!"; // Should not ever get here, right?
      }
    }
    catch () {
      top.location = "/error/naughtypuppy";
    }
    </script>
    

    if 以上这些本身就足以防止iframe的跨域成帧。它应该永远不会回来 false throw 浏览器中的语句?

    这是否足以防止仅来自其他域的帧?

    <script>
    try {
      var bogus = top.document.domain;
    }
    catch () {
      top.location = "/error/naughtypuppy";
    }
    </script>
    

    Detect when iframe is cross-domain, then bust out of it . 本质上与“如果发生异常,尝试访问另一个框架和胸围”是相同的解决方案

    2 回复  |  直到 8 年前
        1
  •  2
  •   Gregg    8 年前

    该代码容易受到利用“onbeforeunload”功能的攻击。父(邪恶)页面设置了一个间隔处理程序(由于域的差异,这对代码是不受攻击的)和一个“onbeforeunload”处理程序。第二个处理程序只更新一些全局变量(也不受攻击),以记录窗口“受到攻击”的事实,然后更新间隔计时器(运行速度足够快,应该能够在浏览器完成外部窗口更新之前激活) 你的 合法网址)弹出并更新 window.location

    这是一个更老的问题: Frame Buster Buster ... buster code needed

        2
  •  1
  •   Hafthor    9 年前

    我有一个网站,仍然有框架,我不能删除他们现在。这是我能找到的最好的解决方案:

    <style>html { display:none }</style>
    <script>
    if (self == top) {
      document.documentElement.style.display = 'block';
    } else {
      top.location = self.location;
    }
    </script> 
    

    通过此链接: XFS 101-cross-frame-scripting-explained

    OWASP_AppSec_Research_2010_Busting_Frame_Busting_by_Rydstedt

    这是一个更新的OWasp Clickjacking page