关于MySQL绕过授予information_schema中对象时报ERROR 1044(4200)错误

作者:潇湘隐者 时间:2024-01-24 07:49:27 

这个问题是微信群中网友关于MySQL权限的讨论,有这么一个业务需求(下面是他的原话):

因为MySQL的很多功能都依赖主键,我想用zabbix用户,来监控业务数据库的所有表,是否都建立了主键。

监控的语句是:


FROM  information_schema.tables t1
   LEFT OUTER JOIN information_schema.table_constraints t2
         ON t1.table_schema = t2.table_schema
           AND t1.table_name = t2.table_name
           AND t2.constraint_name IN ( 'PRIMARY' )
WHERE t2.table_name IS NULL
   AND t1.table_schema NOT IN ( 'information_schema', 'myawr', 'mysql',
                 'performance_schema',
                 'slowlog', 'sys', 'test' )
   AND t1.table_type = 'BASE TABLE'

但是我不希望zabbix用户,能读取业务库的数据。一旦不给zabbix用户读取业务库数据的权限,那么information_schema.TABLES 和 information_schema.TABLE_CONSTRAINTS 就不包含业务库的表信息了,也就统计不出来业务库的表是否有建主键。有没有什么办法,即让zabbix不能读取业务库数据,又能监控是否业务库的表没有建立主键?

首先,我们要知道一个事实:information_schema下的视图没法授权给某个用户。如下所示


mysql> GRANT SELECT ON information_schema.TABLES TO test@'%';
ERROR 1044 (42000): Access denied for user 'root'@'localhost' to database 'information_schema'

关于这个问题,可以参考mos上这篇文章:Why Setting Privileges on INFORMATION_SCHEMA does not Work (文档 ID 1941558.1)

APPLIES TO:

MySQL Server - Version 5.6 and later

Information in this document applies to any platform.

GOAL

To determine how MySQL privileges work for INFORMATION_SCHEMA.

SOLUTION

A simple GRANT statement would be something like:

mysql> grant select,execute on information_schema.* to 'dbadm'@'localhost';

ERROR 1044 (42000): Access denied for user 'root'@'localhost' to database 'information_schema'

The error indicates that the super user does not have the privileges to change the information_schema access privileges.

Which seems to go against what is normally the case for the root account which has SUPER privileges.

The reason for this error is that the information_schema database is actually a virtual database that is built when the service is started.

It is made up of tables and views designed to keep track of the server meta-data, that is, details of all the tables, procedures etc. in the database server.

So looking specifically at the above command, there is an attempt to add SELECT and EXECUTE privileges to this specialised database.

The SELECT option is not required however, because all users have the ability to read the tables in the information_schema database, so this is redundant.

The EXECUTE option does not make sense, because you are not allowed to create procedures in this special database.

There is also no capability to modify the tables in terms of INSERT, UPDATE, DELETE etc., so privileges are hard coded instead of managed per user.

那么怎么解决这个授权问题呢? 直接授权不行,那么我们只能绕过这个问题,间接实现授权。思路如下:首先创建一个存储过程(用户数据库),此存储过程找出没有主键的表的数量,然后将其授予test用户。


DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `moitor_without_primarykey`()
BEGIN
  SELECT COUNT(*)
FROM  information_schema.tables t1
   LEFT OUTER JOIN information_schema.table_constraints t2
         ON t1.table_schema = t2.table_schema
           AND t1.table_name = t2.table_name
           AND t2.constraint_name IN ( 'PRIMARY' )
WHERE t2.table_name IS NULL
   AND t1.table_schema NOT IN ( 'information_schema', 'myawr', 'mysql',
                 'performance_schema',
                 'slowlog', 'sys', 'test' )
   AND t1.table_type = 'BASE TABLE';
END //
DELIMITER ;

mysql> GRANT EXECUTE ON PROCEDURE moitor_without_primarykey TO 'test'@'%';
Query OK, 0 rows affected (0.02 sec)

此时test就能间接的去查询information_schema下的对象了。


mysql> select current_user();
+----------------+
| current_user() |
+----------------+
| test@%     |
+----------------+
1 row in set (0.00 sec)

mysql> call moitor_without_primarykey;
+----------+
| COUNT(*) |
+----------+
|    6 |
+----------+
1 row in set (0.02 sec)

Query OK, 0 rows affected (0.02 sec)

查看test用户的权限。


mysql> show grants for test@'%';
+-------------------------------------------------------------------------------+
| Grants for test@%                               |
+-------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO `test`@`%`                       |
| GRANT EXECUTE ON PROCEDURE `zabbix`.`moitor_without_primarykey` TO `test`@`%` |
+-------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

来源:https://www.cnblogs.com/kerrycode/archive/2020/10/16/13826210.html

标签:mysql,ERROR,1044,4200
0
投稿

猜你喜欢

  • Javascript(es2016) import和require用法和区别详解

    2024-04-19 09:57:04
  • Bottle部署web服务及postman接口的方法

    2022-06-14 23:38:53
  • SQL Server 2000安装图解教程

    2009-09-09 19:59:00
  • Web端扫码登录的原理和实现讲解

    2022-07-08 11:40:18
  • python实现简易云音乐播放器

    2021-07-02 10:13:43
  • 使用Python读取二进制文件的实例讲解

    2021-08-20 11:34:48
  • 利用Python将图片中扭曲矩形的复原

    2022-05-24 21:35:54
  • python解析中国天气网的天气数据

    2023-01-20 18:48:39
  • Python中Playwright 与 pyunit 结合使用详解

    2022-04-30 10:42:42
  • 浏览器根据什么来判定脚本失控?[译]

    2009-02-20 13:36:00
  • Python类属性与实例属性用法分析

    2022-10-12 03:14:58
  • php相当简单的分页类

    2023-11-17 01:50:36
  • laravel修改用户模块的密码验证实现

    2023-06-14 12:37:18
  • Python爬虫:url中带字典列表参数的编码转换方法

    2021-11-02 17:50:45
  • QT连接Oracle数据库并实现登录验证的操作步骤

    2024-01-27 13:06:44
  • ajax取消挂起请求的处理方法

    2023-11-20 23:41:53
  • 重新阅读《HTTP协议基础》

    2008-04-04 17:40:00
  • SQLServer2005重建索引前后对比分析

    2024-01-27 17:27:09
  • Python 统计列表中重复元素的个数并返回其索引值的实现方法

    2023-07-15 12:31:24
  • Win 10下Anaconda虚拟环境的教程

    2022-09-18 16:42:06
  • asp之家 网络编程 m.aspxhome.com