Django SCRF跨站点请求伪造
使用Django发POSTt请求的时候经常会遇到Forbidden的错误,然后直接了当的方法就是去setting里面吧csrf中间件注释掉,其实csrf是django给我们提供的防护措施.
CSRF就是一种攻击方式,原理大概是你去A网站登录后本地留下了A网站的cookie,然后去B网站访问收到了CSRF的攻击,拿到了你A网站的cookie,然后攻击者用这个cookie去请求A网站,盗取你的信息财物等.
具体这个博客写得很好:戳CSRF
这里我们主要讲django 中csrf防护的原理和设置.
1.csrf防护在Django中就是一个中间件,生成token验证,token验证就是你要拿到一张牌(一串字符串),django认识这个字符串,才让你访问.
#django settings.py 里面
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',#就是这货
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
2.Form请求中的csrf:
我们写好一盒form表单,提交方式写为post的时,点提交会出现以下页面:
Forbidden (403) CSRF verification failed. Request aborted. Help Reason given for failure: CSRF token missing or incorrect. In general, this can occur when there is a genuine Cross Site Request Forgery, or when Django's CSRF mechanism has not been used correctly. For POST forms, you need to ensure: Your browser is accepting cookies. The view function passes a request to the template's render method. In the template, there is a {% csrf_token %} template tag inside each POST form that targets an internal URL. If you are not using CsrfViewMiddleware, then you must use csrf_protect on any views that use the csrf_token template tag, as well as those that accept the POST data. The form has a valid CSRF token. After logging in in another browser tab or hitting the back button after a login, you may need to reload the page with the form, because the token is rotated after a login. You're seeing the help section of this page because you have DEBUG = True in your Django settings file. Change that to False, and only the initial error message will be displayed. You can customize this page using the CSRF_FAILURE_VIEW setting.
错误中也说明了解决方式:在form表单中加入{% csrf_token %}:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/fm001/myCSRF/" method="post">
name: <input type="text">
<input type="submit" value="提交">
{% csrf_token %} </form>
</body>
</html>
我们再发post请求就可以了,检查发现{% csrf_token %} 在页面上生成了这串东西, 我们提交请求的时候会带着这个东西去请求,而这个东西就是django给我的,django会识别是不是这货

