[oldboy-django][2深入django]xss攻击 + csrf
1 xss攻击
xss攻击(跨站脚本攻击,用户页面提交数据来盗取cookie)
- 慎用safe, 和mark_safe
-- 如果要用,必须要过滤 - 定义:
用户提交内容,在页面展示用html显示的时候
页面提交一些script脚本,盗取cookie # views
msg = []
def xss(request):
if request.method == "GET":
return render(request, 'xss.html')
else:
tmp = request.POST.get('content')
msg.append(tmp)
print(tmp)
return render(request, 'xss.html') def content_index(request):
print(msg)
return render(request, 'content_index.html', {'msg':msg}) # templates
<body>
<h2>评论</h2>
<form action="/xss.html" method="POST">
<input type="text" placeholder="请输入评论" name="content">
<input type="submit" value="提交">
</form>
</body> <body>
<h2>评论</h2>
{% for item in msg %}
<div>{{ item }}</div>
{# <div>{{ item|safe }}</div>#}
{# django已经阻止xss攻击了,#}
{# 如果使用item|safe,那就麻烦了:此时用户提交评论<script>alert()</script>,#}
{# 就会弹出框123(真正的做法是获取cookie,然后别人就可以用你的账户登录网站了,那时候就麻烦)#}
{% endfor %}
</body>
2 csrf
csrf(跨站网站请求伪造)
- 情景: 盗取方自己建立一个网站,上面有一个图片链接
<a href="http://www.cmbchina.com/?to=652222287448748&money=1999999><img></img></a> 上面的链接是招商银行的转账链接, 如果招商银行用户在自己网页登录了招商银行,在没有退出的情况下,
点击上面的图片,就会自动转账到用户6522222874487848,1999999金额 招商银行解决办法:
采用POST,需要自己输入表单
盗取方同样有方法
<form>
<input type='text' name='to' value='' style='display:none'>
<input type='text' name='money' value='' style='display:none'>
<a onclick="ff()"> <img></img>
</a>
</form> 采用csrf方法,
用户登录后,点击转账按钮,GET请求到转账页面,招商银行后台会一个随机字符串写在页面上,
然后提交表单的时候,同时将这个字符串提交给后台 {% crsf_token %};当然这个字符串是隐藏的 - 使用方式
a. 基本应用(解除settings.py)
'django.middleware.csrf.CsrfViewMiddleware'
在表单上{% crsf_token %} b. 全部禁用
#'django.middleware.csrf.CsrfViewMiddleware' c. 局部禁用
- 'django.middleware.csrf.CsrfViewMiddleware' - 对test函数禁用csrf
from django.views.decorators.csrf import csrf_exempt
@csrf_exempt
def test(request):
pass - 对类Foo所有函数禁用
from django.views.decorators.csrf import csrf_exempt
from django.utils.decorators import method_decorator @method_decorator(csrf_exempt, name='dispatch')
def test(request):
pass d. 局部使用
- #'django.middleware.csrf.CsrfViewMiddleware' - 对test函数使用
from django.views.decorators.csrf import csrf_protect @csrf_protect
def test(request):
pass - 对类Foo所有函数使用(只支持这种)
from django.views.decorators.csrf import csrf_protect
from django.utils.decorators import method_decorator @method_decorator(csrf_protect, name='dispatch')
def test(request):
pass
2 补充csrf攻击原理:
2 补充csrf防范机制
3 补充cbv添加装饰器的方法
ps CBV添加装饰器方法
- 定义装饰器
def wrapper(func):
def inner(*args, **kwargs):
return func(*args, **kwargs)
return inner - 导入django使用FBV装饰器的方法
from django.utils.decorators import method_decorator - 添加装饰器方式
. 在类里面的多个函数添加
class Foo(View): @method_decorator(wrapper)
def get(self,request):
pass @method_decorator(wrapper)
def post(self,request):
pass .对类的多个函数都添加装饰器
@method_decorator(wrapper, name='post')
@method_decorator(wrapper, name='get')
class Foo(View): .对类所有函数添加装饰器
@method_decorator(wrapper, name='dispatch')
class Foo(View): # 请求来了,到dispatch函数,dispatch根据反射调用不同的函数
4 如何将csrf中隐藏的字符串发给后台(form表单 + ajax请求)
4.1 form表单
<form action="/csrf1.html" method="POST">
{% csrf_token %}
{# # 如果没有这句,会报错#}
<input type="text" name="username" placeholder="用户名" id="user">
<input type="password" name="pwd" placeholder="密码" >
<input type="submit" value="form提交">
</form>
4.2 ajax提交
- csrf(ajax请求) a. ajax发送crsf, 数据携带csrf
<form action="/csrf1.html" method="POST">
{% csrf_token %}
<input type="text" name="username" placeholder="用户名" id="user">
<input type="password" name="pwd" placeholder="密码" >
<a onclick="submit_ajax()">ajax提交</a>
</form> function submit_ajax() {
var csrf_token = $('input[name="csrfmiddlewaretoken"]').val();
var user = $('#user').val();
$.ajax({
url: '/csrf1.html',
type: 'POST',
data: {'username': user, 'csrfmiddlewaretoken': csrf_token},
success: function (arg) {
console.log(arg)
}
})
} b. ajax发送csrf, 消息头携带cookie的csrf
获取cookie里面的csrftoken, 并在请求头里面设置X-CSRFToken: csrftoken
前端js获取cookie,需要引入插件jquery.cookie.js function submit_ajax() {
var token = $.cookie('csrftoken');
var user = $('#user').val();
$.ajax({
url: '/csrf1.html',
type: 'POST',
headers:{'X-CSRFToken': token},
data: {'username': user},
success: function (arg) {
console.log(arg)
}
}) }
5 补充csrf基本使用方法
- 补充csrf的基本使用方式 # 'django.middleware.csrf.CsrfViewMiddleware' 在视图函数上
from django.views.decorators.csrf import csrf_protect
@csrf_protect
def crsf1(request):
pass 在csrf1.html中的form表单中
添加{% crsf_token %} 否则会报以下错误 英文解析Forbidden ()
CSRF verification failed. Request aborted.
You are seeing this message because this site requires a CSRF cookie when submitting forms. This cookie is required for security reasons, to ensure that your browser is not being hijacked by third parties.
If you have configured your browser to disable cookies, please re-enable them, at least for this site, or for 'same-origin' requests.
Help
Reason given for failure:
CSRF cookie not set. 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.
[oldboy-django][2深入django]xss攻击 + csrf的更多相关文章
- 第三百九十二节,Django+Xadmin打造上线标准的在线教育平台—sql注入攻击,xss攻击,csrf攻击
第三百九十二节,Django+Xadmin打造上线标准的在线教育平台—sql注入攻击,xss攻击,csrf攻击 sql注入攻击 也就是黑客通过表单提交的地方,在表单里输入了sql语句,就是通过SQL语 ...
- Django是如何防止注入攻击-XSS攻击-CSRF攻击
注入攻击-XSS攻击-CSRF攻击介绍请访问:https://www.cnblogs.com/hwnzy/p/11219475.html Django防止注入攻击 Django提供一个抽象的模型层来组 ...
- XSS攻击&CSRF攻击 ----Django解决方案
XSS攻击: XSS又叫CSS (Cross Site Script) ,跨站脚本攻击.它指的是恶意攻击者往Web页面里插入恶意html代码,当用户浏览该页之时,嵌入其中Web里面的html代码会被执 ...
- Django项目开发,XSS攻击,图片防盗链,图片验证码,kindeditor编辑器
目录 一.Django项目开发 1. 项目开发流程 2. auth模块的补充 (1)django的admin可视化管理页面 (2)将admin可视化管理页面的模型表显示成中文 (3)auth模块的用户 ...
- XSS攻击 CSRF攻击
XSS攻击: 跨站脚本攻击(Cross Site Scripting),为不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆, 故将跨站脚本攻击缩写为XSS.恶意攻击者 ...
- XSS攻击 && CSRF攻击 基础理解
一个网站,不管多么的帅气,多么的风骚,如果你不安全,那始终都是一个弟弟啊~ 今天又看了下XSS和CSRF攻击的文章,我也想发点什么普及下大家的安全意识,毕竟作为一名拥有伟大梦想的程序员,基本的安全意识 ...
- 注入攻击-XSS攻击-CSRF攻击
1.注入攻击 注入攻击包括系统命令注入,SQL注入,NoSQL注入,ORM注入等 1.1攻击原理 在编写SQL语句时,如果直接将用户传入的数据作为参数使用字符串拼接的方式插入到SQL查询中,那么攻击者 ...
- XSS和CSRF的理解
声明:转自 http://www.cnblogs.com/hyddd/archive/2009/04/09/1432744.html XSS攻击:跨站脚本攻击(Cross Site Scripting ...
- Django—XSS及CSRF
一.XSS 跨站脚本攻击(Cross Site Scripting),为不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS.恶意攻击者往W ...
随机推荐
- hiho一下 第三十七周 二分查找之k小数
题目链接:http://hihocoder.com/contest/hiho37/problem/1 , 简单二分. 算法: 题目即为求一个无序数组数组中第k小的数,由于数据量太大,排序是会超时的. ...
- EOS签名R值过大导致报错"is_canonical( c ): signature is not canonical"
简要 EOS中规定签名的R和S必须同时小于N/2才是合法的签名. 详细 EOS签名交易相对BTC和ETH来说,对签名的要求更加严格了. BTC中bip62规定了((Low S values in si ...
- Mac终端下使用***
首先安装proxychains: brew install proxychains-ng 然后创建文件~/.proxychains/proxychains.conf,写入以下内容: strict_ch ...
- [VC]strcpy memcpy memset区别与简介
strcpy 原型:extern char *strcpy(char *dest,char *src); 用法:#include <string.h> 功能:把src所指由NULL结束的字 ...
- IOS 绘制画画板(封装上下文)
封装上下文 UIImage (CaptureView).h / .m @interface UIImage (CaptureView) + (UIImage *)captureImageWithVie ...
- java中list强转为map类型
起因:读取数据库文件的测试用例,测试用例需要存放到一个map中,方便下次调用, 读取的内容返回的内容存放在一个list中,并且数据内容是key=value的形式,最开始使用切片方式,做了很多无用功,后 ...
- hdu-1856 More is better---带权并查集
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1856 题目大意: 一个并查集 计算每个集合的元素 找出元素最多的那个集合,输出元素的个数 解题思路: ...
- BSGS算法初探
前言 \(BSGS\)算法,全称\(Baby\ Step\ Giant\ Step\),即大小步算法.某些奆佬也称其为拔(Ba)山(Shan)盖(Gai)世(Shi)算法. 它的主要作用是求解形式如\ ...
- 2017.12.14 Java实现-----图书管理系统
通过对图书的增删改查操作 用数组实现 Manager类 package demo55; import java.util.*; public class Manager { Scanner sc = ...
- DeepLearning tutorial(3)MLP多层感知机原理简介+代码详解
本文介绍多层感知机算法,特别是详细解读其代码实现,基于python theano,代码来自:Multilayer Perceptron,如果你想详细了解多层感知机算法,可以参考:UFLDL教程,或者参 ...