首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

PHP 網頁病毒掃描與清除類實作指南

     編輯:PHP 相關知識

PHP 網頁病毒掃描與清除類實作指南

許多開發者都遇過網站檔案被植入惡意代碼的情況,PHP、ASP、HTML、JS 等檔案常在不知不覺中被插入木馬連結。本文提供一個實用的 PHP 類別 clear_virus,能自動掃描並清除受感染的檔案,省去人工逐一檢查的繁瑣工作。

核心原理

該類別運作流程如下:

  1. 讀取病毒特徵碼列表(virus.txt)
  2. 檢查指定的偵測檔案(e.php)是否含有惡意代碼
  3. 若偵測到感染,讀取待掃描檔案清單(save.txt)
  4. 遍歷清單中的每個檔案,根據特徵碼進行字串替換清除
  5. 記錄清除時間至 time.txt

完整程式碼實作

<?php
class clear_virus {
    public $infectFile = 'virus.txt';   // 病毒特徵碼列表
    public $savefile   = 'save.txt';    // 待掃描檔案清單
    public $timep      = 'time.txt';    // 清除時間記錄
    public $checkFile  = 'e.php';       // 偵測觸發檔案
    public $run        = 0;
    public $virus_type = [];
    public $replace    = '';
    public $filepath   = [];
    public $tag        = 0;

    public function open_file() {
        $this->read_virus();
        $this->check_File();
        if ($this->run) {
            $this->update_time();
            $this->read_file();
            foreach ($this->filepath as $tmppath) {
                if (file_exists($tmppath)) {
                    $tmp_file = file_get_contents($tmppath);
                    for ($i = 0; $i < count($this->virus_type); $i++) {
                        if (strpos($tmp_file, $this->virus_type[$i]) !== false) {
                            $tmp_file = str_replace($this->virus_type[$i], '', $tmp_file);
                            $this->tag = 1;
                        }
                    }
                    if ($this->tag) {
                        file_put_contents($tmppath, $tmp_file);
                        unset($tmp_file);
                    }
                }
            }
        }
    }

    private function check_File() {
        if (file_exists($this->checkFile)) {
            $temp = file_get_contents($this->checkFile);
            foreach ($this->virus_type as $v_tmp) {
                if (strpos($temp, $v_tmp) !== false) {
                    $this->run = 1;
                    break;
                }
            }
        } else {
            $this->show_error(5);
        }
    }

    private function update_time() {
        if (file_exists($this->timep)) {
            $tmp_time = date("Y-m-d H:i:s") . "\r\n";
            file_put_contents($this->timep, $tmp_time, FILE_APPEND);
        }
    }

    private function read_file() {
        if (file_exists($this->savefile)) {
            $this->filepath = file($this->savefile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
        } else {
            $this->show_error(4);
        }
    }

    private function read_virus() {
        if (file_exists($this->infectFile)) {
            $this->virus_type = file($this->infectFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
        } else {
            $this->show_error(2);
        }
    }

    private function show_error($number) {
        $errors = [
            1 => '病毒特徵碼檔案無法讀取!',
            2 => '病毒特徵碼列表檔案不存在!',
            3 => '檔案清單格式錯誤',
            4 => '待掃描檔案清單不存在',
            5 => $this->checkFile . ' 不存在,請設定觸發檢測的檔案'
        ];
        echo $errors[$number] ?? '未知錯誤';
    }
}

$virus = new clear_virus();
$virus->open_file();
?>

使用說明

  1. 準備 virus.txt,每行一個惡意代碼特徵字串
  2. 在 save.txt 列出需掃描的檔案路徑(每行一個)
  3. 建立觸發檔案 e.php(可包含常被感染的代碼片段)
  4. 執行腳本即可自動清除

注意事項

  • 執行前請務必備份網站檔案
  • 特徵碼需精準,避免誤刪正常代碼
  • 建議搭配定時任務定期執行
  • 生產環境建議搭配更完善的 WAF 方案
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved