12306曝光sql注入漏洞,我试着发布解决方案

简介:

12306曝光sql注入漏洞,我试着发布解决方案

在项目中,运用Ibatis中Like写法,没有研究下,结果SQL语句存在SQL注入,整理下,下次谨记啊!

 
sql语句:
 
 
select  * 
        from  ( select  1  from  poll 
        <dynamic  prepend= " where " > 
            <isNotEmpty prepend=" and "  property= "title" > 
                title like   '%$title$%'  
            </isNotEmpty> 
            <isNotEmpty property="used" > 
                <isEqual compareValue="true"  prepend= " and "  property= "used" > 
                    <![CDATA[status & 2 > 0 and  status & 1 <= 0  and  status & 8 <= 0 ]]> 
                </isEqual> 
            </isNotEmpty> 
            <isNotEmpty prepend=" and "  property= "startTimeBegin" > 
                <![CDATA[ gmt_create >= #startTimeBegin# ]]> 
            </isNotEmpty> 
            <isNotEmpty prepend=" and "  property= "startTimeEnd" > 
                <![CDATA[ gmt_create <= #startTimeEnd# ]]> 
            </isNotEmpty> 
        </dynamic > 
        limit 10000 
        ) as  t 
 
select * 
        from (select 1 from poll 
        <dynamic prepend=" where "> 
            <isNotEmpty prepend=" and " property="title"> 
                title like '%$title$%' 
            </isNotEmpty> 
            <isNotEmpty property="used"> 
                <isEqual compareValue="true" prepend=" and " property="used"> 
                    <![CDATA[status & 2 > 0 and status & 1 <= 0 and status & 8 <= 0 ]]> 
                </isEqual> 
            </isNotEmpty> 
            <isNotEmpty prepend=" and " property="startTimeBegin"> 
                <![CDATA[ gmt_create >= #startTimeBegin# ]]> 
            </isNotEmpty> 
            <isNotEmpty prepend=" and " property="startTimeEnd"> 
                <![CDATA[ gmt_create <= #startTimeEnd# ]]> 
            </isNotEmpty> 
        </dynamic> 
        limit 10000 
        ) as t 
 请关注此写法的:
 
 
title  like   '%$title$%'  
 
title like '%$title$%' 
存在SQL注入漏洞。
 
下面是一段单元测试:
 
Java代码
PollQuery query =  new  PollQuery(); 
query.setCurrentPage(1 ); 
query.setPageSize(50 ); 
query.setTitle("1231%' or '1%' = '1" ); //很简单的写法:(  
List<SnsPollDO> l = pollDAO.findPollList(query); 
System.out.println(l.size()) 
[java] view plaincopy
PollQuery query = new PollQuery(); 
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 * from poll   where    title like  '%1231%'  or  '1%'  =  '1%'  
[java] view plaincopy
1. select * from poll   where    title like '%1231%' or '1%' = '1%' 
尽管title 没匹配对,但是or后面那句是恒等的。哎!
 
看来下面的写法只是简单的转义下:
 
 
title  like   '%$title$%'  
 
title like '%$title$%' 
如何解决:
 
在oracle下面改成:title like '%'||#title#||'%',这样肯定是可以的。
 
但是在mysql中,上述写法是不行,还是有上面的问题的:
 
 
select   *  from  poll  where   title  like   '%' ||?|| '%'    order   by  gmt_create  desc    limit ?, ? 
 
select  * from poll where  title like '%'||?||'%'  order by gmt_create desc   limit ?, ? 
 还能查出结果来!哎!
 
得用:title CONCAT('%',#title#,'%')
 
 
select  *  from  poll   where   title  like  CONCAT( '%' ,?, '%' )   order   by  gmt_create  desc  limit ?, ?
 

呵呵,多次测试均没有发现问题!




      本文转自yjflinchong 51CTO博客,原文链接:http://blog.51cto.com/yjflinchong/1165028,如需转载请自行联系原作者




相关文章
|
1月前
|
SQL 监控 安全
SQL注入的实现原理以及防止
SQL注入的实现原理以及防止
|
2月前
|
SQL 数据库
20、绕过去除and、or、union select、空格的sql注入
20、绕过去除and、or、union select、空格的sql注入
32 0
|
2月前
|
SQL 数据库
小课堂 -- 绕过去除特殊字符的sql注入
小课堂 -- 绕过去除特殊字符的sql注入
22 0
|
2月前
|
SQL Java 数据库连接
[SQL]SQL注入与SQL执行过程(基于JDBC)
[SQL]SQL注入与SQL执行过程(基于JDBC)
50 0
|
1月前
|
SQL Java 应用服务中间件
Java项目防止SQL注入的四种方案
Java项目防止SQL注入的四种方案
40 0
|
2月前
|
SQL 安全 关系型数据库
接上篇文章,在测试宝塔 WAF 的未授权访问漏洞时无意间还发现了一个 SQL 注入漏洞
接上篇文章,在测试宝塔 WAF 的未授权访问漏洞时无意间还发现了一个 SQL 注入漏洞,品相还不错,可执行任意 SQL 语句。 总之,吃了一惊,一个防 SQL 注入的工具居然也有 SQL 注入漏洞。 请看这段代码
416 1
|
24天前
|
SQL
启动mysq异常The server quit without updating PID file [FAILED]sql/data/***.pi根本解决方案
启动mysq异常The server quit without updating PID file [FAILED]sql/data/***.pi根本解决方案
17 0
|
8天前
|
SQL 安全 Go
如何在 Python 中进行 Web 应用程序的安全性管理,例如防止 SQL 注入?
在Python Web开发中,确保应用安全至关重要,主要防范SQL注入、XSS和CSRF攻击。措施包括:使用参数化查询或ORM防止SQL注入;过滤与转义用户输入抵御XSS;添加CSRF令牌抵挡CSRF;启用HTTPS保障数据传输安全;实现强身份验证和授权系统;智能处理错误信息;定期更新及审计以修复漏洞;严格输入验证;并培训开发者提升安全意识。持续关注和改进是保证安全的关键。
17 0
|
16天前
|
SQL 安全 PHP
CTF--Web安全--SQL注入之Post-Union注入
CTF--Web安全--SQL注入之Post-Union注入
|
16天前
|
SQL 数据库 索引
SQL索引失效原因分析与解决方案
SQL索引失效原因分析与解决方案
22 0