在 sqlmap 中指定注入技术

Kali LinuxBeginner
立即练习

引言

SQL 注入是一种 Web 安全漏洞,它允许攻击者干扰应用程序对其数据库发出的查询。通常情况下,它允许攻击者查看他们通常无法检索的数据。这可能包括其他用户的数据,或者应用程序本身能够访问的任何其他数据。在许多情况下,攻击者可以修改或删除这些数据,从而对应用程序的内容或行为进行持久性更改。

sqlmap 是一个开源的渗透测试工具,它自动化了检测和利用 SQL 注入漏洞以及接管数据库服务器的过程。它配备了强大的检测引擎、为终极渗透测试人员提供的许多独特功能,以及从数据库指纹识别到从数据库获取数据、访问底层文件系统以及通过带外连接在操作系统上执行命令的广泛开关。

在本实验中,你将学习如何使用 --technique 选项在 sqlmap 中指定不同的 SQL 注入技术。这允许你控制 sqlmap 尝试用于注入的方法,这对于针对特定漏洞或优化扫描时间非常有用。

理解六种 SQLi 技术代码 (B, E, U, S, T, Q)

在本步骤中,你将了解 sqlmap 使用的不同 SQL 注入技术代码。sqlmap 支持多种 SQL 注入技术,你可以使用 --technique 选项指定要使用的技术。每种技术都用一个单独的字符表示。

六种主要的 SQL 注入技术代码是:

  • B: 基于布尔(Boolean-based)的盲注。此技术依赖于发送返回 TRUE 或 FALSE 结果的 SQL 查询,然后观察应用程序的响应(例如,页面内容变化)来推断信息。
  • E: 基于错误(Error-based)的 SQL 注入。此技术强制数据库生成包含数据库结构或数据信息的错误消息。
  • U: 基于 UNION 查询(UNION query-based)的 SQL 注入。此技术使用 UNION SQL 操作符将两个或多个 SELECT 语句的结果合并到一个结果集中,允许攻击者从其他表中检索数据。
  • S: 堆叠查询(Stacked queries)SQL 注入。此技术允许攻击者在单个查询中执行多个 SQL 语句,通常用于在数据库服务器上执行任意命令。
  • T: 基于时间(Time-based blind)的盲注。此技术依赖于根据 TRUE 或 FALSE 条件使数据库暂停指定的时间,允许攻击者通过观察响应时间来推断信息。
  • Q: 内联查询(Inline queries)SQL 注入。此技术涉及将子查询直接注入到原始查询中。

理解这些技术对于有效使用 sqlmap 和理解 SQL 注入的基本原理至关重要。

使用 --technique=B 强制进行基于布尔的盲注测试

在本步骤中,你将学习如何强制 sqlmap 只使用基于布尔的盲注 SQL 注入技术。当你怀疑目标容易受到这种特定类型的注入攻击时,或者当你希望通过专注于单一技术来减少扫描时间时,这会很有用。

--technique=B 选项告诉 sqlmap 仅使用基于布尔的盲注。

让我们模拟使用此选项运行 sqlmap。在实际场景中,你需要将 http://testphp.vulnweb.com/artists.php?id=1 替换为你的目标 URL。在本实验中,我们将仅演示命令。

打开你的终端并执行以下命令:

sqlmap -u "http://testphp.vulnweb.com/artists.php?id=1" --technique=B --batch --eta --skip-waf
  • -u "http://testphp.vulnweb.com/artists.php?id=1": 指定目标 URL。这是一个已知的易受攻击的测试站点。
  • --technique=B: 强制 sqlmap 仅使用基于布尔的盲注。
  • --batch: 以非交互模式运行 sqlmap,接受默认选项。
  • --eta: 显示每个输出的预计到达时间。
  • --skip-waf: 跳过 Web 应用程序防火墙 (WAF) 检测。

你将观察到 sqlmap 的输出,它将表明它主要在测试基于布尔的盲注漏洞。输出将显示 sqlmap 尝试不同的 payload 并分析响应。

        _
       ___ ___ ___ ___
      |_ -| . | . | . |
      |___|_  |_  |_  |
        |_|   |_|   |_|   3.7-dev (r12345)

