PostgreSQL security - don't use password method in pg_hba.conf

请不要在pg_hba.conf中配置客户端认证方法为password, 这样将会在网络中传输密码明文. 非常危险.

除非你用的是hostssl数据传输模式. 否则请至少要使用md5认证方法.

以下截取自pg_hba.conf

# METHOD can be "trust", "reject", "md5", "password", "gss", "sspi",
# "krb5", "ident", "peer", "pam", "ldap", "radius" or "cert".  Note that
# "password" sends passwords in clear text; "md5" is preferred since
# it sends encrypted passwords.

如果使用了password 配置, 那么认证过程中, 密码将以明文形式在网络中传输.

下面来测试一下 : 

1. hostnossl, password方法

vi pg_hba.conf
hostnossl all all 0.0.0.0/0 password
pg_ctl reload

抓包 : 

[root@db-172-16-3-33 libpq]# tcpdump -i eth0 host 172.16.3.39 -s 0 -w plain.dmp

连接 : 

pg92@db-172-16-3-39-> psql -h 172.16.3.33 -p 1999 -U postgres digoal
psql (9.2beta1, server 9.3devel)
WARNING: psql version 9.2, server version 9.3.
         Some psql features might not work.
Type "help" for help.

digoal=# \dt
        List of relations
 Schema | Name | Type  |  Owner
--------+------+-------+----------
 public | test | table | postgres
(1 row)
#查询
digoal=# select * from test limit 10;
 id |               info               |          crt_time
----+----------------------------------+----------------------------
  1 | 8c6488c425f041c8ed28514ef2985afd | 2013-05-22 20:55:42.940045
  2 | f92ecbe588516e2f59dc23b69305afc9 | 2013-05-22 20:55:42.940422
  3 | b98827408bdd1865757f8db7a7001111 | 2013-05-22 20:55:42.940435
  4 | 85911d5a2060917c7d98a1ed22ac3247 | 2013-05-22 20:55:42.940443
  5 | db863ff0911485f6fc58559b58b56042 | 2013-05-22 20:55:42.940451
  6 | 95636eb443f4925f310a2472edd2b064 | 2013-05-22 20:55:42.940458
  7 | ed7ca0280469fb1e3e497c33fc338978 | 2013-05-22 20:55:42.940466
  8 | 48cea37b756d00e4309db46152df3918 | 2013-05-22 20:55:42.940473
  9 | 04cd192c0500a0b76e9bbb3e3a31f416 | 2013-05-22 20:55:42.940493
 10 | a6a83937ffc053baa82cfbbed26b86ce | 2013-05-22 20:55:42.940502
(10 rows)

使用wireshark分析包 : 

密码postgres, 明文 : 


 

SQL, 明文 : 


 

结果, 明文 : 

2. hostnossl, md5方法

改成md5认证后, 抓包 :

vi pg_hba.conf
hostnossl all all 0.0.0.0/0 md5
pg_ctl reload

抓包 : 

[root@db-172-16-3-33 libpq]# tcpdump -i eth0 host 172.16.3.39 -s 0 -w md5.dmp

连接查询 : 

pg92@db-172-16-3-39-> psql -h 172.16.3.33 -p 1999 -U postgres digoal
psql (9.2beta1, server 9.3devel)
WARNING: psql version 9.2, server version 9.3.
         Some psql features might not work.
Type "help" for help.

digoal=# select * from test limit 10;
 id |               info               |          crt_time
----+----------------------------------+----------------------------
  1 | 8c6488c425f041c8ed28514ef2985afd | 2013-05-22 20:55:42.940045
  2 | f92ecbe588516e2f59dc23b69305afc9 | 2013-05-22 20:55:42.940422
  3 | b98827408bdd1865757f8db7a7001111 | 2013-05-22 20:55:42.940435
  4 | 85911d5a2060917c7d98a1ed22ac3247 | 2013-05-22 20:55:42.940443
  5 | db863ff0911485f6fc58559b58b56042 | 2013-05-22 20:55:42.940451
  6 | 95636eb443f4925f310a2472edd2b064 | 2013-05-22 20:55:42.940458
  7 | ed7ca0280469fb1e3e497c33fc338978 | 2013-05-22 20:55:42.940466
  8 | 48cea37b756d00e4309db46152df3918 | 2013-05-22 20:55:42.940473
  9 | 04cd192c0500a0b76e9bbb3e3a31f416 | 2013-05-22 20:55:42.940493
 10 | a6a83937ffc053baa82cfbbed26b86ce | 2013-05-22 20:55:42.940502
