提供一個獨立文件 checkpostandget.php,在每個 PHP 文件開頭 include,自動合併 $_POST、$_GET (兼容舊版 $HTTP_POST_VARS),遍歷檢測是否包含黑名單關鍵字 (', ;, union 等),發現則彈窗警告並跳轉。
<?php
$ArrFiltrate = array("'", ";", "union"); // 黑名單
$StrGoUrl = ""; // 跳轉目標
function FunStringExist($StrFiltrate, $ArrFiltrate) {
foreach ($ArrFiltrate as $value) {
if (eregi($value, $StrFiltrate)) return true; // 不區分大小寫匹配
}
return false;
}
// 合併 POST/GET (兼容 PHP 4 無 array_merge)
if (function_exists('array_merge')) {
$ArrPostAndGet = array_merge($HTTP_POST_VARS, $HTTP_GET_VARS);
} else { /* 手動合併 */ }
// 驗證循環
foreach ($ArrPostAndGet as $value) {
if (FunStringExist($value, $ArrFiltrate)) {
echo "";
// ... 跳轉邏輯
exit;
}
}
?>
UN/**/ION, UnIoN, 0x756e696f6e (Hex), CHAR(117,110,105,111,110)。', ;, union。缺少 select, insert, update, delete, drop, or, and, where, load_file, outfile, into, --, #, /* 等。id=1 OR 1=1 無需引號/分號/union 即可繞過。eregi (PHP 5.3 棄用, 7.0 移除),$HTTP_*_VARS (PHP 4.1+ 棄用, 5.4 移除)。header('Location: ...') 服務端跳轉。// 1. 參數化查詢 (根治)
$stmt = $pdo->prepare("SELECT * FROM articles WHERE title LIKE ?");
$stmt->execute(["%{$keyword}%"]);
// 2. 輸入驗證 (白名單/類型)
$id = (int)$_GET['id']; // 強制整數
// 3. 輸出編碼 (防 XSS)
echo htmlspecialchars($title, ENT_QUOTES, 'UTF-8');
// 4. 最小權限數據庫賬號
// GRANT SELECT, INSERT, UPDATE ON db.* TO 'web_user'@'localhost';
// 5. WAF/RASP 作為纵深防禦
// ModSecurity + OWASP CRS / OpenRASP
黑名單過濾是**安全劇場**,提供虛假安全感,極易繞過且誤傷業務。**參數化查詢 (預處理語句) 是防止 SQL 注入的唯一正確方案**。現代框架 (Laravel, Symfony, ThinkPHP 等) ORM/查詢構建器底層均使用預處理,開發者應杜絕手動拼接 SQL。