Home Backend Development PHP Tutorial [转]十大PHP绝佳安全实践

[转]十大PHP绝佳安全实践

Jun 13, 2016 am 10:40 AM
etc html ini nbsp php

[转]十大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模块,然后键入以下命令:

引用
# mv gd.{disable,ini} 

# /sbin/service httpd restart
4. 记录PHP错误信息

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

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

引用
log_errors=On

error_log=/var/log/httpd/php_scripts_error.log 

5. 禁用远程执行代码

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

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

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

引用
allow_url_include=Off
6. 禁用PHP中的危险函数

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

引用
disable_functions =exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source
7. 资源控制

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

引用
# set in seconds 

max_execution_time = 30

max_input_time = 30

memory_limit = 40M

8. 限制PHP访问文件系统

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

; Limits the PHP process from accessing files outside 
; of specifically designated directories such as /var/www/html/ 
open_basedir="/var/www/html/"
; ------------------------------------ 
; Multiple dirs example 
; open_basedir="/home/httpd/vhost/cyberciti.biz/html/:/home/httpd/vhost/nixcraft.com/html/:/home/httpd/vhost/theos.in/html/"
; ------------------------------------
9.限制文件/目录访问

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

# chown -R apache:apache /var/www/
10.编译保护Apache,PHP和MySQL的配置文件

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

# chattr +i /etc/php.ini 

# chattr +i /etc/php.d/* 

# chattr +i /etc/my.ini 

# chattr +i /etc/httpd/conf/httpd.conf 

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

引用
# chattr +i /var/www/html/file1.php 

# chattr +i /var/www/html/
(注:本文由夏梦竹编译,转载请注明出处。)

中文出处:http://sd.csdn.net/a/20120130/311203.html
英文出处:http://www.ansoncheung.tk/articles/top-10-php-best-security-practices-sys-admins
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

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

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

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles