2023年7月31日发(作者:)

HighGoDB⽤户密码安全策略⽬录环境系统平台:Linux x86-64 Red Hat Enterprise Linux 7版本:4.3.2详细信息⼀般来说数据库密码安全管理要考虑以下⼏个⽅⾯ :

1. 密码过期策略, 决定密码的有效期, 多长时间过期.

2. 密码复⽤策略, 密码修改时需要对⽐以前的密码, 多少次以后才可以复⽤, 或者永不能使⽤与以前密码相同的密码.3. 密码长度策略, 密码的最⼩长度, 太短的话很容易被穷举法破解, ⼀般⾄少使⽤8位以上长度的密码.4. 密码复杂度策略, 密码包含的字符复杂度, ⼀般要求包含数字字母⼤⼩写以及特殊字符. 另外不要使⽤与数据库名, ⽤户名相似或⼀致的密码.5. 密码字典过滤, 过滤掉⼀些⽤户常⽤的或者有意义的字符, 也是为了避免密码很容易被穷举破解.

6. 密码存储策略, 使⽤加密存储, 不要使⽤明⽂存储.

加密存储在创建⽤户时使⽤encrypted password即可.

如果设置了password_encryption=off, 同时创建⽤户时不加encrypted, 那么密码将以明⽂存储.加密存储⽅法 :

highgo=# create role u1 login encrypted password 'highgo';CREATE ROLEhighgo=# select * from pg_shadow where usename='u1'; usename | usesysid | usecreatedb | usesuper | userepl | usebypassrls | passwd | valuntil | useconfig---------+----------+-------------+----------+---------+--------------+-------------------------------------+----------+----------- u1 | 131294 | f | f | f | f | md50a0a7a2ef5607a4424f00e4903877f2e |

|(1 row)7. 密码锁策略, 密码输⼊错误多少次后锁⽤户, 多长时间解锁.HighgoDB不⽀持密码锁策略.8. 密码保护策略, 密码输⼊错误多少次后延迟认证. 可⽤来防⽌暴⼒破解.⽬前HighgoDB在密码管理这块做得较弱, 以下举例通过函数来实现其中的部分安全策略 :

创建⼀个字典表, ⽤于存放已经使⽤过的密码的md5值, 以及⽤来进⾏暴⼒破解的密码字典的md5值.highgo=# create table pwd_dictionary(pwd text unique);CREATE TABLE创建⼀个记录⽤户最后⼀次修改密码时间的表. ⽤于实施密码过期提醒策略.highgo=# create table user_pwd(rolename name not null unique, pwd_modify_time timestamp not null);CREATE TABLE创建⽤户的函数 :

highgo=# create or replace function create_role(i_rolename name, i_pwd text) returns void as $$declare v_length int := 8;begin -- 密码长度策略 if length(i_pwd) < v_length then raise notice 'password too short, please use password long than %.', v_length; return; end if; -- 密码复杂度策略, 包含数字, 字母⼤⼩写. if not(i_pwd ~ '[a-z]' and i_pwd ~ '[A-Z]' and i_pwd ~ '[0-9]') then raise notice 'password too simple, please ensure password contain a-z,A-Z and 0-9.'; return; end if; -- 密码复⽤策略, 不允许重复使⽤已经存在的密码. -- 密码字典策略, 不允许使⽤密码字典中的密码. insert into pwd_dictionary(pwd) values (md5(i_pwd)); -- 插⼊⽤户表, 记录⽤户最后⼀次修改密码的时间, ⽤于密码过期策略 insert into user_pwd(rolename, pwd_modify_time) values (i_rolename, now()); -- 创建⽤户 execute 'create role '||i_rolename||' encrypted password '||quote_literal(i_pwd); raise notice 'create role % successed.', i_rolename; return;end;$$ language plpgsql strict;CREATE FUNCTION创建⽤户密码过短, 不允许创建.highgo=# select * from create_role('u4','pwd');NOTICE: password too short, please use password long than 8. create_role-------------(1 row)创建⽤户密码过于简单, 不允许创建.highgo=# select * from create_role('u4','abcdefee');NOTICE: password too simple, please ensure password contain a-z,A-Z and 0-9. create_role-------------(1 row)创建⽤户正常.highgo=# select * from create_role('u4','aA0ffffffff');NOTICE: create role u4 successed. create_role-------------(1 row)创建⽤户密码与现有密码重复, 不允许创建.highgo=# select * from create_role('new','aA0ffffffff');ERROR: duplicate key value violates unique constraint "pwd_dictionary_pwd_key"DETAIL: Key (pwd)=(2b9aa88182d13d35930180b4cc791beb) already T: SQL statement "insert into pwd_dictionary(pwd) values (md5(i_pwd))"PL/pgSQL function create_role(name,text) line 17 at SQL statement修改⽤户密码的函数 :

