Common SQL injection methods

L
Release: 2020-05-30 11:28:08
forward
2955 people have browsed it

Common SQL injection methods

Common SQL injection methods

WEB Security SQL Injection

Introduction:

When developing a website, for security reasons, it is necessary to filter the characters passed from the page. Usually, users can call the content of the database through the following interfaces: URL address bar, login interface, message board, search box, etc. This often leaves opportunities for hackers to take advantage of. At worst, the data may be leaked, and at worst, the server may be taken down.

1. SQL injection steps

a) Find the injection point and construct a special statement

The controllable parameters of the incoming SQL statement are divided into two categories
1. For numeric types, parameters do not need to be enclosed in quotation marks, such as ?id=1
2. For other types, parameters must be enclosed in quotation marks, such as?name="phone"

b) The user constructs a SQL statement (such as: 'or 1=1#;admin'# (this injection is also called PHP's universal password, which can bypass entering the password when the user name is known). I will explain it later)

c) Send the SQL statement to the DBMS database

d) DBMS receives the returned result, interprets the request into machine code instructions, and performs the necessary operations

e) DBMS Accept the returned result, process it, and return it to the user

Because the user constructs a special SQL statement, special results must be returned (as long as your SQL statement is flexible enough)

Below, I pass a Examples to demonstrate SQL injection in detail
2. Detailed explanation of SQL injection examples (the above tests assume that magic_quote_gpc is not turned on on the server)

1) Preliminary preparations
Let’s demonstrate first SQL injection vulnerability, log in to the backend administrator interface
First, create a data table for testing:

CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(64) NOT NULL, `password` varchar(64) NOT NULL, `email` varchar(64) NOT NULL,PRIMARY KEY (`id`),UNIQUE KEY `username` (`username`) ) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
Copy after login

Add a record for testing:

INSERT INTO users (username,password,email)VALUES('MarcoFly',md5('test'),'marcofly@test.com');
Copy after login

Next, paste the login The source code of the interface

  Sql注入演示  
Sql注入演示
用户名:
密 码:
Copy after login

Attached is the rendering:

Common SQL injection methods

When the user clicks the submit button, the form data will be submitted to the validate.php page , the validate.php page is used to determine whether the user name and password entered by the user meet the requirements (this step is very important, and is often the location of SQL vulnerabilities)

!    登录验证  
请重新登录!"; } ?>
Copy after login

Have you noticed it? We will submit the user directly. The data (user name and password) are directly executed, and special character filtering is not implemented. You will understand later that this is fatal.
Code analysis: If the username and password match successfully, it will jump to the administrator operation interface (manager.php). If it fails, a friendly prompt message will be given.
Successful login interface:

Common SQL injection methods

Login failure prompt:

Common SQL injection methods

At this point, the preliminary work has been done Okay, next we will start our highlight: SQL injection

2) Construct SQL statement
After filling in the correct user name (marcofly) and password (test), click Submit, and it will be returned to Our "Welcome Administrator" interface.
Because the username and password we submitted are synthesized into the SQL query statement like this:

select * from users where username='marcofly' and password=md5('test')
Copy after login


Obviously, the username and password are the same as what we gave before , you will definitely be able to log in successfully. But what if we enter a wrong username or password? Obviously, I definitely can’t log in. Well, this is the case under normal circumstances, but for websites with SQL injection vulnerabilities, as long as a special "string" is constructed, you can still log in successfully.

For example: enter: ' or 1=1# in the user name input box, enter the password casually, the synthesized SQL query statement at this time is:

select * from users where username='' or 1=1#' and password=md5('')
Copy after login
Copy after login


Semantic analysis: "#" is a comment character in mysql, so the content after the pound sign will be regarded as comment content by mysql, so it will not be executed. In other words, the following two sql statements, etc. Valence:

select * from users where username='' or 1=1#' and password=md5('')
Copy after login
Copy after login

is equivalent to

select* from users where usrername='' or 1=1
Copy after login

because 1=1 is always true, that is, the where clause is always true. After further simplifying the sql, etc. The value is the following select statement:

select * from users
Copy after login

Yes, the function of this sql statement is to retrieve all fields in the users table

The above is an input method, here is another one Injection method, this method is also called PHP's universal password

If we know the user name, we can log in without a password. Suppose the user name is: admin

Construct the statement:

select * from users where username='admin'#' and password=md5('')
Copy after login

is equivalent to

select * from users where username='admin'
Copy after login

so that you can log in without entering a password.

The database will mistakenly think that you can log in without a user name, bypassing the background verification and achieving the purpose of injection.

also exploits vulnerabilities in SQL syntax.

See, a constructed SQL statement can have such terrible destructive power. I believe that after seeing this, you will begin to have a rational understanding of SQL injection~
Yes, SQL injection It's that easy. However, it is not so easy to construct flexible SQL statements according to the actual situation. After you have the basics, you can slowly explore on your own.
Have you ever thought about what if the data submitted through the background login window are filtered out by the administrator with special characters? In this case, our universal username' or 1=1# cannot be used. But this does not mean that we have no countermeasures. We must know that there is more than one way for users to interact with the database.

Recommended: "mysql tutorial"

The above is the detailed content of Common SQL injection methods. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!