Take you to understand SQL injection (details)
This article brings you knowledge about sql injection. SQL injection is a behavior in which the server does not strictly verify the data sent by the client, causing the server-side SQL statement to be maliciously modified and successfully executed. I hope it will be useful to everyone. helpful.
What is SQL?
Structured Query Language (SQL) is a special programming language used for standard data queries in databases. In October 1986, the American National Standards Institute standardized SQL and used it as the standard language for relational database systems. In 1987, it received support from the International Standards Organization and became an international standard.
What is SQL injection
SQL injection is a behavior in which the server does not strictly verify the data sent by the client, resulting in the server's SQL statement being maliciously modified and successfully executed
Principle of Vulnerability
SQL injection attack behavior can be described as injecting SQL syntax into user-controllable parameters, destroying the original SQL structure, and achieving unexpected results when writing programs. The resulting attack behavior. The cause can be attributed to the superposition of the following two reasons.
- When programmers interact with the program and the database, they use string concatenation to construct SQL statements.
- There is insufficient filtering of user-controllable parameters. Then the parameter content is spliced into the SQL statement
Cause of the vulnerability
- The user can control the input
- The input check is insufficient, causing the SQL statement to be The illegal data submitted by the user is executed as part of the statement
Why is there SQL injection
- The code does not strictly filter the parameters brought into the SQL statement
- The security configuration of the framework is not enabled, for example: PHP's magic_quotes_gpc
- The framework security query method is not used
- The test interface is not deleted
- The firewall is not enabled
- No other security protection equipment is used
Possible location of the injection point
According to the principle of SQL injection vulnerability, the user injects SQL into the "controllable parameters" In other words, where the Web application obtains user input, as long as it is brought into the database query, there is the possibility of SQL injection. These places usually include:
- GET data
- POST data
- Cookie data
- HTTP header (other fields in the HTTP header)
Vulnerability hazard
- Database information leakage, acquisition, modification of sensitive data: leakage of users’ private information (account, password) stored in the database
- Bypass login verification: use a universal password to log in to the website backend, etc.
- File system operations: list directories, read, write files, etc.
- Web page tampering: tamper with specific web pages by operating the database, embed network horse links, and carry out horse-mounting attacks
- Registry operations: read, write, delete registry, etc.
- Execute system commands: execute commands remotely
- The server is remotely controlled and Trojans are planted: hackers can modify or control the operating system
Submission methods
Submission methods include: get, post, cookie, request, etc.
Among them: request support is better, you can use get method, post method, cookie method Submission is possible
Determine the injection point
Will try to submit data at the suspected injection point or behind the parameters to determine whether there is a SQL injection vulnerability .
Test data | Test judgment | Attack ideas | |||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
-1 or 1 | Whether the previous or next page can be echoed (to determine whether there is an echo) | Joint injection | |||||||||||||||||||||||||
' or"
|
Whether the database error message is displayed; whether the echoed page is different (character type or numeric type) | Error injection | |||||||||||||||||||||||||
and 1=1 or and 1=2 | Whether the echoed pages are different (determine whether the page has a Boolean type status) | Boolean blind injection | |||||||||||||||||||||||||
and sleep(5) | Judge the return time of the page | Delay injection | |||||||||||||||||||||||||
\ | Judgment Escape | ||||||||||||||||||||||||||
Meaning | |||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Restrict mysqld to not allow import and export operations | |||||||||||||||||||||||||||
will limit the import and export operations of mysqld to a fixed directory, and the subdirectory is valid | |||||||||||||||||||||||||||
No restrictions on the import and export operations of mysqld |
##MySQL | SQLServerOracle | PostgreSQL | Access | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
#-- |
-- |
-- |
none |
Multi-line comments | |||||||||||||||||||||||
/**/ |
/**/ |
/**&*/ |
None |
Database port | 33061433 | ||||||||||||||||||||||
5432 | is a file database, so No port number required |
减减空格 | "-- " | "–%20" | “–+” |
---|---|---|---|
# | “#” | "%23" | |
内联注释 | /* 被注释掉的内容 */ | ||
点
数据库中,符号.
代表下一级,如dvwa.user表示dvwa数据库下的user表
常用语句和函数
推荐阅读:SQL注入必备知识初级
1:mysql -uroot -proot登录数据库
2:show databases; 查看有哪些数据库
3:use informatin_schema; 使用某数据库
4:limit的用法
- limit的使用格式是limit m,n
- 其中m是指记录开始的位置,从0开始表示第一条记录
- n是指提取n条记录
5:select 函数名; 查询某内容
函数名有以下:
防御措施
防御SQL注入的核心思想是对用户输入的数据进行严格的检查,并且对数据库的使用采用最小权限分配原则。目前SQL注入的防御手段有以下几种:
代码层
- 内置过滤系统(本质是黑名单,很常见但是不推荐)
- 采用参数化查询&预编译(推荐)
强迫使用参数化语句。参数化的语句使用参数而不是将用户输入变量嵌入到SQL语句中。采用这种措施,可以杜绝大部分的SQL注入式攻击
- 采用框架的安全写法
例如Mybatis中使用#
可以防止SQL注入,$
并不能防止SQL注入
thinkphp使用数组方式将自动使用框架自带的字段类型检测防止注入、PDO驱动参数绑定、预处理等
Thinkphp框架的安全写法 安全的替换写法 $data=M('Member')->where(array('id'=>$_GET['id']))->find();//使用数组方式将自动使用框架自带的字段类型检测防止注入 $data=M('Member')->where(array('id'=>(int)$_GET['id']))->find();//类型约束 $data=M('Member')->where('id='.intval($_GET['id']))->find();//类型转换 $data=M('Member')->where(array('id'=>I('get.id','','intval')))->find();//$data=M('Member')- >where(array('id'=>':id'))->bind(':id',I('get.id'))->select();//PDO驱动可以使用参数绑定 $data=M('Member')->where("id=%d",array($_GET['id']))->find();//预处理机制 //不安全的写法举例 $_GET['id']=8;//希望得到的是正整数 $data=M()->query('SELECT * FROM `member` WHERE id='.$_GET['id']);//执行的SQL语句 $_GET['id']='8 UNION SELECT * FROM `member`';;//隐患:构造畸形语句进行注入;
数据库加固
主要包括:
- 最小权限原则,禁止将任何高权限帐户(例如sa、dba、root等)用于应用程序数据库访问。更安全的方法是单独为应用创建有限访问帐户。
- 禁用敏感函数拒绝用户访问敏感的系统存储过程,如xp_dirtree、xp_cmdshell、into_outfile 等
- 网站与数据层的编码统一,建议全部使用UTF-8编码,避免因上下层编码不一致导致一些过滤模型被绕过,比如宽字节注入等。
- 限制用户所能够访问的数据库表
其他
例如,避免网站显示SQL执行出错信息,防止攻击者使用基于错误的方式进行注入;每个数据层编码统一,防止过滤模型被绕过等。使用WAF。
相关推荐:《mysql教程》
The above is the detailed content of Take you to understand SQL injection (details). For more information, please follow other related articles on the PHP Chinese website!

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

