PHP是广泛使用的开源服务端脚本语言。通过HTTP或HTTPS协议,Apache Web服务允许用户访问文件或内容。服务端脚本语言的错误配置会导致各种问题。因此,PHP应该小心使用。以下是为系统管理员准备的,安全配置PHP的25个实践事例。
用于下文的PHP设置样例
下午列出的大部分操作,都是基于 root 用户能在 bash 或其他现代 shell 上执行操作的假设。
$ php -v
样例输出
PHP 5.3.3 (cli) (built: Oct 24 2011 08:35:41) Copyright (c) 1997-2010 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
本文使用的操作系统
$ cat /etc/redhat-release
样例输出
Red Hat EnterPRise Linux Server release 6.1 (Santiago)
#1:知彼
基于PHP的应用面临着各种各样的攻击:
#2:减少内建的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
从性能与安全性的角度考虑,我建议使用PHP时减少不必要的模块。例如上面的sqlite3是不必要的。那么可以通过删除或重命名/etc/php.d/sqlite3.ini文件来取消它:
# rm /etc/php.d/sqlite3.ini
或
# mv /etc/php.d/sqlite3.ini /etc/php.d/sqlite3.disable
有些模块则只能通过使用重新编译安装PHP来移除。例如,从php.net下载PHP源码后,使用下面指令编译GD,fastcgi和MySQL支持:
./configure --with-libdir=lib64 --with-gd --with-mysql --prefix=/usr --exec-prefix=/usr --bindir=/usr/bin --sbindir=/usr/sbin --sysconfdir=/etc --datadir=/usr/share --includedir=/usr/include --libexecdir=/usr/libexec --localstatedir=/var --sharedstatedir=/usr/com --mandir=/usr/share/man --infodir=/usr/share/info --cache-file=../config.cache --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --enable-fastcgi --enable-force-cgi-redirect
更多信息请查看:how to compile and reinstall php on Unix like Operating system
#3:防止PHP信息泄漏
可以通过取消export_php,对PHP信息泄漏进行限制。编辑/etc/php.d/security.ini如下:
expose_php=Off
expose_php会在HTTP Header中添加服务器上,包括版本在内的PHP信息(例如X-Powered-By: PHP/5.3.3)。同时,PHP的全局统一标识符也会暴露。如果export_php启用的话,可以通过下面命令查看PHP版本信息:
$ curl -I http://www.cyberciti.biz/index.php
样例输出:
HTTP/1.1 200 OK X-Powered-By: PHP/5.3.3 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 ...
建议同时隐藏Apache版本等信息:ServerTokens and ServerSignature directives in httpd.conf to hide Apache version
#4:最小化可载入的PHP模块(动态Extension)
PHP支持“Dynamic Extensions”。默认情况下,RHEL会载入/etc/php.d/目录下的所有Extension模块。如需启用或取消某一模块,只需把/etc/php.d/目录下配置文件把该模块注释掉。也可以把文件删除或重命名该模块的配置文件。为了最优化PHP的性能和安全性,应只启用Web应用所需的Extension。例如,用下面命令取消GD模块:
# cd /etc/php.d/ # mv gd.{ini,disable} # <span style="text-decoration: underline;">/sbin/service httpd restart</span>
启用则是:
# mv gd.{disable,ini} # <span style="text-decoration: underline;">/sbin/service httpd restart</span>
#5:记录所有PHP错误
不要把PHP错误信息输出给所用用户。编辑/etc/php.d/security.ini,如下修改:
display_errors=Off
确保把所有错误信息记录到日志文件
log_errors=Onerror_log=/var/log/httpd/php_scripts_error.log
#6:禁止文件上传
为安全考虑,如下编辑/etc/php.d/security.ini取消文件上传
file_uploads=Off
如用户的确需要上传文件,那么把它启用,而后限制PHP接受的最大文件大小:
file_uploads=On# user can only upload upto 1MB via phpupload_max_filesize=1M
#7:关闭远程代码执行
如果这个特性被启动,PHP可以通过allow_url_fopen,在file_get_contents()、include、require中获取诸如FTP或网页内容这些远程数据。程序员经常忘记了对用户输入进行过滤,而如果这些函数调用了这些数据,则形成了注入漏洞。在基于PHP的Web应用中,大量代码中的注入漏洞都由此产生。可以通过编辑/etc/php.d/security.ini来关闭该特性:
allow_url_fopen=Off
除此之外,建议把allow_url_include也取消掉:
allow_url_include=Off
#8:启用SQL安全模式
如下修改/etc/php.d/security.ini:
sql.safe_mode=On
当此特性被启用,mysql_connect()和mysql_pconnect()会忽略传入的所有参数。与此同时,你需要在代码上做些相应的修改。第三方以及开源应用,如Wordpress,在sql.safe_mode下可能无法正常工作。同时建议关闭5.3.x版本的PHP的magic_quotes_gpc过滤,因为它简单粗暴又没效率。使用mysql_escape_string()以及自定义的过滤函数会更好一些
magic_quotes_gpc=Off
#9:控制POST的数据大小
HTTP POST通常作为请求的一部分,被客户端用于向Apache Web服务器发送数据,如上传文件或提交表单。攻击者会尝试发送超大的POST请求去消耗服务器的资源。如下编辑/etc/php.d/security.ini限制POST的最大大小:
; 在这里设置一个靠谱的数值 post_max_size=1K
这里设置了1K的最大大小。这个设置会影响到文件上传。要上传大文件,这个值需要比update_max_filesize大。建议在Apache中限制可用的请求方法,编辑httpd.conf如下:
<Directory /var/www/html> <LimitExcept GET POST> Order allow,deny </LimitExcept> ## Add rest of the config goes here... ## </Directory>
#10:资源控制(DoS控制)
设置每个PHP脚本的最大运行时间。另外建议限制用于处理请求数据的最大时间,以及最大可用内存数。
# 单位:秒
max_execution_time = 30max_input_time = 30memory_limit = 40M
#11:为PHP安装Suhosin高级保护系统
具体参考Suhosin项目页:project page
#12:取消危险的PHP函数
PHP有大量可用于入侵服务器的函数,如使用不当则会成为漏洞。如下取消这些函数:
disable_functions =exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source
#13:PHP Fastcgi / CGI – cgi.force_redirect管理
PHP可与Fastcgi协同工作。Fastcgi可以减少Web服务器的内存足迹(memory footprint),并改善PHP性能。可以参考这个来配置Apache2+PHP+FastCGI。在这个配置中,cgi.force_redirect会阻止用户通过访问URL来调用PHP。为安全考虑,启用该特性:
; Enable cgi.force_redirect for security reasons in a typical *Apache+PHP-CGI/FastCGI* setup cgi.force_redirect=On
#14:PHP用户与用户组ID
mod_fastcgi是Apache Web服务的一个cgi模块,可连接到外部的FASTCGI服务器。你需要确保PHP使用非root用户运行。若其使用root或是UID小于100的用户权限,它就可以访问,乃至操作系统文件。通过Apache’s suEXEC或mod_suPHP,可在非特权用户下执行PHP CGI。suEXEC可以是Apache调用CGI程序的user ID不同于运行Apache的user ID。如下:
# ps aux | grep php-cgi
样例输出:
phpcgi 6012 0.0 0.4 225036 60140 S Nov22 0:12 /usr/bin/p
新闻热点
疑难解答