Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> 關於 Linux

Linux 服務器中毒應急響應:find + grep 快速定位 Webshell 與後門

編輯:關於 Linux

Web 服務器被植入 Webshell、挖礦程式、反彈 Shell 是常見安全事件。利用 find 定位可疑文件,配合 grep 特徵碼掃描,是應急排查的核心技能。本文提供實戰級掃描腳本、特徵庫分類與清理建議。

Webshell 掃描流程圖

核心掃描命令模板

# 基礎模板:find 定位文件 + xargs grep 內容掃描 + 輸出結果
find /path/to/webroot -type f -name "*.php" | xargs grep -l "SUSPICIOUS_PATTERN" > /tmp/scan_result.txt

# 實戰高頻掃描組合
# 1. 高危函數調用
find /var/www -type f -name "*.php" | xargs grep -l "eval\s*(" > /tmp/eval.txt
find /var/www -type f -name "*.php" | xargs grep -l "assert\s*(" > /tmp/assert.txt
find /var/www -type f -name "*.php" | xargs grep -l "system\s*(" > /tmp/system.txt
find /var/www -type f -name "*.php" | xargs grep -l "shell_exec\s*(" > /tmp/shell_exec.txt

# 2. 編碼混淆特徵
find /var/www -type f -name "*.php" | xargs grep -l "base64_decode" > /tmp/base64.txt
find /var/www -type f -name "*.php" | xargs grep -l "gzuncompress\|gzinflate" > /tmp/gzip.txt

# 3. 一句話後門特徵
find /var/www -type f -name "*.php" | xargs grep -l '\$_\(POST\|GET\|REQUEST\|COOKIE\)' > /tmp/superglobals.txt

分類特徵碼庫(持續更新)

類別特徵碼示例威脅等級
代碼執行eval、assert、system、exec、shell_exec、passthru、popen、proc_openCritical
編碼混淆base64_decode、gzuncompress、gzinflate、str_rot13、chr(High
超級全局變量直接調用\$_POST[、\$_GET[、\$_REQUEST[、\$_COOKIE[High
文件操作file_put_contents、fwrite、fputs、copy、move_uploaded_fileMedium
已知 Webshell 簽名c99shell、r57shell、phpspy、webshell、WSO、b374kCritical
數據庫風險into outfile、load_file、UNION SELECTMedium
.htaccess 惡意配置SetHandler application/x-httpd-php、auto_prepend_file、auto_append_fileHigh

一鍵掃描腳本(生產環境可直接用)

#!/bin/bash
# webshell_scan.sh
WEBROOT="/var/www/html"
OUTDIR="/tmp/webshell_scan_$(date +%F_%H%M)"
mkdir -p "$OUTDIR"

patterns=(
    "eval\s*\("
    "assert\s*\("
    "base64_decode"
    "gzuncompress"
    "gzinflate"
    "shell_exec"
    "system\s*\("
    "passthru"
    "exec\s*\("
    "popen"
    "proc_open"
    "str_rot13"
    "file_put_contents"
    "fwrite"
    "\$_\(POST\|GET\|REQUEST\|COOKIE\)"
    "c99shell|r57shell|phpspy|webshell|WSO|b374k"
)

for p in "${patterns[@]}"; do
    find "$WEBROOT" -type f -name "*.php" -print0 | \
    xargs -0 grep -lE "$p" 2>/dev/null > "$OUTDIR/$(echo $p | tr -cd 'a-zA-Z0-9').txt"
done

echo "掃描完成,結果在 $OUTDIR"
ls -lh "$OUTDIR"

輔助排查:時間維度定位

# 查找 24 小時內被修改的 PHP 文件(極大概率被植入)
find /var/www -type f -name "*.php" -mtime -1 -ls

# 查找異常權限文件
find /var/www -type f -name "*.php" ! -perm 644 -ls

# 查找異常擁有者文件
find /var/www -type f -name "*.php" ! -user www-data -ls

清理與加固建議

  1. 隔離而非直接刪除: mv suspicious.php suspicious.php.quarantine 保留證據
  2. 權限收斂: find /var/www -type f -name "*.php" -exec chmod 444 {} \; find /var/www -type d -exec chmod 555 {} \;
  3. 禁用危險函數: php.ini 添加 disable_functions = eval,assert,system,exec,shell_exec,passthru,popen,proc_open,...
  4. 部署 WAF/RASP: ModSecurity、OpenRASP、SafeLine 等實時攔截
  5. 文件完整性監控: AIDE、Tripwire 監控核心目錄變更

總結

「find 定位 + grep 特徵 + 時間關聯 + 權限審計」四維掃描體系,配合特徵庫持續更新與自動化腳本,能將 Webshell 檢出率提升至 95% 以上。記住:掃描只是開始,權限收斂、函數禁用、流量監控才是防線。

Copyright © Windows教程網 All Rights Reserved