1)从系统运维角度

  • 及时关注阿里云暴露的SQL漏洞,及时修复
  • 从请求入口(nginx或者slb),过滤掉敏感字符、SQL关键字,溢出攻击,user-agents头过滤等
  • 严格限制数据库权限

1.1)后面带的单引号为非法的注入常用字符

  1. if ( $query_string ~* ".*[;'<>].*" ) {
  2. return 404;
  3. }

1.2)sql语句过滤(几乎不用的关键字,又容易被黑客使用的,可以在入口,统一屏蔽掉)

  1. if ($request_uri ~* "[+|(%20)]union[+|(%20)]") {
  2. return 444;
  3. }
  4. if ($request_uri ~* "[+|(%20)]and[+|(%20)]") {
  5. return 444;
  6. }
  7. if ($request_uri ~* "[+|(%20)]select[+|(%20)]") {
  8. return 444;
  9. }
  10. if ($request_uri ~ "(cost()|(concat()") {
  11. return 444;
  12. }

1.3)防止文件注入
https://www.maxoffice.com/bangongshi/lianhe?a=310115&d=2426
query_string: /bangongshi/lianhe?a=310115&d=2426

  1. set $block_file 0;
  2. if ($query_string ~ “[a-zA-Z0-9_]=http://”) {
  3. set $block_fil 1;
  4. }
  5. if ($query_string ~ “[a-zA-Z0-9_]=(..//?)+”) {
  6. set $block_file 1;
  7. }
  8. if ($query_string ~ “[a-zA-Z0-9_]=/([a-z0-9_.]//?)+”) {
  9. set $block_file 1;
  10. }
  11. if ($block_file = 1) {
  12. return 444;
  13. }

1.4)溢出攻击过滤

  1. set $block_common_exploits 0;
  2. if ($query_string ~ "(<|%3C).*script.*(>|%3E)") {
  3. set $block_common_exploits 1;
  4. }
  5. if ($query_string ~ "GLOBALS(=|[|%[0-9A-Z]{0,2})") {
  6. set $block_common_exploits 1;
  7. }
  8. if ($query_string ~ "_REQUEST(=|[|%[0-9A-Z]{0,2})") {
  9. set $block_common_exploits 1;
  10. }
  11. if ($query_string ~ "proc/self/environ") {
  12. set $block_common_exploits 1;
  13. }
  14. if ($query_string ~ "mosConfig_[a-zA-Z_]{1,21}(=|%3D)") {
  15. set $block_common_exploits 1;
  16. }
  17. if ($query_string ~ "base64_(en|de)code(.*)") {
  18. set $block_common_exploits 1;
  19. }
  20. if ($block_common_exploits = 1) {
  21. return 444;
  22. }

1.5) user-agents头过滤

  1. set $block_user_agents 0;
  2. if ($http_user_agent ~ Wget | Indy Library | libwww-perl | GetRight | Go!Zilla | Download Demon |Go-Ahead-Got-It | TurnitinBot |GrabNet ”) {
  3. set $block_user_agents 1;
  4. }
  5. if ($block_user_agents = 1) {
  6. return 444;
  7. }

2)从前端开发角度

sql注入,前端防护比较少,主要是后端和入口

  • 恶意JS代码被执行

3)从后端JAVA开发角度

  • 杜绝SQL拼接,使用参数化执行(使用预编译语句来组装SQL查询)
  • 过滤一些sql注入的非法参数
  • 对用户提交的的参数安全过滤,像一些特殊的字符(,()*&……%#等等)
  • 我们使用 mybatis 编写 SQL 语句时,难免会使用模糊查询的方法,mybatis 提供了两种方式#{}${},建议都用#{}
  • 拒绝一切非规范格式的编码
  • 每张表都必须要有主键(建议显式定义,不要让MySQL自选)

4)从整体安全角度

  1. SQL注入有多种方式,多种渠道。当配置以上方式,仍无法抵御攻击,造成服务中断;从整体安全考虑,为业务的稳定运行,需要考虑前端引入专业的《web防火墙》<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/12595061/1615449685517-8acf0e02-0dca-47bc-93db-f3cf61021033.png#align=left&display=inline&height=832&margin=%5Bobject%20Object%5D&name=image.png&originHeight=832&originWidth=965&size=85158&status=done&style=none&width=965)