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

PHP 掃描清除網頁木馬/惡意腳本:字串替換與特徵庫防護

     編輯:PHP 相關知識

基本清除原理

讀取檔案 → 搜尋惡意特徵字串 → 替換/移除 → 寫回檔案。

基礎掃描類別

<?php
class WebShellCleaner {
    private $targetFiles = [];
    private $signatures = [
        // 常見單行木馬
        'eval(base64_decode',
        'eval(gzinflate',
        'assert(',
        'preg_replace.*\/e',
        'create_function',
        'system(',
        'shell_exec(',
        'passthru(',
        'exec(',
        '$_GET[', '$_POST[', '$_REQUEST[', '$_COOKIE[',
        '@eval', '@assert',
        'GLOBALS[',
        'file_put_contents',
        'fwrite',
        'base64_decode',
        'gzinflate',
        'str_rot13',
    ];

    public function addTarget($path) {
        if (is_file($path)) $this->targetFiles[] = $path;
        elseif (is_dir($path)) $this->scanDir($path);
    }

    private function scanDir($dir) {
        $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
        foreach ($it as $file) {
            if ($file->isFile() && in_array($file->getExtension(), ['php', 'inc', 'phtml', 'php5'])) {
                $this->targetFiles[] = $file->getPathname();
            }
        }
    }

    public function scan() {
        $results = [];
        foreach ($this->targetFiles as $file) {
            $content = file_get_contents($file);
            $matches = [];
            foreach ($this->signatures as $sig) {
                if (stripos($content, $sig) !== false) {
                    $matches[] = $sig;
                }
            }
            if ($matches) {
                $results[$file] = $matches;
            }
        }
        return $results;
    }

    public function clean($dryRun = true) {
        $cleaned = 0;
        foreach ($this->targetFiles as $file) {
            $content = file_get_contents($file);
            $original = $content;
            foreach ($this->signatures as $sig) {
                $content = str_ireplace($sig, '', $content);
            }
            if ($content !== $original) {
                if (!$dryRun) {
                    file_put_contents($file, $content);
                }
                $cleaned++;
            }
        }
        return $cleaned;
    }
}

// 使用
$cleaner = new WebShellCleaner();
$cleaner->addTarget('/var/www/html');
$results = $cleaner->scan();
print_r($results);
// 確認無誤後
$cleaner->clean(false);
?>

進階特徵庫來源

  • PHP-Shell-Detector — 知名開源掃描器
  • NAXSI — Nginx WAF 規則庫
  • Malware Signatures: php-malware-finder, Lokimedia 等專案

防護最佳實踐(比清除更重要)

  1. 檔案權限:網站目錄 555、上傳目錄 755 + 禁用腳本執行
  2. 停用危險函數:php.ini disable_functions = eval,assert,exec,shell_exec,system,passthru,popen,proc_open,...
  3. open_basedir 限制:限制 PHP 存取路徑
  4. WAF/防火牆:ModSecurity + OWASP CRS、Cloudflare WAF
  5. 定期備份 + 離線備份:遭加密勒索可恢復
  6. 完整性監控:AIDE、Tripwire、或簡單 find /var/www -type f -name '*.php' -exec md5sum {} \; 建立基線

誤報處理

合法代碼可能包含特徵字串(如 base64_decode 用於圖片處理)。建議:

  • 掃描結果人工複核
  • 結合語法分析(PHP-Parser)而非單純字串匹配
  • 白名單機制排除已知正常檔案
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved