Home php教程 php手册 系统管理员应知:十大PHP最佳安全实践

系统管理员应知:十大PHP最佳安全实践

Jun 06, 2016 pm 07:58 PM
php top ten Safety practice optimal administrator system

导读:本文来自知名时尚媒体ELLE(大陆版即《世界时装之苑》)网站的香港工程师Anson Cheung。文中他例举了有关PHP10个方面的最佳安全实践方式,供系统管理员学习与参考。 原文是《Top 10 PHP Best Security Practices for Sys Admins》, 以下是译文: PHP

导读:本文来自知名时尚媒体ELLE(大陆版即《世界时装之苑》)网站的香港工程师Anson Cheung。文中他例举了有关PHP10个方面的最佳安全实践方式,供系统管理员学习与参考。原文是《Top 10 PHP Best Security Practices for Sys Admins》,以下是译文:

PHP被广泛用于各种Web开发。而当服务器端脚本配置错误时会出现各种问题。现今,大部分Web服务器是基于Linux环境下运行(比如:Ubuntu,Debian等)。本文例举了十大PHP最佳安全实践方式,能够让您轻松、安全配置PHP。

系统管理员应知:十大PHP最佳安全实践

PHP安全性设置提示:

DocumentRoot: /var/www/

Default Web server: Apache

Default PHP configuration file: /etc/php.ini

Default PHP extensions config directory: /etc/php.d/

Our sample php security config file: /etc/php.d/security.ini (you need to create this file using a text editor)

Operating systems: Ubuntu (the instructions should work with any other Linux distributions such as RHEL / CentOS / Fedora or other Unix like operating systems such as OpenBSD/FreeBSD/HP-UX). 

1. 减少PHP内置模块

为了增强性能和安全性,强烈建议,减少PHP中的模块。来看看下面这个被执行命令安装的模块。

<ol><li><span><span># php –m </span></span></li></ol>
Copy after login

你将会得到类似的结果:

[PHP Modules]
apc
bcmath
bz2
calendar
Core
ctype
curl
date
dom
ereg
exif
fileinfo
filter
ftp
gd
gettext
gmp
hash
iconv
imap
json
libxml
mbstring
memcache
mysql
mysqli
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
readline
Reflection
session
shmop
SimpleXML
sockets
SPL
sqlite3
standard
suhosin
tokenizer
wddx
xml
xmlreader
xmlrpc
xmlwriter
xsl
zip
zlib
[Zend Modules]
Suhosin
 

删除一个模块,并执行此命令。例如:删除模块sqlite3

<ol>
<li><span><span># rm /etc/php.d/sqlite3.ini  </span></span></li>
<li><span> </span></li>
</ol>
Copy after login

或者

<ol><li><span><span># mv /etc/php.d/sqlite3.ini /etc/php.d/sqlite3.disableRestrict </span></span></li></ol>
Copy after login

2. 使PHP信息泄露最小化

在默认PHP时在HTTP抬头处会生成一条线介于每个响应中,(比如X-Powered-By: PHP/5.2.10)。而这个在系统信息中为攻击者创建了一个非常有价值的信息。

HTTP示例:

<ol>
<li><span><span>HTTP/1.1 200 OK  </span></span></li>
<li><span>X-Powered-By: PHP/5.2.10  </span></li>
<li>
<span>Content-type: text/html; </span><span>charset</span><span>=</span><span>UTF</span><span>-8  </span>
</li>
<li><span>Vary: Accept-Encoding, Cookie  </span></li>
<li>
<span>X-Vary-Options: Accept-Encoding;</span><span>list-contains</span><span>=</span><span>gzip</span><span>,Cookie;</span><span>string-contains</span><span>=</span><span>wikiToken</span><span>;  </span>
</li>
<li>
<span>string-contains</span><span>=</span><span>wikiLoggedOut</span><span>;</span><span>string-contains</span><span>=</span><span>wiki_session</span><span> </span>
</li>
<li><span>Last-Modified: Thu, 03 Nov 2011 22:32:55 GMT  </span></li>
<li><span>... </span></li>
</ol>
Copy after login

因此,我们强烈建议,禁止PHP信息泄露,想要要禁止它,我们要编辑/etc/php.d/secutity.ini,并设置以下指令:

<ol>
<li><span><span>expose_php</span><span>=</span><span>Off</span><span> </span></span></li>
<li><span> </span></li>
</ol>
Copy after login

3. 使PHP加载模块最小化

在默认情况下,RHEL加载的所有模块可以在/etc/php.d/目录中找到。要禁用或启用一个特定的模块,只需要在配置文件/etc/php.d/目录中中注释下模块名称。而为了优化PHP性能和安全性,当你的应用程序需要时,我们强烈建议建议启用扩展功能。举个例子:当禁用GD扩展时,键入以下命令:

<ol>
<li><span><span># cd /etc/php.d/  </span></span></li>
<li><span> </span></li>
<li><span># mv gd.{ini,disable}  </span></li>
<li><span> </span></li>
<li><span># /etc/init.d/apache2 restart </span></li>
</ol>
Copy after login

为了扩展PGP GD模块,然后键入以下命令:

