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

PHP 清除 WordPress 惡意代碼:批量掃描與自動修復腳本

     編輯:PHP 綜合知識

針對 WordPress 站點被注入惡意代碼(如 eval(gzinflate(base64_decode(...)))、混淆變數名、駭客後門)的緊急清理方案。提供兩版腳本:單次掃描版與支援大量檔案的遞歸版,均含自動備份機制。

惡意代碼特徵識別

典型特徵:

  • 開頭 <?php if(!isset($GLOBALS["\x61\156\x75\156\x61"])) 十六進制混淆
  • 包含 $bssaiikhvn、$jtgibaqypx 等隨機變數名
  • 大量 %x5c%x78 URL 編碼字符串拼接
  • 結尾 $jtgibaqypx=$urvbwkljhb; $jtgibaqypx=(775-654); $bssaiikhvn=$jtgibaqypx-1; ?>

正則匹配規則

$pattern = '@<\?php\s*if\(!isset\(\$GLOBALS\["\\\x61\\\156\\\x75\\\156\\\x61"\]\)\).*\$bssaiikhvn=\$jtgibaqypx-1;\s*\?>@si';
  • @...@si:s 讓 . 匹配換行,i 忽略大小寫
  • 匹配從 <?php 到結尾特徵代碼的整個惡意區塊

版本一:單目錄掃描修復(delUnwantedCode.php)

<?php
$path = __DIR__;
$bakPath = $path . DIRECTORY_SEPARATOR . basename(__FILE__, '.php');
$fileType = ['php'];
$search = [$pattern]; // 上述正則

$stats = ['all'=>0, 'clean'=>0, 'infected'=>0];

$files = listDir($path, $fileType);
foreach ($files as $file) {
    $stats['all']++;
    $content = file_get_contents($file);
    $clean = preg_replace($search, '', $content);

    if (strlen($content) !== strlen($clean)) {
        $stats['infected']++;
        // 備份
        $bakFile = str_replace($path, $bakPath, $file);
        @mkdir(dirname($bakFile), 0777, true);
        file_put_contents($bakFile, $content);
        // 寫入清理後內容
        file_put_contents($file, $clean);
    } else {
        $stats['clean']++;
    }
}
echo sprintf('掃描 %d 個檔案,清理 %d 個感染檔案,備份至 %s', $stats['all'], $stats['infected'], $bakPath);

// 遞歸列出目錄下所有 php 檔
function listDir($dir, $exts) {
    $res = [];
    $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
    foreach ($it as $f) {
        if ($f->isFile() && in_array(strtolower($f->getExtension()), $exts)) {
            $res[] = $f->getPathname();
        }
    }
    return $res;
}

版本二:大量檔案遞歸處理(delAllUnwantedCode.php)

<?php
set_time_limit(0);
ignore_user_abort(true);

$path = __DIR__;
$bakPath = $path . DIRECTORY_SEPARATOR . basename(__FILE__, '.php');
$fileType = ['php'];
$search = [$pattern];
$stats = ['all'=>0, 'fixed'=>0];

replaceUnwantedCode($path);

echo sprintf('完成:掃描 %d,修復 %d,備份 %s', $stats['all'], $stats['fixed'], $bakPath);

function replaceUnwantedCode($dir) {
    global $bakPath, $fileType, $search, $stats;
    $it = new DirectoryIterator($dir);
    foreach ($it as $f) {
        if ($f->isDot()) continue;
        $path = $f->getPathname();
        if ($f->isDir()) {
            replaceUnwantedCode($path);
        } elseif (in_array(strtolower($f->getExtension()), $fileType)) {
            $stats['all']++;
            $content = file_get_contents($path);
            $clean = preg_replace($search, '', $content);
            if (strlen($content) !== strlen($clean)) {
                $stats['fixed']++;
                $bakFile = str_replace($dir, $bakPath, $path);
                @mkdir(dirname($bakFile), 0777, true);
                file_put_contents($bakFile, $content);
                file_put_contents($path, $clean);
            }
        }
    }
}

部署與執行步驟

  1. 將腳本上傳至 WordPress 根目錄(或 wp-content、plugins、themes 子目錄)
  2. 瀏覽器訪問 https://yoursite.com/delUnwantedCode.php 或 CLI 執行 php delAllUnwantedCode.php
  3. 查看輸出統計,確認清理數量
  4. **立即刪除腳本檔案**,防止被攻擊者利用
  5. 檢查備份目錄確認原始檔案完整

事後加固清單

  • 更新 WordPress 核心、插件、主題至最新版
  • 刪除未使用的插件/主題
  • 安裝安全插件(Wordfence、Sucuri、iThemes Security)
  • 強制強密碼、限制登入嘗試、啟用 2FA
  • 定期備份檔案與資料庫(異地備份)
  • 檢查 wp-config.php、.htaccess、index.php 是否被篡改
  • 資料庫檢查 wp_options、wp_posts、wp_users 可疑內容

原理局限說明

僅針對**已知特徵**的惡意代碼變體。新型混淆、多態病毒、文件少型後門需人工排查或專業掃描工具(如 maldet、clamav、Wordfence CLI)。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved