页面被恶意iframe后,将可能导致csrf攻击。而且页面上的元素能够被DIV覆盖,将可能加重这种攻击。iframe被DIV覆盖可以参考下面的链接:http://www.hiermenuscentral.com/issues/safiframe/iframeover.html。
一般的解决方法如下:
<script type="text/javascript"> if(top != self) top.location.replace(location); </script>
但其实,这种方式能够被绕过。比如在父页面中,加入如下代码(限于某些对204处理时不会跳转的浏览器):
var prevent_bust = 0; // Event handler to catch execution of the busting
script. window.onbeforeunload = function() { prevent_bust++ }; // Continuously monitor whether busting script has fired.
setInterval(function() { if (prevent_bust > 0) { // Yes: it has fired.
prevent_bust -= 2; // Avoid further action. // Get a 'No Content' status which keeps us on the same page. window.top.location = 'http://server-which-responds-with-204.com'; } }, 1);
或者:
var framekiller = true; window.onbeforeunload = function() { if(framekiller) { return "..."; // any message that helps user to make decision } };
2010年,有人提出这样的解决方法:
<style> html{display : none ; } </style> <script> if( self == top ) { document.documentElement.style.display = 'block' ; } else { top.location = self.location ; } </script>
当然,这些解决方法可能是有缺陷的,比如浏览器没有开启执行JS,或者有缺陷等。
IE 8可以通过设置下面的HTTP Header:
点击劫持 (Clickjacking) 防御: 某些黑客会尝试诱骗用户去单击一些看起来像是执行安全或无害功能的按钮,但执行的却是不相关的任务。 点击劫持者 (Clickjacker) 通过使用透明框架,将特定的 UI 元素用令人误解的文本和图像加以覆盖,从而将恶意代码或伪装代码 (Redress) 嵌入到用户界面中。 为了帮助防止点击劫持攻击,网站所有者可以随 HTML 页面发送名为 X-Frame-Options 的 HTTP 响应标头,以限制设置页面框架的方式。 X-Frame-Options: Deny如果 X-Frame-Options 值包含 Deny 标记,则 Internet Explorer 8 将阻止呈现包含在某个框架中的页面。 如果该值包含 SameOrigin 标记,并且顶级浏览上下文不同于包含该指令的页面的源,则 Internet Explorer 将不呈现该页面。 所阻止的页将被替换为“此内容无法在框架中显示”错误页。
