略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: 保安措施和安全模式

2024-03-28

保安措施和安全模式

保安措施和安全模式配置指令
名字 默认 可修改范围 更新日志
safe_mode "0" PHP_INI_SYSTEM  
safe_mode_gid "0" PHP_INI_SYSTEM 自 PHP 4.1.0 起可用,在 PHP 5.4.0 中移除。
safe_mode_include_dir NULL PHP_INI_SYSTEM 自 PHP 4.1.0 起可用
safe_mode_exec_dir "" PHP_INI_SYSTEM  
safe_mode_allowed_env_vars "PHP_" PHP_INI_SYSTEM  
safe_mode_protected_env_vars "LD_LIBRARY_PATH" PHP_INI_SYSTEM  
有关 PHP_INI_* 样式的更多详情与定义,见 配置可被设定范围

这是配置指令的简短说明。

safe_mode boolean

是否启用 PHP 的安全模式。

safe_mode_gid boolean

默认情况下,安全模式在打开文件时会做 UID 比较检查。如果想将其放宽到 GID 比较,则打开 safe_mode_gid。是否在文件访问时使用 UIDFALSE)或者 GIDTRUE)来做检查。

safe_mode_include_dir string

当从此目录及其子目录(目录必须在 include_path 中或者用完整路径来包含)包含文件时越过 UID/GID 检查。

从 PHP 4.2.0 开始,本指令可以接受和 include_path 指令类似的风格用冒号(Windows 中是分号)隔开的路径,而不只是一个目录。 指定的限制实际上是一个前缀,而非一个目录名。这也就是说“safe_mode_include_dir = /dir/incl”将允许访问“/dir/include”和“/dir/incls”,如果它们存在的话。如果希望将访问控制在一个指定的目录,那么请在结尾加上一个斜线,例如:“safe_mode_include_dir = /dir/incl/”。 如果本指令的值为空,在 PHP 4.2.3 中以及 PHP 4.3.3 起具有不同 UID/GID 的文件将不能被包含。在较早版本中,所有文件都能被包含。
safe_mode_exec_dir string

如果 PHP 使用了安全模式,system() 和其它程序执行函数将拒绝启动不在此目录中的程序。必须使用 / 作为目录分隔符,包括 Windows 中。

safe_mode_allowed_env_vars string

设置某些环境变量可能是潜在的安全缺口。本指令包含有一个逗号分隔的前缀列表。在安全模式下,用户只能改变那些名字具有在这里提供的前缀的环境变量。默认情况下,用户只能设置以 PHP_ 开头的环境变量(例如 PHP_FOO = BAR)。

Note:

如果本指令为空,PHP 将使用户可以修改任何环境变量!

safe_mode_protected_env_vars string

本指令包含有一个逗号分隔的环境变量的列表,最终用户不能用 putenv() 来改变这些环境变量。甚至在 safe_mode_allowed_env_vars 中设置了允许修改时也不能改变这些变量。

参见 open_basedirdisable_functionsdisable_classesregister_globalsdisplay_errorslog_errors

safe_mode 设置为 on,PHP 将通过文件函数或其目录检查当前脚本的拥有者是否和将被操作的文件的拥有者相匹配。例如:

-rw-rw-r--    1 rasmus   rasmus       33 Jul  1 19:20 script.php
-rw-r--r--    1 root     root       1116 May 26 18:01 /etc/passwd
运行 script.php
<?php
 readfile
('/etc/passwd');
?>
如果安全模式被激活,则将会导致以下错误:
Warning: SAFE MODE Restriction in effect. The script whose uid is 500 is not
allowed to access /etc/passwd owned by uid 0 in /docroot/script.php on line 2

同时,或许会存在这样的环境,在该环境下,宽松的 GID 检查已经足够,但严格的 UID 检查反而是不适合的。可以用 safe_mode_gid 选项来控制这种检查。如果设置为 On 则进行宽松的 GID 检查;设置为 Off(默认值)则进行 UID 检查。

除了 safe_mode 以外,如果设置了 open_basedir 选项,则所有的文件操作将被限制在指定的目录下。例如:

<Directory /docroot>
  php_admin_value open_basedir /docroot
</Directory>
如果在设置了 open_basedir 选项后运行同样的 script.php,则其结果会是:
Warning: open_basedir restriction in effect. File is in wrong directory in
/docroot/script.php on line 2

也可以单独地屏蔽某些函数。请注意 disable_functions 选项不能在 php.ini 文件外部使用,也就是说无法在 httpd.conf 文件的按不同虚拟主机或不同目录的方式来屏蔽函数。如果将如下内容加入到 php.ini 文件:

disable_functions readfile,system
则会得到如下的输出:
Warning: readfile() has been disabled for security reasons in
/docroot/script.php on line 2
Warning

当然,这些 PHP 限制不适用于可执行文件。

add a note add a note

User Contributed Notes 3 notes

up
8
rayro at gmx dot de
10 years ago
Theres a failure with open_basedir and per-host configuration
in apache as described in bug #42836: http://bugs.php.net/bug.php?id=42836

I got the same errors on my development windows system and apache 2.2.4 with php 5.3.beta1.

This error (or similar) is shown:
Warning: Unknown: open_basedir restriction in effect. File(...)
is not within the allowed path(s): (� �� �@5�,�)

Fix:
  - try slashes at the end of the folder name
  or
  - put "php_admin_value open_basedir ..." at first of all in the configuration
up
-2
Anonymous
1 year ago
bad/missing config values for the Plesk server running the whole thing. I just followed the directions here: https://vb.3dlat.com/

You can configure PHP to have a separate error log file for each VirtualHost definition. The trick is knowing exactly how to set it up, because you can’t touch the configuration directly without breaking Plesk. Every domain name on your (dv) has its own directory in /var/www/vhosts. A typical directory has the following top level directories:

cgi-bin/
conf/
error_docs/
httpdocs/
httpsdocs/
...and so on

You’ll want to create a vhost.conf file in the domain directory’s conf/ folder with the following lines:

php_value error_log /path/to/error_log
php_flag display_errors off
php_value error_reporting 6143
php_flag log_errors on

Change the first value to match your actual installation (I used /tmp/phperrors.log). After you’re done editing the vhost.conf file, test the configuration from the console with:

apachectl configtest
…or if you don’t have apachectl (as Plesk 8.6 doesn’t seem to)…

/etc/init.d/httpd configtest

And finally tell Plesk that you’ve made this change.

/usr/local/psa/admin/bin/websrvmng -a
up
-5
Anonymous
1 year ago
Whether to enable PHP's safe mode. If PHP is compiled with --enable-safe-mode then defaults to On, otherwise Off. http://www.wikibanat.com

官方地址:https://www.php.net/manual/en/ini.sect.safe-mode.php

冷却塔厂家 广告
-- 广告
北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3