数据库
首页 > 数据库> > Postgresql从MD5密码验证改为SCRAM-SHA-256

Postgresql从MD5密码验证改为SCRAM-SHA-256

作者:互联网

随着密码学技术的发展,MD5哈希算法构造的密码越来越不安全,所以,PG顺应发展,从10版本开始支持了SCRAM-SHA-256加密算法,因为使用的新的哈希算法。使得在暴力破解的时候花费的代价更加昂贵。那么,接下来,在PG中看一下如何从MD5切换到SCRAM-SHA-256。
首先,太老的的驱动不支持SCRAM-SHA-256,都会报错,如JDBC,会报The authentication type 10 is not supported。那么就需要升级驱动。

接下来就是数据库服务端修改配置:

#修改postgresql.conf参数文件
password_encryption = scram-sha-256
#重新载入配置文件
pg_ctl reload -D $PGDATA 
或
SELECT pg_reload_conf();
#查看参数配置是否修改成功
SHOW password_encryption;
#重置用户密码
\password user_name
#修改pg_hba.conf验证方法,然后reload即可
如:
host  all  all  0.0.0.0/0 md5
改为
host  all  all  0.0.0.0/0 scram-sha-256

实例:

#通过\password user_name修改了hank用户密码
postgres=# select rolname,rolpassword from pg_authid where rolname in('hank','zabbix');
 rolname |                                                              rolpassword                                                              
---------+---------------------------------------------------------------------------------------------------------------------------------------
 zabbix  | md5435f13d666b53dd9b4b829e237213fd8
 hank    | SCRAM-SHA-256$4096:yqNGsct76W5ZjPvMxxjzRw==$NmR4QIuHRlhu+I/HP1TQ4OC6stmtgN6Oc7oZa76tKxQ=:kNOwGMU+tSbJ2QQiD1Hb/rCKX7/coQEdkeUjD9+pEhE=

#pg_hba.conf没有修改,但是同样可以通过密码登陆,这是pg的一个兼容性特性,即使没写SCRAM-SHA-256,可以自动识别密码为SCRAM-SHA-256加密的,然后验证通过,而且使用老的MD5加密的用户也可以正常登陆
host  all  all  0.0.0.0/0 md5

psql -h ******* -p 1921 -U hank
Password for user hank: 
psql (12.6)
Type "help" for help.
hank=> 

#如果修改pg_hba.conf为scram-sha-256,即使zabbix用户写对密码,也无法正常登陆,如下,只有通过scram-sha-256加密的hank用户可正常登陆,也可以查看视图pg_hba_file_rules
host  all  all  0.0.0.0/0 scram-sha-256

psql -h xxxxx -p 1921 -U zabbix
Password for user zabbix: 
psql: error: FATAL:  password authentication failed for user "zabbix"
 psql -h xxxxxx -p 1921 -U hank
Password for user hank: 
psql (12.6)
Type "help" for help.

hank=> 

#修改密码后,可正常登陆
postgres=# \password zabbix
Enter new password: 
Enter it again: 
postgres=# select rolname,rolpassword from pg_authid where rolname in('hank','zabbix');
 rolname |                                                              rolpassword                                                              
---------+---------------------------------------------------------------------------------------------------------------------------------------
 hank    | SCRAM-SHA-256$4096:yqNGsct76W5ZjPvMxxjzRw==$NmR4QIuHRlhu+I/HP1TQ4OC6stmtgN6Oc7oZa76tKxQ=:kNOwGMU+tSbJ2QQiD1Hb/rCKX7/coQEdkeUjD9+pEhE=
 zabbix  | SCRAM-SHA-256$4096:Q2LODw88k0a3G9cr36Crvw==$EhBkOVbaNIxpqZADeYGCQE6LIam/k+aqmdQqBuxJM28=:Xw+ySsSGidAdvfQ/98LL38NnK1wsT96sivwtJZ144vU=
(2 rows)

psql -h xxxxxxxx -p 1921 -U zabbix
Password for user zabbix: 
psql (12.6)
Type "help" for help.

zabbix=> 


标签:hank,Postgresql,SHA,zabbix,pg,SCRAM,256
来源: https://blog.csdn.net/dazuiba008/article/details/118308665