
使用show effective grants查看权限
1、问题描述
用户 show grants
显示只有连接权限,但该用户却能执行 sbtest.*下的所有操作
2、官方文档
MySQL 官方手册,有这样一段话
https://dev.mysql.com/doc/refman/8.0/en/show-grants.html
SHOW GRANTS
does not display privileges that are available to the named account but are granted to a different account. For example, if an anonymous account exists, the named account might be able to use its privileges, but SHOW GRANTS
does not display them.
Percona Server 官方手册,有类似一段话
https://docs.percona.com/percona-server/8.0/management/extended_show_grants.html
In Oracle MySQL SHOW GRANTS
displays only the privileges granted explicitly to the named account. Other privileges might be available to the account, but they are not displayed. For example, if an anonymous account exists, the named account might be able to use its privileges, but SHOW GRANTS
will not display them. Percona Server for MySQL offers the SHOW EFFECTIVE GRANTS
command to display all the effectively available privileges to the account, including those granted to a different account.
概括如下:
- 用户 A 的 user 与用户 B 的 user 相同,或者用户 A 是匿名用户
- 用户 B 的 host 范围是用户 A 的 host 范围的子集
满足上述两个条件,此时用户 B 拥有显式授予给用户 A 的权限,但 SHOW GRANTS
不会显示这部分权限。在 Percona Server 可以通过 SHOW EFFECTIVE GRANTS
查看。
3、测试验证
3.1、同 user 用户
1)、创建用户并授权
2)、查看权限
权限列表没有显示 grantee@localhost
对 sbtest 库的权限,但实际 grantee@localhost
已经拥有 sbtest 库下所有操作权限
3)、grantee@localhost 登录,执行操作
4)、使用 SHOW EFFECTIVE GRANTS
查看权限
SHOW EFFECTIVE GRANTS
显示出拥有的同 user 用户权限
3.2、匿名用户
匿名用户请参考:https://dev.mysql.com/doc/refman/8.0/en/connection-access.html
1)、创建匿名用户并授权
2)、查看权限
权限列表没有显示 grantee@localhost
对 sbtest1 库的权限,但实际 grantee@localhost
已经拥有 sbtest1 库下所有操作权限
3)、grantee@localhost 登录,执行操作
4)、使用 SHOW EFFECTIVE GRANTS
查看权限
注意:SHOW EFFECTIVE GRANTS
没有显示出拥有的匿名用户权限,sbtest.*是拥有的同 user 用户权限
4、建议
1)、使用 SHOW EFFECTIVE GRANTS
代替 SHOW GRANTS
(GreatDB、GreatSQL、Percona Server)
2)、账号加固
- 匿名用户,禁止匿名用户登录
- 同 user 不同 host
到各权限表查看对应user信息,核实权限'错乱'的原因
user 表只有 user1@'172.%',db 表只有 user1@'%',对应算两个用户。
可能是手动更新过权限表:例如创建用户xx@'%',授权db.*所有权限,后来更新mysql.user表中的记录为xx@'172.%'限制登录来源。
根据精确匹配原则,user1可以从172.%主机连接数据库,全局权限为N(mysql.user),db权限匹配上user1@'%',拥有sbtest库的所有操作权限。
Enjoy GreatSQL :)
文章转载自公众号:GreatSQL社区