3:Ajax请求中的csrf:
写好html页面:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="/static/jquery-3.2.1.min.js"></script>
</head>
<body>
<form action="/fm001/myCSRF/" method="post">
name: <input type="text">
<input type="submit" value="提交"> <input id="but" type="button" value="ajax提交">
</form>
</body>
<script>
$("#but").click(function () {
$.ajax({
url:"/fm001/myCSRF/",
type:"POST",
data:{"xx":"yy"},
success:function (arg) {
alert("ok")
} })
})
</script>
</html>
提交后出现如下错误:Forbidden (CSRF token missing or incorrect.): /fm001/myCSRF/
这就是我们没有提交token给django,需要在请求头里面加上token值,用jq获取cookie,所以导入jquery.cookie.js 具体:
<script>
$("#but").click(function () {
$.ajax({
url:"/fm001/myCSRF/",
type:"POST",
data:{"xx":"yy"},
headers:{"X-CSRFToken":$.cookie("csrftoken")},
success:function (arg) {
alert("ok")
} })
})
</script>
这样提交就可以了.
为了方便,不每次发ajax请求都去写headers,可以设置ajax请求每次发送之前加上添加token到headers的操作:
$.ajaxSetup({
beforeSend: function(xhr, settings) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
});
当然的django中也可以试着那些请求需要token,那些不需要,django提供了装饰器:
from dajngo.views.decorators.csrf import csrf_protect,csrf_exempt
@csrf_protect,#为当前函数强制设置防跨站请求伪造功能,即便settings中没有设置全局中间件。加载views函数上就可以了.
@csrf_exempt,#取消当前函数防跨站请求伪造功能,即便settings中设置了全局中间件。
Django SCRF跨站点请求伪造的更多相关文章
- Python Django框架笔记(四):数据分页和CSRF跨站点请求伪造
(一)数据分页 可以参考 https://docs.djangoproject.com/en/2.0/topics/pagination/ 模板:如果只要显示 1.2.3.4.5.6....的话, ...
- 密码学系列之:csrf跨站点请求伪造
目录 简介 CSRF的特点 CSRF的历史 CSRF攻击的限制 CSRF攻击的防范 STP技术 Cookie-to-header token Double Submit Cookie SameSite ...
- 跨站点请求伪造(CSRF)学习
一.CSRF介绍 伪造一个站点,在站点中伪造一个向其他站点的请求,在用户访问该站点时让用户执行 假设有如下URL能删除一篇文章: 攻击者在自己的域中构造一个页面: 内容为: 使用一个img标签,其地址 ...
- asp.net mvc 安全测试漏洞 "跨站点请求伪造" 问题解决
IBM Security Appscan漏洞筛查-跨站请求伪造,该漏洞的产生,有多种情况: 1.WebApi的跨站请求伪造,需要对WebApi的请求头部做限制(此文不做详细介绍): 2.MVC Act ...
- python---xss(Cross Site Scripting)跨站脚本攻击和csrf(xsrf)跨站点请求伪造(Cross—Site Request Forgery)攻击
xss跨站脚本攻击:恶意攻击者往Web页面里插入恶意Script代码,当用户浏览该页之时,嵌入其中Web里面的Script代码会被执行,从而达到恶意攻击用户的目的. 例如:某些论坛允许用户自由发言,而 ...
- 跨站点请求伪造(CSRF)
一.前言 跨站点请求伪造(Cross-SiteRequest Forgeries, CSRF),是指攻击者通过设置好的陷阱,强制对已完成认证的用户进行非预期的个人信息或设定信息等某些状态更新,属于被动 ...
- web客户端安全之跨站点请求伪造攻击
CSRF攻击,Cross-site request forgery,跨站点请求伪造,也被称为“One Click Attack”或者Session Riding,通常缩写为CSRF或者XSRF,是一种 ...
- [不常用] - CSRF(跨站点请求伪造)
CSRF,Cross Site Request Forgery,即跨站点请求伪造. 这种攻击是指,在用户正常登录系统以后,攻击者诱使用户访问一些非法链接,以执行一些非法操作. 比如:如果删除用户操 ...
- Appscan漏洞之跨站点请求伪造(CSRF)
公司前段时间使用了Fortify扫描项目代码,在修复完这些Fortify漏洞后,最近又启用了Appscan对项目代码进行漏洞扫描,同样也是安排了本人对这些漏洞进行修复.现在,针对修复过的Appscan ...
随机推荐
- ActiveMq报错Channel was inactive for too (>30000)long
生成环境的activemq 隔一到两周,就报错: 查看 activeme的日志: 2018-12-04 11:59:44,744 | WARN | Transport Connection to: ...
- [蓝桥杯]PREV-26.历届试题_最大子阵
问题描述 给定一个n*m的矩阵A,求A中的一个非空子矩阵,使这个子矩阵中的元素和最大. 其中,A的子矩阵指在A中行和列均连续的一块. 输入格式 输入的第一行包含两个整数n, m,分别表示矩阵A的行数和 ...
- TCP/IP学习20180805-数据链路层-IP选路
转,原文链接https://blog.csdn.net/wh1511995112/article/details/51474692 IP选路 什么是IP选路? IP选路,即IP寻路,就是根据路由表中的 ...
- [ZZ]MTSC 2017 Mobile Testing@Google 演讲的感受
原文地址: https://testerhome.com/topics/9364 Mobile Testing@Google 其实在开始听谷歌的张南和潘岩开始演讲前,了解下 Google Test C ...
- Android Studio 1.0~3.3加载android源码 笔记
一. AS3.3上出现问题: 1. File Z:\Project\****\***\AndroidManifest.xml doesnt exist 分析引用: ------------------ ...
- vue2.9.5 引入vue-strap时报错
1.vue2.9.5 引入vue-strap时出错 2.组件中引入vue-strap的具体代码如下: 1 import DatePicker from 'vue-strap/src/Datepicke ...
- back-to-top回到顶部
function backTop(back) { back.hide(); $(window).scroll(function () { $(window).scrollTop() > 0 ? ...
- Redis单线程架构
参考链接: http://blog.csdn.net/qqqqq1993qqqqq/article/details/77538202 单线程模型: redis中的数据结构并不全是简单的kv,还有lis ...
- ExcelPackage 读取、导出excel
private static string GetString(object obj) { try { return obj.ToString(); } catch (Exception ex) { ...
- Eclipse Tomcat部署web项目时出现There are no resources that can be added or removed from the server解决办法
问题原因是:tomcat版本和java版本不匹配.