赞
踩
数据库密码管理是数据库安全的重要环节之一。密码管理及配置策略主要包括:
pg中密码始终以加密方式存储在系统目录中。ENCREPED 关键字没有任何效果, 但被接受向后兼容。加密方式可以通过password_encryption参数配置
postgres=# show password_encryption;
password_encryption
---------------------
md5
(1 row)
postgres=# select * from pg_shadow where usename='test';
usename | usesysid | usecreatedb | usesuper | userepl | usebypassrls | passwd | valuntil | usec
onfig
---------+----------+-------------+----------+---------+--------------+-------------------------------------+------------------------+-----
------
test | 49156 | f | f | f | f | md52d308906cb4ea734a22f76e7927c046b | 2019-04-10 16:58:00+08 |
(1 row)
postgres=#
pg支持密码有效期配置,可以通过配置密码有效期,制定密码更换周期。
服务器端设置有效期
postgres=# alter role test valid until '2019-04-10 16:58:00';
ALTER ROLE
postgres=# select * from pg_user where usename='test';
usename | usesysid | usecreatedb | usesuper | userepl | usebypassrls | passwd | valuntil | useconfig
---------+----------+-------------+----------+---------+--------------+----------+------------------------+-----------
test | 49156 | f | f | f | f | ******** | 2019-04-10 16:58:00+08 |
(1 row)
客户端连接测试
[postgres@pg2 ~]$ date
Wed Apr 10 17:11:49 CST 2019
[postgres@pg2 ~]$ psql -h 192.168.6.12 -U test -d postgres -p 5432
Password for user test:
psql: FATAL: password authentication failed for user "test"
[postgres@pg2 ~]$
注意:
passwordcheck.so模块可以实现密码复杂度要求,此模块可以检查密码,如果密码太弱,他会拒绝连接
创建用户或修改用户密码时,强制限制密码的复杂度,限制密码不能重复使用
例如密码长度,包含数字,字母,大小写,特殊字符等,同时排除暴力破解字典中的字符串
添加'$libdir/passwordcheck'到参数shared_preload_libraries,重启生效
默认so文件都存放在$libdir目录下
[pg@pg ~]$ ls -atl $LD_LIBRARY_PATH/passwordcheck*
-rwxr-xr-x 1 pg pg 8640 Feb 1 14:23 /opt/postgres/lib/passwordcheck.so
postgres=# select name,setting from pg_settings where name like '%dynamic%';
name | setting
----------------------------+---------
dynamic_library_path | $libdir
dynamic_shared_memory_type | posix
(2 rows)
postgres=# alter system set shared_preload_libraries=pg_pathman,pg_stat_statements,passwordcheck;
ALTER SYSTEM
postgres=#
重启生效
shared_preload_libraries参数使用参考“Postgresql共享库预加载(Shared Library Preloading)”
密码复杂度检查模块Passwordcheck
postgres=# alter role test with password 'test';
ERROR: password is too short
postgres=# alter role test password '12345678';
ERROR: password must contain both letters and nonletters
postgres=# alter role test with password 'test1234';
ERROR: password must not contain user name
postgres=# alter role test with password 'tttt1234';
ALTER ROLE
postgres=#
auth_delay.so模块会导致服务器在报告身份验证失败之前短暂停留,这个主要用于防止暴力破解. 验证失败后, 延迟一个时间窗口才能继续验证。请注意, 它不会阻止拒绝服务攻击, 甚至可能会加剧这些攻击, 因为在报告身份验证失败之前等待的进程仍将使用连接插槽。
需要配置以下参数,实现密码验证延迟失败延迟
so文件存储在$libdir下
[pg@pg lib]$ ls -atl $LD_LIBRARY_PATH/auth_delay*
-rwxr-xr-x 1 pg pg 8432 Feb 1 14:23 /opt/postgres/lib/auth_delay.so
参数修改
shared_preload_libraries --预加载模块
auth_delay.milliseconds (int) --指定延迟时间
postgres=# alter system set shared_preload_libraries=pg_pathman, pg_stat_statements, passwordcheck,auth_delay;
ALTER SYSTEM
重启生效
postgres=# alter system set auth_delay.milliseconds=5000;
ALTER SYSTEM
reload生效
[pg@pg ~]$ psql -h 192.168.6.12 -U test -p 5432 -d postgres
Password for user test:
--5s
psql: FATAL: password authentication failed for user "test"
[pg@pg ~]$
输入密码后,如果密码不正确,会等待5s,然后返回密码失败提示
[pg@pg ~]$ psql -h 192.168.6.12 -U test -p 5432 -d postgres
Password for user test:
psql (10.4)
Type "help" for help.
postgres=>
输入密码后,如果密码正确,没有等待。
目前PostgreSQL不支持这个安全策略, 目前只能使用auth_delay来延长暴力破解的时间.
密码的配置命令可能会被记录到history文件及csvlog日志文件中(如果开启了DDL或更高级别审计log_statement),这些文件明文记录了密码,可能造成密码泄露风险。
HISTFILE
The file name that will be used to store the history list. If unset, the file name is taken from the PSQL_HISTORY environment variable. If that is not set either, the default is ~/.psql_history, or %APPDATA%\postgresql\psql_history on Windows. For example, putting:
\set HISTFILE ~/.psql_history- :DBNAME
in ~/.psqlrc will cause psql to maintain a separate history for each database.
Note
This feature was shamelessly plagiarized from Bash. --??!!
csvlog
数据库错误日志
事例:
如以下命令,会记录到HISTFILE和csvlog日志中
postgres=# alter role test with password 'tttt1234';
ALTER ROLE
history file记录
[pg@pg ~]$ cat ~/.psql_history |grep tttt1234
alter role test with password 'tttt1234';
[pg@pg ~]$
csvlog记录
[pg@pg ~]$ cat $PGDATA/postgresql.conf |grep log_statement
#log_statement = 'none' # none, ddl, mod, all
log_statement = 'ddl'
#log_statement_stats = off
[pg@pg ~]$
[pg@pg ~]$ cat $PGDATA/pg_log/postgresql-2019-04-12_092557.csv |grep tttt1234
2019-04-12 09:33:23.036 CST,"pg","postgres",1309,"[local]",5cafeadb.51d,3,"idle",2019-04-12 09:33:15 CST,3/21,0,LOG,00000,"statement: alter role test with password 'tttt1234';",,,,,,,,,"psql"
[pg@pg ~]$
[pg@pg ~]$ createuser -l -h 127.0.0.1 -p 5432 -U pg -W tuser
Password:
[pg@pg ~]$
[pg@pg ~]$ cat $PGDATA/pg_log/postgresql-2019-04-12_092557.csv |grep tuser
2019-04-12 11:17:48.348 CST,"pg","postgres",1574,"localhost:42560",5cb0035c.626,3,"idle",2019-04-12 11:17:48 CST,3/236,0,LOG,00000,"statement: CREATE ROLE tuser NOSUPERUSER NOCREATEDB NOCREATEROLE INHERIT LOGIN;",,,,,,,,,"createuser"
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。