Home Backend Development PHP Tutorial Strengthen PHP security by configuring server-side features_PHP Tutorial

Strengthen PHP security by configuring server-side features_PHP Tutorial

Jul 21, 2016 pm 04:07 PM
php Home page author strengthen Safety right server of pass Configuration

Author: san < A more detailed introduction to the problems encountered in the programming process of PHP and CGI programs, and how to break through the system through application vulnerabilities. In this article, we will strengthen the security of PHP by configuring some server-side features of PHP. When writing cgi scripts, we must pay attention to various security issues and strictly filter user input. However, there is no way you can walk on the shore without getting your shoes wet, or eat sesame seeds without losing sesame seeds. People may stumble, and horses may miss. Even the famous phpnuke, phpMyAdmin and other programs have had serious problems, let alone scripts written by gangsters like me. So now we assume that serious problems have occurred in php scripts. For example, a while ago, phpnuke had a big problem with uploading php scripts. How can we configure the server to prevent the script from breaking through the system if such problems occur?

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, making PHP applications The program can break through the restrictions of safe_mode and execute commands. Therefore, when using 4.0.5 and 4.0.6, we need to modify the
ext/standard/mail.c file in the php source code package before compilation 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) {
followed by extra_cmd=NULL; or extra_cmd = php_escape_shell_cmd(extra_cmd);
Then compile php, then we will fix this vulnerability.

2. Modify the php.ini configuration file

Modify it 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
By default, php turns on the error message display. We changed it to:
display_errors = Off

After turning off the error display, the php function executes the error The information will no longer be displayed to the user. This can
prevent the attacker from knowing the physical location of the script and some other useful information from the error message to a certain extent. This will at least cause certain obstacles to the attacker's black box detection. . These error messages may be useful to ourselves. We can write it to the specified file, then modify the following:
log_errors = Off
to:
log_errors = On

and the specified 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
In this way, all errors will be written to the php_error.log file.

2)Safe Mode

PHP’s safe_mode function restricts 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 other execution functions such as: exec(), system(), passthru(), popen() will be restricted to only execute files in the directory specified by safe_mode_exec_dir. program. 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 the /usr/local/php/exec directory, so that the restricted functions like the above
can also execute the program in the directory.

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

3) disable_functions

If you are not sure about the harmfulness of some functions and do not use them, simply disable these functions
.Find the following line:
disable_functions =
Add the function 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 display is turned on

it 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
Wait.

4. Compile the php code

Zend has made a great contribution to php. The engine of php4 uses Zend, and it has also developed ZendOptimizer
and ZendEncode and many other php Strengthen components. The optimizer ZendOptimizer can be obtained for free by just registering at
http://www.zend.com. The following are
ZendOptimizers for 4.0.5 and 4.0.6. The file names are for their respective systems. :

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 and unzip 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 connected.
But the compiler ZendEncode is not free. Here is a compiler shell designed by Ma Yong of
http://www.PHPease.com. If it is used for commercial purposes, please contact
http Contact ://www.zend.com to obtain a license agreement.

After the php script is compiled, the execution speed of the script increases a lot, and 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 original php script Passwords stored in clear text
are also kept confidential, such as mysql passwords. 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

In addition to the upload directory, the permissions of other directories and files in the web directory must not allow the nobody user to have write permissions
. Otherwise, the attacker can modify the home page file, so the permissions of the web directory must be set
. Also, the owner of the php script must not be root, because the function of reading files in safe_mode is restricted
The owner of the file to be read must be the same as the owner of the currently executing script in order to be read, otherwise if
If the error display is turned on, errors 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
In 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. In safe_mode
these should be noted.

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 in
/etc/rc.local and other system startup scripts:
su mysqladm -c "/usr/local/mysql/share/mysql/mysql.server start"

like this After the system restarts, the mysql process will be automatically started as the mysqladmin user.

7. Review of log files and upload directories

Viewing logs has a lot to do with human laziness. Finding attack traces from such a large log file is like finding a needle in a haystack, and it may not be possible. have. 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

Similarly, 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 practical consequences. of destruction. If you have any weirder or more perverted configuration methods, I hope you can share them;)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/315065.htmlTechArticleAuthor: san xuzhikun@nsfocus.com Homepage: http://www.nsfocus.com Date: 2001-11 -15 Earlier, Shaun Clowes and rfp introduced in detail the problems encountered in php and cgi programs during the programming process...
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

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

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 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.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

See all articles