简要了解web安全之sql注入

什么是sql注入?

  通俗来讲就是通过 将可执行sql语句作为参数 传入查询sql 中,在sql编译过程中 执行了传入进来的恶意 sql,从而 得到 不应该查到或者不应该执行的sql语句,对网站安全,信息 造成了威胁。

简单sql注入的几种情况

1.第一种情况:闭合 where条件 的语句来进行 查询 不属于自己 的内容

select * from account where mobile = '';   


   这是一条普通的sql语句。可见 where 条件  是按照  ''  英文引号 闭合来确定  条件是什么。

(1)假设我们传的参数是这样的  '' or 1= 1。则会闭合住引号从而导致查询出全库的数据来

http://local.com:8999/login/ajax-get-account?mobile='' or 1 = 1

 

 2.第二种情况:通过  特殊sql句中注释符号   –  来强行将后面的查询条件省略

http://local.com:8999/login/ajax-get-account?mobile='' or 1 = 1-- sql

 

3.第三种情况:通过传各种异常参数 通过你的 报错信息来确定你的数据库名,表名,甚至查询语句都是什么

 

http://local.com:8999/login/ajax-get-account?mobile='' or 1=(SELECT count(1) from acc)

 

"message": "\n### Error querying database.  Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'busi.acc' doesn't exist\n### The error may exist in com/itshare/jj/module/Account/dao/IAccountDAO.java (best guess)\n### The error may involve com.itshare.jj.module.Account.dao.IAccountDAO.getAccountsByMobile-Inline\n### The error occurred while setting parameters\n### SQL: select * from account.account where mobile = '' or 1=(SELECT count(1) from acc) and status = 0\n### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'business.acc' doesn't exist\n; bad SQL grammar []; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'business.acc' doesn't exist",

 

这是这次操作报出来的异常信息。能看出查询语句。库名,表名。

 

 

4.第四种情况:指定多条语句,通过闭合参数里面的   英文引号  + ; + 另一句sql + ; + – (忽略后面语句)  

例如 :

select * from account.account where mobile = '' or 1 = 1;update account set status = 1;-- in and status = 0

 

    目前我们系统用的是  mybatis,我们主要用他的预编译功能来避免上述问题,预编译之后,上述查询语句就变成了 

select * from account where mobile =  ; 

   可见  参数处成了占位符,参数和 语句分离运行,参数仅作为字符串参数 替换占位符,不再将参数中的任何语句编译,从而达到不执行 参数中语句的功能。但是这就没问题了吗,当然不是

(1)mybatis中   如果变量占位符用的是  #{变量}  那么会执行预编译动作,生成不会被注入的sql语句,但是使用${变量} 不会进行预编译,从而还是会出现  sql注入问题。

(2)mybatis  中的  like语句很多时候可能会和 $ 一起出现  。比如:'%${finalId}%'   其实可以替换成   concat('%', #{finalId}, '%');   所以可以通过 搜索  %$ 或者  $%  强行让开发替换,这里举个例子:

 

@Select("select * from fl.fpe_sting where status=1 and final_ids like '%${finalId}%' and is_del =0  ")
List<FpeSettingPO> selectFinalId(@Param("finalId") String finalId);

可见例子:

http://local.com:8999/login/ajax-get-account?mobile=' or 1=1 or mobile like '%

传了这么个奇怪的参数,结果查出了所有的账号

5.第五种情况:利用没有验证的 like型 参数查出不属于自己的东西
http://local.com:8999/login/ajax-get-account?mobile=1
posted @ 2017-11-26 23:30  jiguojing  阅读(513)  评论(0编辑  收藏  举报