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 ROLE

highgo=# select * from pg_shadow where usename='u1';

 usename | usesysid | usecreatedb | usesuper | userepl | usebypassrls |               passwd                | v

aluntil | 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 exists.

CONTEXT:  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

更多详细信息请登录【瀚高技术支持平台】查看

https://support.highgo.com/#/index/docContent/8c0459e40368252a

猜你喜欢

转载自blog.csdn.net/pg_hgdb/article/details/87879130