C#-防止用户输入具有风险的敏感字符
最近有涉及到要防止用户在网页文本框中输入具有风险的敏感字符所以特地编写了一套针对用户输入的字符进行安全过滤的一个方法,在后台接收到用户输入的字符后调用执行该方法即可完成过滤操作,主要使用正则来匹配并替换掉敏感字符!
/// <summary>
/// obj向string转换,替换具有风险的敏感字符并去除多余的空格;
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
public static string RequestFilter(this object o)
{
if (o == DBNull.Value || o == null)
{
return "";
}
else
{
string str = o.ToString().Trim();
//删除脚本
str = Regex.Replace(str, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
//删除HTML
str = Regex.Replace(str, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"-->", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"<!--.*", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"&#(\d+);", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "xp_cmdshell", "", RegexOptions.IgnoreCase); //删除与数据库相关的词
str = Regex.Replace(str, "select", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "insert", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "delete from", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "count''", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "drop table", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "truncate", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "asc", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "mid", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "char", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "xp_cmdshell", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "exec master", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "net localgroup administrators", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "and", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "net user", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "or", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "net", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "delete", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "drop", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "script", "", RegexOptions.IgnoreCase); str = str.Replace("<", "");
str = str.Replace(">", "");
str = str.Replace("*", "");
str = str.Replace("--", "");
str = str.Replace("?", "");
str = str.Replace(",", "");
str = str.Replace("/", "");
str = str.Replace(";", "");
str = str.Replace("*/", "");
str = str.Replace("\r\n", "");
return str;
}
}
C#-防止用户输入具有风险的敏感字符的更多相关文章
- C 统计用户输入的总行数和字符长度
C 统计用户输入的总行数和字符长度 #include <stdio.h> #include <Windows.h> int main(void) { ]; ; ; printf ...
- strtr对用户输入的敏感词汇进行过滤
/** * 过滤用户输入的基本数据,防止script攻击 * * @access public * @return string */ function compile_str($str) { $ar ...
- [ PyQt入门教程 ] PyQt5基本控件使用:消息弹出、用户输入、文件对话框
本文主要介绍PyQt界面实现中常用的消息弹出对话框.提供用户输入的输入框.打开文件获取文件/目录路径的文件对话框.学习这三种控件前,先想一下它们使用的主要场景: 1.消息弹出对话框.程序遇到问题需要退 ...
- 防御XSS攻击-encode用户输入内容的重要性
一.开场先科普下XSS 跨站脚本攻击(Cross Site Scripting),为不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS.恶 ...
- python学习笔记(基础二:注释、用户输入、格式化输出)
注释 单行:# 多行:上下各用3个连续单引号或双引号 3个引号除了多行注释,还可以打印多行 举例: msg = ''' name = "Alex Li" name2 = name ...
- 第4章 Java接收用户输入
第4章 Java接收用户输入 1.输入 使用Scanner工具类可以换取用户输入的数据Scanner类位于java.util包中,使用时需要导入此包使用步骤: 1.导入java.util.Scanne ...
- Python学习【第四篇】用户输入及判断
用户输入: 例1.写一个用户输入密码的小程序,流程如下: 1.用户输入自己的用户名 2.打印"Hello+用户名" #!/usr/bin/env python #name = ra ...
- 使用scanner工具类来获取用户输入的信息
使用scanner工具类来获取用户输入的成绩信息. 步骤:1.导入java.util.Scanner包 2.创建Scanner对象 3.接受并保存用户输入的值 例子:通过用户输入来获取学生成绩 pac ...
- alertDialog创建登陆界面,判断用户输入
alertDialog创建登陆界面,需要获取用户输入的用户名和密码,获取控件对象的时候不能像主布局文件那样获得, 需要在onClickListener中获取,代码如下: public boolean ...
随机推荐
- Day009 二维数组
多维数组 多维数组是数组的嵌套(数组的元素是数组,数组的数组元素的元素是数组...),比如二维数组就是一个特殊的一维数组,其每一个元素都是一个一维数组. 二维数组 int a[][]=new int ...
- 如何在centos上配置802.1Q VLAN标记,linux单网卡多vlan多网段Ip配置案例
介绍 VLAN使将大型网络分成较小且易于管理的网络成为可能.802.1Q是所有供应商都在其网络设备中实施的标准.某些交换机能够将多个VLAN分配给单个网络端口.使用此功能,您可以将多个VLAN分配给单 ...
- QFNU 10-09 training
1.F - Three displays 题意:就是给出了两个数组,然后第一组数中找到i,j,k满足i<j<k,第二组数中找到a[i],a[j],a[k],满足a[i]<a[j]&l ...
- NumPy之:ndarray多维数组操作
NumPy之:ndarray多维数组操作 目录 简介 创建ndarray ndarray的属性 ndarray中元素的类型转换 ndarray的数学运算 index和切片 基本使用 index wit ...
- 如何用Vim搭建IDE?
推荐:http://harttle.com/2015/07/18/vim-cpp.html 转自:http://harttle.com/2015/11/04/vim-ide.html 一年前我从Vim ...
- OCR横向评测 -- 软工案例分析
目录 第一部分 调研&评测 使用感受 1. 使用门槛 2. 界面设计 3. 数据标注 4. 模型训练 5. 模型预测 6. 体验评价与改进建议 好的方面: 可能需要改进的方面: 7. BUG反 ...
- 技能Get·BOM头是什么?
阅文时长 | 0.26分钟 字数统计 | 472.8字符 主要内容 | 1.引言&背景 2.BOM头是什么? 3.如何创建或取消BOM头? 4.如何判断文件是否包含BOM头? 5.声明与参考资 ...
- CSS filter 有哪些神奇用途
背景 基本概念 CSS filter 属性将模糊或颜色偏移等图形效果应用于元素形成滤镜,滤镜通常用于调整图像,背景和边框的渲染.它的值可以为 filter 函数 <filter-function ...
- 关于flask的模板注入的学习
flask模板注入的学习 关于flask模版注入,之前不太理解,看了很多文章才弄懂,主要原理就是渲染函数的参数用户可控就造成了模板注入 就会使用户构造恶意的代码进行逃逸从而进行攻击 flask模板渲染 ...
- libminipng,压缩png的swift-framework
libminipng 通过lodepng解析png图片,使用pngquant算法进行压缩的swift-framework 方法说明: /// 通过PNG图片Data压缩 /// /// - Param ...