Nginx is a fast, high-performance, scalable web server, and its security is an issue that cannot be ignored in web application development. Especially SQL injection attacks, which can cause huge damage to web applications. In this article, we will discuss how to use Nginx to prevent SQL injection attacks to protect the security of web applications. What is a SQL injection attack? SQL injection attack is an attack method that exploits vulnerabilities in web applications. Attackers can inject malicious code into web applications

Overview of detection and repair of PHP SQL injection vulnerabilities: SQL injection refers to an attack method in which attackers use web applications to maliciously inject SQL code into the input. PHP, as a scripting language widely used in web development, is widely used to develop dynamic websites and applications. However, due to the flexibility and ease of use of PHP, developers often ignore security, resulting in the existence of SQL injection vulnerabilities. This article will introduce how to detect and fix SQL injection vulnerabilities in PHP and provide relevant code examples. check

Laravel Development Notes: Methods and Techniques to Prevent SQL Injection With the development of the Internet and the continuous advancement of computer technology, the development of web applications has become more and more common. During the development process, security has always been an important issue that developers cannot ignore. Among them, preventing SQL injection attacks is one of the security issues that requires special attention during the development process. This article will introduce several methods and techniques commonly used in Laravel development to help developers effectively prevent SQL injection. Using parameter binding Parameter binding is Lar

0x01 Preface Overview The editor discovered another Double data overflow in MySQL. When we get the functions in MySQL, the editor is more interested in the mathematical functions. They should also contain some data types to save values. So the editor ran to test to see which functions would cause overflow errors. Then the editor discovered that when a value greater than 709 is passed, the function exp() will cause an overflow error. mysql>selectexp(709);+-----------------------+|exp(709)|+---------- ------------+|8.218407461554972

PHP Programming Tips: How to Prevent SQL Injection Attacks Security is crucial when performing database operations. SQL injection attacks are a common network attack that exploit an application's improper handling of user input, resulting in malicious SQL code being inserted and executed. To protect our application from SQL injection attacks, we need to take some precautions. Use parameterized queries Parameterized queries are the most basic and most effective way to prevent SQL injection attacks. It works by comparing user-entered values with a SQL query

In the field of network security, SQL injection attacks are a common attack method. It exploits malicious code submitted by malicious users to alter the behavior of an application to perform unsafe operations. Common SQL injection attacks include query operations, insert operations, and delete operations. Among them, query operations are the most commonly attacked, and a common method to prevent SQL injection attacks is to use PHP. PHP is a commonly used server-side scripting language that is widely used in web applications. PHP can be related to MySQL etc.

PHP form filtering: SQL injection prevention and filtering Introduction: With the rapid development of the Internet, the development of Web applications has become more and more common. In web development, forms are one of the most common ways of user interaction. However, there are security risks in the processing of form submission data. Among them, one of the most common risks is SQL injection attacks. A SQL injection attack is an attack method that uses a web application to improperly process user input data, allowing the attacker to perform unauthorized database queries. The attacker passes the

Improving system security: MyBatis tips for preventing SQL injection attacks With the continuous development of information technology, database applications have become an indispensable part of modern software systems. However, what follows is database security issues, the most common and serious of which is probably SQL injection attacks. SQL injection attacks refer to attackers inserting malicious SQL code into input fields to illegally obtain information in the database or destroy the integrity of the database. To protect against SQL
