


Complete explanation of PHP vulnerabilities (5)-SQL injection attack
SQL injection attack (SQL Injection) is when the attacker submits a carefully constructed SQL statement in a form and changes the original SQL statement. If the web program does not check the submitted data, it will cause a SQL injection attack.
General steps of SQL injection attack:
1. The attacker visits a site with SQL injection vulnerability and looks for the injection point
2. The attacker constructs an injection statement, and combines the injection statement with the SQL statement in the program to generate a new SQL statement
3. The new sql statement is submitted to the database for processing
4. The database executes the new SQL statement, causing a SQL injection attack
Instance
Database
CREATE TABLE `postmessage` ( `id` int(11) NOT NULL auto_increment, `subject` varchar(60) NOT NULL default ”, `name` varchar(40) NOT NULL default ”, `email` varchar(25) NOT NULL default ”, `question` mediumtext NOT NULL, `postdate` datetime NOT NULL default ’0000-00-00 00:00:00′, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=gb2312 COMMENT=’运用者的留言’ AUTO_INCREMENT=69 ; grant all privileges on ch3.* to ‘sectop’@localhost identified by ’123456′; //add.php 插入留言 //list.php 留言列表 //show.php 显示留言
Page http://www. netsos.com.cn/show.php?id=71 There may be an injection point, let’s test it
http://www.netsos.com.cn/show.php?id=71 and 1=1
The record was queried once and not once. Let’s take a look at the source code
//show.php lines 12-15
//Execute the mysql query statement
$query = "select * from postmessage where id = " .$_GET["id"];
$result = mysql_query($query)
or die("Failed to execute ySQL query statement:" . mysql_error());
After the parameter id is passed in, and the preceding characters Put the combined sql statement into the database to execute the query
Submit and 1=1, the statement becomes select * from postmessage where id = 71 and 1=1. The values before and after this statement are all true, and after and is also true, return The queried data
is submitted and 1=2, and the statement becomes select * from postmessage where id = 71 and 1=2. The first value of this statement is true, the last value is false, and the next value is false, and no data can be queried
Normal SQL queries, after passing through the statements we constructed, form SQL injection attacks. Through this injection point, we can further obtain permissions, for example, use union to read the management password, read database information, or use mysql's load_file, into outfile and other functions to further penetrate.
Prevention method
Integer parameters:
Use the intval function to convert data into integers
Function prototype
int intval (mixed var, int base)
var is the variable to be converted into an integer
base, you can Select, it is the basic number, the default is 10
Floating point parameters:
Use floatval or doubleval function to convert single precision and double precision floating point parameters respectively
Function prototype
int floatval (mixed var)
var is Variable to be converted
int doubleval (mixed var)
var is the variable to be converted
Character parameters:
Use the addslashes function to convert single quotes "'" to "'" and double quotes """ into """, backslash "" is converted into "\", NULL character plus backslash ""
Function prototype
string addslashes (string str)
str is the string to be checked
So just now We can fix the code loopholes like this
// Execute mysql query statement
$query = "select * from postmessage where id = ".intval($_GET["id"]);
$result = mysql_query( $query)
or die("Failed to execute ySQL query statement: " . mysql_error());
If it is a character type, first determine whether magic_quotes_gpc can be On, and use addslashes to escape when it is not On. Special characters
if(get_magic_quotes_gpc())
{
$var = $_GET["var"];
}
else
{
$var = addslashes($_GET["var"]) ;
}
Tested again, the vulnerability has been fixed
The above is the content of PHP vulnerability solution (5) - SQL injection attack. For more related content, please pay attention to the PHP Chinese website (www.php.cn )!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











There are four main error types in PHP: 1.Notice: the slightest, will not interrupt the program, such as accessing undefined variables; 2. Warning: serious than Notice, will not terminate the program, such as containing no files; 3. FatalError: the most serious, will terminate the program, such as calling no function; 4. ParseError: syntax error, will prevent the program from being executed, such as forgetting to add the end tag.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

In PHPOOP, self:: refers to the current class, parent:: refers to the parent class, static:: is used for late static binding. 1.self:: is used for static method and constant calls, but does not support late static binding. 2.parent:: is used for subclasses to call parent class methods, and private methods cannot be accessed. 3.static:: supports late static binding, suitable for inheritance and polymorphism, but may affect the readability of the code.
