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!

0x01前言概述小编又在MySQL中发现了一个Double型数据溢出。当我们拿到MySQL里的函数时,小编比较感兴趣的是其中的数学函数,它们也应该包含一些数据类型来保存数值。所以小编就跑去测试看哪些函数会出现溢出错误。然后小编发现,当传递一个大于709的值时,函数exp()就会引起一个溢出错误。mysql>selectexp(709);+-----------------------+|exp(709)|+-----------------------+|8.218407461554972

Nginx是一个快速、高性能、可扩展的Web服务器,它的安全性是Web应用程序开发中不可忽略的问题。尤其是SQL注入攻击,它可以对Web应用程序造成巨大的破坏。在本篇文章中,我们将讨论如何使用Nginx来防范SQL注入攻击,以保护Web应用程序的安全。什么是SQL注入攻击?SQL注入攻击是一种利用Web应用程序漏洞的攻击方式。攻击者会在Web应用程序中注入恶

PHP编程技巧:如何防止SQL注入攻击在进行数据库操作时,安全是至关重要的。SQL注入攻击是一种常见的网络攻击,它利用了应用程序对用户输入的不正确处理,从而导致恶意的SQL代码被插入并执行。为了保护应用程序免受SQL注入攻击的影响,我们需要采取一些防范措施。使用参数化查询参数化查询是最基本也是最有效的防范SQL注入攻击的方法。它通过将用户输入的值与SQL查询

PHPSQL注入漏洞的检测和修复概述:SQL注入是指攻击者利用Web应用程序对输入进行恶意注入SQL代码的一种攻击方式。PHP作为一种广泛应用于Web开发的脚本语言,被广泛用于开发动态网站和应用程序。然而,由于PHP的灵活性和易用性,开发者常常忽略了安全性,导致了SQL注入漏洞的存在。本文将介绍如何检测和修复PHP中的SQL注入漏洞,并提供相关代码示例。检

Laravel开发注意事项:防止SQL注入的方法与技巧随着互联网的发展和计算机技术的不断进步,Web应用程序的开发也变得越来越普遍。在开发过程中,安全性一直是开发者不可忽视的重要问题。其中,防止SQL注入攻击是开发过程中需要特别关注的安全问题之一。本文将介绍几种Laravel开发中常用的方法和技巧,帮助开发者有效地防止SQL注入。使用参数绑定参数绑定是Lar

在网络安全领域里,SQL注入攻击是一种常见的攻击方式。它利用恶意用户提交的恶意代码来改变应用程序的行为以执行不安全的操作。常见的SQL注入攻击包括查询操作、插入操作和删除操作。其中,查询操作是最常被攻击的一种,而防止SQL注入攻击的一个常用的方法是使用PHP。PHP是一种常用的服务器端脚本语言,它在web应用程序中的使用非常广泛。PHP可以与MySQL等关系

PHP表单过滤:SQL注入防范与过滤引言:随着互联网的快速发展,Web应用程序的开发变得越来越普遍。在Web开发中,表单是最常见的用户交互方式之一。然而,表单提交数据的处理过程中存在着安全风险。其中,最常见的风险之一就是SQL注入攻击。SQL注入攻击是一种利用Web应用程序对用户输入数据进行处理不当而导致攻击者能够执行非授权数据库查询的攻击方式。攻击者通过在

SQL注入是一种常见的网络攻击方式,它利用应用程序对输入数据的不完善处理,成功将恶意的SQL语句注入到数据库中。这种攻击方式特别常见于使用PHP语言开发的应用程序中,因为PHP对用户输入的处理通常相对较弱。本文将介绍一些应对SQL注入漏洞的策略,并提供PHP代码示例。使用预处理语句预处理语句是一种建议的防御SQL注入的方法。它使用绑定参数的方式,将输入数据与


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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools

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

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),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.