php.ini 的 disable_functions 指令可直接在引擎層禁用指定函式,無需修改程式碼即可阻斷整類攻擊向量。被禁用函式被呼叫時會拋出警告:
Warning: phpinfo() has been disabled for security reasons
; 基礎防護:資訊洩露、動態載入、指令執行
disable_functions = phpinfo, dl, exec, system, passthru, shell_exec, popen, proc_open, proc_close, escapeshellarg, escapeshellcmd
; 進階防護:檔案系統、程式控制(視業務評估啟用)
disable_functions = chdir, chroot, opendir, readdir, scandir, fopen, unlink, mkdir, rmdir, rename, file_get_contents, fwrite, chmod, chown, symlink, link
許多開發者嘗試將 eval 加入禁用清單,但 eval() 是語言結構而非函式,不受 disable_functions 限制。防禦 eval 注入需從程式碼層面著手:避免動態程式碼執行、啟用 Opcode 快取防範注入、部署 RASP 方案。
; CLI 快速驗證
php -r "system('id');"
php -r "phpinfo();"
php -r "eval('phpinfo();');" ; 此行不受 disable_functions 影響
php.ini 設定,避免運行時修改php.iniopen_basedir 形成纵深防禦| 方案 | 層級 | 彈性 | 效能影響 | 適用場景 |
|---|---|---|---|---|
| disable_functions | 引擎層 | 低(全域生效) | 無 | 基礎防護、合規要求 |
| RASP (如 OpenRASP) | 運行時 | 高(可上下文感知) | 微小 | 精細防禦、零日漏洞緩解 |
| SuPHP / PHP-FPM pool 隔離 | 進程層 | 中 | 低 | 多租戶共享主機 |