假设您不希望其他网站在以下位置"构建"您的网站<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 …