網頁被植入惡意腳本(如 <script src="https://ave3.cn></script>)是常見的安全問題。這類病毒通常透過檔案寫入漏洞注入靜態頁面,導致訪客瀏覽時載入外部惡意程式碼。本文提供一個 PHP 類別範例,示範如何掃描並清除指定檔案中的已知病毒特徵碼。
透過 file_get_contents() 讀取檔案內容,使用 strpos() 或 strrpos() 檢測是否存在病毒特徵字串,若發現則以 str_replace() 移除後寫回檔案。此為針對已知特徵碼的「事後清除」手段,無法防禦未知變種,實務上仍需配合 WAF、檔案權限控管、定期掃描等防護措施。
<?php
class ClearVirus {
public $index = 'b.html'; // 目標檔案
public $filepath = array('b.html'); // 可擴充為多檔案掃描
public $virus_type = "<script src=https://%61%76%65%33%2E%63%6E></script>"; // URL 編碼後的惡意腳本
public function open_file() {
if (file_exists($this->index)) {
$content = file_get_contents($this->index);
if (strrpos($content, $this->virus_type) !== false) {
$clean = str_replace($this->virus_type, '', $content);
$handle = fopen($this->index, 'w');
fwrite($handle, $clean);
fclose($handle);
echo "病毒已清除:" . $this->index;
} else {
echo "未發現病毒特徵碼。";
}
} else {
echo "檔案不存在:" . $this->index;
}
}
}
$scanner = new ClearVirus();
$scanner->open_file();
?>
glob() 或遞迴掃描整個網站目錄,並記錄清除日誌。若需更完整的掃描與清除,可考慮整合 PHP Malware Scanner 等開源專案,或使用 ClamAV、Linux Malware Detect (LMD) 等系統級工具定期掃描伺服器檔案。