PostgreSQL 拒绝服务DDOS攻击与防范

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
云原生数据库 PolarDB 分布式版,标准版 2核8GB
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 标签PostgreSQL , ddos , 拒绝服务 , 锁 , SLOT背景连接数据库的过程中,需要数据库有足够的SLOT(连接槽,通过max_connections配置),认证。如果把连接槽位占用,或者在认证过程加锁(使得认证过程被锁),则可以制造DDOS攻击。

标签

PostgreSQL , ddos , 拒绝服务 , 锁 , SLOT


背景

连接数据库的过程中,需要数据库有足够的SLOT(连接槽,通过max_connections配置),认证。如果把连接槽位占用,或者在认证过程加锁(使得认证过程被锁),则可以制造DDOS攻击。

占用连接槽的攻击与防范方法:

《PostgreSQL 连接攻击(类似DDoS)》

认证过程加锁的攻击方法,本文会提到。

https://paquier.xyz/postgresql-2/postgres-12-dos-prevention/

https://www.postgresql.org/message-id/152512087100.19803.12733865831237526317@wrigleys.postgresql.org

BUG #15182: Canceling authentication due to timeout aka Denial of Service Attack  
From:	PG Bug reporting form <noreply(at)postgresql(dot)org>  
To:	pgsql-bugs(at)lists(dot)postgresql(dot)org  
Cc:	lalbin(at)scharp(dot)org  
Subject:	BUG #15182: Canceling authentication due to timeout aka Denial of Service Attack  
Date:	2018-04-30 20:41:11  
Message-ID:	152512087100.19803.12733865831237526317@wrigleys.postgresql.org  
Views:	Raw Message | Whole Thread | Download mbox  
Thread:	  
Lists:	pgsql-bugs pgsql-hackers  
The following bug has been logged on the website:  
  
  
  
Bug reference:      15182  
Logged by:          Lloyd Albin  
Email address:      lalbin(at)scharp(dot)org  
PostgreSQL version: 10.3  
Operating system:   OpenSUSE  
Description:          
  
  
  
Over the last several weeks our developers caused a Denial of Service Attack  
against ourselves by accident. When looking at the log files, I noticed that  
we had authentication timeouts during these time periods. In researching the  
problem I found this is due to locks being held on shared system catalog  
items, aka system catalog items that are shared between all databases on the  
same cluster/server. This can be caused by beginning a long running  
transaction that queries pg_stat_activity, pg_roles, pg_database, etc and  
then another connection that runs either a REINDEX DATABASE, REINDEX SYSTEM,  
or VACUUM FULL. This issue is of particular importance to database resellers  
who use the same cluster/server for multiple clients, as two clients can  
cause this issue to happen inadvertently or a single client can either cause  
it to happen maliciously or inadvertently. Note: The large cloud providers  
give each of their clients their own cluster/server so this will not affect  
across cloud clients but can affect an individual client. The problem is  
that traditional hosting companies will have all clients from one or more  
web servers share the same PostgreSQL cluster/server. This means that one or  
two clients could inadvertently stop all the other clients from being able  
to connect to their databases until the first client does either a COMMIT or  
ROLLBACK of their transaction which they could hold open for hours, which is  
what happened to us internally.  
  
  
  
In Connection 1 we need to BEGIN a transaction and then query a shared  
system item; pg_authid, pg_database, etc; or a view that depends on a shared  
system item; pg_stat_activity, pg_roles, etc. Our developers were accessing  
pg_roles.  
  
  
  
Connection 1 (Any database, Any User)  
BEGIN;  
SELECT * FROM pg_stat_activity;  
  
  
  
Connection 2 (Any database will do as long as you are the database owner)  
REINDEX DATABASE postgres;  
  
  
  
Connection 3 (Any Database, Any User)  
psql -h sqltest-alt -d sandbox  
  
  
  
All future Connection 3's will hang for however long the transaction in  
Connection 1 runs. In our case this was hours and denied everybody else the  
ability to log into the server until Connection 1 was committed. psql will  
just hang for hours, even overnight in my testing, but our apps would get  
the "Canceling authentication due to timeout" after 1 minute.  
  
  
  
Connection 2 can also do any of these commands to also cause the same  
issue:  
REINDEX SYSTEM postgres;  
VACUUM FULL pg_authid;  
vacuumdb -f -h sqltest-alt -d lloyd -U lalbin  
  
  
  
Even worse is that the VACUUM FULL pg_authid; can be started by an  
unprivileged user and it will wait for the AccessShareLock by connection 1  
to be released before returning the error that you don't have permission to  
perform this action, so even an unprivileged user can cause this to happen.  
The privilege check needs to happen before the waiting for the  
AccessExclusiveLock happens.  
  
  
  
