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

本文涉及的产品
云原生数据库 PolarDB PostgreSQL 版,标准版 2核4GB 50GB
云原生数据库 PolarDB MySQL 版,通用型 2核4GB 50GB
简介:
请不要在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, 明文 : 
PostgreSQL security - dont use password method in pg_hba.conf - 德哥@Digoal - PostgreSQL
 
SQL, 明文 : 
PostgreSQL security - dont use password method in pg_hba.conf - 德哥@Digoal - PostgreSQL
 
结果, 明文 : 
PostgreSQL security - dont use password method in pg_hba.conf - 德哥@Digoal - PostgreSQL

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 : 
PostgreSQL security - dont use password method in pg_hba.conf - 德哥@Digoal - PostgreSQL
 
加密后的md5明文 : 
PostgreSQL security - dont use password method in pg_hba.conf - 德哥@Digoal - PostgreSQL
 
不是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明文 : 
PostgreSQL security - dont use password method in pg_hba.conf - 德哥@Digoal - PostgreSQL
 
结果明文 : 
PostgreSQL security - dont use password method in pg_hba.conf - 德哥@Digoal - PostgreSQL
 

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的配置参考 : 

抓包 :
[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, 查询结果全部被加密. 
图略.

[参考]
相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
存储 网络协议 安全
|
关系型数据库 PostgreSQL
PostgreSQL sharding : citus 系列7 - topn 加速(count(*) group by order by count(*) desc limit x) (use 估值插件 topn)
标签 PostgreSQL , topn , topn.number_of_counters , count(*) group by order by count(*) desc limit x 背景 count(*) group by order by count(*) desc limit x 用来统计 topn。
1431 0
|
关系型数据库 PostgreSQL
PostgreSQL sharding : citus 系列6 - count(distinct xx) 加速 (use 估值插件 hll|hyperloglog)
标签 PostgreSQL , hll , hyperloglog , distinct , 加速 , citus.count_distinct_error_rate 背景 在分布式数据库中,计算count(distinct xxx),需要对distinct 的字段, 1、去重, 2、重分布去重后的数据,(这一步,如果distinct值特别多,那么就会比较耗时) 3、然后再去重, 4、最后count (xxx), 5、求所有节点的count SUM。
1834 0
|
弹性计算 关系型数据库 测试技术
PostgreSQL 11 tpcc 测试(103万tpmC on ECS) - use sysbench-tpcc by Percona-Lab
标签 PostgreSQL , tpcc 背景 环境 阿里云虚拟机 [root@pg11-test ~]# lscpu Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: ...
4388 0
|
网络协议 关系型数据库 网络安全
PostgreSQL 10.1 手册_部分 III. 服务器管理_第 20 章 客户端认证_20.1. pg_hba.conf文件
20.1. pg_hba.conf文件 客户端认证是由一个配置文件(通常名为pg_hba.conf并被存放在数据库集簇目录中)控制(HBA表示基于主机的认证)。在initdb初始化数据目录时,它会安装一个默认的pg_hba.conf文件。
1629 0
|
安全 关系型数据库 数据库
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.
5869 0