(10 rows)

抓包结束 : 

tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
31 packets captured
31 packets received by filter
0 packets dropped by kernel

使用wireshark分析包 : 

密码为md5加salt的二次加密后的md5值, 

salt : 


 

加密后的md5明文 : 


 

不是pg_shadow中存储的md5值 :

digoal=# select * from pg_shadow where usename='postgres';
 usename  | usesysid | usecreatedb | usesuper | usecatupd | userepl |               passwd                | valuntil | useconfig
----------+----------+-------------+----------+-----------+---------+-------------------------------------+----------+-----------
 postgres |       10 | t           | t        | t         | t       | md53175bce1d3201d16594cebf9d7eb3f9d |          |
(1 row)

SQL明文 : 


 

结果明文 : 


 

3. hostssl, password方法

改成hostssl, 但是依旧使用password方法.

vi pg_hba.conf
hostssl all all 0.0.0.0/0 password
#hostnossl all all 0.0.0.0/0 password
pg_ctl reload

ssl的配置参考 : 

http://blog.163.com/digoal@126/blog/static/163877040201342233131835/

抓包 :

[root@db-172-16-3-33 libpq]# tcpdump -i eth0 host 172.16.3.39 -s 0 -w ssl_plain.dmp

连接查询 : 

pg92@db-172-16-3-39-> psql -h 172.16.3.33 -p 1999 -U postgres digoal
psql (9.2beta1, server 9.3devel)
WARNING: psql version 9.2, server version 9.3.
         Some psql features might not work.
SSL connection (cipher: RC4-SHA, bits: 128)
Type "help" for help.

digoal=# select * from test limit 10;
 id |               info               |          crt_time
----+----------------------------------+----------------------------
  1 | 8c6488c425f041c8ed28514ef2985afd | 2013-05-22 20:55:42.940045
  2 | f92ecbe588516e2f59dc23b69305afc9 | 2013-05-22 20:55:42.940422
  3 | b98827408bdd1865757f8db7a7001111 | 2013-05-22 20:55:42.940435
  4 | 85911d5a2060917c7d98a1ed22ac3247 | 2013-05-22 20:55:42.940443
  5 | db863ff0911485f6fc58559b58b56042 | 2013-05-22 20:55:42.940451
  6 | 95636eb443f4925f310a2472edd2b064 | 2013-05-22 20:55:42.940458
  7 | ed7ca0280469fb1e3e497c33fc338978 | 2013-05-22 20:55:42.940466
  8 | 48cea37b756d00e4309db46152df3918 | 2013-05-22 20:55:42.940473
  9 | 04cd192c0500a0b76e9bbb3e3a31f416 | 2013-05-22 20:55:42.940493
 10 | a6a83937ffc053baa82cfbbed26b86ce | 2013-05-22 20:55:42.940502
(10 rows)

抓包结束 :

tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
21 packets captured
21 packets received by filter
0 packets dropped by kernel

使用wireshark 分析包 : 

密码, SQL, 查询结果全部被加密. 

图略.

[参考]

1. http://blog.163.com/digoal@126/blog/static/163877040201342233131835/

时间: 2024-09-19 09:18:58

PostgreSQL security - don't use password method in pg_hba.conf的相关文章

PostgreSQL 连接问题 FATAL: no pg_hba.conf entry for host

The server doesn't grant access to the database: the server reports FATAL: no pg_hba.conf entry for host "192.168.0.123", user "postgres", database "postgres" FATAL: no pg_hba.conf entry for host "192.168.0.123", us