[!] legal disclaimer: Usage of sqlmap for attacking targets without prior mutual consent is illegal. It is the end user's responsibility to obey all applicable local, state and federal laws. sqlmap developers assume no liability and are not responsible for any misuse or damage caused by this program.

[*] starting @ 12:00:00 /2023-01-01/

[12:00:00] [INFO] starting sqlmap 3.7-dev (r12345)
[12:00:00] [INFO] testing connection to the target URL
[12:00:01] [INFO] checking if the target is stable
[12:00:01] [INFO] target URL is stable
[12:00:01] [INFO] testing for SQL injection on URL 'http://testphp.vulnweb.com/artists.php?id=1'
[12:00:01] [INFO] testing 'Boolean-based blind - Parameter: id'
...

输出证实 sqlmap 正在专注于基于布尔的盲注技术。

使用 --technique=T 强制进行基于时间的盲注测试

在本步骤中,你将学习如何强制 sqlmap 只使用基于时间的盲注 SQL 注入技术。当其他方法(如基于错误或基于布尔的方法)未能产生结果时,通常会使用此技术,尤其是在应用程序的响应无论注入如何都保持一致的情况下。

--technique=T 选项告诉 sqlmap 仅使用基于时间的盲注。

打开你的终端并执行以下命令:

sqlmap -u "http://testphp.vulnweb.com/artists.php?id=1" --technique=T --batch --eta --skip-waf
  • -u "http://testphp.vulnweb.com/artists.php?id=1": 指定目标 URL。
  • --technique=T: 强制 sqlmap 仅使用基于时间的盲注。
  • --batch: 以非交互模式运行 sqlmap
  • --eta: 显示预计到达时间。
  • --skip-waf: 跳过 WAF 检测。

你将观察到 sqlmap 的输出,它将显示它正在注入导致服务器响应延迟的 payload。此方法可能比其他方法慢,因为它需要等待时间延迟。

        _
       ___ ___ ___ ___
      |_ -| . | . | . |
      |___|_  |_  |_  |
        |_|   |_|   |_|   3.7-dev (r12345)

[!] legal disclaimer: Usage of sqlmap for attacking targets without prior mutual consent is illegal. It is the end user's responsibility to obey all applicable local, state and federal laws. sqlmap developers assume no liability and are not responsible for any misuse or damage caused by this program.

[*] starting @ 12:00:00 /2023-01-01/

[12:00:00] [INFO] starting sqlmap 3.7-dev (r12345)
[12:00:00] [INFO] testing connection to the target URL
[12:00:01] [INFO] checking if the target is stable
[12:00:01] [INFO] target URL is stable
[12:00:01] [INFO] testing for SQL injection on URL 'http://testphp.vulnweb.com/artists.php?id=1'
[12:00:01] [INFO] testing 'Time-based blind - Parameter: id'
...

输出证实 sqlmap 现在正专注于基于时间的盲注技术。

使用 --technique=U 强制进行 UNION 查询注入测试

在本步骤中,你将学习如何强制 sqlmap 只使用 UNION 查询注入 SQL 注入技术。当应用程序直接在页面上显示 SQL 查询结果时,此技术非常有效,因为它允许攻击者从数据库的其他表中检索数据。

--technique=U 选项告诉 sqlmap 仅使用 UNION 查询注入。

打开你的终端并执行以下命令:

sqlmap -u "http://testphp.vulnweb.com/artists.php?id=1" --technique=U --batch --eta --skip-waf
  • -u "http://testphp.vulnweb.com/artists.php?id=1": 指定目标 URL。
  • --technique=U: 强制 sqlmap 仅使用 UNION 查询注入。
  • --batch: 以非交互模式运行 sqlmap
  • --eta: 显示预计到达时间。
  • --skip-waf: 跳过 WAF 检测。

你将观察到 sqlmap 的输出,它将显示它正在尝试注入 UNION SELECT 语句。如果成功,此技术可以快速揭示数据库结构和数据。

        _
       ___ ___ ___ ___
      |_ -| . | . | . |
      |___|_  |_  |_  |
        |_|   |_|   |_|   3.7-dev (r12345)

