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

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),