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就會默認的幫咱們定義一個處理「點擊劫持攻擊」的中間件,默認狀況下就是開啓的。
相關文章
相關標籤/搜索