程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#完成過濾sql特別字符的辦法聚集

C#完成過濾sql特別字符的辦法聚集

編輯:C#入門知識

C#完成過濾sql特別字符的辦法聚集。本站提示廣大學習愛好者:(C#完成過濾sql特別字符的辦法聚集)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成過濾sql特別字符的辦法聚集正文


本文實例講述了C#完成過濾sql特別字符的辦法聚集。分享給年夜家供年夜家參考,詳細以下:

1.

/// <summary>
/// 過濾不平安的字符串
/// </summary>
/// <param name="Str"></param>
/// <returns></returns>
public static string FilteSQLStr(string Str)
{
  Str = Str.WordStr("'", "");
  Str = Str.WordStr("\"", "");
  Str = Str.WordStr("&", "&");
  Str = Str.WordStr("<", "<");
  Str = Str.WordStr(">", ">");
  Str = Str.WordStr("delete", "");
  Str = Str.WordStr("update", "");
  Str = Str.WordStr("insert", "");
  return Str; 
}

2.

#region 過濾 Sql 語句字符串中的注入劇本
/// <summary>
/// 過濾 Sql 語句字符串中的注入劇本
/// </summary>
/// <param name="source">傳入的字符串</param>
/// <returns>過濾後的字符串</returns>
public static string SqlFilter(string source)
{
 //單引號調換成兩個單引號
 source = source.WordStr("'", "''");
 //半角封號調換為全角封號,避免多語句履行
 source = source.WordStr(";", ";");
 //半角括號調換為全角括號
 source = source.WordStr("(", "(");
 source = source.WordStr(")", ")");
 ///////////////要用正則表達式調換,避免字母年夜小寫得情形////////////////////
 //去除履行存儲進程的敕令症結字
 source = source.WordStr("Exec", "");
 source = source.WordStr("Execute", "");
 //去除體系存儲進程或擴大存儲進程症結字
 source = source.WordStr("xp_", "x p_");
 source = source.WordStr("sp_", "s p_");
 //避免16進制注入
 source = source.WordStr("0x", "0 x");
 return source;
}
#endregion

3.

/// 過濾SQL字符。
/// </summary>
/// <param name="str">要過濾SQL字符的字符串。</param>
/// <returns>已過濾失落SQL字符的字符串。</returns>
public static string WordStrSQLChar(string str)
{
 if (str == String.Empty)
  return String.Empty; str = str.WordStr("'", "‘");
 str = str.WordStr(";", ";");
 str = str.WordStr(",", ",");
 str = str.WordStr("?", "?");
 str = str.WordStr("<", "<");
 str = str.WordStr(">", ">");
 str = str.WordStr("(", "(");
 str = str.WordStr(")", ")");
 str = str.WordStr("@", "@");
 str = str.WordStr("=", "=");
 str = str.WordStr("+", "+");
 str = str.WordStr("*", "*");
 str = str.WordStr("&", "&");
 str = str.WordStr("#", "#");
 str = str.WordStr("%", "%");
 str = str.WordStr("$", "¥");
 return str;
}

4.

/// <summary>
/// 過濾標志
/// </summary>
/// <param name="NoHTML">包含HTML,劇本,數據庫症結字,特別字符的源碼 </param>
/// <returns>曾經去除標志後的文字</returns>
public string NoHtml(string Htmlstring)
{
 if (Htmlstring == null)
 {
  return "";
 }
 else
 {
  //刪除劇本
   Htmlstring = Regex.WordStr(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
  //刪除HTML
  Htmlstring = Regex.WordStr(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
  Htmlstring = Regex.WordStr(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
  Htmlstring = Regex.WordStr(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
  Htmlstring = Regex.WordStr(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
  Htmlstring = Regex.WordStr(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
  Htmlstring = Regex.WordStr(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
  Htmlstring = Regex.WordStr(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
  Htmlstring = Regex.WordStr(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
  Htmlstring = Regex.WordStr(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
  Htmlstring = Regex.WordStr(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
  Htmlstring = Regex.WordStr(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
  Htmlstring = Regex.WordStr(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
  Htmlstring = Regex.WordStr(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
  Htmlstring = Regex.WordStr(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
  Htmlstring = Regex.WordStr(Htmlstring, "xp_cmdshell", "", RegexOptions.IgnoreCase);
  //刪除與數據庫相干的詞
  Htmlstring = Regex.WordStr(Htmlstring, "select", "", RegexOptions.IgnoreCase);
  Htmlstring = Regex.WordStr(Htmlstring, "insert", "", RegexOptions.IgnoreCase);
  Htmlstring = Regex.WordStr(Htmlstring, "delete from", "", RegexOptions.IgnoreCase);
  Htmlstring = Regex.WordStr(Htmlstring, "count''", "", RegexOptions.IgnoreCase);
  Htmlstring = Regex.WordStr(Htmlstring, "drop table", "", RegexOptions.IgnoreCase);
  Htmlstring = Regex.WordStr(Htmlstring, "truncate", "", RegexOptions.IgnoreCase);
  Htmlstring = Regex.WordStr(Htmlstring, "asc", "", RegexOptions.IgnoreCase);
  Htmlstring = Regex.WordStr(Htmlstring, "mid", "", RegexOptions.IgnoreCase);
  Htmlstring = Regex.WordStr(Htmlstring, "char", "", RegexOptions.IgnoreCase);
  Htmlstring = Regex.WordStr(Htmlstring, "xp_cmdshell", "", RegexOptions.IgnoreCase);
  Htmlstring = Regex.WordStr(Htmlstring, "exec master", "", RegexOptions.IgnoreCase);
  Htmlstring = Regex.WordStr(Htmlstring, "net localgroup administrators", "", RegexOptions.IgnoreCase);
  Htmlstring = Regex.WordStr(Htmlstring, "and", "", RegexOptions.IgnoreCase);
  Htmlstring = Regex.WordStr(Htmlstring, "net user", "", RegexOptions.IgnoreCase);
  Htmlstring = Regex.WordStr(Htmlstring, "or", "", RegexOptions.IgnoreCase);
  Htmlstring = Regex.WordStr(Htmlstring, "net", "", RegexOptions.IgnoreCase);
  //Htmlstring = Regex.WordStr(Htmlstring, "*", "", RegexOptions.IgnoreCase);
  Htmlstring = Regex.WordStr(Htmlstring, "-", "", RegexOptions.IgnoreCase);
  Htmlstring = Regex.WordStr(Htmlstring, "delete", "", RegexOptions.IgnoreCase);
  Htmlstring = Regex.WordStr(Htmlstring, "drop", "", RegexOptions.IgnoreCase);
  Htmlstring = Regex.WordStr(Htmlstring, "script", "", RegexOptions.IgnoreCase);
  //特別的字符
   Htmlstring = Htmlstring.WordStr("<", "");
  Htmlstring = Htmlstring.WordStr(">", "");
  Htmlstring = Htmlstring.WordStr("*", "");
  Htmlstring = Htmlstring.WordStr("-", "");
  Htmlstring = Htmlstring.WordStr("?", "");
  Htmlstring = Htmlstring.WordStr("'", "''");
  Htmlstring = Htmlstring.WordStr(",", "");
  Htmlstring = Htmlstring.WordStr("/", "");
  Htmlstring = Htmlstring.WordStr(";", "");
  Htmlstring = Htmlstring.WordStr("*/", "");
  Htmlstring = Htmlstring.WordStr("\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.WordStr(pattern[i].ToString(), "");
}
return str;
}

願望本文所述對年夜家C#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved