Home php教程 PHP开发 Configure some server-side features of PHP to enhance PHP security

Configure some server-side features of PHP to enhance PHP security

Dec 05, 2016 am 11:19 AM
linux nosql php php+mysql php server web server database server

Shaun Clowes and rfp have previously introduced in detail the problems encountered by PHP and CGI programs during the programming process, and how to break through the system through application vulnerabilities. In this article, we will go through some of PHP's
server-side features To configure and enhance the security of php. When writing cgi scripts, we must pay attention to various security issues and strictly filter user input. However, how can we often walk on the shore without getting our shoes wet, and how can we eat sesame seeds without dropping sesame seeds? , even the famous phpnuke, phpMyAdmin and other programs have experienced serious problems problem, not to mention scripts written by gangsters like me. So now we assume that the php script has serious problems, such as a while ago There is a big problem with phpnuke being able to upload php scripts. How can we configure the server to prevent the script from breaking through the system if there is such a problem?

1. Pay attention to patching known vulnerabilities when compiling

Starting from 4.0.5, PHP’s mail function has added a fifth parameter, but it is not filtered properly, allowing PHP applications to break through the safe_mode restrictions. Excuting an order. So when using 4.0.5 and 4.0.6
Before compilation, we need to modify the ext/standard/mail.c file in the php source code package to disable the fifth parameter of the mail function or filter shell characters. In line 152 of the mail.c file, which is the following line:
if (extra_cmd != NULL) {
Add extra_cmd=NULL after
; or extra_cmd = php_escape_shell_cmd(extra_cmd);
Then compile php, then we will patch it found this vulnerability.

2. Modify the php.ini configuration file

Make modifications based on the php.ini-dist of the php distribution version.
1)Error handling and logging
You can make some settings in the Error handling and logging section. First find:
display_errors = On
php turns on the error message display by default, we change it to:
display_errors = Off

After turning off the error display, the php function execution error message will no longer be displayed to the user, so To a certain extent, it can prevent attackers from learning the physical location of the script and some other useful information from the error message, which at least creates certain obstacles for the attacker's black box detection. These error messages may be useful to ourselves. We can let it be written to the specified file, then modify the following:
log_errors = Off
Change to:
log_errors = On

And specify the file, find the following line:
;error_log = filename
Remove the previous; comment and change filename to the specified file, such as
/usr/local/apache/logs/php_error.log

error_log = /usr/local/apache/logs/php_error.log
All errors will appear like this Will be written to the php_error.log file.

2)Safe Mode

php’s safe_mode function limits or disables many functions, which can solve php’s security issues to a great extent. Find in the Safe Mode section:
safe_mode = Off
Change to:
safe_mode = On

This turns on the safe_mode function. Some functions like shell_exec() and `` that can execute system commands are prohibited, and some other execution functions such as: exec(), system(), passthru(), popen() will be restricted to executing programs in the directory specified by safe_mode_exec_dir. If you really want to execute some commands or programs, find the following:
safe_mode_exec_dir =
Specify the path of the program to be executed, such as:
safe_mode_exec_dir = /usr/local/php/exec

Then copy the program you want to use to/ usr/local/php/exec directory, so that restricted functions like the above can also execute programs in this directory.

For detailed information about restricted functions in safe mode, please see the instructions on the main php website:
[url]http://www.php.net/manual/en/features.safe-mode.php[/url]

3)disable_functions

If you are not clear about the harmfulness of some functions and do not use them, simply disable these functions. Find the following line:
disable_functions =
Add the functions to be disabled after "=", and separate multiple functions with ",".

3. Modify httpd.conf

If you only allow your php script program to operate in the web directory, you can also modify the httpd.conf file to limit the operation path of php. For example, if your web directory is /usr/local/apache/htdocs, then add these lines to
httpd.conf:


php_admin_value open_basedir /usr/local/apache /htdocs


In this way, if the script wants to read files other than /usr/local/apache/htdocs, it will not be allowed. If the error message is opened,

will prompt an error like this:
Warning: open_basedir restriction in effect. File is in wrong directory in /usr/local/apache/htdocs/open.php on line 4 and so on.

4. Compile the php code

