[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 ...
随机推荐
- bzoj3887: [Usaco2015 Jan]Grass Cownoisseur
题意: 给一个有向图,然后选一条路径起点终点都为1的路径出来,有一次机会可以沿某条边逆方向走,问最多有多少个点可以被经过?(一个点在路径中无论出现多少正整数次对答案的贡献均为1) =>有向图我们 ...
- hdu-1879 继续畅通工程---确定部分边的MST
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1879 题目大意: 省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的 ...
- Android(java)学习笔记81:在TextView组件中利用Html插入文字或图片
1. TextView中利用Html插入文字或者图片: 首先我们看看代码: (1)activity_main.xml: <LinearLayout xmlns:android="htt ...
- 解决wget下载https时报错 --no-check-certificate (不检查证书)
如果使用 wget下载https开头的网址域名 时报错,你需要加上 --no-check-certificate (不检查证书)选项 例如: wget https://pypi.python.org/ ...
- CUDA中记录执行时间-GPU端
事件eventcudaEvent_t start,stop;cudaEventCreate(&start);cudaEventCreate(&stop);cudaEventRecord ...
- 新装Ubuntu后的一些配置
一:Ubuntu 16.04 开启root用户和使用root用户登陆 1. 编辑/etc/lightdm/lightdm.conf autologin-guest=false autologin-us ...
- phpstorm —— Xdebug 的配置和使用
给phpstorm 配置Xdebug(Xdebug 是 PHP 的一个扩展, 用于帮助调试和开发.它包含一个与 ide 一起使用的单步调试器.它升级了 PHP 的 var_dump () 功能) 这篇 ...
- linux 下nginx除了首页404的问题
今天在部署tp5的时候除了首页能访问.其他都是not found 原因是 Nginx服务器默认不支持pathinfo,index.php后面的参数都没带上 在需要pathinfo支持的程序中 则无 ...
- Q&A - ABTesting是啥?
举个简单的例子,当你有一个日IP过千的网站,而你的网站首页几百年没有更改了,这个时候你想启用新的网页,而你有害怕新的页面用户不一定就非常喜欢,那么这个时候你就需要进行A/B测试了.测试的方法是将老页面 ...
- 调用python-nmap实现扫描局域网存活主机
使用环境:Raspberry 3b+ +netifaces+python-nmap+nmap 调用netifaces自动获取ip地址: def get_gateways(): return netif ...