过去几个月一直运行良好;突然开始注意到应用程序中的这个错误,
FATAL: pg_hba.conf rejects connection for host "127.0.0.1", user "postgres", database "prod", SSL off
pg_hba.conf 有,
# IPv4 local connections:
host all all 127.0.0.1/32 md5
host all all 0.0.0.0/0 md5
postgresql.conf 有,
listen_addresses = '*'
这两个文件已经好几个月没有被触及/更改了。
有人在运行环境中遇到过类似的问题吗?
我在 stoackoverflow 上经历了几个与连接相关的问题;但它们都指向这两个文件之一配置错误。在这种情况下,这不是问题所在。
找到并修复了根本原因。
这就是发生的事情(为了那些可能遇到这种奇怪问题的人的利益)
- 在 pg_hba.conf 中发现了三个神秘的整体,就在文件的顶部
- 这些为用户 postgres、pgsql 和 pgdbadm 配置了reject方法
- 我们的团队成员都没有添加它们
- 因为这些就在顶部,甚至在“# PostgreSQL Client Authentication Configuration File...”评论开始之前,我们都没有注意到它。
- 我还是不确定,这些是怎么出现的
- 这可能是一些升级问题 - 但我们还没有更新 Postgres
- 这可能是一次部分成功的黑客尝试 - 仍在调查中
- 但为了安全起见,我们更改了服务器凭据并研究了其他强化方法。
如果在完美运行的环境中出现此类问题,它可能会让某人度过一个不眠之夜。
最佳答案
我遇到了同样的问题。这是一个黑客。这里描述了同样的事情:
https://dba.stackexchange.com/questions/215834/postgres-9-6-10-pg-hba-conf-altered
我有一个新的管理员 postgres 用户“pgdbadm”,无法删除,因为 template1 数据库中的 3 个对象依赖于它。从 template0 恢复 template1 数据库后,我设法删除了用户。
pg_hba.conf 文件顶部有两条新规则:
host all postgres 0.0.0.0/0 reject
host all pgdbadm 0.0.0.0/0 md5
文件的其余部分是相同的。
通过在 pgAdmin 4 中执行以下操作,我成功地复制了完全没有 shell 访问权限的 hack:
-- creating a new table
create table test(a text);
-- inserting the contents of pg_hba.conf into the table
copy test from '/var/lib/pgsql/data/pg_hba.conf';
-- overwriting the pg_hba.conf file with the contents from the table prepended with one random rule (just to test it)
copy (select 'host all all 127.0.0.1/32 md5' union all select * from test) TO '/var/lib/pgsql/data/pg_hba.conf';
-- cleanup
drop table a;
-- reloading the server config
select pg_reload_conf();
当然,这是唯一可能的,因为 postgres 是在 AWS EC2 实例上设置的,所有端口都打开,默认 postgres 用户,默认端口,一个愚蠢的 super 容易猜到的密码:“asd123”。实例中的安全日志充满了尝试连接各种端口、使用各种用户名等,因此很可能是一次随机攻击。
关于postgresql - FATAL : pg_hba. conf 拒绝主机 "127.0.0.1"的连接,用户 "postgres",数据库 "prod",SSL 关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52197486/