This bug report has been simplified and shorted drastically. To read the  
full information about this issue please see my blog post:  
http://lloyd.thealbins.com/Canceling%20authentication%20due%20to%20timeout  
  
  
  
Lloyd Albin  
Database Administrator  
Statistical Center for HIV/AIDS Research and Prevention (SCHARP)  
Fred Hutchinson Cancer Research Center  

复现方法如上。

防范

1、对于连接占用DDOS攻击的防范(1,设置认证超时参数。2,不要在公网监听。3,设置网络层防火墙。)

2、对于锁攻击(通常是无意识攻击),建议在操作大锁的SQL前,加锁超时,或者语句超时(尽量减少等待时长)。 (lock_timeout, statement_timeout都可以)

参考

《PostgreSQL 锁等待监控 珍藏级SQL - 谁堵塞了谁》

《PostgreSQL 设置单条SQL的执行超时 - 防雪崩》

《如何防止数据库雪崩(泛洪 flood)》

https://paquier.xyz/postgresql-2/postgres-12-dos-prevention/

《PostgreSQL 连接攻击(类似DDoS)》

相关文章
|
28天前
|
运维 安全 网络安全
DDoS攻击升级,解读防御DDoS攻击的几大有效方法
DDoS攻击升级,解读防御DDoS攻击的几大有效方法
31 0
|
2月前
|
存储 人工智能 安全
DDoS攻击激增,分享高效可靠的DDoS防御方案
DDoS攻击激增,分享高效可靠的DDoS防御方案
54 0
|
12天前
|
存储 安全 应用服务中间件
解密Nginx限流机制:有效应对DDoS攻击与高并发流量
解密Nginx限流机制:有效应对DDoS攻击与高并发流量
22 0
|
1月前
|
Linux 网络安全 Windows
如何通过隐藏服务器真实IP来防御DDOS攻击
如何通过隐藏服务器真实IP来防御DDOS攻击
|
1月前
|
运维 安全 网络安全
一文读懂DDoS,分享防御DDoS攻击的几大有效方法
一文读懂DDoS,分享防御DDoS攻击的几大有效方法
34 0
|
2月前
|
安全 应用服务中间件 网络安全
遭遇DDOS攻击忍气吞声?立刻报警!首都网警重拳出击,犯罪分子无所遁形
公元2024年2月24日18时许,笔者的个人网站突然遭遇不明身份者的DDOS攻击,且攻击流量已超过阿里云DDos基础防护的黑洞阈值,服务器的所有公网访问已被屏蔽,由于之前早已通过Nginx屏蔽了所有国外IP,在咨询了阿里云客服之后,阿里网安的老同事帮助分析日志并进行了溯源,客服建议笔者选择立刻报警处理! 我国《刑法》二百八十六条规定,违反国家规定,对计算机信息系统功能进行删除、修改、增加、干扰,造成计算机信息系统不能正常运行,后果严重的,处五年以下三年以上有期徒刑或者拘役;后果特别严重的,处五年以上有期徒刑。
遭遇DDOS攻击忍气吞声?立刻报警!首都网警重拳出击,犯罪分子无所遁形
|
4月前
|
监控 安全 网络安全
深度解析:DDoS攻击与先进防御策略
DDoS 介绍 DDoS(分布式拒绝服务)攻击是一种恶意网络活动,旨在通过同时向目标系统发送大量请求或流量,使其无法正常运行或提供服务。攻击者通常利用网络上的多个计算机和设备,形成一个"僵尸网络"或"僵尸军团",并协调这些设备以集中地向目标发动攻击。 DDoS 攻击理论 目标系统(Target System):DDoS攻击的目标是一个网络服务、网站、服务器或应用程序,攻击旨在使其无法正常运行,从而造成服务中断。 攻击者(Attackers):攻击者是发起DDoS攻击的个人、组织或恶意软件的开发者。他们试图通过制造大量的流量来超过目标系统的处理能力。
108 0
|
4月前
|
云安全 负载均衡 监控
遇到攻击怎么办,有什么办法解决网络层和应用层的DDoS攻击
解决网络层和应用层的DDoS攻击,应对网络安全的挑战,保护数字化信息时代的网络安全提供有效的安全解决方案。
|
5月前
|
云安全 负载均衡 安全
掌握抗DDoS攻击,守护网络服务无懈可击!
抗DDoS攻击是一种防御措施,旨在保护网络服务免受分布式拒绝服务(DDoS)攻击的影响。DDoS攻击是一种常见的网络攻击,通过发送大量无用的请求来耗尽网络资源,从而使得合法用户无法访问受攻击的网络服务。
57 0
|
5月前
|
网络安全

相关产品

  • 云原生数据库 PolarDB