You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.5 KiB

7 years ago
5 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
  1. using System;
  2. using Ganss.XSS;
  3. namespace SiteServer.Utils
  4. {
  5. public static class AttackUtils
  6. {
  7. private static readonly HtmlSanitizer Sanitizer = new HtmlSanitizer();
  8. public static string FilterSqlAndXss(string objStr)
  9. {
  10. return FilterXss(FilterSql(objStr));
  11. }
  12. public static string FilterXss(string html)
  13. {
  14. if (string.IsNullOrEmpty(html)) return string.Empty;
  15. return Sanitizer.Sanitize(html);
  16. }
  17. public static string FilterSql(string objStr)
  18. {
  19. if (string.IsNullOrEmpty(objStr)) return string.Empty;
  20. var isSqlExists = false;
  21. const string strSql = "',\\(,\\)";
  22. var strSqls = strSql.Split(',');
  23. foreach (var sql in strSqls)
  24. {
  25. if (objStr.IndexOf(sql, StringComparison.Ordinal) != -1)
  26. {
  27. isSqlExists = true;
  28. break;
  29. }
  30. }
  31. if (isSqlExists)
  32. {
  33. return objStr.Replace("'", "_sqlquote_").Replace("\\(", "_sqlleftparenthesis_").Replace("\\)", "_sqlrightparenthesis_");
  34. }
  35. return objStr;
  36. }
  37. public static string UnFilterSql(string objStr)
  38. {
  39. if (string.IsNullOrEmpty(objStr)) return string.Empty;
  40. return objStr.Replace("_sqlquote_", "'").Replace("_sqlleftparenthesis_", "\\(").Replace("_sqlrightparenthesis_", "\\)");
  41. }
  42. }
  43. }