<ol>
<li><span><span># mv gd.{disable,ini}  </span></span></li>
<li><span> </span></li>
<li><span># /sbin/service httpd restart </span></li>
</ol>
Copy after login

4. 记录PHP错误信息

为了提高系统和Web应用程序的安全,PHP错误信息不能被暴露出。要做到这一点,需要编辑/etc/php.d/security.ini 文件,并设置以下指令:

<ol><li><span><span>display_errors</span><span>=</span><span>Off</span><span> </span></span></li></ol>
Copy after login

为了便于开发者Bug修复,所有PHP的错误信息都应该记录在日志中。

<ol>
<li><span><span>log_errors</span><span>=</span><span>On</span><span> </span></span></li>
<li><span> </span></li>
<li>
<span>error_log</span><span>=/var/log/httpd/php_scripts_error.log  </span>
</li>
<li><span> </span></li>
</ol>
Copy after login

5. 禁用远程执行代码

如果远程执行代码,允许PHP代码从远程检索数据功能,如FTP或Web通过PHP来执行构建功能。比如:file_get_contents()。

很多程序员使用这些功能,从远程通过FTP或是HTTP协议而获得数据。然而,此法在基于PHP应用程序中会造成一个很大的漏洞。由于大部分程序员在传递用户提供的数据时没有做到适当的过滤功能,打开安全漏洞并且创建了代码时注入了漏洞。要解决此问题,需要禁用_url_fopen in /etc/php.d/security.ini,并设置以下命令:

<ol><li><span><span>allow_url_fopen</span><span>=</span><span>Off</span><span> </span></span></li></ol>
Copy after login

除了这个,我还建议禁用_url_include以提高系统的安全性。

<ol><li><span><span>allow_url_include</span><span>=</span><span>Off</span><span> </span></span></li></ol>
Copy after login

6. 禁用PHP中的危险函数

PHP中有很多危险的内置功能,如果使用不当,它可能使你的系统崩溃。你可以创建一个PHP内置功能列表通过编辑/etc/php.d/security.ini来禁用它。

<ol><li><span><span>disable_functions</span><span> =</span><span>exec</span><span>,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source </span></span></li></ol>
Copy after login

7. 资源控制

为了提高系统的稳定性,强烈建议设置每个脚本解析请求数据所花费的时间和脚本可能消耗的最大内存量。正确的配置这些参数可以防止PHP任何脚本消耗太多的资源或是内存,从而避免系统不安全或降低安全系数。

<ol>
<li><span><span># set in seconds  </span></span></li>
<li><span> </span></li>
<li>
<span>max_execution_time</span><span> = </span><span>30</span><span> </span>
</li>
<li><span> </span></li>
<li>
<span>max_input_time</span><span> = </span><span>30</span><span> </span>
</li>
<li><span> </span></li>
<li>
<span>memory_limit</span><span> = </span><span>40M</span><span> </span>
</li>
<li><span> </span></li>
</ol>
Copy after login

8. 限制PHP访问文件系统

该open_basedir指令指定的目录是允许PHP访问使用fopen()等功能。如果任何脚本试图访问超出open_basdir定义的路径文件,PHP将拒绝打开。值得注意的是,你不能使用一个符号链接作为一种变通方法。

<ol>
<li><span><span>; Limits the PHP process from accessing files outside  </span></span></li>
<li><span>; of specifically designated directories such as /var/www/html/  </span></li>
<li>
<span>open_basedir</span><span>=</span><span>"/var/www/html/"</span><span> </span>
</li>
<li><span>; ------------------------------------  </span></li>
<li><span>; Multiple dirs example  </span></li>
<li>
<span>; </span><span>open_basedir</span><span>=</span><span>"/home/httpd/vhost/cyberciti.biz/html/:/home/httpd/vhost/nixcraft.com/html/:/home/httpd/vhost/theos.in/html/"</span><span> </span>
</li>
<li><span>; ------------------------------------ </span></li>
</ol>
Copy after login

9.限制文件/目录访问

进行适当的安全设置:确保Apache作为非root用户运行,比如www-data或www。对于文件和目录在基于/var/www/下同样属于非root用户。想要更改所有者,执行以下命令:

<ol><li><span><span># chown -R apache:apache /var/www/ </span></span></li></ol>
Copy after login

10.编译保护Apache,PHP和MySQL的配置文件

使用charrt命令编译保护配置文件

<ol>
<li><span><span># chattr +i /etc/php.ini  </span></span></li>
<li><span> </span></li>
<li><span># chattr +i /etc/php.d/*  </span></li>
<li><span> </span></li>
<li><span># chattr +i /etc/my.ini  </span></li>
<li><span> </span></li>
<li><span># chattr +i /etc/httpd/conf/httpd.conf  </span></li>
<li><span> </span></li>
<li><span># chattr +i /etc/ </span></li>
</ol>
Copy after login

使用charrt命令可以编译保护PHP文件或者是文件中的/var/www/html的目录:

<ol>
<li><span><span># chattr +i /var/www/html/file1.php  </span></span></li>
<li><span> </span></li>
<li><span># chattr +i /var/www/html/ </span></li>
</ol>
Copy after login

英文出自:ansoncheung.tk

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles