SQL注射原理

所谓SQL注入,就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令。具体来说,它是利用现有应用程序,将(恶意)的SQL命令注入到后台数据库引擎执行的能力,它可以通过在Web表单中输入(恶意)SQL语句得到一个存在安全漏洞的网站上的数据库,而不是按照设计者意图去执行SQL语句。

攻击方式

猜表名

And (Select count(*) from 表名)<>0
and exists (select * from 表名)

猜列名

And (Select count(列名) from 表名)<>0
and exists (select 列名 from 表名)

拉全表

' or 1 = 1 #

#可以注释掉后面的一行SQL代码

实战演练

正常请求

http://xxxxxx/sc?cn=sc

响应结果数量:

注入请求

http://xxxxxx/sc?cn=sc' or 1= 1 or '' = '

响应结果数量:

可以从响应结果数中看到,它已经几乎把我们所有的数据给拉了出来。

防御调整

原查询语句

<!--查询学校-->
<select id="selectSchool" resultMap="BaseResultMap" parameterType="map">
    select xxx from yyyy
    where zzz = 'section' and deleted_at is null and is_open = 1
    <if test="name !=null">
        and name like '%${name}%'
    </if>
    <if test="pinyin !=null">
        and pinyin_initial like '${pinyin}%'
    </if>
    order by id
</select>

调整后语句

<!--查询学校-->
<select id="selectSchool" resultMap="BaseResultMap" parameterType="map">
    select xxx from yyyy
    where zzz = 'section' and deleted_at is null and is_open = 1
    <if test="name !=null">
        and name like concat(concat('%',#{name}),'%')
    </if>
    <if test="pinyin !=null">
        and pinyin_initial like concat((#{pinyin}),'%')
    </if>
    order by id
</select>

调整后执行日志

2016-03-11 16:06:29.661 [http-nio-8080-exec-8] DEBUG org.mybatis.spring.SqlSessionUtils 106 getSqlSession- Creating a new SqlSession
2016-03-11 16:06:29.663 [http-nio-8080-exec-8] DEBUG org.mybatis.spring.SqlSessionUtils 142 getSqlSession- SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@647a5210] was not registered for synchronization because synchronization is not active
2016-03-11 16:06:29.677 [http-nio-8080-exec-8] DEBUG o.m.spring.transaction.SpringManagedTransaction 85 openConnection- JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@69787b57] will not be managed by Spring
2016-03-11 16:06:29.678 [http-nio-8080-exec-8] DEBUG c.z.customer.base.dao.DistrictMapper.selectSchool 132 debug- ooo Using Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@69787b57]
2016-03-11 16:06:29.678 [kafka-producer-network-thread | producer-2] DEBUG org.apache.kafka.clients.NetworkClient 385 maybeUpdateMetadata- Trying to send metadata request to node -1
2016-03-11 16:06:29.679 [http-nio-8080-exec-8] DEBUG c.z.customer.base.dao.DistrictMapper.selectSchool 132 debug- ==>  Preparing: select id ,name ,parent_id,district_type,pinyin,pinyin_initial from districts where district_type = 'section' and deleted_at is null and is_open = 1 and name like concat(concat('%',?),'%') order by id
2016-03-11 16:06:29.682 [http-nio-8080-exec-8] DEBUG c.z.customer.base.dao.DistrictMapper.selectSchool 132 debug- ==> Parameters: sh' or 1 =1  or '' = '(String)
2016-03-11 16:06:29.705 [http-nio-8080-exec-8] DEBUG org.mybatis.spring.SqlSessionUtils 170 closeSqlSession- Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@647a5210]

调整后响应数据为空

{
  "success"true,
  "data": {
    "schools": []
  }
}

#号与$区别

#号表示参数,$代表一个字符串。

select a,b,c from table1 where id=#value# 传入参数后如:value="1"则可生成:select a,b,c from table1 where id=‘1’。
 
select a,b,c from table1 where city like '%$value$%' 传入参数后:value="berg"则可生成:select a,b,c from table1 where city like '%berg%'
 
${} 为原样输出,你传什么,sql里就填入什么。比如有引号它也会原样填到sql里。
 
#{} 会使用 PreparedStatement,变量处用 ? 代替。 在能使用 #{} 尽量使用它吧,可以防止sql注入。
 
在用#时,会在数据库里面生成一颗执行树,每一个参数就像是填空题一样被放入这颗执行树中。每次SQL执行时,执行树都是生成好的,不用重复解析SQL。同时也提升了SQL执行效率。