PostgreSQL disable pg_hba.conf trust auth method

在PostgreSQL中有一项认证为trust认证, 使用该认证方法不需要输入密码即可登录数据库. 也就是说数据库服务器管理员只要有操作系统root权限, 很容易就可以登录数据库查询数据. 在比较高的安全场合, 可能需要禁止掉trust方法. 这样的话即使配置了trust认证方法也需要提供密码. 方法很简单, 只要修改一下hba.c即可. 如下. vi src/backend/libpq/hba.c if (strcmp(token->string, "trust") == 0)

PostgreSQL pg_hba.conf "search bind" configure Use LDAP auth

上一篇BLOG讲了一下在PostgreSQL的pg_hba.conf中配置ldap simple bind的认证方式. PostgreSQL还支持另一种bind方式. search+bind 相当于PostgreSQL server需要两次和ldap server交互. 交互过程如下. In the second mode, which we will call the search+bind mode, the server first binds to the LDAP directory

PostgreSQL 10.0 preview 功能增强 - 客户端ACL(pg_hba.conf动态视图)

标签 PostgreSQL , ACL , pg_hba.conf 背景 pg_hba.conf文件是用于控制客户端访问PostgreSQL数据库的防火墙配置(ACL),以往我们要了解数据库配置的ACL,必须打开这个文件进行查看. 例如 cat $PGDATA/pg_hba.conf # PostgreSQL Client Authentication Configuration File # ===================================================

PostgreSQL pg_hba.conf "simple bind" configure Use LDAP auth

上一篇BLOG介绍了OpenLDAP Server的配置,  http://blog.163.com/digoal@126/blog/static/163877040201454112329931/ 本文将要讲一下ldap client的配置, 以及PostgreSQL的ldap认证方法. ldap认证方法指的是通过ldap进行认证, 而非数据库本身的用户密码进行认证. 首先要确保PostgreSQL编译时带上了--with-ldap选项. 例如以下数据库不可用使用ldap认证, 因为带了--w

PostgreSQL 10.0 preview 变化 - 逻辑复制pg_hba.conf变化,不再使用replication条目

标签 PostgreSQL , 10.0 , 变化 , pg_hba.conf , replication , 逻辑复制 背景 pg_hba.conf的replication条目,在10.0后,将仅仅适用于物理复制. 逻辑复制使用普通DATABASE条目,但是逻辑复制的角色依旧需要带replication属性. 配置时请注意了. Change logical replication pg_hba.conf use Logical replication no longer uses the "r

Use LDAP store PostgreSQL Connection parameter & client use it with .pg_service.conf

前面几篇BLOG谈了一下PostgreSQL的用户密码认证可以通过LDAP 来做AUTH. 客户端提交用户和密码, PostgreSQL server根据提供客户端的用户, 以及pg_hba.conf中的配置, 到LDAP server查找匹配条目. 如果找到了匹配的话, 根据客户端提供的密码在LDAP server进行认证. 客户端只和PostgreSQL server交互, 认证部分由PostgreSQL server和LDAP server完成. 所以PostgreSQL server编译

PostgreSQL HOT STANDBY using Stream replication

案例解析二.PostgreSQL HOT STANDBY by stream replication 测试:一.准备硬件1. 主节点硬件配置DISK : 146GB*6MEM : 14GBCPU : 2.83GHz*82. standby节点硬件配置DISK : 146GB*4MEM : 8GBCPU : 2.0GHz*8 二.准备环境1. 系统Red Hat Enterprise Linux Server release 5.5 (Tikanga) x642. 时钟同步8 * * * * /u

postgresql简单搭建流复制

一.准备环境 准备两台相同环境的虚拟机,安装相同版本的pg. pg版本:9.3 master:192.168.23.128 slave:192.168.23.129 二.主库配置 1.创建备库访问用户: 修改postgresql.conf中的参数 listen_addresses='*'(默认值是localhost,改为*代表监听所有ip) 创建用户 CREATE USER repuser replication LOGIN CONNECTION LIMIT 5 ENCRYPTED PASSWO