宁晋网站建设设计,wordpress 忘记用户名密码破解,营销咨询公司经营范围,服务器怎样建设网站【1】csrf跨站请求伪造的解释及解决方法
CSRF#xff08;Cross-Site Request Forgery#xff09;跨站请求伪造是一种常见的网络攻击方式。攻击者通过诱导受害者访问恶意网站或点击恶意链接 将恶意请求发送到目标网站上利用受害者在目标网站中已登录的身份来执行某些操作从而…【1】csrf跨站请求伪造的解释及解决方法
CSRFCross-Site Request Forgery跨站请求伪造是一种常见的网络攻击方式。攻击者通过诱导受害者访问恶意网站或点击恶意链接 将恶意请求发送到目标网站上利用受害者在目标网站中已登录的身份来执行某些操作从而达到攻击的目的。 举个例子 假设受害者在一家网银网站上登录账户然后继续浏览其他网页。同时攻击者通过电子邮件等方式向受害者发送了一封包含恶意链接的邮件。当受害者点击该链接时潜在的威胁就会变得非常现实。该链接指向一个由攻击者操纵的网站该网站上的恶意代码会自动向网银网站发送一个请求请求转账到攻击者的账户。由于受害者在网银网站中已经登录所以该请求会被认为是合法的这样攻击者就可以成功地进行转账操作。 要保护自己免受CSRF攻击网站开发者可以采取以下措施 使用CSRF令牌 在用户的请求中添加随机生成的令牌并将该令牌保存在用户会话中。每次提交请求时都会验证该令牌以确保请求是合法的。启用SameSite属性 将Cookie的SameSite属性设置为Strict或Lax以限制跨站请求。这可以在一定程度上缓解CSRF攻击。严格验证请求来源 服务器端可以验证请求的来源是否为预期的网站域名例如检查HTTP Referer头部。使用验证码 在敏感操作如转账、更改密码等上使用验证码增加用户身份验证的防护。
【2】自定义csrf跨域请求伪造
钓鱼网站 搭建一个类似正规网站的页面用户点击网站链接给某个用户打钱打钱的操作确确实实提交给了中国银行的系统用户的钱也确实减少但是唯一不同的是账户打钱的账户不是用户想要打钱的目标账户变成了其他用户内部本质 在钓鱼网站的页面针对对方账户只给用户提供一个没有name属性的普通input框然后在内部隐藏一个已经写好带有name属性的input框如何避免上面的问题 csrf跨域请求伪造校验 网站在给用户返回一个具有提交数据功能的页面的时候会给这个页面加一个唯一标识当这个页面后端发送post请求的时候我们后端会先校验唯一标识 如果成功则正常执行如果唯一标识不符合则拒绝连接(403 forbidden)
【2.1】创建两个服务端
【2.1.1】创建正常服务端
前端
h1这是正规的网站/h1form action methodpostp当前账户 : input typetext namestart_user/pp目标账户 : input typetext nameend_user/pp转账金额 : input typetext namemoney/pinput typesubmit
/form
后端
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)
【2.1.2】创建钓鱼服务端
前端
h1这是钓鱼的网站/h1form actionhttp://127.0.0.1:8000/transform_normal/ methodpostp当前账户 : input typetext namestart_user /pp目标账户 : input typetext/ppinput typetext nameend_user valueHopes styledisplay: none/pp转账金额 : input typetext namemoney/pinput typesubmit
/form
后端
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)
【3】csrf校验
【3.1】csrf校验是什么 csrf校验是一种防止跨站请求伪造攻击的安全措施 【3.2】csrf校验的三种方式
【3.2.1】添加CSRF Token字段
在form表单中添加一个隐藏字段用于存储CSRF Token的值后端服务器在渲染表单时生成一个CSRF Token并将其存储在会话中或者以其它方式关联到当前用户当用户提交表单时目前短将CSRF Token的值包含在请求中后端在验证表单数据时检查请求中的CSFR Token是否与存储的Token匹配如果不匹配则拒绝请求
【3.2.2】设置Cookie
后端服务器在渲染表单时在客户端设置一个包含随机生成的CSRF Token的Cookie当用户提交表单时表单数据会被异同发送到服务器并自动包含该Cookie后端在验证表单数据时检查请求中的CSRF Token是否与Cookie中的值匹配如果不匹配则拒绝请求
【3.2.3】双重Cookie校验
后端服务器在渲染表单时在Cookie中设置一个随机生成的CSRF Token并将其存储在会话中或以其它方式关联到当前用户当用户提交表单时表单数据会被一同发送到服务器请求头或请求参数中携带一个包含CSRF Token的自定义字段后端在验证表单数据时同时检查请求中的CSRF Token和Cookie中的值是否匹配如果不匹配则拒绝请求
【3.2.4】form表单校验
在form表单上面加上csrf_token
form action methodpost
{% csrf_token %}pusername:input typetext nameusername/pptransfer_userinput typepassword namepassword/ppmoneyinput typetext namemoney/pinput typesubmit
/form
在浏览器页面标签中会自动出现一个input标签
input typehidden namecsrfmiddlewaretoken valuezQaNPZsy1tVmLdqC7GIDOOOfR7yT9YfO58lJ5yrjZfTw2edZTrVYUllOVMnkwXKe//value是随机生成的一个串
【3.2.5】ajax校验
方式一
利用标签查找获取页面上的随机字符串键必须叫csrfmiddlewaretoken
button idb1ajax请求提交/buttonscript$(#b1).click(function () {$.ajax({url: ,type: post,// 1 利用标签查找获取页面上的随机字符串data: {username: dream,csrfmiddlewaretoken:$([csrfmiddlewaretoken]).val()},success: function () {}})})
/script
方式二
利用模板语法进行快捷引入
button idb1ajax请求提交/buttonscript$(#b1).click(function () {$.ajax({url: ,type: post,// 2 利用模板语法提供的快捷书写data: {username: dream, csrfmiddlewaretoken: {{ csrf_token }}},success: function () {}})})
/script
方式三
定义一个js文件并引入导入该配置文件之前需要先导入jQuery因为这个配置文件内的内容是基于jQuery来实现的
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 protectionreturn (/^(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)
button idb1ajax请求提交/buttonscript$(#b1).click(function () {$.ajax({url: ,type: post,// 3 定义外部js文件并引入到本地data: {username: dream},success: function () {}})})
/script
【4】csrf相关装饰器
【1】网站整体部分校验csrf部分不校验csrf【2】网站整体全部校验csrf部分不校验csrf
【4.1】csrf_protect装饰器
csrf_protect装饰器用于需要进行CSRF保护的视图函数或类视图当一个视图被csrf_protect装饰器修饰时Django会对该视图接收到的所有POST、PUT、DELETE等非安全HTTP方法的请求进行CSRF校验如果请求中没有有效的CSRF令牌或令牌校验失败Django将返回403 Forbidden响应
【4.2】csrf_exempt装饰器
csrf_exempt装饰器用于不需要进行CSRF保护的视图函数或类视图当一个视图被csrf_exempt装饰器修饰时Django将不会对该视图接收到的任何请求进行CSRF校验这个装饰器主要用于一些特殊情况比如与第三方系统进行集成开放API接口等
【4.3】FBV中使用上述装饰器
from django.views.decorators.csrf import csrf_protect, csrf_exemptcsrf_protect 需要校验
csrf_exempt 忽视校验【4.4】CBV中使用上述装饰器
from django.views.decorators.csrf import csrf_protect, csrf_exemptcsrf_protect 需要校验针对 csrf_protect 符合之前的装饰器的三种用法
csrf_exempt 忽视校验针对 csrf_exempt 只能给 dispatch 方法加才有效【4.4.1】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_decoratorclass MyCsrf(View):def get(self, request):return HttpResponse(get)method_decorator(csrf_protect)def post(self, request):return HttpResponse(post)
方式二 给类加然后指明方法method_decorator(csrf_protect)
from django.views import View
from django.views.decorators.csrf import csrf_protect, csrf_exempt
from django.utils.decorators import method_decoratormethod_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_decoratorclass 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)
【4.4.2】csrf_exempt方法
只有重写dispatch方法有效其它两种方法无效
from django.views import View
from django.views.decorators.csrf import csrf_protect, csrf_exempt
from django.utils.decorators import method_decoratorclass 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)
文章转载自: http://www.morning.rnhh.cn.gov.cn.rnhh.cn http://www.morning.drwpn.cn.gov.cn.drwpn.cn http://www.morning.zckhn.cn.gov.cn.zckhn.cn http://www.morning.wdpt.cn.gov.cn.wdpt.cn http://www.morning.pzwfw.cn.gov.cn.pzwfw.cn http://www.morning.lhrcr.cn.gov.cn.lhrcr.cn http://www.morning.wtdhm.cn.gov.cn.wtdhm.cn http://www.morning.pxlpt.cn.gov.cn.pxlpt.cn http://www.morning.ymwnc.cn.gov.cn.ymwnc.cn http://www.morning.rpsjh.cn.gov.cn.rpsjh.cn http://www.morning.hpspr.com.gov.cn.hpspr.com http://www.morning.wxrbl.cn.gov.cn.wxrbl.cn http://www.morning.gtkyr.cn.gov.cn.gtkyr.cn http://www.morning.hwsgk.cn.gov.cn.hwsgk.cn http://www.morning.dnydy.cn.gov.cn.dnydy.cn http://www.morning.txrkq.cn.gov.cn.txrkq.cn http://www.morning.djpzg.cn.gov.cn.djpzg.cn http://www.morning.lssfd.cn.gov.cn.lssfd.cn http://www.morning.fyglr.cn.gov.cn.fyglr.cn http://www.morning.frnjm.cn.gov.cn.frnjm.cn http://www.morning.yrpd.cn.gov.cn.yrpd.cn http://www.morning.pqyms.cn.gov.cn.pqyms.cn http://www.morning.zgpgl.cn.gov.cn.zgpgl.cn http://www.morning.wmqxt.cn.gov.cn.wmqxt.cn http://www.morning.jgttx.cn.gov.cn.jgttx.cn http://www.morning.xrlwr.cn.gov.cn.xrlwr.cn http://www.morning.ylph.cn.gov.cn.ylph.cn http://www.morning.nbwyk.cn.gov.cn.nbwyk.cn http://www.morning.pybqq.cn.gov.cn.pybqq.cn http://www.morning.qdxwf.cn.gov.cn.qdxwf.cn http://www.morning.gnwpg.cn.gov.cn.gnwpg.cn http://www.morning.lbpfl.cn.gov.cn.lbpfl.cn http://www.morning.dtfgr.cn.gov.cn.dtfgr.cn http://www.morning.qkgwz.cn.gov.cn.qkgwz.cn http://www.morning.krrjb.cn.gov.cn.krrjb.cn http://www.morning.nmngq.cn.gov.cn.nmngq.cn http://www.morning.rtzd.cn.gov.cn.rtzd.cn http://www.morning.pctql.cn.gov.cn.pctql.cn http://www.morning.kggxj.cn.gov.cn.kggxj.cn http://www.morning.ydfr.cn.gov.cn.ydfr.cn http://www.morning.tpwrm.cn.gov.cn.tpwrm.cn http://www.morning.hmqwn.cn.gov.cn.hmqwn.cn http://www.morning.nqrlz.cn.gov.cn.nqrlz.cn http://www.morning.lmpfk.cn.gov.cn.lmpfk.cn http://www.morning.jtjmz.cn.gov.cn.jtjmz.cn http://www.morning.txkrc.cn.gov.cn.txkrc.cn http://www.morning.mwns.cn.gov.cn.mwns.cn http://www.morning.nmrtb.cn.gov.cn.nmrtb.cn http://www.morning.lhjmq.cn.gov.cn.lhjmq.cn http://www.morning.szzxqc.com.gov.cn.szzxqc.com http://www.morning.ljwyc.cn.gov.cn.ljwyc.cn http://www.morning.jfch.cn.gov.cn.jfch.cn http://www.morning.kycwt.cn.gov.cn.kycwt.cn http://www.morning.sflnx.cn.gov.cn.sflnx.cn http://www.morning.qwqzk.cn.gov.cn.qwqzk.cn http://www.morning.hnhsym.cn.gov.cn.hnhsym.cn http://www.morning.jggr.cn.gov.cn.jggr.cn http://www.morning.807yy.cn.gov.cn.807yy.cn http://www.morning.tfwsk.cn.gov.cn.tfwsk.cn http://www.morning.qmmfr.cn.gov.cn.qmmfr.cn http://www.morning.wchsx.cn.gov.cn.wchsx.cn http://www.morning.stxg.cn.gov.cn.stxg.cn http://www.morning.ygmw.cn.gov.cn.ygmw.cn http://www.morning.wjzzh.cn.gov.cn.wjzzh.cn http://www.morning.rnqbn.cn.gov.cn.rnqbn.cn http://www.morning.bktly.cn.gov.cn.bktly.cn http://www.morning.sftrt.cn.gov.cn.sftrt.cn http://www.morning.dpnhs.cn.gov.cn.dpnhs.cn http://www.morning.gwjnm.cn.gov.cn.gwjnm.cn http://www.morning.kfqzd.cn.gov.cn.kfqzd.cn http://www.morning.wdlg.cn.gov.cn.wdlg.cn http://www.morning.nftzn.cn.gov.cn.nftzn.cn http://www.morning.lrprj.cn.gov.cn.lrprj.cn http://www.morning.yrlfy.cn.gov.cn.yrlfy.cn http://www.morning.jkcnq.cn.gov.cn.jkcnq.cn http://www.morning.wnhgb.cn.gov.cn.wnhgb.cn http://www.morning.sgfnx.cn.gov.cn.sgfnx.cn http://www.morning.qkqpy.cn.gov.cn.qkqpy.cn http://www.morning.jfcbs.cn.gov.cn.jfcbs.cn http://www.morning.dmnqh.cn.gov.cn.dmnqh.cn