一、什么是sql注入
sql注入即是指web应用程序对用户输入数据的合法性没有判断或过滤不严,攻击者可以在web应用程序中事先定义好的查询语句的结尾上添加额外的sql语句,在管理员不知情的情况下实现非法操作,以此来实现欺骗数据库服务器执行非授权的任意查询,从而进一步得到相应的数据信息。
sql案列:
string sql = "delete from table1 where id = " + "id";
这个id从请求参数中获取,若参数被拼接为:
"1001 or 1 = 1"
最执行语句变为:
string sql = "delete from table1 where id = 1001 or 1 = 1";
此时,数据库的数据都会被清空掉,后果非常严重
二、java项目防止sql注入方式
这里总结4种:
- preparedstatement防止sql注入
- mybatis中#{}防止sql注入
- 对请求参数的敏感词汇进行过滤
- nginx反向代理防止sql注入
1、preparedstatement防止sql注入
preparedstatement具有预编译功能,以上述sql为例
使用preparedstatement预编译后的sql为:
delete from table1 where id= ?
此时sql语句结构已固定,无论"?"被替换为任何参数,sql语句只认为where后面只有一个条件,当再传入 1001 or 1 = 1时,语句会报错,从而达到防止sql注入效果。
2、mybatis中#{}防止sql注入
mybatis中#{}表达式防止sql注入与preparedstatement类似,都是对sql语句进行预编译处理
ps 注意:
#{} :参数占位符
${} :拼接替换符,不能防止sql注入,一般用于:
- 传入数据库对象(如:数据库名称、表名)
- order by 后的条件
3、对请求参数的敏感词汇进行过滤
这里是springboot的写法,如下:
import org.springframework.context.annotation.configuration; import javax.servlet.*; import javax.servlet.annotation.webfilter; import java.io.ioexception; import java.util.enumeration; /** * @auther: admin * @description: sql防注入过滤器 */ @webfilter(urlpatterns = "/*",filtername = "sqlfilter") @configuration public class sqlfilter implements filter { @override public void init(filterconfig filterconfig) throws servletexception {} /** * @description sql注入过滤 */ @override public void dofilter(servletrequest servletrequest, servletresponse servletresponse, filterchain filterchain) throws ioexception, servletexception { servletrequest request = servletrequest; servletresponse response = servletresponse; // 获得所有请求参数名 enumeration<string> names = request.getparameternames(); string sql = ""; while (names.hasmoreelements()){ // 得到参数名 string name = names.nextelement().tostring(); // 得到参数对应值 string[] values = request.getparametervalues(name); for (int i = 0; i < values.length; i++) { sql += values[i]; } } if (sqlvalidate(sql)) { //todo 这里直接抛异常处理,前后端交互项目中,请把错误信息按前后端"数据返回的vo"对象进行封装 throw new ioexception("您发送请求中的参数中含有非法字符"); } else { filterchain.dofilter(request,response); } } /** * @description 匹配效验 */ protected static boolean sqlvalidate(string str){ // 统一转为小写 string s = str.tolowercase(); // 过滤掉的sql关键字,特殊字符前面需要加\\进行转义 string badstr = "select|update|and|or|delete|insert|truncate|char|into|substr|ascii|declare|exec|count|master|into|drop|execute|table|"+ "char|declare|sitename|xp_cmdshell|like|from|grant|use|group_concat|column_name|" + "information_schema.columns|table_schema|union|where|order|by|" + "'\\*|\\;|\\-|\\--|\\+|\\,|\\//|\\/|\\%|\\#"; //使用正则表达式进行匹配 boolean matches = s.matches(badstr); return matches; } @override public void destroy() {} }
4、nginx反向代理防止sql注入
越来越多网站使用nginx进行反向代理,该层我们也可以进行防止sql注入配置。
将下面的nginx配置文件代码放入到server块中,然后重启nginx即可。
if ($request_method !~* get|post) { return 444; } #使用444错误代码可以更加减轻服务器负载压力。 #防止sql注入 if ($query_string ~* (\$|'|--|[+|(%20)]union[+|(%20)]|[+|(%20)]insert[+|(%20)]|[+|(%20)]drop[+|(%20)]|[+|(%20)]truncate[+|(%20)]|[+|(%20)]update[+|(%20)]|[+|(%20)]from[+|(%20)]|[+|(%20)]grant[+|(%20)]|[+|(%20)]exec[+|(%20)]|[+|(%20)]where[+|(%20)]|[+|(%20)]select[+|(%20)]|[+|(%20)]and[+|(%20)]|[+|(%20)]or[+|(%20)]|[+|(%20)]count[+|(%20)]|[+|(%20)]exec[+|(%20)]|[+|(%20)]chr[+|(%20)]|[+|(%20)]mid[+|(%20)]|[+|(%20)]like[+|(%20)]|[+|(%20)]iframe[+|(%20)]|[\<|%3c]script[\>|%3e]|javascript|alert|webscan|dbappsecurity|style|confirm\(|innerhtml|innertext)(.*)$) { return 555; } if ($uri ~* (/~).*) { return 501; } if ($uri ~* (\\x.)) { return 501; } #防止sql注入 if ($query_string ~* "[;'<>].*") { return 509; } if ($request_uri ~ " ") { return 509; } if ($request_uri ~ (\/\.+)) { return 509; } if ($request_uri ~ (\.+\/)) { return 509; } #if ($uri ~* (insert|select|delete|update|count|master|truncate|declare|exec|\*|\')(.*)$ ) { return 503; } #防止sql注入 if ($request_uri ~* "(cost\()|(concat\()") { return 504; } if ($request_uri ~* "[+|(%20)]union[+|(%20)]") { return 504; } if ($request_uri ~* "[+|(%20)]and[+|(%20)]") { return 504; } if ($request_uri ~* "[+|(%20)]select[+|(%20)]") { return 504; } if ($request_uri ~* "[+|(%20)]or[+|(%20)]") { return 504; } if ($request_uri ~* "[+|(%20)]delete[+|(%20)]") { return 504; } if ($request_uri ~* "[+|(%20)]update[+|(%20)]") { return 504; } if ($request_uri ~* "[+|(%20)]insert[+|(%20)]") { return 504; } if ($query_string ~ "(<|%3c).*script.*(>|%3e)") { return 505; } if ($query_string ~ "globals(=|\[|\%[0-9a-z]{0,2})") { return 505; } if ($query_string ~ "_request(=|\[|\%[0-9a-z]{0,2})") { return 505; } if ($query_string ~ "proc/self/environ") { return 505; } if ($query_string ~ "mosconfig_[a-za-z_]{1,21}(=|\%3d)") { return 505; } if ($query_string ~ "base64_(en|de)code\(.*\)") { return 505; } if ($query_string ~ "[a-za-z0-9_]=http://") { return 506; } if ($query_string ~ "[a-za-z0-9_]=(\.\.//?)+") { return 506; } if ($query_string ~ "[a-za-z0-9_]=/([a-z0-9_.]//?)+") { return 506; } if ($query_string ~ "b(ultram|unicauca|valium|viagra|vicodin|xanax|ypxaieo)b") { return 507; } if ($query_string ~ "b(erections|hoodia|huronriveracres|impotence|levitra|libido)b") {return 507; } if ($query_string ~ "b(ambien|bluespill|cialis|cocaine|ejaculation|erectile)b") { return 507; } if ($query_string ~ "b(lipitor|phentermin|pro[sz]ac|sandyauer|tramadol|troyhamby)b") { return 507; } #这里大家根据自己情况添加删减上述判断参数,curl、wget这类的屏蔽有点儿极端了,但要“宁可错杀一千,不可放过一个”。 if ($http_user_agent ~* yisouspider|apachebench|webbench|jmeter|joedog|havij|getright|turnitinbot|grabnet|masscan|mail2000|github|wget|curl|java|python) { return 508; } #同上,大家根据自己站点实际情况来添加删减下面的屏蔽拦截参数。 if ($http_user_agent ~* "go-ahead-got-it") { return 508; } if ($http_user_agent ~* "getweb!") { return 508; } if ($http_user_agent ~* "go!zilla") { return 508; } if ($http_user_agent ~* "download demon") { return 508; } if ($http_user_agent ~* "indy library") { return 508; } if ($http_user_agent ~* "libwww-perl") { return 508; } if ($http_user_agent ~* "nmap scripting engine") { return 508; } if ($http_user_agent ~* "~17ce.com") { return 508; } if ($http_user_agent ~* "webbench*") { return 508; } if ($http_user_agent ~* "spider") { return 508; } #这个会影响国内某些搜索引擎爬虫,比如:搜狗 #拦截各恶意请求的ua,可以通过分析站点日志文件或者waf日志作为参考配置。 if ($http_referer ~* 17ce.com) { return 509; } #拦截17ce.com站点测速节点的请求,所以明月一直都说这些测速网站的数据仅供参考不能当真的。 if ($http_referer ~* webbench*") { return 509; } #拦截webbench或者类似压力测试工具,其他工具只需要更换名称即可。
到此这篇关于java项目中防止sql注入的四种方法推荐的文章就介绍到这了,更多相关java防止sql注入内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论