修改 httpd.conf (Apache) 或 php.ini,限制腳本只能存取指定目錄:
php_admin_value open_basedir "/usr/local/apache/htdocs"
嘗試讀取目錄外檔案將觸發警告:
Warning: open_basedir restriction in effect. File is in wrong directory...
在 php.ini 設定 disable_functions,二選一或皆啟用:
disable_functions = passthru,exec,shell_exec,system
在上述清單後追加檔案處理函數:
disable_functions = passthru,exec,shell_exec,system,
fopen,mkdir,rmdir,chmod,unlink,dir,
fopen,fread,fclose,fwrite,file_exists,
closedir,is_dir,readdir,opendir,
fileperms,copy,unlink,delfile
副作用:合法的檔案上傳、快取、日誌功能將失效。生產環境建議改用 open_basedir 限制路徑,而非全盤禁用函數。
Apache 預設以 System 權限執行,風險極高。建立低權限帳號執行 Apache:
net user apache fuckmicrosoft /add
net localgroup users apache /del
# 在服務屬性 → 登入 → 此帳號 → 輸入 apache 帳號密碼 → 重啟服務
進一步可針對各目錄設定 ACL,只授予必要讀寫權限(虛擬主機商常用做法)。
open_basedir + disable_functions 組合,而非單一手段。find /var/www -name "*.php" -mtime -1)。expose_php = Off、display_errors = Off、log_errors = On。php_flag engine off 禁止 PHP 執行。