Ibatis sql注入 注意!!
在项目中,运用Ibatis中Like写法,没有研究下,结果SQL语句存在SQL注入漏洞,整理下,下次谨记啊!
sql语句:
select*
from(select1frompoll
<dynamicprepend="where">
<isNotEmptyprepend="and"property="title">
titlelike'%$title$%'
</isNotEmpty>
<isNotEmptyproperty="used">
<isEqualcompareValue="true"prepend="and"property="used">
<![CDATA[status&2>0andstatus&1<=0andstatus&8<=0]]>
</isEqual>
</isNotEmpty>
<isNotEmptyprepend="and"property="startTimeBegin">
<![CDATA[gmt_create>=#startTimeBegin#]]>
</isNotEmpty>
<isNotEmptyprepend="and"property="startTimeEnd">
<![CDATA[gmt_create<=#startTimeEnd#]]>
</isNotEmpty>
</dynamic>
limit10000
)ast
select*
from(select1frompoll
<dynamicprepend="where">
<isNotEmptyprepend="and"property="title">
titlelike'%$title$%'
</isNotEmpty>
<isNotEmptyproperty="used">
<isEqualcompareValue="true"prepend="and"property="used">
<![CDATA[status&2>0andstatus&1<=0andstatus&8<=0]]>
</isEqual>
</isNotEmpty>
<isNotEmptyprepend="and"property="startTimeBegin">
<![CDATA[gmt_create>=#startTimeBegin#]]>
</isNotEmpty>
<isNotEmptyprepend="and"property="startTimeEnd">
<![CDATA[gmt_create<=#startTimeEnd#]]>
</isNotEmpty>
</dynamic>
limit10000
)ast
请关注此写法的:
titlelike'%$title$%'
titlelike'%$title$%'
存在SQL注入漏洞。
下面是一段单元测试:
Java代码
PollQueryquery=newPollQuery();
query.setCurrentPage(1);
query.setPageSize(50);
query.setTitle("1231%'or'1%'='1");//很简单的写法:(
List<SnsPollDO>l=pollDAO.findPollList(query);
System.out.println(l.size())
[java]viewplaincopy
PollQueryquery=newPollQuery();
query.setCurrentPage(1);
query.setPageSize(50);
query.setTitle("1231%'or'1%'='1");//很简单的写法:(
List<SnsPollDO>l=pollDAO.findPollList(query);
System.out.println(l.size())
测试结果(打印处的sql语句):
select*frompollwheretitlelike'%1231%'or'1%'='1%'
[java]viewplaincopy
1.select*frompollwheretitlelike'%1231%'or'1%'='1%'
尽管title没匹配对,但是or后面那句是恒等的。哎!
看来下面的写法只是简单的转义下:
titlelike'%$title$%'
titlelike'%$title$%'
如何解决:
在oracle下面改成:titlelike'%'||#title#||'%',这样肯定是可以的。
但是在mysql中,上述写法是不行,还是有上面的问题的:
select*frompollwheretitlelike'%'||?||'%'orderbygmt_createdesclimit?,?
select*frompollwheretitlelike'%'||?||'%'orderbygmt_createdesclimit?,?
还能查出结果来!哎!
得用:titleCONCAT('%',#title#,'%')
select*frompollwheretitlelikeCONCAT('%',?,'%')orderbygmt_createdesclimit?,?
呵呵,多次测试均没有发现问题!
------------------------------------------
以下读者注:
是否为:titlelikeCONCAT('%',#title#,'%')
作者oswin_jiang的专栏