PHP security settings

Source: Internet
Author: User
The server cannot prevent all security issues, such as program vulnerabilities, user input forms, and PHP file permissions. You can also use some means to confuse hackers or those with ulterior motives.

The server cannot prevent all security issues, such as program vulnerabilities, user input forms, and PHP file permissions. You can also use some means to confuse hackers or those with ulterior motives.
1. program code vulnerability

The major weakness of many PHP programs is not the problem of the PHP language, but caused by the low security awareness of programmers. Therefore, you must always pay attention to the possible problems in each piece of code to find out the possible impact of incorrect data submission.

 

 
  1. unlink ($evil_var);
  2. fwrite ($fp, $evil_var);
  3. system ($evil_var);
  4. exec ($evil_var);
  5. ?>

 

Always pay attention to your code to ensure that all the variables submitted from the client are properly checked and then ask yourself the following questions:

  • Can this script only affect the expected files?
  • Can an abnormal data be used after it is submitted?
  • Can this script be used for unplanned purposes?
  • Can this script be combined with other scripts to do bad things?
  • Are all transactions fully recorded?

Ask yourself these questions when writing code. Otherwise, you may need to rewrite the code to increase security. If you pay attention to these problems, you may not be able to guarantee the security of the system, but at least you can improve the security.

You can also consider disabling register_globals, magic_quotes, or other settings that make programming more convenient but make the legitimacy of a variable, and the sources and values of the variable messy.


2. user input form problems
Verify any data entered by the user to ensure the security of PHP code.
Note 1: JS is generated only to improve the user experience, rather than the verification tool. This is because any visiting user may accidentally disable the execution of client scripts, thus skipping this layer of verification. Therefore, we must check the data on the PHP server program.
Note 2: Do not use the super variable $ _ SERVER ['http _ referer'] to check the data source address. a very small Cainiao hacker will use a tool to forge the data of this variable, use Md5, rand, and other functions as much as possible to generate a token. when verifying the source, verify that the token matches.

3. php file permissions

PHP is designed to access the file system at the user level, so it is entirely possible to write a piece of PHP code to read system files such as/etc/passwd, change the network connection and send a large number of print tasks. Therefore, make sure that PHP code is suitable for reading and writing files. See the following code to delete an object in your home directory. In this case, the file system is managed through the web interface. Therefore, Apache users have the right to delete files in the user directory.

 

 
  1. $username = $_POST['user_submitted_name'];
  2. $homedir = "/home/$username";
  3. $file_to_delete = "$userfile";
  4. unlink ("$homedir/$userfile");
  5. echo "$file_to_delete has been deleted!";
  6. ?>

 

Since the username variable can be submitted through the user form, you can submit another user name and file name and delete the file. In this case, you must consider other authentication methods:

  • Only PHP web users have limited permissions.
  • Check all submitted variables.

The following describes how to verify and check more secure file names and variables:

 

 
  1. $username = $_SERVER['REMOTE_USER'];
  2. $homedir = "/home/$username";
  3. if (!ereg('^[^./][^/]*$', $userfile))
  4. die('bad filename');
  5. if (!ereg('^[^./][^/]*$', $username))
  6. die('bad username');
  7. ?>

 

4. hide the PHP extension

In general, increasing security through hidden means is considered to be of little effect. But in some cases, it is worthwhile to add as much security as possible.

Some simple methods can help to hide PHP, which can make it more difficult for attackers to discover system vulnerabilities. Setting expose_php = off in the php. ini file can reduce the useful information they can obtain.

Another policy is to let the web server use PHP to parse different extensions. Whether using the. htaccess file or Apache configuration file, you can set a file extension that can mislead attackers:

# Make PHP look like other programming languages
AddType application/x-httpd-php. asp. py. pl

# Make PHP look like an unknown file type
AddType application/x-httpd-php. bop. foo. 133 t

# Make PHP code look like an HTML page
AddType application/x-httpd-php. htm. html

To make this method take effect, you must change the extension of the PHP file to the above extension. In this way, security is improved through Hiding, although the defense capability is low and there are some shortcomings.

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.