Home > Article > Backend Development > How to prevent sql injection in php_PHP tutorial
Because SQL injection requires operating the database, it generally looks for SQL statement keywords: insert, delete, update, select, and checks whether the passed variable parameters are user-controllable and whether security processing has been done
How SQL injection works
Constructing a database query is a very straightforward process. Typically, it will be implemented along the following lines. Just for illustrative purposes, we'll assume you have one
There is a wine database table "wines", one of which is "variety" (i.e. wine type):
1. Provide a form - allow users to submit something to search for. Let's assume that the user chooses to search for wine of type "lagrein".
2. Retrieve the user's search term and save it - by assigning it to a variable like this:
The code is as follows | Copy code | ||||
|
lagrein
3. Then, use the variable to construct a database query in the WHERE clause:
代码如下 | 复制代码 |
$query = “SELECT * FROM wines WHERE variety=’$variety’”; |
The code is as follows | Copy code |
$query = “SELECT * FROM wines WHERE variety=’$variety’”; |
代码如下 | 复制代码 |
SELECT * FROM wines WHERE variety=’lagrein’ |
The code is as follows | Copy code |
SELECT * FROM wines WHERE variety=’lagrein’
|
5. MySQL returns all records in the wines table - among them, the value of field variety is "lagrein".
By now, this should be a familiar and easy process. Unfortunately, sometimes the processes we are familiar with and comfortable with are easy
Leading us to complacency. Now, let's reanalyze the query we just constructed.
代码如下 | 复制代码 |
$query = ” SELECT * FROM wines WHERE variety = ‘”; |
1. The fixed part of the query you create ends with a single quote, which you will use to describe the beginning of the variable value:
The code is as follows | Copy code | ||||
|
代码如下 | 复制代码 |
$ query .= “‘”; |
The code is as follows | Copy code |
$query .= $variety; |
The code is as follows | Copy code |
$ query .= “‘”; |
So, the value of $query is as follows:
The code is as follows | Copy code | ||||
|
So the query is constructed without any issues and the result is what you would expect - a list of wines with the wine type "lagrein". Now
Now, let’s imagine that instead of entering a simple wine type of type “lagrein”, your user enters the following (note the package
(including two punctuation marks):
代码如下 | 复制代码 |
lagrein’ or 1=1; |
The code is as follows | Copy code |
lagrein’ or 1=1; |
代码如下 | 复制代码 |
SELECT * FROM wines WHERE variety = ‘ |
The code is as follows | Copy code | ||||
SELECT * FROM wines WHERE variety = ‘
|
You then connect to it using the value of the variable containing the user input (shown here in bold):
The code is as follows | Copy code | ||||
|
The code is as follows | Copy code |
SELECT * FROM wines WHERE variety = ‘lagrein’ or 1=1;’ |
We can prevent the above problems by writing a function.
The code is as follows | Copy code | ||||
+------------------------------------------------- ---------- * Anti-horse, anti-cross-site attack, anti-sql injection function+------------------------------------------------- ---------- *The parameter passed in by $date must be a variable or array; the magic reference of the $ignore_magic_quotes variable +------------------------------------------------- ---------- */
function in($data,$ignore_magic_quotes=false) { $data=trim(htmlspecialchars($data));//Prevent being hacked and cross-site attacksif(($ignore_magic_quotes==true)||(!get_magic_quotes_gpc())) {$data = addslashes($data);//Prevent sql injection }return $data; }else if(is_array($data))//If it is an array, use recursive filtering {foreach($data as $key=>$value) {$data[$key]=in($value); }Return $data; }else {Return $data; }} |
When we accept the data above, we can prevent malware, cross-site attacks, and SQL injection waiting
The following will introduce security configuration on the server side代码如下 | 复制代码 |
safe_mode_exec_dir = D:/usr/bin |
(1) Turn on the safe mode of php
代码如下 | 复制代码 |
safe_mode_exec_dir = D:/tmp/cmd |
At the same time, the permissions of many file operation functions are controlled, and certain key files, such as /etc/passwd, are not allowed.
代码如下 | 复制代码 |
safe_mode_exec_dir = D:/usr/www |
The code is as follows | Copy code |
safe_mode_exec_dir = D:/usr/bin |
The code is as follows | Copy code |
safe_mode_exec_dir = D:/tmp/cmd |
The code is as follows | Copy code |
safe_mode_exec_dir = D:/usr/www |
(4) Include files in safe mode
If you want to include certain public files in safe mode, then change the options:
The code is as follows | Copy code | ||||
|
(5) Control the directories that php scripts can access
Use the open_basedir option to control the PHP script to only access the specified directory, which can prevent the PHP script 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 |
The code is as follows | Copy code |
open_basedir = D:/usr/www |
(6) Turn off dangerous functions
If safe mode is turned on, function prohibition is not necessary, but we still consider it for safety. For example,
We don’t think we want to execute PHP functions including system() that can execute commands, or that can view PHP information
代码如下 | 复制代码 |
disable_functions = system,passthru,exec,shell_exec,popen,phpinfo |
The code is as follows | Copy code | ||||
disable_functions = system,passthru,exec,shell_exec,popen,phpinfo
|
If you want to prohibit any file and directory operations, you can close many file operations
The code is as follows | Copy code |
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 combine the above execution command function with this function,
代码如下 | 复制代码 |
expose_php = Off |
(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 turn off the information in the http header:
The code is as follows | Copy code |
expose_php = Off
|
代码如下 | 复制代码 |
register_globals = Off |
The code is as follows | Copy code |
register_globals = Off |
Of course, if this is set, then reasonable methods must be used to obtain the corresponding variables, such as obtaining the variable var submitted by GET,
Then you need to use $_GET['var'] to get it. PHP programmers should pay attention to this.
(9) Turn on magic_quotes_gpc to prevent SQL injection
SQL injection is a very dangerous problem. In the smallest case, the website backend may be invaded, or in the worst case, the entire server may collapse.
So be careful. There is a setting in php.ini:
The code is as follows | Copy code | ||||
|
For example, convert ' to ', etc., which plays a significant role in preventing sql injection. So we recommend setting it to:
代码如下 | 复制代码 |
magic_quotes_gpc = On |
The code is as follows | Copy code |
magic_quotes_gpc = On |
(10) Error message control
Generally, PHP will prompt an error when it is not connected to the database or under other circumstances. Generally, the error message will contain a PHP script when
The path information before代码如下 | 复制代码 |
display_errors = Off |
The code is as follows | Copy code | ||||
display_errors = Off
|
If you want to display an error message, be sure to set the level of display error, for example, only display information above warning:
The code is as follows | Copy code |
error_reporting = E_WARNING & E_ERROR |
Of course, I still recommend turning off error prompts.
代码如下 | 复制代码 |
log_errors = On |
It is recommended to record the error information after turning off display_errors, so as to find the reason why the server is running:
代码如下 | 复制代码 |
error_log = D:/usr/local/apache2/logs/php_error.log |
The code is as follows | Copy code |
log_errors = On |
The code is as follows | Copy code |
error_log = D:/usr/local/apache2/logs/php_error.log |
Note: The file must be given to allow the apache user and group to have write permissions.
MYSQL’s privilege-reduced operation
Create a new user such as mysqlstart
The code is as follows
|
Copy code
|
||||||||
net user mysqlstart fuckmicrosoft /add net localgroup users mysqlstart /del
Does not belong to any group If MYSQL is installed in d:mysql, then give mysqlstart full control permissions
Then set the MYSQL service properties in the system service. In the login properties, select this user mysqlstart and enter the password and confirm. Restart the MYSQL service, and then MYSQL will run with low privileges. 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, this makes people feel very uncomfortable. Then let’s lower the permissions of apache.
|