PHP 木馬(Webshell)是伺服器被植入惡意腳本後,攻擊者用於持久化控制、竊取資料、橫向移動的核心工具。防禦核心在於最小權限原則與攻擊面縮減。
在 httpd.conf(Apache)或 php.ini 中設定:
php_admin_value open_basedir "/var/www/html:/tmp:/usr/share/php"
/etc/passwd 等敏感路徑將拋出 Warning: open_basedir restriction in effect。在 php.ini 設定 disable_functions:
; 命令執行類
disable_functions = passthru,exec,shell_exec,system,proc_open,popen,pcntl_exec
; 檔案系統操作類(視業務需求酌情保留)
disable_functions = fopen,mkdir,rmdir,chmod,unlink,dir,fread,fwrite,fclose,file_exists,closedir,is_dir,readdir,opendir,fileperms,copy,rename,file_get_contents,file_put_contents
; 資訊洩露類
disable_functions = phpinfo,ini_get_all,get_cfg_var
注意:過度禁用會導致正常功能(如模板引擎編譯、快取寫入、Composer 安裝)失效,建議採用「預設拒絕、按需開放」策略,並搭配 open_basedir 限制影響範圍。
預設 Apache 以 SYSTEM 權限運行,一旦被植入 Webshell 即獲得最高控制權。降權步驟:
net user apache_weak Password123! /add
net localgroup users apache_weak /del
allow_url_include、固定模板路徑、避免動態 include $_GET['file']。eval/base64_decode 等特徵)。eval/assert/preg_replace.*e 等特徵)。「沒有絕對安全,只有不斷提高攻擊成本」。將 open_basedir、disable_functions、進程降權、上傳隔離、代碼審計、WAF、FIM、日誌審計串聯起來,構建纵深防禦體系,才能有效遏制 PHP 木馬攻擊。