btslab PHP SQL注入,btslab全攻略——注入之sql盲注1、2

之前做btslab的时候就想找篇完整教程来对照学习的,可是发现网上只有xss部分,并无完整攻略,所以就自己单独完成了btslab,整体感觉还是比较容易,不过对于新手还是很不错,可以学到很多漏洞知识与代码审计,为了造福刚入门或是想入门的新手,我打算开始出一系列教程,全面介绍btslab(尊重作者劳动成果,转载需注明出处,否则必究)

sql盲注1

维基百科对sql盲注的简介:

Blind SQL (Structured Query Language) injection is a type of SQL Injection attack that asks the database true or false questions and determines the answer based on the applications response. This attack is often used when the web application is configured to show generic error messages, but has not mitigated the code that is vulnerable to SQL injection.

When an attacker exploits SQL injection, sometimes the web application displays error messages from the database complaining that the SQL Query's syntax is incorrect. Blind SQL injection is nearly identical to normal SQL Injection, the only difference being the way the data is retrieved from the database. When the database does not output data to the web page, an attacker is forced to steal data by asking the database a series of true or false questions. This makes exploiting the SQL Injection vulnerability more difficult, but not impossible.

按照我对sql盲注的理解给大家解释一下,sql盲注和此前的注入不同在于,之前的可以直接显示查询的数据,就比如账号密码是有现实点可以直接看到的,而sql盲注通常只有两个显示结果,一是正常显示,另一个就是不正常显示,正常显示就是sql语句正确且查询到了数据,不正常显示就是sql语句的执行结果没有得到任何数据。

先来测试三个

9cbcbfa11be8d1f09e3c19d7778fc676.png

c7749ecfd3c6ff889d3d7df377e18010.png

bb875c2665251786e5e64d10cc4bac68.png

可以看到,就算查询成功也无1,2,3,4,5,6,7的回显(其实对于这里的盲注题我还是有办法回显的,不过此节重点在盲注,所以回显作为扩展内容在文末讲)

1.猜解数据库名

那么没有了回显是不是就不能知道数据库中的数据了呢?当然不是,没有了直接的显示,我们还可以利用逻辑判断啊,下面以猜解数据库名为例

测试

3f951470847915d7917e3ecbbc286b66.png

8e3a85b2e927d41523c0f54874a5591d.png

f7280af20233c937672cf425e43fa8af.png

最终确定了98这个ascii码(先使用二分法确定范围,再用等号确定具体值),对应的字符为'b',即数据库名"bts"的第一个字母'b'。

现在对上面做一下解释,substr()在mysql中是个切割字符串的函数,第一个参数为被操作的字符串,第二个为起始位置,第三个为长度,例如substr("hello",1,1)返回的是'h',substr("hello",2,1)返回的是'e',substr("hello",2,2)返回的是"el",ascii()把字符化为对应的ascii码,ascii('b')返回98。

所以我们可以把数据库的名字一个一个猜解出来,就像这样,ascii(substr(database(),1,1))、ascii(substr(database(),2,1))、ascii(substr(database(),3,1))。

有人可能想问这样猜解到什么时候为止呢?我又不知道数据库名的长度,其实这个无所谓,当超出长度后取出空字符'',而ascii('')返回0,比如ascii(substr("abc",4,1))返回0,所以如下语句是正确的

cd3ab8d0825e14d59afd5dafbe76af0f.png

这样便可以判断出,当前数据库名长度为3,当然也可以直接判断数据库名长度,如下

2.猜解表名

http://localhost/btslab/vulnerability/sqli/UserInfo.php?id=1 and  ascii( substr((select group_concat(table_name) from information_schema.tables where table_schema=database()),1,1))=109

613ee1d172517f4a1b375234aeff6cf2.png

乍一看很复杂,其实不然,先分开来看,这里假定str为一个字符串变量,sql为sql语句的一部分,上面就可以拆分为:

$str=(select group_concat(table_name) from information_schema.tables where table_schema=database())

$sql=and  ascii( substr($str,1,1))=109

看到$str就是一句sql查询语句,返回了一个字符串,这个字符串就是由多个表名组成,其中由逗号隔开。

根据这种构造便可以猜解后面的数据,如下

and  ascii( substr((select group_concat(table_name) from information_schema.tables where table_schema=database()),2,1))=?

and  ascii( substr((select group_concat(table_name) from information_schema.tables where table_schema=database()),3,1))=?

最终就会猜解出messages,posts,tdata,users(全手动注入工作量太大,建议用sqlmap,或者用python写脚本跑,至于怎么用python写盲注脚本不在此教程内容范围内,有空我会单独写篇文章来讲解)

3.猜解列名

和上面同理,我就写一条猜解users表的示例

http://localhost/btslab/vulnerability/sqli/UserInfo.php?id=1 and  ascii( substr((select group_concat(column_name) from information_schema.columns where table_name='users' and table_schema=database()),1,1))=73

0544a11b8b8acf139b522f772f2168d2.png

最终会得到ID,username,email,password,about,privilege,avatar

4.猜解数据

得到username为admin,password为5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8

sql盲注2

其实sql盲注2也完全可以用我以上的方法来猜解,但为什么这里要专门区分两个盲注呢,其实sql盲注1可以想办法让数据显示出来,来试试这条语句

878583b35f2817e0820f92a4cea0b1e7.png

看到有两个显示点,2和5,那么为什么之前没有显示呢,这是因为这里的服务端不管在后台查了多少条数据,都只会显示第一条,而之前的union联合查找不仅查出了id为1的文章,也查出了1,2,3,4,5,6,7这几个数,但是只显示一条,所以,只显示了首先查出的那个文章。

而这里看到我的id为0,然而数据库中没有id为0的文章,所以只查到1,2,3,4,5,6,7,自然就显示出来了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值