代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C#
】
sql关键字过滤
作者:
coder
/ 发布于
2017/3/16
/
588
/// <summary> ///SQL注入过滤 /// </summary> /// <param name="InText">要过滤的字符串</param> /// <returns>如果参数存在不安全字符,则返回true</returns> public bool SqlFilter(string InText) { string word="and|exec|insert|select|delete|update|chr|mid|master|or|truncate|char|declare|join"; if(InText==null) return false; foreach(string i in word.Split('|')) { if((InText.ToLower().IndexOf(i+" ")>-1)||(InText.ToLower().IndexOf(" "+i)>-1)) { return true; } } return false; } 1. /// <summary> /// 过滤不安全的字符串 /// </summary> /// <param name="Str"></param> /// <returns></returns> public static string FilteSQLStr(string Str) { Str = Str.Replace("'", ""); Str = Str.Replace("\"", ""); Str = Str.Replace("&", "&"); Str = Str.Replace("<", "<"); Str = Str.Replace(">", ">"); Str = Str.Replace("delete", ""); Str = Str.Replace("update", ""); Str = Str.Replace("insert", ""); return Str; } 2. #region 过滤 Sql 语句字符串中的注入脚本 /// <summary> /// 过滤 Sql 语句字符串中的注入脚本 /// </summary> /// <param name="source">传入的字符串</param> /// <returns>过滤后的字符串</returns> public static string SqlFilter(string source) { //单引号替换成两个单引号 source = source.Replace("'", "''"); //半角封号替换为全角封号,防止多语句执行 source = source.Replace(";", ";"); //半角括号替换为全角括号 source = source.Replace("(", "("); source = source.Replace(")", ")"); ///////////////要用正则表达式替换,防止字母大小写得情况//////////////////// //去除执行存储过程的命令关键字 source = source.Replace("Exec", ""); source = source.Replace("Execute", ""); //去除系统存储过程或扩展存储过程关键字 source = source.Replace("xp_", "x p_"); source = source.Replace("sp_", "s p_"); //防止16进制注入 source = source.Replace("0x", "0 x"); return source; } #endregion 3. /// 过滤SQL字符。 /// </summary> /// <param name="str">要过滤SQL字符的字符串。</param> /// <returns>已过滤掉SQL字符的字符串。</returns> public static string ReplaceSQLChar(string str) { if (str == String.Empty) return String.Empty; 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("=", "="); str = str.Replace("+", "+"); str = str.Replace("*", "*"); str = str.Replace("&", "&"); str = str.Replace("#", "#"); str = str.Replace("%", "%"); str = str.Replace("$", "¥"); return str; } 4. /// <summary> /// 过滤标记 /// </summary> /// <param name="NoHTML">包括HTML,脚本,数据库关键字,特殊字符的源码 </param> /// <returns>已经去除标记后的文字</returns> public string NoHtml(string Htmlstring) { if (Htmlstring == null) { return ""; } else { //删除脚本 Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase); //删除HTML Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, "xp_cmdshell", "", RegexOptions.IgnoreCase); //删除与数据库相关的词 Htmlstring = Regex.Replace(Htmlstring, "select", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, "insert", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, "delete from", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, "count''", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, "drop table", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, "truncate", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, "asc", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, "mid", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, "char", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, "xp_cmdshell", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, "exec master", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, "net localgroup administrators", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, "and", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, "net user", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, "or", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, "net", "", RegexOptions.IgnoreCase); //Htmlstring = Regex.Replace(Htmlstring, "*", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, "-", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, "delete", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, "drop", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, "script", "", RegexOptions.IgnoreCase); //特殊的字符 Htmlstring = Htmlstring.Replace("<", ""); Htmlstring = Htmlstring.Replace(">", ""); Htmlstring = Htmlstring.Replace("*", ""); Htmlstring = Htmlstring.Replace("-", ""); Htmlstring = Htmlstring.Replace("?", ""); Htmlstring = Htmlstring.Replace("'", "''"); Htmlstring = Htmlstring.Replace(",", ""); Htmlstring = Htmlstring.Replace("/", ""); Htmlstring = Htmlstring.Replace(";", ""); Htmlstring = Htmlstring.Replace("*/", ""); Htmlstring = Htmlstring.Replace("\r\n", ""); Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim(); return Htmlstring; } } 5. public static bool CheckBadWord(string str) { string pattern = @"select|insert|delete|from|count\(|drop table|update|truncate|asc\(|mid\(|char\(|xp_cmdshell|exec master|netlocalgroup administrators|net user|or|and"; if (Regex.IsMatch(str, pattern, RegexOptions.IgnoreCase)) return true; return false; } public static string Filter(string str) { string[] pattern ={ "select", "insert", "delete", "from", "count\\(", "drop table", "update", "truncate", "asc\\(", "mid\\(", "char\\(", "xp_cmdshell", "exec master", "netlocalgroup administrators", "net user", "or", "and" }; for (int i = 0; i < pattern.Length; i++) { str = str.Replace(pattern[i].ToString(), ""); } return str; }
试试其它关键字
同语言下
.
C#实现的html内容截取
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
.
实现对图片上传的接收
.
去除字符串中的空格,回车,换行符转变成‘;’在按‘
.
按照回车换行符分割字符串
.
文件MD5码 比较,检测文件是否一样
可能有用的
.
C#实现的html内容截取
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
.
实现对图片上传的接收
.
去除字符串中的空格,回车,换行符转变成‘;’在按‘
.
按照回车换行符分割字符串
.
文件MD5码 比较,检测文件是否一样
coder
贡献的其它代码
(
20
)
.
java实现Redis工具类
.
request获取请求头和请求数据
.
取得eth0网卡的IP地址
.
将返回记录集中,多个记录的某些字段拼接组合成一个字
.
使用HttpWebRequest Post提交数据,携带Cookie和相关参
.
冒泡法和数组转换成字符串
.
hive模糊搜索表
.
自动建立hive表,并且导入数据到相应目录
.
将PC端固定布局页面改成移动端流体布局
.
sql关键字过滤
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3