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

 

这篇文章主要介绍了关于MySQL绕过授予information_schema中对象时报ERROR 1044(4200)错误,本文给大家分享解决方法,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

这个问题是微信群中网友关于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 ;
ERROR 1044 (42000): Access denied for user 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 //
CREATEDEFINER=`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 fortest@%;

+-------------------------------------------------------------------------------+
| Grants fortest@%|

+-------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO    `test`@`%`     |

| GRANT EXECUTE ON PROCEDURE `zabbix`.`moitor_without_primarykey` TO`test`@`%`|

+-------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

到此这篇关于关于MySQL绕过授予information_schema中对象时报ERROR 1044(4200)错误的文章就介绍到这了,更多相关mysql ERROR 1044(4200)内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

来源:脚本之家

链接:https://www.jb51.net/article/197565.ht

免责申明:
1. 本站所有下载资源均不包含技术支持和安装服务!需要讨论请进群!
2. 分享目的仅供大家学习和交流,请不要用于商业用途!
3. 如果你也有好源码或者教程,可以到审核区发布,分享有KR奖励和额外收入!
4. 如有链接无法下载、失效或广告,请联系管理员处理!
5. 本站无法保证资源或破解时效性,如某些授权码过期等问题,恕不在修复范围内。
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!故不接受任何形式的退款,如确认资源确有问题的,会补给相应KR以供再次购买。
7. 53Kr源码暂未发现后门代码,但无法保证100%安全,推荐检测方法:上传到 https://www.virustotal.com/在线查看是否有恶意代码以及其他有后门嫌疑的代码。
8. 在本站下载的源码我还是不建议正式使用,有特别喜欢的可以去程序官方购买。
53kr资源站仅提供学习的平台,所有资料均来自于网络,版权归原创者所有!本站不提供任何保证,并不承担任何法律责任,如果对您的版权或者利益造成损害,请提供相应的资质证明,我们将于3个工作日内予以删除。
53kr资源分享 » 关于MySQL绕过授予information_schema中对象时报ERROR 1044 4200 错误

发表回复