Home > Backend Development > PHP Tutorial > Parsing safe mode (safe_mode) in PHP

Parsing safe mode (safe_mode) in PHP

藏色散人
Release: 2023-04-08 11:46:02
forward
6451 people have browsed it

Parsing safe mode (safe_mode) in PHP

What is PHP safe mode: safe_mode

Simply put, PHP safe mode is Run php in safe mode.

php's safe mode provides a basic secure shared environment on a php open web server where multiple user accounts exist. When PHP is running in safe mode on a web server, some functions will be completely disabled and some available functions will be limited.

In safe mode, some functions that try to access the file system will be restricted. Running the web server user ID, if you want to operate a certain file, you must have access permission to read or write the file. There is no problem for PHP to implement this restriction function.

When safe mode is turned on, when trying to read or write a local file, PHP will check whether the current accessing user is the owner of the target file. If you are not the owner, the operation will be disabled.

(Writing permissions: Under lower-level file access permissions, it may be allowed to read or write files in the system operating system. This is achieved through PHP's safe mode to prevent you from operating another user's files. Operation. Of course, a web server may be able to access an arbitrary file with global write permission.)

When safe mode is turned on, the functionality of the following function list will be restricted:

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

Same, Some functions in PHP extensions will also be affected. (Loading module: The dl function will be prohibited in safe mode. If you want to load an extension, you can only modify the extension options in php.ini and load it when PHP starts)

Opened in PHP safe mode When you need to execute a system program, it must be the program in the directory specified in the safe_mode_exec_dir option, otherwise the execution will fail. Even if execution is allowed, it will automatically be passed to the escapeshellcmd function for filtering.

The following function list for executing commands will be affected:

exec,shell_exec,passthru,system,popen

