Configure some server-side features of php to Enhance php Security

Source: Internet
Author: User
Previously, ShaunClowes and rfp both described in detail the problems encountered by php and cgi programs during programming, and how to break through the system through application vulnerabilities, such as Shaun Clowes and rfp, which have described in detail the problems encountered by php and cgi programs during programming, and how to break through the system through application vulnerabilities.
Server features to Enhance php Security. When writing cgi scripts, we must pay attention to various security issues and strictly filter user input. However, we often go to the shore to see whether there are wet shoes and how can we eat biscuits?
No Sesame seeds are lost, people are lost, and even the famous phpnuke, phpMyAdmin and other programs have encountered very serious problems, not to mention the scripts I am waiting. So now let's assume that php scripts have serious problems, such as the large problem that phpnuke could Upload php scripts a while ago, we cannot break through the system by configuring the server so that the script has such a problem.

1. pay attention to the known vulnerabilities during compilation.

Since 4.0.5, php's mail function has added the fifth parameter, but it does not properly filter it, so that php applications can break through the safe_mode restrictions to execute commands. So when we use 4.0.5 and 4.0.6
Before compilation, 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 2 of the mail. c file, that is, the following line:
If (extra_cmd! = NULL ){
Add extra_cmd = NULL; or extra_cmd = php_escape_shell_cmd (extra_cmd );
Then compile php, then we fix this vulnerability.

2. modify the php. ini configuration file.

Modify it based on php. ini-dist of the php release version.
1) Error handling and logging
Some settings can be made in the Error handling and logging sections. First find:
Display_errors = On
By default, the error message is displayed in php. we will change it:
Display_errors = Off

After the error display is disabled, the php function execution error information will not be displayed to the user. This will prevent attackers from learning the physical location of the script from the error information to a certain extent, as well as some other useful information, at least it may cause some obstacles to the attacker's black box detection. These error messages may be useful to us and can be written to a specified file. modify the following:
Log_errors = Off
Changed:
Log_errors = On

And the specified file. find the following line:
; Error_log = filename
Remove the comment and change filename to the specified file, as shown in figure
/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

The safe_mode function of php limits or disables many functions, which can solve php security problems to a great extent. In the Safe Mode section, find:
Safe_mode = Off
Changed:
Safe_mode = On

The safe_mode function is enabled. Shell_exec () and ''functions that can execute system commands are prohibited. other execution functions include exec (), system (), passthru (), and popen () only programs in the directory specified by safe_mode_exec_dir can be executed. If you want to execute some commands or programs, find the following:
Safe_mode_exec_dir =
Specifies the path of the program to be executed, for example:
Safe_mode_exec_dir =/usr/local/php/exec

Then, copy the program to the/usr/local/php/exec Directory. in this way, the restricted function can execute the program in this directory.

For details about restricted functions in safe mode, refer to the instructions on the php main site:
[Url] http://www.php.net/manual/en/features.safe-mode.php#/url]

3) disable_functions

If you are not clear about the dangers of some functions and are not using them, simply disable them. Find the following line:
Disable_functions =
Add the function to be disabled after "=". multiple functions are separated.

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 php operation path. For example, if your web directory is/usr/local/apache/htdocs
Add the following lines to httpd. conf:


Php_admin_value open_basedir/usr/local/apache/htdocs  

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

The following error is prompted:
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 php code

Zend contributes a lot to php. The php P4 engine uses Zend, and it also develops many php enhancement components such as ZendOptimizer and ZendEncode. Optimizer ZendOptimizer can be free of registration in the [url] http://www.zend.com [/url ?... What is the South and low frequency of the straight-forward complex? /A>

Zendoptimizer-11_12.16.1.0-php_4.0.5-freebsd4.0-i386.tar.gz
Zendoptimizer-11_12.16.1.0-php_4.0.5-linux_glibc21-i386.tar.gz
Zendoptimizer-11_12.16.1.0-php_4.0.5-solaris-sparc.tar.gz
Zendoptimizer-11_12.16.1.0-php_4.0.5-windows-i386.zip


The Optimizer is very convenient to install. the package contains detailed instructions. Take the UNIX version as an example. check the operating system and extract the ZendOptimizer. so file in the package to a directory. assume it is/usr/local/lib.
In php. ini, add the following two sentences:
Zend_optimizer.optimization_level = 15
Zend_extension = "/usr/local/lib/ZendOptimizer. so"

You can. Use phpinfo () to see the following text on the left of the Zend icon:
With Zend Optimizer v1.1.0, Copyright (c) 1998-2000, by Zend Technologies

The Optimizer is mounted successfully.
But the compiler ZendEncode is not free of charge, here to provide you with a [url] http://www.PHPease.com [/url] Ma Yong design... chitao Kang Mo Yao? /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. only a bunch of garbled characters can be seen in the script file, which will prevent attackers from further analyzing script programs on the server, the passwords originally stored in plain text in php scripts are also kept confidential, such as mysql passwords. However, it is quite troublesome to change the script on the server. you can change the script locally and upload it again.


5. file and directory permission settings

In addition to the upload directory, the permissions on other directories and files in the web directory must not allow the nobody user to have the write permission. Otherwise, attackers can modify the home page file, so the permissions of the web Directory must be set. Also, the owner of the php script cannot be the root user, because the function for reading files in safe_mode is restricted to the owner of the read file, it must be the same as the owner of the current script to be read, otherwise, 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 prevents many system files from being read, such as/etc/passwd.

The owner of the upload directory and the upload script must be set to the same type. otherwise, errors may occur. pay attention to these errors in safe_mode.

6. mysql startup permission settings


Mysql should note that it is better to create another mysqladm user instead of using root to start mysql. You can add the following statement to the/etc/rc. local startup script:
Su mysqladm-c "/usr/local/mysql/share/mysql. server start"

After the system is restarted, the mysql process is automatically started with the mysqladmin user.

7. Audit and upload of log files and directories

There is a lot to do with viewing logs and human inertia. it is necessary to search for attack traces from such a large log file, and there may not be a haystack. Files in the web-uploaded directory should also be checked frequently. maybe there is a program problem. The user uploads some illegal files, such as executing scripts.

8. operating system patches

Similarly, patches for known system vulnerabilities are the most basic responsibilities of the system administrator. this is the last line of defense.


After the above configuration, although it cannot be fixed, it also causes a lot of trouble for attackers to test, even if the php script program has a serious vulnerability, attackers cannot cause actual damage.
If you still have a more eccentric and abnormal configuration method, I hope you can share it with me ;)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.