您查看的文章来源于http://www.oklinux.cn
对这类威胁的反击很简单:不要让普通用户访问或创建任何内容。清单 1 展示了如何对一个表提供保护。
清单 1. 对表提供保护
postgres=# SET SESSION AUTHORIZATION postgres;SETpostgres=# CREATE ROLE user1 WITH LOGIN UNENCRYPTED PASSWORD '123';CREATE ROLEpostgres=# CREATE SCHEMA user1 CREATE TABLE t1(i int);CREATE SCHEMApostgres=# INSERT INTO user1.t1 VALUES(1);INSERT 0 1postgres=# GRANT USAGE ON SCHEMA user1 TO user1;GRANTpostgres=# SELECT I FROM user1.t1; i--- 2(1 row)postgres=# SET SESSION AUTHORIZATION user1;SETpostgres=> SELECT I FROM user1.t1;ERROR: permission denied for relation t1postgres=> SET SESSION AUTHORIZATION postgres;SETpostgres=# GRANT SELECT ON user1.t1 TO user1;GRANTpostgres=# SET SESSION AUTHORIZATION user1;SETpostgres=> SELECT I FROM user1.t1; i--- 2(1 row)
清单 2 演示了对 PUBLIC 模式的访问被禁止。
清单 2. 禁止角色 user1 创建任何实体
postgres=> SET SESSION AUTHORIZATION postgres;SETpostgres=# REVOKE ALL PRIVILEGES ON SCHEMA PUBLIC FROM user1;REVOKEpostgres=# SET SESSION AUTHORIZATION user1;SETThe error message of "ERROR: permission denied for schema user1" means that this defensive measure works:postgres=> CREATE TABLE X;ERROR: permission denied for schema user1
访问由其他用户控制的对象
如下面清单 3 所示的攻击媒介假设用户可以访问 PUBLIC 模式;例如,GRANT USAGE ON SCHEMA PUBLIC TO user1。它以下面的假设作为前提:
?所有用户在默认情况下都有权访问集群中的任何数据库。
?假设集群允许用户创建并操作 PUBLIC 模式中的所有实体。
?一个普通用户帐户有权访问系统目录。否则,用户帐户不能正常工作(固有的 PostgreSQL 服务器行为)。
清单 3. 收集有关表的信息
postgres=> SELECT * FROM user1.t2;ERROR: permission denied for relation t2postgres=> insert into user1.t2 values(10);ERROR: permission denied for relation t2postgres=>postgres=> \d List of relations Schema | Name | Type | Owner-------- ------ ------- ---------- user1 | t1 | table | postgres user1 | t2 | table | postgres(2 rows)postgres=> \d t? Table "user1.t1" Column | Type | Modifiers-------- --------- ----------- i | integer | Table "user1.t2" Column | Type | Modifiers-------- --------- ----------- i | integer |