Mybatlis SQL 注入与防范的更多相关文章

  1. 常见sql注入的防范总结

    在平时的开发过程中,我们可能很少会刻意的去为项目做一个sql注入的防范,这是因为你可能因为使用了某些框架,而无意间已经有了对应sql注入的一些防范操作(比如mybatis使用#{XX}传参,属于预编译 ...

  2. Java开发工程师(Web方向) - 03.数据库开发 - 第3章.SQL注入与防范

    第3章--SQL注入与防范 SQL注入与防范 经常遇到的问题:数据安全问题,尤其是sql注入导致的数据库的安全漏洞 国内著名漏洞曝光平台:WooYun.org 数据库泄露的风险:用户信息.交易信息的泄 ...

  3. PHP SQL注入的防范

    说到网站安全就不得不提到SQL注入(SQL Injection),如果你用过ASP,对SQL注入一定有比较深的理解,PHP的安全性相对较高,这是因为MYSQL4以下的版本不支持子语句,而且当php.i ...

  4. php web开发安全之sql注入和防范:(一)简单的select语句注入和防范

    sql注入主要是指通过在get.post请求参数中构造sql语句,以修改程序运行时所执行的sql语句,从而实现获取.修改信息甚至是删除数据的目的,sql被注入的原因主要是代码编写的有问题(有漏洞),只 ...

  5. PHP 关于SQL注入的防范措施。

    最近在使用框架的时候还是有点不安,不知道框架的设计者有没有考虑到SQL-Injection的问题,我在顶层需不需要做一些必要的过滤等等,由 此我特意的去StackOverflow看了下,真是获益良多, ...

  6. sql 注入的防范(一)

    为了保证程序的健壮性,我们必须对用户输入的数据做有效性验证,防止用户恶意提交数据. 关于防止 sql 注入 我主要从三个方面入手: 1.确认为正整数的,强制转化为int,$id  =$_GET('id ...

  7. SQL注入与防范

    首先给大家看个例子: 1)小编首先在数据库中建立了一张测试表logintable,表内有一条测试信息: 然后写了个测试程序: package com.java.SqlInject; import ja ...

  8. MySQL 及 SQL 注入与防范方法

    所谓SQL注入,就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令. 我们永远不要信任用户的输入,我们必须认定用户输入的数据都是不安全的, ...

  9. MySQL防范SQL注入风险

    MySQL防范SQL注入风险 0.导读 在MySQL里,如何识别并且避免发生SQL注入风险 1.关于SQL注入 互联网很危险,信息及数据安全很重要,SQL注入是最常见的入侵手段之一,其技术门槛低.成本 ...

随机推荐

  1. Error:No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android

    https://www.jianshu.com/p/fd3d49c7f1f8 通过Android Studio 的Sdk Manager安装NDK,安装完之后编译失败,报错信息如下: Error:No ...

  2. QPS/TPS/并发量/系统吞吐量的概念

    我们在日常工作中经常会听到QPS/TPS这些名词,也会经常被别人问起说你的系统吞吐量有多大.这个问题从业务上来讲,可以理解为应用系统每秒钟最大能接受的用户访问量.或者每秒钟最大能处理的请求数: QPS ...

  3. Pushlet实现后台信息推送(二)

    上一篇日志利用推送源周期性地向订阅了某一事件的所有网页端推送信息,但怎么实现向特定的某一个用户推送信息呢,想象一个网络聊天室,怎么向单独的一个好友私聊呢.问题的关键就是那个SessionID,Push ...

  4. BOM 表

    ';--查看BOM创建日期时间 SELECT * FROM SAPSR3.ZSTPO_OUT2011_1@SAP_SEP; SELECT * FROM SAPSR3.ZSTPO_OUT2012_1@S ...

  5. Camstar :新加的modeling对象没有在 modeling的下拉框中显示

    解决: 对象的maint 的 advance option client ui 要打上勾.

  6. SSL、TLS中间人攻击

    生成私钥 生成一个ca.key私钥签名 然后用私钥生成一个伪造证书服务器的根证书 生成一个伪造证书服务器的根证书ca.crt 在攻击主机启动路由转发功能 或者echo 1 > /proc/sys ...

  7. try cache

    try{ $did = DB::insert('vmi_sales_orders',array_keys($value))->values($value)->execute('newerp ...

  8. php拓展

    https://github.com/phalcon/zephirhttp://blog.csdn.net/black_OX/article/details/43700707

  9. pta l2-1紧急救援(Dijkstra)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805073643683840 题意:给n个城市,m条边,每个城市 ...

  10. 第十一章 串 (c2)KMP算法:查询表