首页 技术 正文
技术 2022年11月14日
0 收藏 770 点赞 3,139 浏览 2888 个字

一、什么是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  views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/', views.index),
url(r'^comment/',views.comment),
]

————————————######################_——————————-

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

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})

views.py

<!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>

index.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>

comment.html

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,997
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,511
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,356
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,139
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848