萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 作業系統 >  >> Linux 教學

Linux Shell 掃描 PHP Webshell 實戰:特征匹配、文件監控與權限加固

PHP Webshell(如蠕蟲、大馬、一句話木馬)是網站被植入後門的主要形式。利用 Shell 命令結合正則特征掃描、近期文件變更監控、權限最小化,可構建輕量級應急響應體系。本文提供生產可用的掃描命令、自動化腳本模板與防禦加固清單。

核心特征庫(持續更新)

常見危險函數與混淆模式:

  • 代碼執行:eval、assert、system、exec、shell_exec、passthru、proc_open、popen
  • 編碼混淆:base64_decode、gzuncompress、gzinflate、str_rot13、chr、ord
  • 超全局變量直接調用:$_POST[、$_GET[、$_REQUEST[、$_COOKIE[、$_FILES[
  • 文件操作:file_put_contents、fwrite、copy、move_uploaded_file
  • 知名 Webshell 簽名:c99shell、r57shell、phpspy、wso、b374k、milw0rm

一鍵掃描命令集(可直接執行)

1. 綜合特征掃描(輸出文件路徑去重)


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

2. 一句話木馬專項(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

3. 文件寫入型後門(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

4. 編碼混淆載荷掃描(base64/gzip 多層嵌套)


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

5. 近 24 小時被修改的 PHP 文件(應急排查首選)


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

權限加固:事前預防勝於事後查殺

1. 文件權限最小化(核心防線)


# 站點目錄:所有者 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 配置)

2. 禁用危險 PHP 函數(php.ini)


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,需按實際情況調整白名單。

3. Web 服務器層面禁止寫入目錄執行 PHP

Nginx:


location ~ ^/uploads/.*\.php$ {
    deny all;
    return 403;
}
location ~ ^/runtime/.*\.php$ {
    deny all;
}

Apache (.htaccess):



    Order Deny,Allow
    Deny from all

4. open_basedir 限制跨目錄訪問


# php.ini 或 .user.ini
open_basedir = /var/www/html/:/tmp/:/var/lib/php/session/

應急響應檢查清單(發現 Webshell 後)

  1. 隔離:將可疑文件移至隔離區 mv suspect.php /tmp/quarantine/
  2. 溯源:檢查訪問日誌 grep "suspect.php" /var/log/nginx/access.log,定位攻擊者 IP、時間、參數
  3. 清理:比對備份文件,恢復被篡改代碼
  4. 封禁:防火牆封禁攻擊 IP iptables -I INPUT -s attacker_ip -j DROP
  5. 加固:修復漏洞入口(文件上傳驗證、SQL 注入、反序列化等)
  6. 全盤掃描:運行完整掃描腳本,排查殘留後門

Webshell 查殺是動態對抗過程,單純特征匹配無法捕獲 0day 或高度混淆樣本。建立「定時掃描 + 文件完整性監控 + 權限最小化 + 應用層 WAF」纵深防禦體系,才是長久之計。

copyright © 萬盛學電腦網 all rights reserved