- 举个例子
- 假设受害者在一家网银网站上登录账户,然后继续浏览其他网页。
- 同时,攻击者通过电子邮件等方式向受害者发送了一封包含恶意链接的邮件。
- 当受害者点击该链接时,潜在的威胁就会变得非常现实。
- 该链接指向一个由攻击者操纵的网站,该网站上的恶意代码会自动向网银网站发送一个请求,请求转账到攻击者的账户。
- 由于受害者在网银网站中已经登录,所以该请求会被认为是合法的,这样攻击者就可以成功地进行转账操作。
这是正规的网站
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校验:是一种防止跨站请求伪造攻击的安全措施
//value是随机生成的一个串
方式三:
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);
}
}
});
Cross Site Request Forgery protection | Django documentation | Django (djangoproject.com)
from django.views.decorators.csrf import csrf_protect, csrf_exempt
'''
csrf_protect 需要校验
csrf_exempt 忽视校验
'''
from django.views.decorators.csrf import csrf_protect, csrf_exempt
'''
csrf_protect 需要校验
针对 csrf_protect 符合之前的装饰器的三种用法
csrf_exempt 忽视校验
针对 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):
def get(self, request):
return HttpResponse("get")
@method_decorator(csrf_protect)
def post(self, request):
return HttpResponse("post")
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")
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")
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")