1)从系统运维角度
- 及时关注阿里云暴露的SQL漏洞,及时修复
- 从请求入口(nginx或者slb),过滤掉敏感字符、SQL关键字,溢出攻击,user-agents头过滤等
- 严格限制数据库权限
1.1)后面带的单引号为非法的注入常用字符
if ( $query_string ~* ".*[;'<>].*" ) {return 404;}
1.2)sql语句过滤(几乎不用的关键字,又容易被黑客使用的,可以在入口,统一屏蔽掉)
if ($request_uri ~* "[+|(%20)]union[+|(%20)]") {return 444;}if ($request_uri ~* "[+|(%20)]and[+|(%20)]") {return 444;}if ($request_uri ~* "[+|(%20)]select[+|(%20)]") {return 444;}if ($request_uri ~ "(cost()|(concat()") {return 444;}
1.3)防止文件注入
https://www.maxoffice.com/bangongshi/lianhe?a=310115&d=2426
query_string: /bangongshi/lianhe?a=310115&d=2426
set $block_file 0;if ($query_string ~ “[a-zA-Z0-9_]=http://”) {set $block_fil 1;}if ($query_string ~ “[a-zA-Z0-9_]=(..//?)+”) {set $block_file 1;}if ($query_string ~ “[a-zA-Z0-9_]=/([a-z0-9_.]//?)+”) {set $block_file 1;}if ($block_file = 1) {return 444;}
1.4)溢出攻击过滤
set $block_common_exploits 0;if ($query_string ~ "(<|%3C).*script.*(>|%3E)") {set $block_common_exploits 1;}if ($query_string ~ "GLOBALS(=|[|%[0-9A-Z]{0,2})") {set $block_common_exploits 1;}if ($query_string ~ "_REQUEST(=|[|%[0-9A-Z]{0,2})") {set $block_common_exploits 1;}if ($query_string ~ "proc/self/environ") {set $block_common_exploits 1;}if ($query_string ~ "mosConfig_[a-zA-Z_]{1,21}(=|%3D)") {set $block_common_exploits 1;}if ($query_string ~ "base64_(en|de)code(.*)") {set $block_common_exploits 1;}if ($block_common_exploits = 1) {return 444;}
1.5) user-agents头过滤
set $block_user_agents 0;if ($http_user_agent ~ “Wget | Indy Library | libwww-perl | GetRight | Go!Zilla | Download Demon |Go-Ahead-Got-It | TurnitinBot |GrabNet ”) {set $block_user_agents 1;}if ($block_user_agents = 1) {return 444;}
2)从前端开发角度
sql注入,前端防护比较少,主要是后端和入口
- 恶意JS代码被执行
3)从后端JAVA开发角度
- 杜绝SQL拼接,使用参数化执行(使用预编译语句来组装SQL查询)
- 过滤一些sql注入的非法参数
- 对用户提交的的参数安全过滤,像一些特殊的字符(,()*&……%#等等)
- 我们使用 mybatis 编写 SQL 语句时,难免会使用模糊查询的方法,mybatis 提供了两种方式
#{}和${},建议都用#{} 拒绝一切非规范格式的编码- 每张表都必须要有主键(建议显式定义,不要让MySQL自选)
4)从整体安全角度
SQL注入有多种方式,多种渠道。当配置以上方式,仍无法抵御攻击,造成服务中断;从整体安全考虑,为业务的稳定运行,需要考虑前端引入专业的《web防火墙》<br />
