SQLInjectionHelper.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.HtmlControls;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Text;
  11. using System.Text.RegularExpressions;
  12. namespace Ant.Common
  13. {
  14. /// <summary>
  15. ///SQLInjectionHelper 的摘要说明
  16. /// </summary>
  17. public class SQLInjectionHelper
  18. {
  19. private const string StrKeyWord = @".*(select|insert|delete|from|count(|drop table|update|truncate|asc(|mid(|char(|xp_cmdshell|exec master|netlocalgroup administrators|:|net user|""|or|and).*";
  20. private const string StrRegex = @"[-|;|,|/|(|)|[|]|}|{|%|@|*|!|']";
  21. /// <summary>
  22. /// 获取Post的数据
  23. /// </summary>
  24. public static bool ValidUrlPostData()
  25. {
  26. bool result = false;
  27. for (int i = 0; i < HttpContext.Current.Request.Form.Count; i++)
  28. {
  29. result = ValidData(HttpContext.Current.Request.Form[i].ToString());
  30. if (result)
  31. {
  32. break;
  33. }//如果检测存在漏洞
  34. }
  35. return result;
  36. }
  37. /// <summary>
  38. /// 获取QueryString中的数据
  39. /// </summary>
  40. public static bool ValidUrlGetData()
  41. {
  42. bool result = false;
  43. for (int i = 0; i < HttpContext.Current.Request.QueryString.Count; i++)
  44. {
  45. result = ValidData(HttpContext.Current.Request.QueryString[i].ToString());
  46. if (result)
  47. {
  48. break;
  49. }//如果检测存在漏洞
  50. }
  51. return result;
  52. }
  53. /// <summary>
  54. /// 验证是否存在注入代码
  55. /// </summary>
  56. /// <param name="inputData"></param>
  57. public static bool ValidData(string inputData)
  58. {
  59. //里面定义恶意字符集合
  60. //验证inputData是否包含恶意集合
  61. if (Regex.IsMatch(inputData, GetRegexString()))
  62. {
  63. return true;
  64. }
  65. else
  66. {
  67. return false;
  68. }
  69. }
  70. /// <summary>
  71. /// 获取正则表达式
  72. /// </summary>
  73. /// <param name="queryConditions"></param>
  74. /// <returns></returns>
  75. private static string GetRegexString()
  76. {
  77. //构造SQL的注入关键字符
  78. string[] strBadChar = {"and"
  79. ,"exec"
  80. ,"insert"
  81. ,"select"
  82. ,"delete"
  83. ,"update"
  84. ,"count"
  85. ,"from"
  86. ,"drop"
  87. ,"asc"
  88. ,"char"
  89. ,"or"
  90. //,"*"
  91. ,"%"
  92. ,";"
  93. ,":"
  94. ,"\'"
  95. ,"\""
  96. ,"-"
  97. ,"chr"
  98. ,"mid"
  99. ,"master"
  100. ,"truncate"
  101. ,"char"
  102. ,"declare"
  103. ,"SiteName"
  104. ,"net user"
  105. ,"xp_cmdshell"
  106. ,"/add"
  107. ,"exec master.dbo.xp_cmdshell"
  108. ,"net localgroup administrators"};
  109. //构造正则表达式
  110. string str_Regex = ".*(";
  111. for (int i = 0; i < strBadChar.Length - 1; i++)
  112. {
  113. str_Regex += strBadChar[i] + "|";
  114. }
  115. str_Regex += strBadChar[strBadChar.Length - 1] + ").*";
  116. return str_Regex;
  117. }
  118. }
  119. }