PHP Webshell(如蠕蟲、大馬、一句話木馬)是網站被植入後門的主要形式。利用 Shell 命令結合正則特征掃描、近期文件變更監控、權限最小化,可構建輕量級應急響應體系。本文提供生產可用的掃描命令、自動化腳本模板與防禦加固清單。
常見危險函數與混淆模式:
eval、assert、system、exec、shell_exec、passthru、proc_open、popenbase64_decode、gzuncompress、gzinflate、str_rot13、chr、ord$_POST[、$_GET[、$_REQUEST[、$_COOKIE[、$_FILES[file_put_contents、fwrite、copy、move_uploaded_filec99shell、r57shell、phpspy、wso、b374k、milw0rm
find /var/www -type f -name "*.php" -print0 | \
xargs -0 egrep -l "(eval\(|assert\(|system\(|exec\(|shell_exec\(|passthru\(|proc_open\(|popen\()" 2>/dev/null | \
sort -u > /tmp/webshell_suspect.txt
eval($_POST[...]) 模式)
grep -r --include="*.php" -E '[^a-zA-Z0-9_](eval|assert)\s*\(\s*\$_(POST|GET|REQUEST|COOKIE)\b' /var/www \
> /tmp/one_liner_webshell.txt 2>/dev/null
file_put_contents($_POST...))
grep -r --include="*.php" -E 'file_put_contents\s*\(.*\$_(POST|GET|REQUEST)\b' /var/www \
> /tmp/file_put_webshell.txt 2>/dev/null
find /var/www -type f -name "*.php" -print0 | \
xargs -0 egrep -l "eval\s*\(\s*(gzuncompress|gzinflate|base64_decode)\s*\(" 2>/dev/null | \
sort -u > /tmp/encoded_payload.txt
find /var/www -type f -name "*.php" -mtime -1 -printf "%T@ %p\n" | \
sort -rn | head -50 > /tmp/recent_modified_php.txt
#!/bin/bash
# /usr/local/bin/scan_webshell.sh
# 每日運行,發現可疑文件郵件告警
WEB_ROOT="/var/www"
REPORT_DIR="/var/log/webshell_scan"
DATE=$(date +%F)
REPORT="$REPORT_DIR/scan_$DATE.log"
SUSPECT_COUNT=0
mkdir -p "$REPORT_DIR"
echo "=== Webshell Scan Report $DATE ===" > "$REPORT"
echo "Scan root: $WEB_ROOT" >> "$REPORT"
echo "Start: $(date)" >> "$REPORT"
echo "" >> "$REPORT"
# 1. 近期修改文件
echo "[1] Recently modified PHP files (24h):" >> "$REPORT"
find "$WEB_ROOT" -type f -name "*.php" -mtime -1 >> "$REPORT" 2>/dev/null
# 2. 危險函數調用
echo -e "\n[2] Dangerous function calls:" >> "$REPORT"
find "$WEB_ROOT" -type f -name "*.php" -print0 | \
xargs -0 egrep -l "eval\(|assert\(|system\(|exec\(|shell_exec\(|passthru\(|file_put_contents\(" 2>/dev/null | \
sort -u >> "$REPORT"
# 3. 已知 Webshell 簽名
echo -e "\n[3] Known webshell signatures:" >> "$REPORT"
find "$WEB_ROOT" -type f -name "*.php" -print0 | \
xargs -0 egrep -li "c99shell|r57shell|phpspy|wso|b374k|milw0rm" 2>/dev/null | \
sort -u >> "$REPORT"
SUSPECT_COUNT=$(grep -c "^/" "$REPORT" 2>/dev/null || echo 0)
echo -e "\n=== Summary: $SUSPECT_COUNT suspicious files ===" >> "$REPORT"
echo "End: $(date)" >> "$REPORT"
# 發現可疑文件發送告警
if [ "$SUSPECT_COUNT" -gt 0 ]; then
mail -s "[ALERT] Webshell Scan: $SUSPECT_COUNT suspects on $(hostname)" admin@example.com < "$REPORT"
fi
chmod +x /usr/local/bin/scan_webshell.sh
# 加入 crontab 每天凌晨 2 點執行
echo "0 2 * * * /usr/local/bin/scan_webshell.sh" >> /etc/crontab
# 站點目錄:所有者 root,組 www-data,權限 750
chown -R root:www-data /var/www/html
find /var/www/html -type d -exec chmod 750 {} \;
find /var/www/html -type f -exec chmod 640 {} \;
# 僅寫入目錄(uploads、cache、runtime)開放組寫入
chmod -R 770 /var/www/html/uploads
chmod -R 770 /var/www/html/runtime
# 關鍵:寫入目錄禁止 PHP 執行(見下文 Nginx/Apache 配置)
disable_functions = exec,system,shell_exec,passthru,proc_open,popen,pcntl_exec, \
assert,eval,phpinfo,dl,symlink,link,chroot,chgrp,chown, \
ini_set,ini_alter,putenv,set_time_limit,ignore_user_abort
注意:部分框架(如 Laravel/ThinkPHP)可能依賴putenv、proc_open,需按實際情況調整白名單。
Nginx:
location ~ ^/uploads/.*\.php$ {
deny all;
return 403;
}
location ~ ^/runtime/.*\.php$ {
deny all;
}
Apache (.htaccess):
Order Deny,Allow
Deny from all
# php.ini 或 .user.ini
open_basedir = /var/www/html/:/tmp/:/var/lib/php/session/
mv suspect.php /tmp/quarantine/grep "suspect.php" /var/log/nginx/access.log,定位攻擊者 IP、時間、參數iptables -I INPUT -s attacker_ip -j DROPWebshell 查殺是動態對抗過程,單純特征匹配無法捕獲 0day 或高度混淆樣本。建立「定時掃描 + 文件完整性監控 + 權限最小化 + 應用層 WAF」纵深防禦體系,才是長久之計。