網頁被植入惡意 Script 是常見的安全問題。本文提供一個 PHP 類別範例,示範如何掃描指定 HTML 檔案,並移除特定的惡意腳本標籤。此方法適用於已知病毒特徵碼的清除情境。
<?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>";
function open_file() {
if (file_exists($this->index)) {
$tmp = file_get_contents($this->index);
if (strrpos($tmp, $this->virus_type) !== false) {
$temp = str_replace($this->virus_type, '', $tmp);
$handle = fopen($this->index, 'w');
fwrite($handle, $temp);
fclose($handle);
echo "病毒已清除。";
} else {
echo "未發現指定病毒特徵。";
}
} else {
echo "目標檔案不存在。";
}
}
}
$virus = new ClearVirus();
$virus->open_file();
?>
file_get_contents 將整個 HTML 內容讀入字串。strrpos 檢查惡意腳本字串是否存在(採用 URL 編碼後的特徵碼)。str_replace 將其移除,並將乾淨內容寫回原檔案。filepath 陣列並迴圈處理。透過簡單的字串替換即可清除特定注入腳本,但這只是應急處理。根本解決仍需修補網站漏洞(如檔案上傳驗證、SQL 注入防護)並定期檢核檔案完整性。