highgo=# create or replace function alter_role_pwd(i_rolename name, i_pwd text) returns void as $$declare v_length int := 8;begin -- 密码长度策略 if length(i_pwd) < v_length then raise notice 'password too short, please use password long than %.', v_length; return; end if; -- 密码复杂度策略, 包含数字, 字母⼤⼩写. if not(i_pwd ~ '[a-z]' and i_pwd ~ '[A-Z]' and i_pwd ~ '[0-9]') then raise notice 'password too simple, please ensure password contain a-z,A-Z and 0-9.'; return; end if; -- 密码复⽤策略, 不允许重复使⽤已经存在的密码. -- 密码字典策略, 不允许使⽤密码字典中的密码. insert into pwd_dictionary(pwd) values (md5(i_pwd)); -- 更新⽤户表, 记录⽤户最后⼀次修改密码的时间, ⽤于密码过期策略 update user_pwd set pwd_modify_time=now() where rolename=i_rolename; -- 修改⽤户密码 execute 'alter role '||i_rolename||' encrypted password '||quote_literal(i_pwd); raise notice 'modify role % password successed.', i_rolename; return;end;$$ language plpgsql strict;CREATE FUNCTION更多详细信息请登录【瀚⾼技术⽀持平台】查看

2023年7月31日发(作者:)

HighGoDB⽤户密码安全策略⽬录环境系统平台:Linux x86-64 Red Hat Enterprise Linux 7版本:4.3.2详细信息⼀般来说数据库密码安全管理要考虑以下⼏个⽅⾯ :

1. 密码过期策略, 决定密码的有效期, 多长时间过期.

2. 密码复⽤策略, 密码修改时需要对⽐以前的密码, 多少次以后才可以复⽤, 或者永不能使⽤与以前密码相同的密码.3. 密码长度策略, 密码的最⼩长度, 太短的话很容易被穷举法破解, ⼀般⾄少使⽤8位以上长度的密码.4. 密码复杂度策略, 密码包含的字符复杂度, ⼀般要求包含数字字母⼤⼩写以及特殊字符. 另外不要使⽤与数据库名, ⽤户名相似或⼀致的密码.5. 密码字典过滤, 过滤掉⼀些⽤户常⽤的或者有意义的字符, 也是为了避免密码很容易被穷举破解.

6. 密码存储策略, 使⽤加密存储, 不要使⽤明⽂存储.

加密存储在创建⽤户时使⽤encrypted password即可.

如果设置了password_encryption=off, 同时创建⽤户时不加encrypted, 那么密码将以明⽂存储.加密存储⽅法 :

highgo=# create role u1 login encrypted password 'highgo';CREATE ROLEhighgo=# select * from pg_shadow where usename='u1'; usename | usesysid | usecreatedb | usesuper | userepl | usebypassrls | passwd | valuntil | useconfig---------+----------+-------------+----------+---------+--------------+-------------------------------------+----------+----------- u1 | 131294 | f | f | f | f | md50a0a7a2ef5607a4424f00e4903877f2e |

|(1 row)7. 密码锁策略, 密码输⼊错误多少次后锁⽤户, 多长时间解锁.HighgoDB不⽀持密码锁策略.8. 密码保护策略, 密码输⼊错误多少次后延迟认证. 可⽤来防⽌暴⼒破解.⽬前HighgoDB在密码管理这块做得较弱, 以下举例通过函数来实现其中的部分安全策略 :

