Postgres2015全国用户大会将于11月20至21日在北京丽亭华苑酒店召开。本次大会嘉宾阵容强大,国内顶级PostgreSQL数据库专家将悉数到场,并特邀欧洲、俄罗斯、日本、美国等国家和地区的数据库方面专家助阵:
|
![]() |
postgres=# \d pg_stat_statementsView "public.pg_stat_statements"Column | Type | Modifiers---------------------+------------------+-----------userid | oid |dbid | oid |queryid | bigint |query | text |calls | bigint |total_time | double precision |rows | bigint |shared_blks_hit | bigint |shared_blks_read | bigint |shared_blks_dirtied | bigint |shared_blks_written | bigint |local_blks_hit | bigint |local_blks_read | bigint |local_blks_dirtied | bigint |local_blks_written | bigint |temp_blks_read | bigint |temp_blks_written | bigint |blk_read_time | double precision |blk_write_time | double precision |
postgres@digoal-> pwd/data01/pg_root_1921/pg_stat_tmppostgres@digoal-> lltotal 28K-rw------- 1 postgres postgres 2.5K Sep 24 16:00 db_0.stat-rw------- 1 postgres postgres 9.6K Sep 24 16:00 db_151898.stat-rw------- 1 postgres postgres 607 Sep 24 16:00 global.stat-rw------- 1 postgres postgres 6.4K Sep 24 14:48 pgss_query_texts.stat停库后记录在这里postgres@digoal-> cd ../pg_stat
# yum install -y cracklib-devel cracklib-dicts cracklib
[root@db-172-16-3-221 cracklib]# 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
下载word文件http://sourceforge.net/projects/cracklib/files/cracklib-words/2008-05-07/(可以自行添加word进去)# cd /opt/soft_bak/# wget http://downloads.sourceforge.net/project/cracklib/cracklib-words/2008-05-07/cracklib-words-20080507.gz?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fcracklib%2Ffiles%2Fcracklib-words%2F2008-05-07%2F&ts=1412826278&use_mirror=nchc# tar -zxvf cracklib-words-20080507.gz# gunzip cracklib-words-20080507.gz[root@db-172-16-3-221 soft_bak]# less cracklib-words-20080507``!@#$%^&*()_+^^%$#@!~~!@~!@#~!@#~@!#创建字典文件[root@db-172-16-3-221 soft_bak]# create-cracklib-dict -hUsage: create-cracklib-dict [options] wordlist ...
This script takes one or more word list files as argumentsand converts them into cracklib dictionaries for useby password checking programs. The results are placed inthe default compiled-in dictionary location.
If you wish to store the dictionary in a different location,use the cracklib-format and cracklib-packer commands directly.
Options:-o, --outputAlternative output file for cracklib-packer -h, --help This help output
Example:create-cracklib-dict /usr/share/words[root@db-172-16-3-221 soft_bak]# create-cracklib-dict -o ./cracklib-dict ./cracklib-words-20080507skipping line: 11669426 1669425[root@db-172-16-3-221 soft_bak]# ll cracklib-dict.*-rw-r--r-- 1 root root 1024 Oct 9 12:00 cracklib-dict.hwm-rw-r--r-- 1 root root 7472513 Oct 9 12:00 cracklib-dict.pwd-rw-r--r-- 1 root root 417372 Oct 9 12:00 cracklib-dict.pwi
[root@db-172-16-3-221 cracklib]# cd /opt/soft_bak/postgresql-9.3.5/contrib/passwordcheck/[root@db-172-16-3-221 passwordcheck]# vi passwordcheck.c#ifdef USE_CRACKLIB#include // 如果是源码安装的cracklib, 可能需要修改如下, 本例不需要修改// #include "/opt/cracklib/include/crack.h"#endif/* passwords shorter than this will be rejected, 最小密码长度最好改成20或更大 */#define MIN_PWD_LENGTH 20
[root@db-172-16-3-221 passwordcheck]# vi Makefile# contrib/passwordcheck/Makefile# uncomment the following two lines to enable cracklib supportPG_CPPFLAGS = -DUSE_CRACKLIB '-DCRACKLIB_DICTPATH="/usr/share/cracklib/pw_dict"'# 修改字典文件 /usr/lib/cracklib_dictSHLIB_LINK = -lcrack
[root@db-172-16-3-221 passwordcheck]# make cleanrm -f passwordcheck.so libpasswordcheck.a libpasswordcheck.pcrm -f passwordcheck.o[root@db-172-16-3-221 passwordcheck]# makegcc -O2 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fpic -DUSE_CRACKLIB '-DCRACKLIB_DICTPATH="/usr/share/cracklib/pw_dict"' -I. -I. -I../../src/include -D_GNU_SOURCE -I/usr/include/libxml2 -c -o passwordcheck.o passwordcheck.cgcc -O2 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fpic -shared -o passwordcheck.so passwordcheck.o -L../../src/port -L../../src/common -Wl,--as-needed -Wl,-rpath,'/opt/pgsql9.3.5/lib',--enable-new-dtags -lcrack[root@db-172-16-3-221 passwordcheck]# make install/bin/mkdir -p '/opt/pgsql9.3.5/lib'/usr/bin/install -c -m 755 passwordcheck.so '/opt/pgsql9.3.5/lib/passwordcheck.so'
[root@db-172-16-3-221 passwordcheck]# su - postgrespostgres@db-172-16-3-221-> cd $PGDATApostgres@db-172-16-3-221-> vi postgresql.confshared_preload_libraries = 'passwordcheck'postgres@db-172-16-3-221-> pg_ctl restart -m fast
postgres@db-172-16-3-221-> psqlpsql (9.3.5)Type "help" for help.可以看到, 不符合密码强度(必须包含大小写, 非字符), 或者在密码文件中的密码都不允许使用.digoal=# alter role postgres encrypted password 'helloworld123';ERROR: password is easily crackeddigoal=# alter role postgres encrypted password 'helloworld';ERROR: password must contain both letters and nonlettersdigoal=# alter role postgres encrypted password 'hello';ERROR: password is too shortdigoal=# alter role postgres encrypted password 'postgres';ERROR: password must not contain user namedigoal=# alter role postgres encrypted password 'postgresql';ERROR: password must not contain user namedigoal=# alter role postgres encrypted password 'abcpostgreHAHAHA';ERROR: password must contain both letters and nonlettersdigoal=# alter role postgres encrypted password 'a_b_cpostgreHAHAHA';ERROR: password is too shortdigoal=# alter role postgres encrypted password 'a_b_cpostgreHAHAHAHAHAH';ALTER ROLE
[root@db-172-16-3-221 auth_delay]# cd /opt/soft_bak/postgresql-9.3.5/contrib/auth_delay/[root@db-172-16-3-221 auth_delay]# gmake cleanrm -f auth_delay.so auth_delay.o[root@db-172-16-3-221 auth_delay]# gmakegcc -O2 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fpic -I. -I. -I../../src/include -D_GNU_SOURCE -I/usr/include/libxml2 -c -o auth_delay.o auth_delay.cgcc -O2 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fpic -L../../src/port -L../../src/common -Wl,--as-needed -Wl,-rpath,'/opt/pgsql9.3.5/lib',--enable-new-dtags -shared -o auth_delay.so auth_delay.o[root@db-172-16-3-221 auth_delay]# gmake install/bin/mkdir -p '/opt/pgsql9.3.5/lib'/usr/bin/install -c -m 755 auth_delay.so '/opt/pgsql9.3.5/lib/'
[root@db-172-16-3-221 auth_delay]# su - postgrespostgres@db-172-16-3-221-> cd $PGDATApostgres@db-172-16-3-221-> vi postgresql.confshared_preload_libraries = 'auth_delay,passwordcheck'auth_delay.milliseconds = 5000
postgres@db-172-16-3-221-> pg_ctl restart -m fastpostgres@db-172-16-3-221-> psql -h 172.16.3.221 -U postgres postgresPassword for user postgres: 密码输入错误后, 需要等待5秒返回认证失败. 防止暴力破解密码.psql: FATAL: password authentication failed for user "postgres"
digoal=# alter role postgres valid until '2015-01-01';ALTER ROLEdigoal=# \duList of rolesRole name | Attributes | Member of-----------+------------------------------------------------+-----------postgres | Superuser, Create role, Create DB, Replication+| {}| Password valid until 2015-01-01 00:00:00+08 |
postgres@db-172-16-3-221-> psqlpsql (9.3.5)Type "help" for help.digoal=# alter role postgres encrypted password 'a_b_cpostgreHAHAHAHAHAH';ALTER ROLE
postgres@db-172-16-3-221-> cdpostgres@db-172-16-3-221-> less .psql_historyalter role postgres encrypted password 'a_b_cpostgreHAHAHAHAHAH';\q
postgres@db-172-16-3-221-> cd $PGDATA/pg_log2014-10-09 09:30:53.277 CST,"postgres","digoal",36441,"[local]",5435e54c.8e59,3,"idle",2014-10-09 09:30:52 CST,2/76,0,LOG,00000,"statement: alter role postgres encrypted password 'a_b_cpostgreHAHAHAHAHAH';",,,,,,,,"exec_simple_query, postgres.c:890","psql"
if (encrypted != TRI_NO){char *encrypted_password;
encrypted_password = PQencryptPassword(newpassword,newuser);if (!encrypted_password){fprintf(stderr, _("Password encryption failed.\n"));exit(1);}appendStringLiteralConn(&sql, encrypted_password, conn);PQfreemem(encrypted_password);}
/** PQencryptPassword -- exported routine to encrypt a password** This is intended to be used by client applications that wish to send* commands like ALTER USER joe PASSWORD 'pwd'. The password need not* be sent in cleartext if it is encrypted on the client side. This is* good because it ensures the cleartext password won't end up in logs,* pg_stat displays, etc. We export the function so that clients won't* be dependent on low-level details like whether the enceyption is MD5* or something else.** Arguments are the cleartext password, and the SQL name of the user it* is for.** Return value is a malloc'd string, or NULL if out-of-memory. The client* may assume the string doesn't contain any special characters that would* require escaping.*/char *PQencryptPassword(const char *passwd, const char *user){char *crypt_pwd;
crypt_pwd = malloc(MD5_PASSWD_LEN + 1);if (!crypt_pwd)return NULL;
if (!pg_md5_encrypt(passwd, user, strlen(user), crypt_pwd)){free(crypt_pwd);return NULL;}
return crypt_pwd;}
/** Computes MD5 checksum of "passwd" (a null-terminated string) followed* by "salt" (which need not be null-terminated).** Output format is "md5" followed by a 32-hex-digit MD5 checksum.* Hence, the output buffer "buf" must be at least 36 bytes long.** Returns TRUE if okay, FALSE on error (out of memory).*/boolpg_md5_encrypt(const char *passwd, const char *salt, size_t salt_len,char *buf){size_t passwd_len = strlen(passwd);
/* +1 here is just to avoid risk of unportable malloc(0) */char *crypt_buf = malloc(passwd_len + salt_len + 1);bool ret;
if (!crypt_buf)return false;
/** Place salt at the end because it may be known by users trying to crack* the MD5 output.*/memcpy(crypt_buf, passwd, passwd_len);memcpy(crypt_buf + passwd_len, salt, salt_len);
strcpy(buf, "md5");ret = pg_md5_hash(crypt_buf, passwd_len + salt_len, buf + 3);
free(crypt_buf);
return ret;}