sql注入之select/union select

时间:2025-03-07 07:30:54

目录

  • select
    • select 1 from table
    • select count(1) from table
    • select sum(1) from table
  • union select
    • union语句使用条件
    • 判断字段数 union select 1,2(或union select null,null)
    • 判断回显位置


select

select 1 from table

select 1 from table

在table增加一个临时列,该列的每一行都是1。

同理select n from table /* n表示任意数字 */

表示在table增加一个临时列,该列的每一行都是数字n。


select count(1) from table

select count(1) from table

返回结果是table的行数

select count(n) from table

返回结果依旧是table的行数,与数字n的大小没有关系


select sum(1) from table

select sum(1) from table

返回结果 = table的行数

select sum(2) from table

返回结果 = table的行数 x 2

select sum(N) from table

返回结果 = table的行数 x N


union select

union语句使用条件

(1)union前后两个select的结果集应具有相同列数
(2)union前后两个select的结果集对应列应是相同数据类型


判断字段数 union select 1,2(或union select null,null)

可以用来判断表的字段数(列数),和 order by 的作用类似,当 order by 无法使用的时候,可以使用此方法进行判断。

在保证union的前一个select语句为true的情况下,比如 ?id = 8,假设当面表有3个字段。

union select 1union select 1,2 均不回显正确结果
union select 1,2,3 返回结果和?id = 8的回显结果相同,说明表的字段数=3

和select后面的数字值没有关系,只和数字个数有关。即union select 4,2,8的结果也是和?id = 8的回显结果相同。

union select null,null同理,当回显结果和?id = 8相同时,null的个数就是表的字段数。


判断回显位置

使用union select 1,2,3 可以用来判断回显位置。

如果union的前一个select语句为true,则第一个select的返回结果就会占用显示位置,导致我们想要的内容分无法显示。

所以需要让第一个select语句为false。

union select 1,2,3观察回显结果中各个数字的位置。如果回显了2和3,说明在语句中2和3的位置放入想要注入的语句,就会在相应的回显位置上显示我们想要的信息。

数字的值可以任意,但个数需要和当前表的字段数保持一致。