您的位置:首页技术文章
文章详情页

Django如何实现防止XSS攻击

【字号: 日期:2024-09-20 15:06:43浏览:10作者:猪猪

一、什么是XSS攻击

xss攻击:----->web注入

  xss跨站脚本攻击(Cross site script,简称xss)是一种“HTML注入”,由于攻击的脚本多数时候是跨域的,所以称之为“跨域脚本”。

我们常常听到“注入”(Injection),如SQL注入,那么到底“注入”是什么?注入本质上就是把输入的数据变成可执行的程序语句。SQL注入是如此,XSS也如此,只不过XSS一般注入的是恶意的脚本代码,这些脚本代码可以用来获取合法用户的数据,如Cookie信息。

PS: 把用户输入的数据以安全的形式显示,那只能是在页面上显示字符串。

django框架中给数据标记安全方式显示(但这种操作是不安全的!):

 - 模版页面上对拿到的数据后写上safe. ----> {{XXXX|safe}}  - 在后台导入模块:from django.utils.safestring import mark_safe

  把要传给页面的字符串做安全处理 ----> s = mark_safe(s)

二、测试代码

实施XSS攻击需要具备两个条件:

一、需要向web页面注入恶意代码;

二、这些恶意代码能够被浏览器成功的执行。

解决办法:

1、一种方法是在表单提交或者url参数传递前,对需要的参数进行过滤。

2、在后台对从数据库获取的字符串数据进行过滤,判断关键字。

3、设置安全机制。

django框架:内部机制默认阻止了。它会判定传入的字符串是不安全的,就不会渲染而以字符串的形式显示。如果手贱写了safe,那就危险了,若想使用safe,那就必须在后台对要渲染的字符串做过滤了。所以在开发的时候,一定要慎用安全机制。尤其是对用户可以提交的并能渲染的内容!!!

这里是不存在xss漏洞的写法,因为django已经做了防攻击措施

index.html

<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>Title</title></head><body><h1>评论</h1>{% for item in msg %}{# <div>{{ item|safe }}</div>#} #这里被注释的,是因为,|safe 加了这个就认为是安全的了,写入 <script> alert(123)</script> 就会恶意加载 <div>{{ item}}</div>{% endfor %}</body></html>

conment.html

<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>Title</title></head><body><form action='/comment/' method='POST'> <input type='text' name='content'> <input type='submit' value='提交'></form></body></html>

views.py

from django.shortcuts import render,HttpResponse# Create your views here.msg = []def comment(request): if request.method == 'GET': return render(request,'comment.html') else: v = request.POST.get('content') msg.append(v) return render(request,'comment.html')def index(request): return render(request,'index.html',{'msg':msg})########################################################def test(request): from django.utils.safestring import mark_safe temp = '<a href=’http://www.baidu.com’>百度</a>' newtemp = mark_safe(temp) #这里相当于加了 |safe ,把字符串认为是安全的,执行代码,如果不加 test.html里面 {{ temp }} 就只会显示出字符串,而不是 a 标签 return render(request,’test.html’,{’temp’:newtemp})

urls.py

from app01 import viewsurlpatterns = [ url(r’^admin/’, admin.site.urls), url(r’^index/’, views.index), url(r’^comment/’,views.comment),]

------------------------------------######################_-------------------------------

以下是做了用户输入判断,检测是否有特殊字符

views.py

from django.shortcuts import render,HttpResponse# Create your views here.msg = []def comment(request): if request.method == 'GET': return render(request,'comment.html') else: v = request.POST.get('content') if 'script' in v: return render(request, 'comment.html',{’error’:’小比崽子’}) else: msg.append(v) return render(request,’comment.html’)def index(request): return render(request,'index.html',{'msg':msg})

index.html

<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>Title</title></head><body><h1>评论</h1>{% for item in msg %} <div>{{ item|safe }}</div>{# <div>{{ item}}</div>#}{% endfor %}</body></html>

comment.html

<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>Title</title></head><body><form action='/comment/' method='POST'> <input type='text' name='content'> <input type='submit' value='提交'>{{ error }}</form></body></html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持好吧啦网。

标签: Django
相关文章: