暂无图片
暂无图片
6
暂无图片
暂无图片
暂无图片

PostgreSQL扩展之passwordcheck

原创 贺晓群 2020-09-17
8783

PostgreSQL自带了一个插件passwordcheck可以满足简单的密码复杂度测验, 防止使用过短, 或者与包含用户名的密码,只需要把’$libdir/passwordcheck’加入到postgresql.conf的shared_preload_libraries参数中,然后重启服务器即可,只要通过CREATE ROLE或ALTER ROLE设置用户,passwordcheck模块就会检查用户的口令,如下:

postgres=# create role test password 'Test#2020';
CREATE ROLE
postgres=# alter role test password 'test#2020';
ERROR:  password must not contain user name
postgres=# alter role test password 'tes12020';
ALTER ROLE
postgres=# alter role test password '2020';
ERROR:  password is too short
postgres=# alter role test password '2020abc';
ERROR:  password is too short
postgres=# alter role test password '2020abcd';
ALTER ROLE
postgres=# alter role test password '12345678';
ERROR:  password must contain both letters and nonletters
postgres=# alter role test password '';
ERROR:  password is too short
postgres=# alter role test password 'abcdffgh';
ERROR:  password must contain both letters and nonletters

复制

如果需要实现更复杂的密码检查, 可以让passwordcheck使用CrackLib来检查口令。
安装过程如下:

  1. 安装cracklib以及字典
yum install -y cracklib-devel cracklib-dicts cracklib
复制
  1. 检查安装
# rpm -ql cracklib-dicts
/usr/lib64/cracklib_dict.hwm
/usr/lib64/cracklib_dict.pwd
/usr/lib64/cracklib_dict.pwi
/usr/sbin/mkdict
/usr/sbin/packer
/usr/share/cracklib
/usr/share/cracklib/cracklib-small.hwm
/usr/share/cracklib/cracklib-small.pwd
/usr/share/cracklib/cracklib-small.pwi
/usr/share/cracklib/pw_dict.hwm
/usr/share/cracklib/pw_dict.pwd
/usr/share/cracklib/pw_dict.pwi
复制
  1. 如果需要自己配置生成字典,包括此步骤,否则可跳过
[root@test ~]# mkdir /opt/src
[root@test ~]# cd /opt/src
[root@test src]# wget http://downloads.sourceforge.net/project/cracklib/cracklib-words/2008-05-07/cracklib-words-20080507.gz
[root@test src]# gunzip cracklib-words-20080507.gz
#可以到cracklib-words-20080507添加需要排除的密码,如不允许使用Twsm_20200917密码
[root@test src]# echo 'Twsm_20200917' >> cracklib-words-20080507
[root@test src]# create-cracklib-dict -o ./cracklib-dict ./cracklib-words-20080507
复制
  1. 下载PostgreSQL源码,配置passwordcheck
    如果当前的PG非源码安装,或者以前编译源码已清理,需要重新下载对应的PG源码版本
[root@test src]# wget https://ftp.postgresql.org/pub/source/v10.14/postgresql-10.14.tar.bz2
[root@test src]# tar xjvf postgresql-10.14.tar.bz2
[root@test src]# cd /opt/src/postgresql-10.14/contrib/passwordcheck/
#修改Makefile, 把注释去掉, 并修改字典文件(不要带.pwd后缀).
[root@test passwordcheck]# vi Makefile
#把下面两行注释去掉
#修改字典文件/usr/lib/cracklib_dict为步骤3生产的字典
PG_CPPFLAGS = -DUSE_CRACKLIB '-DCRACKLIB_DICTPATH="/opt/src/cracklib-dict"'
SHLIB_LINK = -lcrack
#修改需要的密码最小长度,修改为13
[root@test passwordcheck]# vi passwordcheck.c
#define MIN_PWD_LENGTH 13
复制
  1. 编译passwordcheck
#因为这里是重新下载的源码,需要全部重新编译
[root@test passwordcheck]# cd /opt/src/postgresql-10.14
[root@test postgresql-10.14]# ./configure --prefix=/opt/pgsql
[root@test postgresql-10.14]# gmake world
#如果有以前的编译的源码,可按以下方式编译
[root@test postgresql-10.14]# cd /opt/src/postgresql-10.14/contrib/passwordcheck
[root@test passwordcheck]# make clean
rm -f passwordcheck.so   libpasswordcheck.a  libpasswordcheck.pc
rm -f passwordcheck.o
[root@test passwordcheck]# make
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -O2 -fPIC -DUSE_CRACKLIB '-DCRACKLIB_DICTPATH="/opt/src/cracklib-dict"' -I. -I. -I../../src/include  -D_GNU_SOURCE   -c -o passwordcheck.o passwordcheck.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -O2 -fPIC -shared -o passwordcheck.so passwordcheck.o  -L../../src/port -L../../src/common -Wl,--as-needed -Wl,-rpath,'/opt/pgsql/lib',--enable-new-dtags  -lcrack 
[root@test passwordcheck]# ls
Makefile  passwordcheck.c  passwordcheck.o  passwordcheck.so
复制
  1. 安装passwordcheck
#以前的先做下备份
[root@test passwordcheck]# mv /opt/pg10/lib/postgresql/passwordcheck.so /opt/pg10/lib/postgresql/passwordcheck.so.bak
#拷贝重新编译后的SO文件
[root@test passwordcheck]# cp /opt/src/postgresql-10.14/contrib/passwordcheck/passwordcheck.so /opt/pg10/10/lib/postgresql/
复制
  1. 添加passwordcheck扩展,重启数据库
[root@test passwordcheck] vi /opt/pg10/data/postgresql.conf
shared_preload_libraries = '$libdir/passwordcheck'
[root@test passwordcheck] service postgresql-10 restart
复制
  1. 验证
#密码少于13位
postgres=# alter role test password '123456789abc';
ERROR:  password is too short
#密码太简单
postgres=# alter role test password '1111111111abc';
ERROR:  password is easily cracked
#设置为字典中排除的密码Twsm_20200917
postgres=# alter role test password 'Twsm_20200917';
ERROR:  password is easily cracked
复制

如果觉得上面的配置太复杂,也可以使用passwordcheck+cracklib的结合体扩展
https://github.com/devrimgunduz/passwordcheck_cracklib

参考:
https://www.postgresql.org/docs/10/passwordcheck.html
https://github.com/digoal/blog/blob/master/201410/20141009_01.md

最后修改时间:2020-11-13 08:38:00
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
1人已赞赏
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论