首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

PHP chmod() 被禁用解決方案與危險函數加固清單

     編輯:PHP 相關知識

錯誤現象

Warning: chmod() has been disabled for security reasons in D:\freehost\xxx\WindFile.php on line 102

原因:服務器 php.ini 中 disable_functions 包含 chmod,出於安全考慮禁止文件權限修改操作。

解決方案

方案一:服務器權限修改 php.ini(推薦)

; 打開 php.ini,找到 disable_functions
; 移除 chmod,或按需精確配置
disable_functions = passthru,exec,system,popen,chroot,scandir,chgrp,chown,escapeshellcmd,escapeshellarg,shell_exec,proc_open,proc_get_status

原則: 僅禁用確實高危且業務不需要的函數,避免影響正常功能。

方案二:代碼層面繞過(無服務器權限時)

以 ECMall 為例,修改錯誤處理邏輯屏蔽該警告:

// eccore/controller/message.base.php
// 原代碼
if ($errno == 2048) { return true; }

// 修改為:同時屏蔽被 @ 抑制的錯誤
if ($errno == 2048 || (($errno & error_reporting()) != $errno)) {
    return true;
}

// eccore/ecmall.php
// 原 _at() 函數使用 restore_error_handler()/reset_error_handler()
// 修改為直接調用,不重置處理器
function _at($fun) {
    $arg = func_get_args();
    unset($arg[0]);
    $ret_val = @call_user_func_array($fun, $arg);
    return $ret_val;
}

注意: 此法僅屏蔽警告,chmod() 實際調用仍會失敗,業務邏輯需評估影響。

生產環境建議禁用函數清單

disable_functions = 
; 命令執行類
system,exec,shell_exec,passthru,proc_open,proc_close,proc_get_status,
; 網絡/系統信息類
checkdnsrr,getmxrr,getservbyname,getservbyport,syslog,popen,
; 信息洩露類
show_source,highlight_file,dl,
; Socket/流操作類
socket_listen,socket_create,socket_bind,socket_accept,socket_connect,
stream_socket_server,stream_socket_accept,stream_socket_client,
; FTP 類
ftp_connect,ftp_login,ftp_pasv,ftp_get,
; 系統資源類
sys_getloadavg,disk_total_space,disk_free_space,
; POSIX 進程/用戶控制類
posix_ctermid,posix_get_last_error,posix_getcwd,posix_getegid,posix_geteuid,
posix_getgid,posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,
posix_getpgid,posix_getpgrp,posix_getpid,posix_getppid,posix_getpwnam,
posix_getpwuid,posix_getrlimit,posix_getsid,posix_getuid,posix_isatty,
posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid,
posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,
posix_ttyname,posix_uname,
; 文件系統高危操作(視業務評估)
chdir,chroot,dir,getcwd,opendir,readdir,scandir,fopen,unlink,delete,
copy,mkdir,rmdir,rename,file,file_get_contents,fputs,fwrite,
chgrp,chmod,chown

加固建議

  • 最小權限原則:Web 進程用戶僅擁有必要目錄的讀/寫權限
  • 目錄執行權限隔離:上傳、緩存、日誌目錄禁用 PHP 解析(Apache: php_flag engine off,Nginx: location ~* \.php$ { deny all; })
  • 定期審計:檢查 disable_functions 是否生效(php -i | grep disable_functions)
  • RASP 部署:運行時防護補齊配置層面的不足
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved