網站被掛馬、植入 Webshell 後,find 配合 xargs grep 能在幾秒內掃出可疑檔案。本文提供**即用型指令模板**,並整理涵蓋 `eval`、`base64_decode`、`assert`、一句話木馬、資料庫操作、.htaccess 竄改等 50+ 高頻特徵碼,可直接貼上執行。

# 掃描 wwwroot 目錄下所有 .php,搜尋關鍵字,結果存檔 find wwwroot/ -type f -name '*.php' | xargs grep -l "關鍵字" > suspicious.txt
-l:僅輸出檔案名(不顯示匹配行),適合後續人工審核。-i:忽略大小寫(建議加上)。-r:`grep -r` 可直接遞歸,**無需 find**,單指令更簡潔:grep -ril "關鍵字" wwwroot/ > suspicious.txt
# 1. 核心危險函數
grep -ril "eval(" wwwroot/ > wwwroot/eval.txt
grep -ril "assert(" wwwroot/ > wwwroot/assert.txt
grep -ril "system(" wwwroot/ > wwwroot/system.txt
grep -ril "passthru(" wwwroot/ > wwwroot/passthru.txt
grep -ril "shell_exec(" wwwroot/ > wwwroot/shell_exec.txt
grep -ril "exec(" wwwroot/ > wwwroot/exec.txt
grep -ril "popen(" wwwroot/ > wwwroot/popen.txt
grep -ril "proc_open" wwwroot/ > wwwroot/proc_open.txt
# 2. 編碼/加密後門
grep -ril "base64_decode(" wwwroot/ > wwwroot/base64_decode.txt
grep -ril "gzinflate(" wwwroot/ > wwwroot/gzinflate.txt
grep -ril "gzuncompress(" wwwroot/ > wwwroot/gzuncompress.txt
grep -ril "gzdecode(" wwwroot/ > wwwroot/gzdecode.txt
# 3. 一句話木馬特徵
grep -ril 'eval($_' wwwroot/ > wwwroot/eval_post.txt
grep -ril 'assert($_' wwwroot/ > wwwroot/assert_post.txt
grep -ril '$_POST\[' wwwroot/ > wwwroot/post_array.txt
grep -ril '$_GET\[' wwwroot/ > wwwroot/get_array.txt
grep -ril '$_REQUEST\[' wwwroot/ > wwwroot/request_array.txt
grep -ril '$_COOKIE\[' wwwroot/ > wwwroot/cookie_array.txt
# 4. 檔案寫入/上傳後門
grep -ril "file_put_contents(" wwwroot/ > wwwroot/file_put_contents.txt
grep -ril "fwrite(" wwwroot/ > wwwroot/fwrite.txt
grep -ril "fputs(" wwwroot/ > wwwroot/fputs.txt
# 5. .htaccess 竄改
grep -ril "SetHandler application/x-httpd-php" wwwroot/ > wwwroot/htaccess.txt
grep -ril "auto_prepend_file" wwwroot/ > wwwroot/prepend.txt
grep -ril "auto_append_file" wwwroot/ > wwwroot/append.txt
# 6. 已知 Webshell 簽名
grep -ril "c99shell" wwwroot/ > wwwroot/c99.txt
grep -ril "r57shell" wwwroot/ > wwwroot/r57.txt
grep -ril "webshell" wwwroot/ > wwwroot/webshell.txt
grep -ril "phpspy" wwwroot/ > wwwroot/phpspy.txt
| 分類 | 特徵碼關鍵字 |
|---|---|
| 代碼執行 | eval(, assert(, system(, passthru(, shell_exec(, exec(, popen(, proc_open(, pcntl_exec( |
| 編碼解碼 | base64_decode(, gzinflate(, gzuncompress(, gzdecode(, str_rot13(, rawurldecode( |
| 一句話木馬 | eval($_POST, assert($_POST, $_POST[, $_GET[, $_REQUEST[, $_COOKIE[, call_user_func(, array_map(, filter_var( |
| 檔案操作 | file_put_contents(, fwrite(, fputs(, fopen(, copy(, move_uploaded_file( |
| 資料庫高危 | load_file(, into outfile, into dumpfile, UDF, create function |
| 已知馬簽名 | c99shell, r57shell, phpspy, b374k, WSO, webshell, c100, tiny, mini |
| .htaccess | SetHandler application/x-httpd-php, auto_prepend_file, auto_append_file, php_value, php_flag |
`grep -ril` 是應急響應的核武器。將上述特徵碼整理為腳本 `scan_webshell.sh` 納入巡檢體系,**定期掃描、異動告警、快速溯源**,將損失降至最低。