目录
一、csrf跨站请求伪造详解
二、csrf跨域请求伪造
【1】正常服务端
【2】钓鱼服务端
三、csrf校验
【介绍】
form表单中进行csrf校验:
【1】form表单如何校验
【2】ajax如何校验
四、csrf相关装饰器
【1】csrf_protect装饰器:
【2】csrf_exempt装饰器:
【3】FBV中使用上述装饰器
【4】CBV中使用上述装饰器
(1) csrf_protect
(2) csrf_exempt方法
- 举个例子
- 假设受害者在一家网银网站上登录账户,然后继续浏览其他网页。
- 同时,攻击者通过电子邮件等方式向受害者发送了一封包含恶意链接的邮件。
- 当受害者点击该链接时,潜在的威胁就会变得非常现实。
- 该链接指向一个由攻击者操纵的网站,该网站上的恶意代码会自动向网银网站发送一个请求,请求转账到攻击者的账户。
- 由于受害者在网银网站中已经登录,所以该请求会被认为是合法的,这样攻击者就可以成功地进行转账操作。
这是正规的网站
def transform_normal(request):
if request.method == "POST":
user_start = request.POST.get("start_user")
user_end = request.POST.get("end_user")
money = request.POST.get("money")
return HttpResponse(f"当前账户 :>>> {user_start} 向目标用户 :>>> {user_end} 转账了 :>>> {money}")
return render(request, 'transform_normal.html')
这是钓鱼的网站
def transform_normal(request):
if request.method == "POST":
user_start = request.POST.get("start_user")
user_end = request.POST.get("end_user")
money = request.POST.get("money")
return HttpResponse(f"当前账户 :>>> {user_start} 向目标用户 :>>> {user_end} 转账了 :>>> {money}")
return render(request, 'transform_normal.html')
添加CSRF Token字段:
设置Cookie:
双重Cookie校验:
csrf_token
{% csrf_token %}
csrfmiddlewaretoken
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;
}
var csrftoken = getCookie('csrftoken');
function csrfSafeMethod(method) {
// these HTTP methods do not require CSRF protection
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
$.ajaxSetup({
beforeSend: function (xhr, settings) {
if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
}
});
from django.views.decorators.csrf import csrf_protect, csrf_exempt
'''
csrf_protect 需要校验
csrf_exempt 忽视校验
'''
当我们没有注释掉csrf校验中间件
的时候,可以在函数头上加上 @csrf_exempt
忽视校验
当我们注释掉csrf校验中间件
的时候,可以在函数头上加上 @csrf_protect
强制启动校验
from django.views.decorators.csrf import csrf_protect, csrf_exempt
'''
csrf_protect 需要校验
针对 csrf_protect 符合之前的装饰器的三种用法
csrf_exempt 忽视校验
针对 csrf_exempt 只能给 dispatch 方法加才有效
'''
csrf_protect
@method_decorator
from django.views import View
from django.views.decorators.csrf import csrf_protect, csrf_exempt
from django.utils.decorators import method_decorator
class MyCsrf(View):
def get(self, request):
return HttpResponse("get")
@method_decorator(csrf_protect)
def post(self, request):
return HttpResponse("post")
@method_decorator
from django.views import View
from django.views.decorators.csrf import csrf_protect, csrf_exempt
from django.utils.decorators import method_decorator
@method_decorator(csrf_protect)
class MyCsrf(View):
def get(self, request):
return HttpResponse("get")
def post(self, request):
return HttpResponse("post")
dispatch
方法from django.views import View
from django.views.decorators.csrf import csrf_protect, csrf_exempt
from django.utils.decorators import method_decorator
class MyCsrf(View):
@method_decorator(csrf_protect)
def dispatch(self, request, *args, **kwargs):
return super(MyCsrf, self).dispatch(request, *args, **kwargs)
def get(self, request):
return HttpResponse("get")
def post(self, request):
return HttpResponse("post")
csrf_exempt
方法dispatch
方法 有效from django.views import View
from django.views.decorators.csrf import csrf_protect, csrf_exempt
from django.utils.decorators import method_decorator
class MyCsrf(View):
@method_decorator(csrf_exempt)
def dispatch(self, request, *args, **kwargs):
return super(MyCsrf, self).dispatch(request, *args, **kwargs)
def get(self, request):
return HttpResponse("get")
def post(self, request):
return HttpResponse("post")