158.Clickjacking点击劫持攻击实现和防护措施

clickjacking攻击:

clickjacking攻击又称为点击劫持攻击,是一种在网页中将恶意代码等隐藏在看似无害的内容(如按钮)之下,并诱使用户点击的手段。

clickjacking攻击场景:

用户进入到一个网页中,里面包含了一个按钮(查看照片),可是这个按钮上面加载了一个透明的iframe标签,这个iframe标签加载了另一个网页,而且他将这个网页的某个按钮和网页中的按钮(查看照片)重合,因此你在点击按钮(查看照片的时候)实际上点的是经过iframe加载的另一个网页的按钮,好比我如今有一个csdn的用户帐号,如今想要用户点击关注。那么咱们就能够准备如下页面:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Clickjacking</title>
    <style>
        iframe {
            width: 100%;
            height: 100%;
            display: block;
            position: absolute;  /*指定iframe和button为绝对定位*/
            z-index: 20;  /*指定在垂直方向上的高低*/
            opacity: 0.01;
            /*指定透明度*/
            <!--注意,iframe的透明度不能设置为0,若是设置为0的话,就不能接受任何的点击事件了-->
        }
        button {
            position: absolute;
            left: 40px;
            top: 65px;
            z-index: 10;
        }
    </style>
</head>
<body>
<h2>哇塞,这张照片里怎么会有我!快来看看有没有你吧!</h2>
<button>查看照片</button>
<iframe src="https://blog.csdn.net/zjy123078_zjy/" frameborder="0"></iframe>
</body>
</html>
clickjacking防护:咱们能够设置咱们的网页不容许使用iframe被加载到其余网页中就能够避免这种状况了,咱们能够经过在响应头中设置X-Frame-Options来设置这种操做,X-Frame-Options能够设置如下三个值:
(1)DEBY:不容许任何网页使用iframe加载我这个页面。
(2)SAMEORIGIN:只容许在相同域名(也就是本身的网站)下使用iframe加载这个页面。
(3)ALLOWED-FROM origin: 容许任何网页经过iframe加载我这个网页。
在Django中,使用中间件django.middleware.clickjacking.XFrameOptionsMiddleware能够帮咱们堵上这个漏洞,这个中间件设置了一个X-Frame-Option为DENY,也就是不容许任何网页使用iframe加载这个网页,这样就能够避免其余的别有用心的网页去经过iframe加载了。

咱们能够查看一下网页源代码,以下:html

class XFrameOptionsMiddleware(MiddlewareMixin):
    """
    Set the X-Frame-Options HTTP header in HTTP responses.

    Do not set the header if it's already set or if the response contains
    a xframe_options_exempt value set to True.

    By default, set the X-Frame-Options header to 'SAMEORIGIN', meaning the
    response can only be loaded on a frame within the same site. To prevent the
    response from being loaded in a frame in any site, set X_FRAME_OPTIONS in
    your project's Django settings to 'DENY'.
    """
    def process_response(self, request, response):
        # Don't set it if it's already in the response
        if response.get('X-Frame-Options') is not None:
            return response

        # Don't set it if they used @xframe_options_exempt
        if getattr(response, 'xframe_options_exempt', False):
            return response

        response['X-Frame-Options'] = self.get_xframe_options_value(request,
                                                                    response)
        return response

    def get_xframe_options_value(self, request, response):
        """
        Get the value to set for the X_FRAME_OPTIONS header. Use the value from
        the X_FRAME_OPTIONS setting, or 'DENY' if not set.

        This method can be overridden if needed, allowing it to vary based on
        the request or response.
        """
        return getattr(settings, 'X_FRAME_OPTIONS', 'DENY').upper()
因此,在咱们使用django建立项目的时候,默认的状况下,Django就会默认的帮咱们定义一个处理“点击劫持攻击”的中间件,默认状况下就是开启的。
相关文章
相关标签/搜索