Home Backend Development PHP Tutorial Several common security details related to PHP

Several common security details related to PHP

Jan 14, 2017 pm 01:39 PM
php php security

We installed PHP manually. The default configuration file of PHP is in /usr/local/apache2/conf/php.ini. Our most important thing is to configure the content in php.ini so that we can execute PHP more safely. The security settings in the entire PHP are mainly to prevent attacks from phpshell and SQL Injection. Let’s discuss it slowly. We first use any editing tool to open /etc/local/apache2/conf/php.ini. If you install it in other ways, the configuration file may not be in this directory.

(1) Open PHP’s safe mode

php’s safe mode is a very important built-in security mechanism that can control some functions in PHP, such as system (),

At the same time, many file operation functions are controlled by permissions, and certain key files are not allowed, such as /etc/passwd,

But the default php.ini is If the safe mode is not turned on, we turn it on:

safe_mode = on
Copy after login

(2) User Group Security

When safe_mode is turned on, safe_mode_gid is turned off, then the php script can The file can be accessed, and users in the same

group can also access the file.

It is recommended to set it to:

safe_mode_gid = off
Copy after login

If you do not set it, we may not be able to operate the files in our server website directory. For example, we need to

operate the files. when.

(3) Home directory for executing programs in safe mode

If safe mode is turned on, but you want to execute certain programs, you can specify the program to be executed. Main directory:

safe_mode_exec_dir = D:/usr/bin
Copy after login

Generally, there is no need to execute any program, so it is recommended not to execute the system program directory. You can point to a directory,

and then copy the program that needs to be executed. For example:

safe_mode_exec_dir = D:/tmp/cmd
Copy after login

However, I recommend not to execute any program, then you can point to our web directory:

safe_mode_exec_dir = D:/usr/www
Copy after login

(4) Include files in safe mode

If you want to include some public files in safe mode, then modify the options:

safe_mode_include_dir = D:/usr/www/include/
Copy after login

In fact, generally the files included in PHP scripts have been written by the program itself. This can be done according to the specific requirements. Requires setup.

(5) Control the directories that PHP scripts can access

Use the open_basedir option to control that PHP scripts can only access specified directories, which can prevent PHP scripts from accessing

Files that should not be accessed limit the harm of phpshell to a certain extent. We can generally set it to only access the website directory:

open_basedir = D:/usr/www
Copy after login

(6) Close dangerous functions

If safe mode is turned on, function prohibition is not necessary, but we still consider it for safety. For example,

we feel that we do not want to execute PHP functions including system() that can execute commands, or

phpinfo() and other functions that can view PHP information, then we can Disable them:

disable_functions = system,passthru,exec,shell_exec,popen,phpinfo
Copy after login

If you want to prohibit any file and directory operations, you can turn off many file operations

disable_functions = chdir,chroot,dir,getcwd,opendir,readdir,scandir,fopen ,unlink,delete,copy,mkdir, rmdir,rename,file,file_get_contents,fputs,fwrite,chgrp,chmod,chown

The above only lists some of the commonly used file processing functions, you can also use the above Combining the execution command function with this function,

can resist most phpshells.

(7) Close the leakage of PHP version information in the http header

In order to prevent hackers from obtaining the PHP version information in the server, we can close the leakage of the information in the http header In the header:

expose_php = Off
Copy after login

For example, when a hacker telnet www.chinaz.com 80, he will not be able to see the PHP information.

(8) Close registration of global variables

Variables submitted in PHP, including variables submitted using POST or GET, will be automatically registered as global variables and can Direct access,

This is very unsafe for the server, so we cannot let it be registered as a global variable, so we turn off the registration global variable option:

register_globals = Off
Copy after login

Of course, if it is set like this, Then when obtaining the corresponding variables, you must use a reasonable method, such as obtaining the variable var submitted by GET,

then you must use $_GET['var'] to obtain it, PHP programmers should pay attention to this.

(9) Open magic_quotes_gpc to prevent SQL injection

SQL injection is a very dangerous problem. In small cases, the backend of the website may be invaded, or in more serious cases, the entire server may fall.

So be careful. There is a setting in php.ini:

magic_quotes_gpc = Off
Copy after login

This is turned off by default. If it is turned on, it will automatically convert user-submitted SQL queries,

for example, convert ' to \' etc. This plays a significant role in preventing sql injection. Therefore, we recommend setting it to:

magic_quotes_gpc = On
Copy after login

(10) Error message control

Generally, PHP will prompt an error when it is not connected to the database or under other circumstances. General error message It will contain the current path information of the php script or the SQL statement of the query. This kind of information is not safe after being provided to hackers, so it is generally recommended that the server disable error prompts:

display_errors = Off
Copy after login

If you want to display error messages, be sure to set the level of error display, such as only displaying information above warnings:

error_reporting = E_WARNING & E_ERROR
Copy after login

Of course, I still recommend turning off error prompts.

(11) Error log

It is recommended to record the error information after turning off display_errors to facilitate finding the reason for the server operation:

log_errors = On

Also set the directory where the error log is stored. It is recommended that the root apache log be stored together:

error_log = D:/usr/local/apache2/logs/php_error.log

Note: The apache user and group must have write permissions for the file.

MYSQL privilege reduction operation

Create a new user such as mysqlstart

net user mysqlstart ****microsoft /add

net localgroup users mysqlstart / del

Does not belong to any group

If MYSQL is installed in d:\mysql, then give mysqlstart full control permission

Then set the MYSQL service in the system service Properties, in the login properties, select this user mysqlstart and enter the password, OK.

Restart the MYSQL service, and then MYSQL will run with low permissions.

If apache is built on a windos platform, we need to pay attention to one thing. Apache runs with system permissions by default.

This is scary and makes people feel very uncomfortable. Then we will give apache Reduce the permissions.

net user apache ****microsoft /add

net localgroup users apache /del

ok. We created a user apche that does not belong to any group.

We open the computer manager, select services, click on the properties of the apache service, we select log on, select this account, we fill in the account and password created above,

Restart the apache service , ok, apache is running with low permissions.

In fact, we can also set the permissions of each folder so that the apache user can only perform what we want it to do, and create a separate read-write user for each directory.

This is also a popular configuration method used by many virtual host providers. However, this method is overkill when used to prevent this.

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

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.

See all articles