|
在日常的项目中,SQL注入漏洞很常见,带来的影响也非常大。不仅能够获取到整个数据,还可以通过进一步的渗透来获取服务器的权限,从而进入内网。
QL注入的本质,是通过用户输入的数据当做代码执行。
SQL注入例子:
当用户发送请求: http://localhost:9080/myDemo?id=10001,这个请求是进入一个详情页面,会显示出该数据的相关详细信息,后台代码会接收这个id参数传递给SQL语句,SQL语句如下:
select userId,userName from userTable where id = 10001
这是SQL的原义,也是开发人员和业务人员想要得到的结果,但是如果用户改变了id的内容,修改成如下:
http://localhost:9080/myDemo?id = 10001 and 1=2 union select username,userpwd from usertable
此时内部程序执行的SQL语句就会变为:
select userId,userName from userTable where id = 10001 and 1=2 union select username,userpwd from usertable
这条SQL语句的原义就会被改变,导致将数据表中的用户名和密码也显示出来,最后,攻击成功。
mybatis如何防止SQL注入呢?
假设mapper文件为:
<select id="getNameByUserId" resultType="String">
select userId,userName from userTable where id = #{userId}
</select>
对应的Java文件为:
public interface UserMapper{
String getNameByUserId(@Param("userId") String userId);
}
可以看到输入的参数是String类型,当接口接收到的userId为“10001 union select username,userpwd from usertable”,打印的语句如下:
select userId,userName from userTable where id = ?
这个正是SQL注入生效的过程。
结论:
在编写mybatis的映射语句时,尽量采用“#{XXX}” 这样的格式,来防止SQL注入攻击
|
|
|