Home  >  Article  >  Backend Development  >  Detailed design instructions PHP security configuration detailed instructions

Detailed design instructions PHP security configuration detailed instructions

WBOY
WBOYOriginal
2016-07-29 08:46:481315browse

【 安全模式 】

  PHP的安全模式提供一个基本安全的共享环境,在一个有多个用户帐户存在的PHP开放的Web服务器上。当一个Web服务器上运行的PHP打开了安全模式,那么一些函数将被完全的禁止,并且会限制一些可用的功能。

[ 使用安全模式来强制限制 ]

  在安全模式下,一些尝试访问文件系统的函数功能将被限制。运行Web服务器用户ID,如果想要操作某个文件,则必须拥有该文件读取或者写入的访问权限,实现这个限制功能对于PHP来说是没有问题的。

  在 安全模式开启的时候,尝试读取或者写入一个本地文件的时候,PHP将检查当前访问用户是否是该目标文件的所有者。如果不是所有者,则该操作会被禁止。(写 入权限:在较低级别的文件访问权限下,可能会允许读取或者写入系统操作系统的文件,通过PHP的安全模式实现了防止你操作另外一个用户文件的操作。当然, 一个Web服务器可能能够访问一个具有全局写入权限的任意文件。)

  当安全模式打开的时候,以下函数列表的功能将会受到限制:

  chdir , move_uploaded_file, chgrp, parse_ini_file, chown, rmdir, copy, rename, fopen, require, highlight_file, show_source, include, symlink, link, touch, mkdir, unlink

  同样的,一些PHP扩展中的函数也将会受到影响。(加载模块:在安全模式下dl函数将被禁止,如果要加载扩展的话,只能修改php.ini中的扩展选项,在PHP启动的时候加载)

  在PHP安全模式打开的时候,需要执行操作系统程序的时候,必须是在safe_mode_exec_dir选项指定目录的程序,否则执行将失败。即使允许执行,那么也会自动的传递给escapeshellcmd函数进行过滤。

  以下执行命令的函数列表将会受到影响:

  exec, shell_exec, passthru, system, popen

  另外,背部标记操作符(`)也将被关闭。

  当运行在安全模式下,虽然不会引起错误,但是 putenv 函数将无效。同样的,其他一些尝试改变PHP环境变量的函数set_time_limit, set_include_path 也将被忽略。

[ 打开安全模式 ]

  打开或者关闭PHP的安全模式是利用php.ini中的safe_mode选项。如果要激活安全模式给当前所有共享Web服务器的用户,只要设置配置选项为:safe_mode = On当函数在访问文件系统的时候将进行文件所有者的检查。缺省情况下,会检查该文件所有者的用户ID,当你能够修改文件所有者的组ID(GID)为 safe_mode_gid 选项所指定的。如 果你有一个共享库文件在你的系统上,当你碰到需要include或require的时候,那么你可以使用 safe_mode_include_dir 选项来设置你的路径,保证你的代码正常工作。(包含路径: 如果你想要使用 safe_mode_include_dir 选项包含更多的包含路径,那么你可以象 include_path 选项一样,在Unix/Linux系统下使用冒号进行分割,在Windows下使用分号进行分割)比如你想要在安全模式下包含 /usr/local/include/php 下的文件,那么你可以设置选项为:safe_mode_include_dir = /usr/local/include/php如果你的包含的文件是需要执行的,那么你可以设置 safe_mode_exec_dir 选项。比如你需要 /usr/local/php-bin 路径下的文件是可以执行的,那么可以设置选项为:safe_mode_exec_dir = /usr/local/php-bin(可执行:如果你执行的程序在 /usr/bin 目录下,那么你可以把这些的二进制文件,连接到你指定选项下能够执行的路径)如果你想设置某些环境变量,那么可以使用 safe_mode_allowed_env_vars 选项。这个选项的值是一个环境变量的前缀,缺省是允许 PHP_ 开头的环境变量,如果你想要改变,可以设置该选项的值,多个环境变量前缀之间使用逗号进行分割。比如下面允许时区的环境变量 TZ ,那么修改该选项的值为:safe_mode_allowed_env_vars = PHP_,TZ【 其他的安全特征 】除了安全模式以外,PHP还提供了许多其他许多特征来保证PHP的安全。

[ 隐藏PHP ]

  你能够在php.ini里使用 expose_php 选项来防止Web服务器泄露PHP的报告信息。如下:expose_php = On利用整个设置,你能够阻碍一些来自自动脚本针对Web服务器的攻击。通常情况下,HTTP的头信息里面包含了如下信息:Server: Apache/1.3.33 (Unix) PHP/5.0.3 mod_ssl/2.8.16

 For OpenSSL/0.9.7c, after the expose_php option is turned on, the PHP version information will not be included in the above header information. Of course, users can also see the .php file extension when they visit the website. If you want to use a different file extension entirely, you need to find the following line in httpd.conf: AddType application/x-httpd .php and you can change .php to any file extension you like. You can specify as many file extensions as you like, separated by spaces. If you want to use PHP on the server side to parse .html and .htm files, then you set the options as follows: AddType application/x-httpd .html .htm (Parse HTML: Configure your web server to use PHP to parse all HTML files, but if non-server-side code also requires PHP to parse, it will affect the performance of the server. You can use different extensions for static pages, which can eliminate the dependence on the PHP script engine and enhance performance)

[ file system. Security]

Safe mode restricts the script owner to only access files that belong to him, but you can use the open_basedir option to specify a directory that you must access. If you specify a directory, PHP will deny access to directories other than that directory and its subdirectories. The open_basedir option works outside of safe mode. To restrict the file system to only access the /tmp directory, then set the option: open_basedir = /tmp [Function Access Control] You can use comma separation to set the function name in the disable_functions option, then these functions will be turned off in the PHP script. This setting works outside of safe mode. disable_functions = dl Of course, you can also use the disable_classes option to turn off access to some classes.

[Database Security]

 Suppose your PHP script contains a Mysql query that is executed based on the form value: $sql = "UPDATE mytable SET col1 = ".

The above introduces the detailed design instructions and detailed instructions for PHP security configuration, including the detailed design instructions. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn