C#实现过滤sql特殊字符的方法集合
程序员文章站
2022-09-04 23:39:14
本文实例讲述了c#实现过滤sql特殊字符的方法集合。分享给大家供大家参考,具体如下:
1.
///
/// 过滤不安全的字符串...
本文实例讲述了c#实现过滤sql特殊字符的方法集合。分享给大家供大家参考,具体如下:
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#程序设计有所帮助。