php安全设置:禁用危险函数【未完全版】2014年12月26日再次更新 -云主机博士

php安全设置:禁用危险函数【未完全版】2014年12月26日再次更新 -云主机博士 第1张

在 php.ini 中找到
disable_functions =

这一行,在其后面添加需要禁止的危险函数名,以英文逗号分隔
给个例子:
disable_functions = passthru,exec,system,popen,chroot,scandir,chgrp,chown,escapesh,ellcmd,escapeshellarg,shell_exec

建议在主机上禁用的函数:
disable_functions = phpinfo,set_time_limit,system,exec,shell_exec,passthru,proc_open,proc_close,proc_get_status,checkdnsrr,getmxrr,getservbyname,getservbyport,syslog,popen,show_source,highlight_file,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,socket_listen,socket_create,socket_bind,socket_accept,socket_connect,stream_socket_server,stream_socket_accept,stream_socket_client,ftp_connect,ftp_login,ftp_pasv,ftp_get,zlib.compress,gzopen,gzpassthru,gzcompress,chroot,scandir,chgrp,chown,ini_alter,ini_restore,openlog,readlink,symlink,popepassthu,stream_socket_srver,error_log,ini_set,dl,putenv,popepassthru,fsocket,fsockopen,pfsockopen
一些需要禁用的PHP危险函数

phpinfo()
功能描述:输出 PHP 环境信息以及相关的模块、WEB 环境等信息。
危险等级:中

passthru()
功能描述:允许执行一个外部程序并回显输出,类似于 exec()。
危险等级:高

exec()
功能描述:允许执行一个外部程序(如 UNIX Shell 或 CMD 命令等)。
危险等级:高

system()
功能描述:允许执行一个外部程序并回显输出,类似于 passthru()。
危险等级:高

chroot()
功能描述:可改变当前 PHP 进程的工作根目录,仅当系统支持 CLI 模式
PHP 时才能工作,且该函数不适用于 Windows 系统。
危险等级:高

scandir()
功能描述:列出指定路径中的文件和目录。
危险等级:中

chgrp()
功能描述:改变文件或目录所属的用户组。
危险等级:高

chown()
功能描述:改变文件或目录的所有者。
危险等级:高

shell_exec()
功能描述:通过 Shell 执行命令,并将执行结果作为字符串返回。
危险等级:高

proc_open()
功能描述:执行一个命令并打开文件指针用于读取以及写入。
危险等级:高

proc_get_status()
功能描述:获取使用 proc_open() 所打开进程的信息。
危险等级:高

error_log()
功能描述:将错误信息发送到指定位置(文件)。
安全备注:在某些版本的 PHP 中,可使用 error_log() 绕过 PHP safe mode,
执行任意命令。
危险等级:低

ini_alter()
功能描述:是 ini_set() 函数的一个别名函数,功能与 ini_set() 相同。
具体参见 ini_set()。
危险等级:高

ini_set()
功能描述:可用于修改、设置 PHP 环境配置参数。
危险等级:高

ini_restore()
功能描述:可用于恢复 PHP 环境配置参数到其初始值。
危险等级:高

dl()
功能描述:在 PHP 进行运行过程当中(而非启动时)加载一个 PHP 外部模块。
危险等级:高

pfsockopen()
功能描述:建立一个 Internet 或 UNIX 域的 socket 持久连接。
危险等级:高

syslog()
功能描述:可调用 UNIX 系统的系统层 syslog() 函数。
危险等级:中

readlink()
功能描述:返回符号连接指向的目标文件内容。
危险等级:中

symlink()
功能描述:在 UNIX 系统中建立一个符号链接。
危险等级:高

popen()
功能描述:可通过 popen() 的参数传递一条命令,并对 popen() 所打开的文件进行执行。
危险等级:高

stream_socket_server()
功能描述:建立一个 Internet 或 UNIX 服务器连接。
危险等级:中

putenv()
功能描述:用于在 PHP 运行时改变系统字符集环境。在低于 5.2.6 版本的 PHP 中,可利用该函数
修改系统字符集环境后,利用 sendmail 指令发送特殊参数执行系统 SHELL 命令。
危险等级:高

最近服务器的phpmyadmin一直不正常,原以为配置错误,结果发现是前段时间修改disable_function引起的,

逐步比较disable_function的参数发现,原来是因为opendir,readdir,dir,chdir这4个参数引起的

禁用opendir报错如下:
phpmyadmin就会出现如下错误
phpMyAdmin-ERROR: cannot open themes folder: ./themes
Default theme pmahomme not found!
Theme original not found!
Theme not found!

但是phpspy就不能列出其他虚拟主机的目录
官方解释:http://cn.php.net/manual/zh/function.opendir.php
opendir:打开目录句柄

加上readdir
phpmyadmin首页能打开,但是不能登陆,502 Bad Gateway

phpspy此时不能列出其他虚拟主机目录

官方解释:http://cn.php.net/manual/zh/function.readdir.php
readdir — 从目录句柄中读取条目,返回目录中下一个文件的文件名

最后总结出比较安全的php.ini的disable_functions如下(服务器没用到采集):

    disable_functions = exec,passthru,popen,proc_open,shell_exec,system,phpinfo,assert,chroot,getcwd,scandir,unlink,delete,rmdir,rename,chgrp,chmod,chown,fopen,copy,mkdir,file,file_get_contents,fputs,fwrite,dir

禁用这些函数,网站和phpmyadmin都正常,而且不能上传文件
利用webshell也不能查看服务器上的文件,不能删除或者修改
唯一的缺点:能够查看其他虚拟主机的目录,但是仅仅只能看看,做不了什么事情

如果服务器用到了采集,需要启用unlink和fopen,列表如下:

    disable_functions = exec,passthru,popen,proc_open,shell_exec,system,phpinfo,assert,chroot,getcwd,scandir,delete,rmdir,rename,chgrp,chmod,chown,copy,mkdir,file,file_get_contents,fputs,fwrite,dir

unlink官方解释为:http://cn.php.net/manual/zh/function.unlink.php
fopen官方解释为:http://cn.php.net/manual/zh/function.fopen.php
如果需要用到采集,需要启用这几个函数:

file_get_contents,fputs,fwrite,dir

禁用的函数列表为:

    disable_functions = exec,passthru,popen,proc_open,shell_exec,system,phpinfo,assert,chroot,getcwd,scandir,delete,rmdir,rename,chgrp,chmod,chown,copy,mkdir,file

如果那位有更完美的解决办法,欢迎和我联系,可以在博文结尾一起讨论!

宝塔服务器面板,一键全能部署及管理,送你3188元礼包,点我领取


iproyal.png
原文链接:,转发请注明来源!
「php安全设置:禁用危险函数【未完全版】2014年12月26日再次更新 -云主机博士」评论列表

发表评论