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.

149 lines
4.9 KiB

  1. package com.xujie.sys.common.utils;
  2. import com.xujie.sys.common.exception.XJException;
  3. import lombok.extern.slf4j.Slf4j;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6. /**
  7. * @CLASSNAME SqlUtils
  8. * @AUTHOR sxm
  9. * @DESCRIPTION
  10. * @DATE 2021/12/3 15:43
  11. * @VERSION 1.0
  12. **/
  13. @Slf4j
  14. public class SqlUtils {
  15. /**
  16. * sign 用于sql加签的盐值SQL漏洞
  17. * 上线修改值 TABLE_DICT_SIGN_SALT同步修改前端的盐值
  18. */
  19. private static final String TABLE_DICT_SIGN_SALT = "20200626";
  20. private static final String xssStr = "'|and |exec |insert |select |delete |update |drop |count |chr |mid |master |truncate |char |declare |;|or |+|,";
  21. private static final String reg = "(?:')|(?:--)|(/\\*(?:.|[\\n\\r])*?\\*/)|"
  22. + "(\\b(select|update|and|or|delete|insert|trancate|char|into|substr|ascii|declare|exec|count|master|into|drop|execute)\\b)";
  23. /**
  24. * 表示忽略大小写
  25. */
  26. private static final Pattern sqlPattern = Pattern.compile(reg, Pattern.CASE_INSENSITIVE);
  27. /**
  28. * sql注入过滤处理遇到注入关键字抛异常
  29. *
  30. * @param value
  31. * @return
  32. */
  33. public static void filterContent(String value) {
  34. if (value == null || "".equals(value)) {
  35. return;
  36. }
  37. // 统一转为小写
  38. value = value.toLowerCase();
  39. String[] xssArr = xssStr.split("\\|");
  40. for (int i = 0; i < xssArr.length; i++) {
  41. if (value.indexOf(xssArr[i]) > -1) {
  42. log.info("请注意,存在SQL注入关键词---> {}", xssArr[i]);
  43. log.info("请注意,值可能存在SQL注入风险!---> {}", value);
  44. throw new XJException("请注意,值可能存在SQL注入风险!--->" + value);
  45. }
  46. }
  47. return;
  48. }
  49. /**
  50. * sql注入过滤处理遇到注入关键字抛异常
  51. *
  52. * @param values
  53. * @return
  54. */
  55. public static void filterContent(String[] values) {
  56. String[] xssArr = xssStr.split("\\|");
  57. for (String value : values) {
  58. if (value == null || "".equals(value)) {
  59. return;
  60. }
  61. // 统一转为小写
  62. value = value.toLowerCase();
  63. for (int i = 0; i < xssArr.length; i++) {
  64. if (value.indexOf(xssArr[i]) > -1) {
  65. log.info("请注意,存在SQL注入关键词---> {}", xssArr[i]);
  66. log.info("请注意,值可能存在SQL注入风险!---> {}", value);
  67. throw new XJException("请注意,值可能存在SQL注入风险!--->" + value);
  68. }
  69. }
  70. }
  71. return;
  72. }
  73. /**
  74. * 注入过滤
  75. * @param value
  76. * @return
  77. */
  78. // @Deprecated
  79. public static void specialFilterContent(String value) {
  80. String specialXssStr = " exec | insert | select | delete | update | drop | count | chr | mid | master | truncate | char | declare |;|+|";
  81. String[] xssArr = specialXssStr.split("\\|");
  82. if (value == null || "".equals(value)) {
  83. return;
  84. }
  85. // 统一转为小写
  86. value = value.toLowerCase();
  87. for (int i = 0; i < xssArr.length; i++) {
  88. if (value.indexOf(xssArr[i]) > -1 || value.startsWith(xssArr[i].trim())) {
  89. log.info("请注意,存在SQL注入关键词---> {}", xssArr[i]);
  90. log.info("请注意,值可能存在SQL注入风险!---> {}", value);
  91. throw new XJException("请注意,值可能存在SQL注入风险!--->" + value);
  92. }
  93. }
  94. return;
  95. }
  96. /**
  97. * 注入过滤
  98. * @param value
  99. * @return
  100. */
  101. //@Deprecated
  102. public static void specialFilterContentForOnlineReport(String value) {
  103. String specialXssStr = " exec | insert | delete | update | drop | chr | mid | master | truncate | char | declare |";
  104. String[] xssArr = specialXssStr.split("\\|");
  105. if (value == null || "".equals(value)) {
  106. return;
  107. }
  108. // 统一转为小写
  109. value = value.toLowerCase();
  110. for (int i = 0; i < xssArr.length; i++) {
  111. if (value.indexOf(xssArr[i]) > -1 || value.startsWith(xssArr[i].trim())) {
  112. log.info("请注意,存在SQL注入关键词---> {}", xssArr[i]);
  113. log.info("请注意,值可能存在SQL注入风险!---> {}", value);
  114. throw new XJException("请注意,值可能存在SQL注入风险!--->" + value);
  115. }
  116. }
  117. return;
  118. }
  119. /**
  120. * 参数校验
  121. * @param str ep: "or 1=1"
  122. */
  123. public static boolean isSqlValid(String str) {
  124. Matcher matcher = sqlPattern.matcher(str);
  125. if (matcher.find()) {
  126. //获取非法字符:or
  127. log.info("参数存在非法字符,请确认:"+matcher.group());
  128. return false;
  129. }
  130. return true;
  131. }
  132. }