[!] legal disclaimer: Usage of sqlmap for attacking targets without prior mutual consent is illegal. It is the end user's responsibility to obey all applicable local, state and federal laws. sqlmap developers assume no liability and are not responsible for any misuse or damage caused by this program.

[*] starting @ 12:00:00 /2023-01-01/

[12:00:00] [INFO] starting sqlmap 3.7-dev (r12345)
[12:00:00] [INFO] testing connection to the target URL
[12:00:01] [INFO] checking if the target is stable
[12:00:01] [INFO] target URL is stable
[12:00:01] [INFO] testing for SQL injection on URL 'http://testphp.vulnweb.com/artists.php?id=1'
[12:00:01] [INFO] testing 'UNION query - Parameter: id'
...

输出证实 sqlmap 现在正专注于 UNION 查询注入技术。

使用 --technique=BEUST 执行多技术扫描

在本步骤中,你将学习如何在一次 sqlmap 扫描中组合多种 SQL 注入技术。这通常是最实用的方法,因为它允许 sqlmap 尝试各种方法来查找漏洞,从而提高成功率。

你可以通过在 --technique= 后面连接相应的代码来指定多种技术。例如,--technique=BEUST 将告诉 sqlmap 尝试基于布尔、基于错误、UNION 查询、堆叠查询和基于时间的盲注。

打开你的终端并执行以下命令:

sqlmap -u "http://testphp.vulnweb.com/artists.php?id=1" --technique=BEUST --batch --eta --skip-waf
  • -u "http://testphp.vulnweb.com/artists.php?id=1": 指定目标 URL。
  • --technique=BEUST: 强制 sqlmap 使用基于布尔、基于错误、UNION 查询、堆叠查询和基于时间的盲注。
  • --batch: 以非交互模式运行 sqlmap
  • --eta: 显示预计到达时间。
  • --skip-waf: 跳过 WAF 检测。

你将观察到 sqlmap 的输出,它将显示它正在系统地测试每种指定的技术。这种全面的方法通常是初始扫描的推荐方法。

        _
       ___ ___ ___ ___
      |_ -| . | . | . |
      |___|_  |_  |_  |
        |_|   |_|   |_|   3.7-dev (r12345)

[!] legal disclaimer: Usage of sqlmap for attacking targets without prior mutual consent is illegal. It is the end user's responsibility to obey all applicable local, state and federal laws. sqlmap developers assume no liability and are not responsible for any misuse or damage caused by this program.

[*] starting @ 12:00:00 /2023-01-01/

[12:00:00] [INFO] starting sqlmap 3.7-dev (r12345)
[12:00:00] [INFO] testing connection to the target URL
[12:00:01] [INFO] checking if the target is stable
[12:00:01] [INFO] target URL is stable
[12:00:01] [INFO] testing for SQL injection on URL 'http://testphp.vulnweb.com/artists.php?id=1'
[12:00:01] [INFO] testing 'Boolean-based blind - Parameter: id'
[12:00:02] [INFO] testing 'Error-based - Parameter: id'
[12:00:03] [INFO] testing 'UNION query - Parameter: id'
[12:00:04] [INFO] testing 'Stacked queries - Parameter: id'
[12:00:05] [INFO] testing 'Time-based blind - Parameter: id'
...

输出证实 sqlmap 现在正在按指定测试多种技术。

总结

在本实验中,你已成功学会如何指定和控制 sqlmap 使用的 SQL 注入技术。你探索了单独的技术代码(B、E、U、S、T、Q),并练习了强制 sqlmap 使用特定方法,如基于布尔的盲注、基于时间的盲注和 UNION 查询注入。最后,你学习了如何组合多种技术以进行更全面的扫描。

理解并利用 sqlmap 中的 --technique 选项,可以让你精细化漏洞评估,优化扫描时间,并更有效地针对特定类型的 SQL 注入漏洞。这项技能对于高效和精确的渗透测试至关重要。