Django: csrf防御機制

csrf攻擊過程

csrf攻擊說明
csrf攻擊說明

1.用戶C打開瀏覽器,訪問受信任網(wǎng)站A,輸入用戶名和密碼請求登錄網(wǎng)站A;

2.在用戶信息通過驗證后,網(wǎng)站A產(chǎn)生Cookie信息并返回給瀏覽器,此時用戶登錄網(wǎng)站A成功,可以正常發(fā)送請求到網(wǎng)站A;

3.用戶未退出網(wǎng)站A之前,在同一瀏覽器中,打開一個TAB頁訪問網(wǎng)站B;

4.網(wǎng)站B接收到用戶請求后,返回一些攻擊性代碼,并發(fā)出一個請求要求訪問第三方站點A;

5.瀏覽器在接收到這些攻擊性代碼后,根據(jù)網(wǎng)站B的請求,在用戶不知情的情況下攜帶Cookie信息,向網(wǎng)站A發(fā)出請求。網(wǎng)站A并不知道該請求其實是由B發(fā)起的,所以會根據(jù)用戶C的Cookie信息以C的權(quán)限處理該請求,導(dǎo)致來自網(wǎng)站B的惡意代碼被執(zhí)行。

csrf的攻擊之所以會成功是因為服務(wù)器端身份驗證機制可以通過Cookie保證一個請求是來自于某個用戶的瀏覽器,但無法保證該請求是用戶允許的。因此,預(yù)防csrf攻擊簡單可行的方法就是在客戶端網(wǎng)頁上添加隨機數(shù),在服務(wù)器端進行隨機數(shù)驗證,以確保該請求是用戶允許的。Django也是通過這個方法來防御csrf攻擊的。

在django防御csrf攻擊

原理

在客戶端頁面上添加csrftoken, 服務(wù)器端進行驗證,服務(wù)器端驗證的工作通過'django.middleware.csrf.CsrfViewMiddleware'這個中間層來完成。在django當(dāng)中防御csrf攻擊的方式有兩種, 1.在表單當(dāng)中附加csrftoken 2.通過request請求中添加X-CSRFToken請求頭。注意:Django默認對所有的POST請求都進行csrftoken驗證,若驗證失敗則403錯誤侍候。

在表單中附加csrftoken

后端

from django.shortcuts import render
from django.template.context_processors import csrf

def ajax_demo(request):
     # csrf(request)構(gòu)造出{‘csrf_token’: token}
    return render(request, 'post_demo.html', csrf(request))

前端

  $('#send').click(function(){
                
    $.ajax({
        type: 'POST',
        url:'{% url 'ajax:post_data' %}',
        data: {
                username: $('#username').val(),
                content: $('#content').val(),
               'csrfmiddlewaretoken': '{{ csrf_token }}'  關(guān)鍵點
            },
        dataType: 'json',
        success: function(data){

        },
        error: function(){
    
        }
    
    });
  });

通過request請求中添加X-CSRFToken請求頭

后端

該方式需要借助于Cookie傳遞csrftoken, 設(shè)置Cookie的方式有兩種。ps:經(jīng)測試即便什么都不做,也會設(shè)置Cookie,不過官方文檔說,不保證每次都有效

1.表單中添加{%csrf_token%}這個模板標簽

<form id="comment_form" action="#"></form>
{% csrf_token %}   就是這個
<p>姓名: <input type="text" name="useranme" id="username"></p>
<p>內(nèi)容: <textarea name="content" id="content" rows="5" cols="30"></textarea></p>
<p><input type="button", id="send" value="提交"></p> 

2.ensure_csrf_cookie裝飾器。

from django.shortcuts import render
from django.views.decorators.csrf import ensure_csrf_cookie

@ensure_csrf_cookie
def ajax_demo(request):
    return render(request, 'ajax_demo.html')

前端
前端要做的事情,在進行post提交時,獲取Cookie當(dāng)中的csrftoken并在請求中添加X-CSRFToken請求頭, 該請求頭的數(shù)據(jù)就是csrftoken。通過$.ajaxSetup方法設(shè)置AJAX請求的默認參數(shù)選項, 在每次ajax的POST請求時,添加X-CSRFToken請求頭

<script type="text/javascript">
    $(function(){

        function getCookie(name) {
            var cookieValue = null;
            if (document.cookie && document.cookie != '') {
                var cookies = document.cookie.split(';');
                for (var i = 0; i < cookies.length; i++) {
                    var cookie = jQuery.trim(cookies[i]);
                    // Does this cookie string begin with the name we want?
                    if (cookie.substring(0, name.length + 1) == (name + '=')) {
                        cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                        break;
                    }
                }
            }
            return cookieValue;
        }

        <!--獲取csrftoken-->
        var csrftoken = getCookie('csrftoken');
        console.log(csrftoken);

        //Ajax call
        function csrfSafeMethod(method) {
            // these HTTP methods do not require CSRF protection
            return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
        }

        $.ajaxSetup({
            crossDomain: false, // obviates need for sameOrigin test
            //請求前觸發(fā)
            beforeSend: function(xhr, settings) {
                if (!csrfSafeMethod(settings.type)) {
                    xhr.setRequestHeader("X-CSRFToken", csrftoken);
                }
            }
        });

        $('#send').click(function(){
            console.log($("#comment_form").serialize());

            $.ajax({
                type: 'POST',
                url:'{% url 'ajax:post_data' %}',
                data: {
                        username: $('#username').val(),
                        content: $('#content').val(),
                       //'csrfmiddlewaretoken': '{{ csrf_token }}'
                    },
                dataType: 'json',
                success: function(data){
                         
                    
                },
                error: function(){

                }

            });
        });


    });
</script>

取消csrftoken驗證

通過csrf_exempt, 來取消csrftoken驗證,方式有兩種。
1 .在視圖函數(shù)當(dāng)中添加csrf_exempt裝飾器

from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def post_data(request):
    pass

2 .在urlconf當(dāng)中

from django.views.decorators.csrf import csrf_exempt
urlpatterns = [
    url(r'^post/get_data/$', csrf_exempt(post_data), name='post_data'),

]

參考:

django官網(wǎng)文檔

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容