Detailed explanation of several common security settings in php_PHP tutorial

WBOY
Release: 2016-07-21 15:39:10
Original
828 people have browsed it

另外,目前闹的轰轰烈烈的SQL Injection也是在PHP上有很多利用方式,所以要保证安全,PHP代码编写是一方面,PHP的配置更是非常关键。

我们php手手工安装的,php的默认配置文件在 /usr/local/apache2/conf/php.ini,我们最主要就是要配置php.ini中的内容,让我们执行php能够更安全。整个PHP中的安全设置主要是为了防止phpshell和SQL Injection的攻击,一下我们慢慢探讨。我们先使用任何编辑工具打开/etc/local/apache2/conf/php.ini,如果你是采用其他方式安装,配置文件可能不在该目录。

(1) 打开php的安全模式

php的安全模式是个非常重要的内嵌的安全机制,能够控制一些php中的函数,比如system(),

同时把很多文件操作函数进行了权限控制,也不允许对某些关键文件的文件,比如/etc/passwd,

但是默认的php.ini是没有打开安全模式的,我们把它打开:

safe_mode = on

(2) 用户组安全

当safe_mode打开时,safe_mode_gid被关闭,那么php脚本能够对文件进行访问,而且相同

组的用户也能够对文件进行访问。

建议设置为:

safe_mode_gid = off

如果不进行设置,可能我们无法对我们服务器网站目录下的文件进行操作了,比如我们需要

对文件进行操作的时候。

(3) 安全模式下执行程序主目录

如果安全模式打开了,但是却是要执行某些程序的时候,可以指定要执行程序的主目录:

safe_mode_exec_dir = D:/usr/bin

一般情况下是不需要执行什么程序的,所以推荐不要执行系统程序目录,可以指向一个目录,

然后把需要执行的程序拷贝过去,比如:

safe_mode_exec_dir = D:/tmp/cmd

但是,我更推荐不要执行任何程序,那么就可以指向我们网页目录:

safe_mode_exec_dir = D:/usr/www

(4) 安全模式下包含文件

如果要在安全模式下包含某些公共文件,那么就修改一下选项:

safe_mode_include_dir = D:/usr/www/include/

其实一般php脚本中包含文件都是在程序自己已经写好了,这个可以根据具体需要设置。

(5) 控制php脚本能访问的目录

使用open_basedir选项能够控制PHP脚本只能访问指定的目录,这样能够避免PHP脚本访问

不应该访问的文件,一定程度上限制了phpshell的危害,我们一般可以设置为只能访问网站目录:

open_basedir = D:/usr/www

(6) 关闭危险函数

如果打开了安全模式,那么函数禁止是可以不需要的,但是我们为了安全还是考虑进去。比如,

我们觉得不希望执行包括system()等在那的能够执行命令的php函数,或者能够查看php信息的

phpinfo()等函数,那么我们就可以禁止它们:

disable_functions = system,passthru,exec,shell_exec,popen,phpinfo

如果你要禁止任何文件和目录的操作,那么可以关闭很多文件操作

disable_functions = chdir,chroot,dir,getcwd,opendir,readdir,scandir,fopen,unlink,delete,copy,mkdir, rmdir,rename,file,file_get_contents,fputs,fwrite,chgrp,chmod,chown

以上只是列了部分不叫常用的文件处理函数,你也可以把上面执行命令函数和这个函数结合,

就能够抵制大部分的phpshell了。

(7) 关闭PHP版本信息在http头中的泄漏

我们为了防止黑客获取服务器中php版本的信息,可以关闭该信息斜路在http头中:

expose_php = Off

比如黑客在 telnet www.chinaz.com 80 的时候,那么将无法看到PHP的信息。

(8) 关闭注册全局变量

在PHP中提交的变量,包括使用POST或者GET提交的变量,都将自动注册为全局变量,能够直接访问,

这是对服务器非常不安全的,所以我们不能让它注册为全局变量,就把注册全局变量选项关闭:

register_globals = Off

当然,如果这样设置了,那么获取对应变量的时候就要采用合理方式,比如获取GET提交的变量var,

那么就要用$_GET['var']来进行获取,这个php程序员要注意。

(9) 打开magic_quotes_gpc来防止SQL注入

SQL注入是非常危险的问题,小则网站后台被入侵,重则整个服务器沦陷,

所以一定要小心。php.ini中有一个设置:

magic_quotes_gpc = Off

这个默认是关闭的,如果它打开后将自动把用户提交对sql的查询进行转换,

比如把 ' 转为 \'等,这对防止sql注射有重大作用。所以我们推荐设置为:

magic_quotes_gpc = On

(10) Error message control

Generally, PHP will prompt an error when it is not connected to the database or under other circumstances. Generally, the error message will contain a PHP script when

The path information before

or the query SQL statement and other information are unsafe if provided to hackers, so it is generally recommended that servers disable error prompts:

display_errors = Off

If you want to display an error message, be sure to set the level of display error, for example, only display information above warning:

error_reporting = E_WARNING & E_ERROR

Of course, I still recommend turning off error prompts.

(11) Error log

It is recommended to record the error information after turning off display_errors, so as to find the reason why the server is running:

log_errors = On

At the same time, you must also set the directory where the error log is stored. It is recommended that the root apache log be stored together:

error_log = D:/usr/local/apache2/logs/php_error.log

Note: The file must be given to allow the apache user and group to have write permissions.

MySQL operation with reduced privileges

Create a new user such as mysqlstart

net user mysqlstart ****microsoft /add

net localgroup users mysqlstart /del

Does not belong to any group

If MYSQL is installed in d:mysql, then give mysqlstart full control permissions

Then set the MYSQL service properties in the system service. In the login properties, select this user mysqlstart and enter the password and confirm.

Restart the MYSQL service, and then MYSQL will run with low privileges.

If apache is built on a windos platform, we need to pay attention to one thing. Apache runs with system permissions by default,

This is scary, this makes people feel very uncomfortable. Then let’s lower the permissions of apache.

net user apache ****microsoft /add

net localgroup users apache /del

ok. We created a user apche that does not belong to any group.

We open the computer manager, select services, click on the properties of the apache service, we select log on, select this account, we fill in the account and password created above,

Restart the apache service, ok, apache is running with low permissions.

In fact, we can also set the permissions of each folder so that the apache user can only perform what we want it to do, and create a separate read-write user for each directory.

This is also a popular configuration method used by many virtual host providers. However, this method is overkill when used to prevent this.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321614.htmlTechArticleIn addition, the currently popular SQL Injection also has many ways to use it on PHP, so you must ensure safety, PHP Code writing is one aspect, and PHP configuration is very critical. We...
source:php.cn
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