Zend has made a great contribution to PHP. The PHP4 engine uses Zend, and it has also developed many PHP enhancement components such as ZendOptimizer and ZendEncode. The optimizer ZendOptimizer only needs to be registered at [url]http://www.zend.com[/url] to be free?...

ZendOptimizer-1[1].1.0-PHP_4.0.5-FreeBSD4.0-i386.tar.gz
ZendOptimizer-1[1].1.0-PHP_4.0.5 -Linux_glibc21-i386.tar.gz
ZendOptimizer-1[1].1.0-PHP_4.0.5-Solaris-sparc.tar.gz
ZendOptimizer-1[1].1.0-PHP_4.0.5-Windows-i386.zip


The installation of the optimizer is very convenient, and there are detailed instructions in the package. Taking the UNIX version as an example, check the operating system clearly and extract the ZendOptimizer.so file in the package to a directory, assuming it is /usr/local/lib. Add two sentences to php.ini:
zend_optimizer.optimization_level= 15
zend_extension="/usr/local/lib/ZendOptimizer.so"

That’s it. Use phpinfo() to see the following text on the left side of the Zend icon:
with Zend Optimizer v1.1.0, Copyright (c) 1998-2000, by Zend Technologies

Then, the optimizer has been successfully attached.
But the compiler ZendEncode is not free. Here is a design by Ma Yong of [url]http://www.PHPease.com[/url] ... Mi Jiantao's Kangmo胗?/a> [url]http://www.zend.com[/url] Contact to obtain the license agreement.

After the PHP script is compiled, the execution speed of the script increases a lot. The script file can only see a bunch of garbled characters, which will prevent the attacker from further analyzing the script program on the server, and the password originally stored in clear text in the PHP script will also be lost. Got confidentiality, such as mysql password. However, it is more troublesome to change the script on the server side. It is better to change it locally and then upload it.


5. Permission settings for files and directories

Except for the upload directory, the permissions for other directories and files in the web directory must not allow the nobody user to write permissions. Otherwise, the attacker can modify the homepage file, so the permissions of the web directory must be set properly. Also, the owner of the php script must not be root, because files are read in safe_mode. The function is restricted so that the owner of the file being read must be the same as the owner of the currently executing script before it can be read. Otherwise, if the error display is turned on, an error such as the following will be displayed:

Warning: SAFE MODE Restriction in effect. The script whose uid is 500 is not allowed to access /etc/passwd owned by uid 0 in /usr/local/apache/htdocs/open.php on line 3
This way we can prevent many system files from being read, such as: /etc/passwd, etc.

The owners of the upload directory and the upload script must also be set to the same, otherwise errors will occur. Please pay attention to these in safe_mode.

6. Mysql startup permission settings


Mysql should be noted not to be started with root. It is best to create another mysqladm user. You can add a sentence to the system startup script such as /etc/rc.local:
su mysqladm -c "/usr/local/mysql/share/mysql/mysql.server start"

In this way, mysqladmin will be automatically used after the system restarts The user starts the mysql process.

7. Review of log files and upload directories and

Viewing logs has a lot to do with human laziness. Finding traces of attacks from such a large log file is like looking for a needle in a haystack, and there may not be one. The files in the directory uploaded by the web should also be checked frequently. There may be a problem with the program and the user has uploaded some illegal files, such as executing scripts.

8. Patches of the operating system itself

Same, patching known vulnerabilities in the system is the most basic responsibility of the system administrator, and it is also the last line of defense.


After the above configuration, although it cannot be said to be impregnable, it has also caused a lot of trouble for the attacker's testing to a certain extent. Even if there are serious vulnerabilities in the php script program, the attacker will not be able to cause actual damage.

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 and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Why Use PHP? Advantages and Benefits Explained Why Use PHP? Advantages and Benefits Explained Apr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP vs. Python: Use Cases and Applications PHP vs. Python: Use Cases and Applications Apr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

PHP: An Introduction to the Server-Side Scripting Language PHP: An Introduction to the Server-Side Scripting Language Apr 16, 2025 am 12:18 AM

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP and the Web: Exploring its Long-Term Impact PHP and the Web: Exploring its Long-Term Impact Apr 16, 2025 am 12:17 AM

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

See all articles