xss过滤方法
用的白名单过滤,是我们的论坛自用的方法,也许考虑不周,欢迎来黑我们的论坛!
https://www.ebcms.com/forum.html
// 安全过滤
function safe_html($html){
$elements = [
'html' => [],
'body' => [],
'a' => ['target', 'href', 'title', 'class', 'style'],
'abbr' => ['title', 'class', 'style'],
'address' => ['class', 'style'],
'area' => ['shape', 'coords', 'href', 'alt'],
'article' => [],
'aside' => [],
'audio' => ['autoplay', 'controls', 'loop', 'preload', 'src', 'class', 'style'],
'b' => ['class', 'style'],
'bdi' => ['dir'],
'bdo' => ['dir'],
'big' => [],
'blockquote'=> ['cite', 'class', 'style'],
'br' => [],
'caption' => ['class', 'style'],
'center' => [],
'cite' => [],
'code' => ['class', 'style'],
'col' => ['align', 'valign', 'span', 'width', 'class', 'style'],
'colgroup' => ['align', 'valign', 'span', 'width', 'class', 'style'],
'dd' => ['class', 'style'],
'del' => ['datetime'],
'details' => ['open'],
'div' => ['class', 'style'],
'dl' => ['class', 'style'],
'dt' => ['class', 'style'],
'em' => ['class', 'style'],
'font' => ['color', 'size', 'face'],
'footer' => [],
'h1' => ['class', 'style'],
'h2' => ['class', 'style'],
'h3' => ['class', 'style'],
'h4' => ['class', 'style'],
'h5' => ['class', 'style'],
'h6' => ['class', 'style'],
'header' => [],
'hr' => [],
'i' => ['class', 'style'],
'img' => ['src', 'alt', 'title', 'width', 'height', 'id', 'class'],
'ins' => ['datetime'],
'li' => ['class', 'style'],
'mark' => [],
'nav' => [],
'ol' => ['class', 'style'],
'p' => ['class', 'style'],
'pre' => ['class', 'style'],
's' => [],
'section' => [],
'small' => [],
'span' => ['class', 'style'],
'sub' => ['class', 'style'],
'sup' => ['class', 'style'],
'strong' => ['class', 'style'],
'table' => ['width', 'border', 'align', 'valign', 'class', 'style'],
'tbody' => ['align', 'valign', 'class', 'style'],
'td' => ['width', 'rowspan', 'colspan', 'align', 'valign', 'class', 'style'],
'tfoot' => ['align', 'valign', 'class', 'style'],
'th' => ['width', 'rowspan', 'colspan', 'align', 'valign', 'class', 'style'],
'thead' => ['align', 'valign', 'class', 'style'],
'tr' => ['rowspan', 'align', 'valign', 'class', 'style'],
'tt' => [],
'u' => [],
'ul' => ['class', 'style'],
'video' => ['autoplay', 'controls', 'loop', 'preload', 'src', 'height', 'width', 'class', 'style'],
'embed' => ['src', 'height','align', 'width', 'class', 'style','type','pluginspage','wmode','play','loop','menu','allowscriptaccess','allowfullscreen'],
'source' => ['src', 'type']
];
$html = strip_tags($html,'<'.implode('><', array_keys($elements)).'>');
$xml = new \DOMDocument();
libxml_use_internal_errors(true);
if (!strlen($html)){
return '';
}
if ($xml->loadHTML('<meta http-equiv="Content-Type" content="text/html; charset=utf-8">' . $html)){
foreach ($xml->getElementsByTagName("*") as $element){
if (!isset($elements[$element->tagName])){
$element->parentNode->removeChild($element);
}else{
for ($k = $element->attributes->length - 1; $k >= 0; --$k) {
if (!in_array($element->attributes->item($k) -> nodeName, $elements[$element->tagName])){
$element->removeAttributeNode($element->attributes->item($k));
}elseif (in_array($element->attributes->item($k) -> nodeName, ['href','src','style','background','size'])) {
$_keywords = ['javascript:','javascript.:','vbscript:','vbscript.:',':expression'];
$find = false;
foreach ($_keywords as $a => $b) {
if (false !== strpos(strtolower($element->attributes->item($k)->nodeValue),$b)) {
$find = true;
}
}
if ($find) {
$element->removeAttributeNode($element->attributes->item($k));
}
}
}
}
}
}
$html = substr($xml->saveHTML($xml->documentElement), 12, -14);
$html = strip_tags($html,'<'.implode('><', array_keys($elements)).'>');
return $html;
}
当然 还有一种漏洞就是url权限操作链接,可能引起版主误操作。
xss过滤方法的更多相关文章
- XSS 防御方法总结
1. XSS攻击原理 XSS原称为CSS(Cross-Site Scripting),因为和层叠样式表(Cascading Style Sheets)重名,所以改称为XSS(X一般有未知的含义,还有扩 ...
- Asp.net Mvc中利用ValidationAttribute实现xss过滤
在网站开发中,需要注意的一个问题就是防范XSS攻击,Asp.net mvc中已经自动为我们提供了这个功能.用户提交数据时时,在生成Action参数的过程中asp.net会对用户提交的数据进行验证,一旦 ...
- XSS过滤JAVA过滤器filter 防止常见SQL注入
Java项目中XSS过滤器的使用方法. 简单介绍: XSS : 跨站脚本攻击(Cross Site Scripting),为不和层叠样式表(Cascading Style Sheets, CSS)的缩 ...
- 如何在springboot项目中进行XSS过滤
简单介绍 XSS : 跨站脚本攻击(Cross Site Scripting),为不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS.恶意 ...
- spring boot集成swagger,自定义注解,拦截器,xss过滤,异步调用,guava限流,定时任务案例, 发邮件
本文介绍spring boot集成swagger,自定义注解,拦截器,xss过滤,异步调用,定时任务案例 集成swagger--对于做前后端分离的项目,后端只需要提供接口访问,swagger提供了接口 ...
- python(Django之组合搜索、JSONP、XSS过滤 )
一.组合搜索 二.jsonp 三.xss过滤 一.组合搜索 首先,我们在做一个门户网站的时候,前端肯定是要进行搜索的,但是如果搜索的类型比较多的话,怎么做才能一目了然的,这样就引出了组合搜索的这个案例 ...
- 04: 使用BeautifulSoup封装的xss过滤模块
目录: 1.1 xss攻击简介 1.2 xss攻击解决方法 1.1 xss攻击简介返回顶部 1.简介 1. 跨站脚本(cross site script)为了避免与样式css混淆,所以简称为XSS. ...
- Python开发【Django】:组合搜索、JSONP、XSS过滤
组合搜索 做博客后台时,需要根据文章的类型做不同的检索 1.简单实现 关联文件: from django.conf.urls import url from . import views urlpat ...
- Bypass xss过滤的测试方法
0x00 背景 本文来自于<Modern Web Application Firewalls Fingerprinting and Bypassing XSS Filters>其中的byp ...
随机推荐
- IDEA "Library source does not match the bytecode for class"问题
问题描述 Jar包更新后,报错信息:"Library source does not match the bytecode for class" 经检查,发现Jar内容还是旧版本的 ...
- 网络分析:WireShark
安装 WireShark 官网 过滤器 类别 显示过滤器模式 捕获过滤器模式 逻辑表达式 and:&& or:|| 成组:() 过滤实例 仅监听某域名 http.host == &qu ...
- VS2015服务器资源管理器连接Mysql数据库
下载安装文件mysql-for-visualstudio-1.2.3.msi 下载成功后执行安装,选择change-->选择Custom安装成功后,发现vs中没有效果. 注意这里再次执行安装文件 ...
- C#通过Oracle.ManagedDataAccess无法访问Oralce (转)
原文转自:https://www.cnblogs.com/duanjt/p/6955173.html 问题描述:通过C#引用Oracle.ManagedDataAccess.dll访问Oracle,写 ...
- PHP架构剖析
一:PHP是什么 PHP("PHP: Hypertext Preprocessor",超文本预处理器的字母缩写)是一种被广泛应用的开放源代码的多用途脚本语言,它可嵌入到 HTML中 ...
- Docker 容器中搭建 nexus npm私库
1,版本 dockers :1.13.1 :nexus 3 2,安装 docker pull liumiaocn/nexus 3,启动 项目 详细 Nexus UI 8081 private re ...
- Linux 学习路径
Linux learning path Mind Map graph LR A[Linux学习路径]-->b[计算机概论与硬件相关知识] A -->c[Linux 初级] A --> ...
- 解决vmware fusion + centos 7安装vmtools时提示The path "" is not a valid path to the xxx kernel headers.
近日使用VMware fushion 8 + centos 7.0时,无法使用共享功能,所以必须安装vmtools.但是安装过程中有2个错误需要解决. 1.gcc错误 Searching for GC ...
- excel经典图表
柱形图: 点击图表,选择数据,添加列 选择展示的列区域数据,编辑水平分类轴,选择按年份统计 效果图: 更改图表类型: 折线图或面积图: 饼图或圆环图: 散点图或气泡图: 组合图: 更改原有图表为组合图 ...
- Elasticsearch-如何识别一篇文档
ES-识别文档 为了识别同一个索引中的某篇文档,ES使用_uid中的文档类型和ID结合体._uid字段是由_id和_type字段组成,当搜索或者检索文档的时候总是能获得这两项信息. FengZhend ...