background image

PHP 的几个安全应用实践方式

PHP 被广泛用于各种 Web 开发。而当服务器端脚本配置错误时会出现各种问题。现今,

大部分

Web 服务器是基于 Linux 环境下运行(比如:Ubuntu,Debian 等)。本文例举了十大

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 中的模块。来看看下面这个被执行

命令安装的模块。

  

# php –m 你将会得到类似的结果:

   

[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

  

# rm /etc/php.d/sqlite3.ini

  或者
  

# mv /etc/php.d/sqlite3.ini /etc/php.d/sqlite3.disableRestrict 2. 使 PHP 信息泄露最小化

  在默认

PHP 时在 HTTP 抬头处会生成一条线介于每个响应中,(比如 X-Powered-

By: PHP/5.2.10)。而这个在系统信息中为攻击者创建了一个非常有价值的信息。

  

HTTP 示例:

   

HTTP/1.1  200  OK  X-Powered-By :   PHP/5.2.10  Content-type :   text/html; 

charset=UTF-8  Vary :   Accept-Encoding ,   Cookie  X-Vary-Options :   Accept-Encoding;list-
contains=gzip

Cookie;string-contains=wikiToken;string-contains=wikiLoggedOut;string-

contains=wiki_session Last-Modified: Thu, 03 Nov 2011 22:32:55 GMT……

    因 此 , 我 们 强 烈 建 议 , 禁 止

PHP 信 息 泄 露 , 想 要 要 禁 止 它 , 我 们 要 编

/etc/php.d/secutity.ini,并设置以下指令:

  

expose_php=Off

  

3. 使 PHP 加载模块最小化

  在默认情况下,

RHEL 加载的所有模块可以在/etc/php.d/目录中找到。要禁用或启

用一个特定的模块,只需要在配置文件

/etc/php.d/目录中中注释下模块名称。而为了优化

PHP 性能和安全性,当你的应用程序需要时,我们强烈建议建议启用扩展功能。举个例子:
当禁用

GD 扩展时,键入以下命令:

  

# cd /etc/php.d/

  

# mv gd.{ini,disable}

  

# /etc/init.d/apache2 restart 为了扩展 PGP GD 模块,然后键入以下命令: