編輯 php.ini,設置 disable_functions 指令,多個函數用半角逗號分隔:
disable_functions = passthru,exec,system,popen,proc_open,proc_get_status,shell_exec,pcntl_exec,pcntl_fork,pcntl_signal,pcntl_waitpid,pcntl_wexitstatus,pcntl_wifexited,pcntl_wifsignaled,pcntl_wifstopped,pcntl_wstopsig,pcntl_wtermsig,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_get_last_error,pcntl_strerror,chroot,chgrp,chown,dl,syslog,openlog,closelog,apache_child_terminate,apache_get_modules,apache_get_version,apache_getenv,apache_lookup_uri,apache_note,apache_request_headers,apache_reset_timeout,apache_response_headers,apache_setenv,define_syslog_variables,posix_kill,posix_mkfifo,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid
| 類別 | 代表函數 | 風險 |
|---|---|---|
| 命令執行 | exec, system, passthru, shell_exec, proc_open, popen | 任意代碼執行,獲取服務器控制權 |
| 進程控制 | pcntl_* 系列 | Fork 炸彈、信號劫持、守護進程竄改 |
| 文件系統高級 | chroot, chgrp, chown, dl | 權限提升、動態加載惡意擴展 |
| 網絡/服務 | apache_*, syslog*, openlog | 服務器信息洩露、日志注入 |
| POSIX 權限 | posix_set*, posix_kill | 用戶/組切換、進程殺害 |
| 信息洩露 | show_source, highlight_file | 源碼泄露 |
disable_functions = exec,system,passthru,shell_exec,proc_open,proc_get_status,popen,pclose,dl,chroot,chgrp,chown,apache_child_terminate,apache_get_modules,apache_get_version,apache_getenv,apache_lookup_uri,apache_note,apache_request_headers,apache_reset_timeout,apache_response_headers,apache_setenv,define_syslog_variables,posix_kill,posix_mkfifo,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,show_source,highlight_file,symlink,link,readlink,stream_socket_server,stream_socket_client,ftp_connect,ftp_login,ftp_pasv,ftp_get,ftp_put,ftp_delete,ftp_mkdir,ftp_rmdir,ftp_chdir,ftp_nlist,ftp_rawlist,ftp_size,ftp_mdtm,ftp_systype,ftp_pwd,ftp_cdup,ftp_chmod,ftp_alloc,ftp_raw,ftp_set_option,ftp_get_option
# 僅禁用最高風險函數,保留 CLI 腳本所需 disable_functions = exec,system,passthru,shell_exec,proc_open,pcntl_fork,pcntl_signal,chroot,dl,show_source,highlight_file,symlink
# 容器內通常不需這些函數 disable_functions = exec,system,passthru,shell_exec,proc_open,proc_get_status,popen,pclose,pcntl_*,posix_*,apache_*,dl,chroot,show_source,highlight_file
<?php
header('Content-Type: text/html; charset=utf-8');
$disabled = ini_get('disable_functions');
$list = $disabled ? array_map('trim', explode(',', $disabled)) : [];
echo "PHP 禁用函數檢測
";
echo "PHP 版本: " . PHP_VERSION . "
";
echo "配置文件: " . php_ini_loaded_file() . "
";
echo "禁用函數數量: " . count($list) . "
";
if (empty($list)) {
echo "⚠️ 警告:未禁用任何危險函數!生產環境極不安全。";
} else {
echo "| 函數名 | 風險等級 | 說明 |
|---|---|---|
$func | $risk | " . getFuncDesc($func) . " |
# 創建測試文件 test_disable.php # 訪問/執行 php test_disable.php # 預期輸出:Warning: system() has been disabled for security reasons
new COM()、Imagick::execute() 等仍可執行命令open_basedir 限制文件訪問範圍expose_php = Off 隱藏版本信息display_errors = Off 生產環境關閉錯誤顯示com_dotnet, ffi)disable_functions 繞過漏洞 (如 CVE-2021-21703)disable_functions 是 PHP 安全基線的第一道防線,**生產環境必須配置**,並根據業務場景精細調整。配合 open_basedir、權限最小化、WAF 等措施構建纵深防禦體系。定期使用檢測腳本驗證配置生效,納入安全基線巡檢項目。