此腳本遞迴掃描目錄下 .php、.inc、.html、.htm 檔案,偵測以下特徵:
include/require(_once)? 引入非 PHP/INC 副檔名檔案eval|proc_open|popen|shell_exec|exec|passthru|system|assert|fwrite|create_function 危險函數呼叫`command` 執行系統指令$_POST|$_GET|$_REQUEST 直接傳入函數呼叫import os, re, sys, time
def listdir(dirs, recurse='0'):
flog = open('check_php_shell.log','a+')
for f in os.listdir(dirs):
fp = os.path.join(dirs,f)
if os.path.isdir(fp) and recurse=='1': listdir(fp,'1')
elif os.path.isfile(fp) and re.search(r'\.(php|inc|html?)$',f,re.I):
scan_file(fp, flog)
flog.close()
python scan.py /var/www/html # 單層掃描
python scan.py /var/www/html 1 # 遞迴掃描子目錄
[/var/www/html/shell.php] :
|-- [eval] line [12]
|-- [system] line [45]
|-- [`] command is [ls -la] in line [78]
|-- [$_POST] line [102]
部署建議:定期排程執行、配合 log 分析告警、嚴格限制上傳目錄執行權限。