In addition, the back marking operator (`) will also be closed.

When running in safe mode, although no error will be caused, the putenv function will be invalid. Similarly, other functions set_time_limit and set_include_path that try to change PHP environment variables will also be ignored.

How to turn on PHP safe mode (please note that PHP5.3 will no longer have a safe mode)

To turn on or off PHP's safe mode, use php.ini The safe_mode option:

safe_mode=On(使用安全模式)
safe_mode=Off(关闭安全模式)
Copy after login

The corresponding setting method of VirtualHost in apache's httpd.conf

php_admin_flag safe_mode On(使用安全模式)
php_admin_flag safe_mode Off(关闭安全模式)
或者:
php_admin_value safe_mode1(使用安全模式)
php_admin_value safe_mode0(关闭安全模式)
Copy after login

The impact of enabling safe mode:

When The function will perform a file owner check when accessing the file system. By default, the user ID of the file owner is checked. When you can modify the file owner's group ID (gid), it is specified by the safe_mode_gid option.

If you have a shared library file on your system, when you encounter the need to include or require, then you can use the safe_mode_include_dir option to set your path to ensure that your code works normally. (Inclusion path: If you want to use the safe_mode_include_dir option to include more include paths, then you can use the colon to separate under unix/linux systems and semicolons under windows like the include_path option)

For example, if you want to include files under /usr/local/include/php in safe mode, then you can set the option to:

safe_mode_include_dir=/usr/local/include/php
Copy after login

If your included files need to be executed, then you The safe_mode_exec_dir option can be set.

For example, if you need the files in the /usr/local/php-bin path to be executable, then you can set the option to:

safe_mode_exec_dir=/usr/local/php-bin
Copy after login

(Executable: If the program you execute is in / usr/bin directory, then you can connect these binary files to the path that can be executed under the options you specify)

If you want to set certain environment variables, you can use the safe_mode_allowed_env_vars option. The value of this option is the prefix of an environment variable. By default, environment variables starting with php_ are allowed. If you want to change it, you can set the value of this option. Use commas to separate multiple environment variable prefixes.

For example, if the environment variable tz of the time zone is allowed below, then modify the value of this option to:

safe_mode_allowed_env_vars=php_,tz
Copy after login

In addition to the safe mode, php also provides many other features to ensure the security of php.

1. [Hide PHP version number]

You can use the expose_php option in php.ini to prevent the web server from leaking PHP report information. As follows:

expose_php=on
Copy after login

With this entire setup, you can thwart some attacks from automated scripts targeting web servers. Normally, the http header information contains the following information:

server:apache/1.3.33(unix)php/5.2.4mod_ssl/2.8.16openssl/0.9.7c
Copy after login

After the expose_php option is turned on, the PHP version information will not be included in the above header information.

当然,用户访问网站的时候同样能够看到.php的文件扩展名。如果你想整个的使用不同的文件扩展名,你需要在httpd.conf中找到如下这行:

addtype application/x-httpd.php
Copy after login

你就可以修改.php为任何你喜欢的文件扩展名。你能够指定任意多个的文件扩展名,中间使用空格进行分割。如果你想在服务器端使用php来解析.html和.htm文件的时候,那么你设置选项如下:

addtype application/x-httpd.html.htm
Copy after login

(解析html:配置你的web服务器使用php去解析所有的html文件,但是如果非服务器端代码也需要php去解析,会影响服务器的性能。静态页面你可以使用不同的扩展名,这样能够消除对php脚本引擎的依赖,增强性能。)

2、[文件系统安全]

安全模式限制了脚本所有者只能访问属于自己的文件,但是你可以使用open_basedir选现来指定一个你必须访问的目录。如果你指定了一个目录,php将拒绝访问除了该目录和该目录子目录的其他目录。open_basedir选项能够工作在安全模式之外。

限制文件系统只能访问/tmp目录,那么设置选项为:

open_basedir=/tmp
Copy after login

3、[函数访问控制]

你能够在disable_functions选项中使用逗号分割来设定函数名,那么这些函数将在php脚本中被关闭。这个设置能够工作在安全模式之外。

disable_functions=dl
Copy after login

当然,同样的你能够使用disable_classes选项来关闭对一些类的访问。

4、[数据库安全]

假设你的php脚本中包含一个基于表单值来执行的mysql查询:

$sql=”update mytable set col1=”.$_post[“value”].”where col2=’somevalue'”;
$res=mysql_query($sql,$db);
Copy after login

你希望$_post[“value”]包含一个整数值来更新你的列col1。可是,一个恶意用户能够输入一个分号在表单字段里,接着,是一段他/她想被任意执行的sql语句。

举例,假设下面是$_post[“value”]提交的值:

0;insert into admin_users(username,password) values (‘me’,’mypassword’);
Copy after login

那么当这个查询发送给mysql查询的时候,那么就变成了下面这条sql:

update mytable set col1=0;
insert into admin_users(username,password) values (‘me’,’mypassword’);
where col2=’somevalue';
Copy after login

这明显是一个有害的查询!首先这个查询会在mytable表里更新col1。这个并没有什么麻烦的,但是第二个表达式,它将执行insert表达式 来插入一个能登陆的新管理员。第三个表达式就废弃了,但同时sql解析器将抛出一个错误,这个有害的查询才完成。这个攻击就是大家常说的sql injection(注:sql注入)。

当然,sql injection存在一个问题,对方必须了解你的数据库结构。在这个例子中,攻击者是知道你有一个表admin_users,并且知道包含username和password字段,同时,存储的密码是没有加密的。

除了你自己,一般的网站访问者是不知道这些关于数据库的信息。可是,如果你使用了一个开发源代码的在线电子商务程序,或者使用一个自由的讨论版程序,这些数据表的定义都是已知的,或者有一些用户能够访问到你的数据库。

此外,你的脚本输出会提示一个查询错误,这些信息里包含了很多关于数据库结构的重要信息。在一个正常工作的网站上,你应该考虑设置 display_errors选项为off,并且使用log_errors来代替display_errors,把警告和错误信息插入到文件中。

(数据库权限:它是一个非常重要的东西,你只有正确的权限,才能通过脚本正确的连接数据库。你应该不要在脚本中使用管理员去连接数据库。如果你这么 做,那么一个攻击者将可能获取全部的数据库权限,并且包括其他相同服务器的权限。攻击者将可能运行grant或create user命令来获取更多的访问权限。)

如果你要防止sql injection攻击,你必须保证用户表单里提交的内容不是一个能够执行的sql表达式。

前一个例子中,我们使用一个整型值来进行更新。如果在单引号后面跟上一个字符串,这个攻击者在分号之前必须提交一个闭合的引用在整个sql表达式中。可是,当magic_quotes_gpc选项是开启的时候,在web表单中提交的引号将自动被转义。

为了防止被恶意的攻击者进行sql injection攻击,你应该总是确认提交的数据是合法的。如果你需要的是一个整数值,那么你可以使用is_numeric函数来测试这个表达值,或者使用settype函数来转换为一个数字,清除任何一个傻傻的sql语句。

如果你开发的程序需要几个提交的值在一个sql表达式里,你能够使用sprintf函数来构建一个sql字符串,使用格式化字符来指示数据类型的每个值。看下面的例子:

$sql=sprintf(“update mytable set col1=%d where col2=’%s'”, $_post[“number”], mysql_escape_string($_post[“string”]));
Copy after login

在上一个例子中,整个mysql的数据已经被使用,所以这个字符串已经通过mysql_escape_string函数进行过滤。对于其他数据库,你可以使用addslashes函数进行转义,或者使用其他方法。

For more php related knowledge, please visit php tutorial!

The above is the detailed content of Parsing safe mode (safe_mode) in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:oschina.net
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template