首页 技术 正文
技术 2022年11月22日
0 收藏 957 点赞 4,850 浏览 1606 个字

风炫安全WEB安全学习第二十七节课 XSS的防御措施

XSS防御措施

总的原则

控制好输入/输出

  • 过滤:根据业务需求进行过滤,对email,手机号码这样的输入框进行验证。
  • 转义:所有输出到前端的数据都根据输出点进行转义,对输入的字符串进行html实体化编码

PHP

php可以用以下函数来防御

htmlspecialchars();
htmlentities();
HTMLPurifier.auto.php插件 http://htmlpurifier.org/download
RemoveXss函数 https://gist.github.com/aligundogdu/1839051

Python

python库

>>> import cgi
>>> cgi.escape('<script>&"', quote=True)
'&lt;script&gt;&amp;&quot;'
>>> HTMLParser.unescape.__func__(HTMLParser, '&lt;script&gt;&amp;&quot;')
u'<script>&"'
import bleach
from bleach.sanitizer import ALLOWED_TAGS,ALLOWED_ATTRIBUTES@require_http_methods(['POST'])defmessage(request):# 从客户端中获取提交的数据
content = request.POST.get('content') # 在默认的允许标签中添加img标签
tags = ALLOWED_TAGS + ['img']
# 在默认的允许属性中添加src属性
attributes = {**ALLOWED_ATTRIBUTES,'img':['src']} # 对提交的数据进行过滤
cleaned_content=bleach.clean(content,tags=tags,attributes=attributes) # 保存到数据库中
Message.objects.create(content=cleaned_content) return redirect(reverse('index'))

JAVA

pom.xml

首先添加一个jar包:commons-lang-2.5.jar ,然后在后台调用这些函数:

StringEscapeUtils.escapeHtml(string);
StringEscapeUtils.escapeJavaScript(string);
StringEscapeUtils.escapeSql(string);<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.5</version>
</dependency>

或者自己编写一个XSSFilter静态类,可以直接调用

JavaScript

  • 设置Httponly

  • 不要动态输出用户的输入

  • 不要使用eval函数解析json,用JSON.parse()

    前后端分离

    eval('(' + jsonstr + ')')  #这种方式 是不严谨的解析JSON.parse()
  • https://github.com/leizongmin/js-xss

    <script src="https://rawgit.com/leizongmin/js-xss/master/dist/xss.js"></script>
    <script>
    // 使用函数名 filterXSS,用法一样
    var html = filterXSS('<script>alert("xss");</scr' + 'ipt>');
    alert(html);
    </script>

参考:

http://blog.evalshell.com/2020/12/20/风炫安全web安全学习第二十七节课-xss的防御措施/

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