PostgreSQL密碼安全策略


問題:今天公司進行軟件測評,在測評期間,測評人員問起PostgreSQL登錄失敗導致用戶鎖定的次數,密碼輸錯幾次賬戶會被鎖定?

網上查了一圈,oracle和mysql都有相關設置,只有pg庫沒有找到相關的設置參數。偶然發現網上的帖子,結果發現PG庫尚不支持相關設置。

image

下面引用一下:

數據庫密碼管理是數據庫安全的重要環節之一。密碼管理及配置策略主要包括:

  • 密碼加密存儲
  • 密碼有效期
  • 密碼復雜度
  • 密碼驗證失敗延遲
  • 密碼驗證失敗次數限制,失敗后鎖定, 以及解鎖時間
  • 設置密碼時防止密碼被記錄到數據庫日志中
    下面會依次講解在PostgreSQL中如何實現密碼相關的安全性配置。

1、密碼加密存儲

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 |
復制代碼

2、密碼有效期

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"
復制代碼

注意:

  • pg密碼有效期僅針對客戶端有效,服務器端不受限制。
  • 網絡訪問控制文件中不能配置為trust認證方式

3、密碼復雜度策略

passwordcheck.so模塊可以實現密碼復雜度要求,此模塊可以檢查密碼,如果密碼太弱,他會拒絕連接
創建用戶或修改用戶密碼時,強制限制密碼的復雜度,限制密碼不能重復使用
例如密碼長度,包含數字,字母,大小寫,特殊字符等,同時排除暴力破解字典中的字符串

3.1、啟用模塊

添加'$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)”

3.2、復雜度功能驗證

密碼復雜度檢查模塊Passwordcheck

  • 驗證創建的用戶密碼是否符合規則。
    密碼:最少8個字符;必須包含數字和字母;密碼中不能含有用戶名字段。
復制代碼
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
復制代碼

4、密碼驗證失敗延遲

auth_delay.so模塊會導致服務器在報告身份驗證失敗之前短暫停留,這個主要用於防止暴力破解. 驗證失敗后, 延遲一個時間窗口才能繼續驗證。請注意, 它不會阻止拒絕服務攻擊, 甚至可能會加劇這些攻擊, 因為在報告身份驗證失敗之前等待的進程仍將使用連接插槽。

4.1、啟用模塊

需要配置以下參數,實現密碼驗證延遲失敗延遲

復制代碼
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生效
復制代碼
4.2、驗證
復制代碼
[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=> 
輸入密碼后,如果密碼正確,沒有等待。
復制代碼

5、密碼驗證失敗次數限制,失敗后鎖定, 以及解鎖時間

目前PostgreSQL不支持這個安全策略, 目前只能使用auth_delay來延長暴力破解的時間.

6、設置密碼時防止密碼被記錄到數據庫日志中

密碼的配置命令可能會被記錄到history文件及csvlog日志文件中(如果開啟了DDL或更高級別審計log_statement),這些文件明文記錄了密碼,可能造成密碼泄露風險。

6.1、密碼記錄到兩個地方
復制代碼
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"
復制代碼
6.2、解決方式
  1. 使用createuser命令行工具-W選項提示輸入密碼。
  2. 使用pg_md5工具生成密碼, 在psql中使用ALTER ROLE填入md5值。
    與上面類似, pg_md5是pgpool提供的一個工具, 實際上就是調用上面的函數。
[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 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM