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

# 基礎模板: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_open | Critical |
| 編碼混淆 | base64_decode、gzuncompress、gzinflate、str_rot13、chr( | High |
| 超級全局變量直接調用 | \$_POST[、\$_GET[、\$_REQUEST[、\$_COOKIE[ | High |
| 文件操作 | file_put_contents、fwrite、fputs、copy、move_uploaded_file | Medium |
| 已知 Webshell 簽名 | c99shell、r57shell、phpspy、webshell、WSO、b374k | Critical |
| 數據庫風險 | into outfile、load_file、UNION SELECT | Medium |
| .htaccess 惡意配置 | SetHandler application/x-httpd-php、auto_prepend_file、auto_append_file | High |
#!/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
mv suspicious.php suspicious.php.quarantine 保留證據find /var/www -type f -name "*.php" -exec chmod 444 {} \; find /var/www -type d -exec chmod 555 {} \;php.ini 添加 disable_functions = eval,assert,system,exec,shell_exec,passthru,popen,proc_open,...「find 定位 + grep 特徵 + 時間關聯 + 權限審計」四維掃描體系,配合特徵庫持續更新與自動化腳本,能將 Webshell 檢出率提升至 95% 以上。記住:掃描只是開始,權限收斂、函數禁用、流量監控才是防線。
Linux 重啟 Oracle 資料庫三大實戰方法:SQL*Plus、dbstart 與 lsnrctl 完整流程
Ubuntu 安裝 Visual Studio Code 完整指南:Ubuntu Make 命令列安裝、官方 .deb 套件與 Flatpak/Snap 多種方式比較
Linux Ncurses 中文亂碼徹底解決:locale 設置、寬字符庫、編譯鏈路完整指南
Linux 使用 PAM (pam_tally2) 限制使用者登入失敗次數與自動解鎖
Linux 入門必學指令精選:六大分類 30+ 核心指令與實戰分類速查表