如何在检测到iFrame时重定向到另一个页面?

时间:2018-01-02 14:59:23

标签: javascript jquery html iframe

这是测试页面。我的网页上有一个iFrame,其中包含我网站上的另一个HTML页面。

<html> 
    <head> 
        <title>Clickjack test page</title> 
    </head> 
    <body> 
        <p>Website is not vulnerable to clickjacking.</p> 
        <iframe src="../page1" width="500" height="500" id="iframe"></iframe>
    </body> 
</html>

这是我在page1.html上的脚本

<script type="text/javascript">
    console.log(window.location != window.parent.location);

    if(window.location != window.parent.location){
       console.log("iFrame Detected");
       window.location.replace("redirectMessage.html");
       window.location.href = "redirectMessage.html";
       console.log("after redirect");
    }

    else {
        // no iframe
    }
</script> 

目标:当我转到ClickJack测试页面时,检测iframe并将iFrame中的页面重定向到redirectMessage.html

我在控制台中收到iFrame Detectedafter redirect

所以我知道我的IF声明已经达成 但是iFrame中的页面没有重定向。

1 个答案:

答案 0 :(得分:0)

您不应该试图弄清楚您的网页是否在iframe中加载,因为攻击者可能只是使用sandbox attribute on the iframe而这会阻止您的脚本使您的(登录)页面容易受到点击劫持。

相反,您网站的后端应返回设置为DENY的X-FRAME-OPTIONS,以阻止浏览器在iframe中呈现您的网站。

请点击此处了解详情:https://steemit.com/security/@gaottantacinque/steemit-security-check-iframe-tricks