某日發現網站遭受大規模 CC 攻擊,單日攻擊次數高達 125,008 次。攻擊特徵:
原有防護機制(600 秒周期)已失效,攻擊者改用「攻擊 2 分鐘、停 5 分鐘」的智能輪流策略繞過防禦。
<?php
// 版本:Anti-IP attack code website 2.0
// 作者:www.mydalle.com
// 1. 檢查 IP 是否在黑名單
$ip = $_SERVER['REMOTE_ADDR'];
$fileht = ".htaccess2";
if (!file_exists($fileht)) file_put_contents($fileht, "");
$filehtarr = @file($fileht);
if (in_array($ip . "\r\n", $filehtarr)) {
die("Warning: Your IP is forbidden. Contact shop@mydalle.com");
}
// 2. 封禁檢查機制(120 秒觀察期)
$time = time();
$fileforbid = "log/forbidchk.dat";
if (file_exists($fileforbid)) {
if ($time - filemtime($fileforbid) > 30) {
unlink($fileforbid); // 超過 30 秒未達標,清除記錄
} else {
$fileforbidarr = @file($fileforbid);
if ($ip == substr($fileforbidarr[0], 0, strlen($ip))) {
if ($time - substr($fileforbidarr[1], 0, strlen($time)) > 120) {
unlink($fileforbid); // 觀察期結束
} elseif ($fileforbidarr[2] > 120) {
// 超過閾值,寫入黑名單
file_put_contents($fileht, $ip . "\r\n", FILE_APPEND);
unlink($fileforbid);
} else {
$fileforbidarr[2]++;
file_put_contents($fileforbid, $fileforbidarr);
}
}
}
}
// 3. 防刷新機制(60 秒內最多 5 次同頁請求)
$allowTime = 60;
$allowNum = 5;
$uri = $_SERVER['REQUEST_URI'];
$checkip = md5($ip);
$checkuri = md5($uri);
$file = "log/ipdate.dat";
if (!file_exists("log") && !is_dir("log")) mkdir("log", 0777);
if (!file_exists($file)) file_put_contents($file, "");
$ipdate = @file($file);
$str = "";
$yesno = true;
foreach ($ipdate as $v) {
$iptem = substr($v, 0, 32);
$uritem = substr($v, 32, 32);
$timetem = substr($v, 64, 10);
$numtem = substr($v, 74);
if ($time - $timetem < $allowTime) {
if ($iptem != $checkip) {
$str .= $v; // 保留其他 IP 記錄
} else {
$yesno = false;
if ($uritem != $checkuri) {
$str .= $iptem . $checkuri . $time . "1\r\n";
} elseif ($numtem < $allowNum) {
$str .= $iptem . $uritem . $timetem . ($numtem + 1) . "\r\n";
} else {
// 觸發封禁流程
if (!file_exists($fileforbid)) {
$addforbidarr = array($ip . "\r\n", time() . "\r\n", 1);
file_put_contents($fileforbid, $addforbidarr);
}
file_put_contents("log/forbided_ip.log", $ip . "--" . date("Y-m-d H:i:s", $time) . "--" . $uri . "\r\n", FILE_APPEND);
$timepass = $timetem + $allowTime - $time;
die("Warning: Don't refresh too frequently. Wait {$timepass} seconds.");
}
}
}
}
if ($yesno) $str .= $checkip . $checkuri . $time . "1\r\n";
file_put_contents($file, $str);
?>
index.php)最頂端log/ 目錄可寫入(權限 0777)$allowTime、$allowNum 參數log/forbided_ip.log 避免檔案過大部署新版本後連續測試兩天,成功攔截 62 個新攻擊 IP(多為土耳其),100% 精準識別攻擊行為,零誤殺正常用戶。
此程式碼僅供參考,實際部署需因地制宜調整參數。核心思想:時間窗口 + 頻率閾值 + 漸進式封禁,兼顧防護效果與用戶體驗。