创建⼀个字典表, ⽤于存放已经使⽤过的密码的md5值, 以及⽤来进⾏暴⼒破解的密码字典的md5值.highgo=# create table pwd_dictionary(pwd text unique);CREATE TABLE创建⼀个记录⽤户最后⼀次修改密码时间的表. ⽤于实施密码过期提醒策略.highgo=# create table user_pwd(rolename name not null unique, pwd_modify_time timestamp not null);CREATE TABLE创建⽤户的函数 :

highgo=# create or replace function create_role(i_rolename name, i_pwd text) returns void as $$declare v_length int := 8;begin -- 密码长度策略 if length(i_pwd) < v_length then raise notice 'password too short, please use password long than %.', v_length; return; end if; -- 密码复杂度策略, 包含数字, 字母⼤⼩写. if not(i_pwd ~ '[a-z]' and i_pwd ~ '[A-Z]' and i_pwd ~ '[0-9]') then raise notice 'password too simple, please ensure password contain a-z,A-Z and 0-9.'; return; end if; -- 密码复⽤策略, 不允许重复使⽤已经存在的密码. -- 密码字典策略, 不允许使⽤密码字典中的密码. insert into pwd_dictionary(pwd) values (md5(i_pwd)); -- 插⼊⽤户表, 记录⽤户最后⼀次修改密码的时间, ⽤于密码过期策略 insert into user_pwd(rolename, pwd_modify_time) values (i_rolename, now()); -- 创建⽤户 execute 'create role '||i_rolename||' encrypted password '||quote_literal(i_pwd); raise notice 'create role % successed.', i_rolename; return;end;$$ language plpgsql strict;CREATE FUNCTION创建⽤户密码过短, 不允许创建.highgo=# select * from create_role('u4','pwd');NOTICE: password too short, please use password long than 8. create_role-------------(1 row)创建⽤户密码过于简单, 不允许创建.highgo=# select * from create_role('u4','abcdefee');NOTICE: password too simple, please ensure password contain a-z,A-Z and 0-9. create_role-------------(1 row)创建⽤户正常.highgo=# select * from create_role('u4','aA0ffffffff');NOTICE: create role u4 successed. create_role-------------(1 row)创建⽤户密码与现有密码重复, 不允许创建.highgo=# select * from create_role('new','aA0ffffffff');ERROR: duplicate key value violates unique constraint "pwd_dictionary_pwd_key"DETAIL: Key (pwd)=(2b9aa88182d13d35930180b4cc791beb) already T: SQL statement "insert into pwd_dictionary(pwd) values (md5(i_pwd))"PL/pgSQL function create_role(name,text) line 17 at SQL statement修改⽤户密码的函数 :

highgo=# create or replace function alter_role_pwd(i_rolename name, i_pwd text) returns void as $$declare v_length int := 8;begin -- 密码长度策略 if length(i_pwd) < v_length then raise notice 'password too short, please use password long than %.', v_length; return; end if; -- 密码复杂度策略, 包含数字, 字母⼤⼩写. if not(i_pwd ~ '[a-z]' and i_pwd ~ '[A-Z]' and i_pwd ~ '[0-9]') then raise notice 'password too simple, please ensure password contain a-z,A-Z and 0-9.'; return; end if; -- 密码复⽤策略, 不允许重复使⽤已经存在的密码. -- 密码字典策略, 不允许使⽤密码字典中的密码. insert into pwd_dictionary(pwd) values (md5(i_pwd)); -- 更新⽤户表, 记录⽤户最后⼀次修改密码的时间, ⽤于密码过期策略 update user_pwd set pwd_modify_time=now() where rolename=i_rolename; -- 修改⽤户密码 execute 'alter role '||i_rolename||' encrypted password '||quote_literal(i_pwd); raise notice 'modify role % password successed.', i_rolename; return;end;$$ language plpgsql strict;CREATE FUNCTION更多详细信息请登录【瀚⾼技术⽀持平台】查看