Learn Labs
Security & Ops

Security Lab: Attacking the Controls

TLS-only pg_hba, a local-only superuser, least-privilege roles, SCRAM, and encrypted backups, each checked by trying to break it

A security setting you haven't tried to get past is a guess. labs/security.py makes 17 attempts that should fail, or checks a property that should hold, and prints what actually happened.

The controls

pg_hba:first match wins- local all postgres peersuperuser: only on the node itself- local all all scram-sha-256- hostssl replication replicator 0.0.0.0/0 scram-sha-256- host all postgres 0.0.0.0/0 rejectno superuser over the network- hostssl all all 0.0.0.0/0 scram-sha-256TLS + SCRAM for everyone else- host all all 0.0.0.0/0 rejectand nothing without TLS

And roles, created by config/post_bootstrap.sh when the cluster is born:

  • app_owner owns the app schema and runs migrations (DDL).
  • app is what the application logs in as: SELECT, INSERT, UPDATE, DELETE on the tables app_owner creates (through ALTER DEFAULT PRIVILEGES), and nothing else.
  • app_ro is read only, for reporting.
  • exporter is a member of pg_monitor, for metrics.
  • postgres, the superuser, can only log in over the local socket: an operator needs docker exec (in production, SSH to the node), not just a password.

The run

docker compose -f compose_prod.yaml run --rm labs python security.py

1. Connections
 [ok] plaintext connection (sslmode=disable) is refused
        -> FATAL:  pg_hba.conf rejects connection for host ...
 [ok] superuser login over the network is refused
        -> FATAL:  pg_hba.conf rejects connection for host ...
 [ok] wrong password is refused
        -> FATAL:  SASL authentication failed
 [ok] the session is encrypted (pg_stat_ssl)
        -> ('TLSv1.3', 'TLS_AES_256_GCM_SHA384', 256)
 [ok] server certificate is verified against the lab CA (verify-full)
2. Least privilege
 [ok] app cannot create tables               -> permission denied for schema app
 [ok] app cannot drop the owner's table      -> must be owner of table secrets_test
 [ok] app cannot create objects in schema public -> permission denied for schema public
 [ok] app can write data                     -> inserted
 [ok] app_ro cannot write                    -> permission denied for table secrets_test
 [ok] app cannot read password hashes (pg_authid)
 [ok] app cannot call PgBouncer's auth lookup -> permission denied for schema pgbouncer
 [ok] the auth lookup never returns superusers -> 0 rows for 'postgres'
 [ok] passwords are stored as SCRAM-SHA-256  -> SCRAM-SHA-256$
3. Backups
 [ok] archived WAL in the bucket is encrypted
        -> ...zst: starts with b'Salted__', 8.000 bits/byte
 [ok] the pgbackrest key cannot read other buckets -> AccessDenied
 [ok] the backup repository is reached over TLS only (plain HTTP to the gateway fails)

17 of 17 checks passed
attemptpostgressuperuserapp_ownermigrationsappapplicationapp_roreportingconnect without TLS✗✗✗✗log in over the network✗✓✓✓CREATE / DROP TABLE✓✓✗✗INSERT / UPDATE data✓✓✓✗SELECT data✓✓✓✓read pg_authid✓✗✗✗

A few results deserve a closer look:

  • The application can't drop its own tables. If an attacker gets SQL injection in the app, they get app's rights: they can read and change data, which is bad enough, but not DROP TABLE, not create functions, not touch other schemas. Migrations run as app_owner, from the deploy pipeline, not from the running app.
  • public is closed. Since Postgres 15 ordinary users can't create objects in the public schema by default. On a database upgraded from 14 or earlier, that permission is still there: REVOKE CREATE ON SCHEMA public FROM PUBLIC.
  • The backup is ciphertext. The lab downloaded an archived WAL file from SeaweedFS with pgBackRest's own S3 key. It begins with Salted__ (OpenSSL's encrypted format) instead of zstd's magic bytes, and its bytes are as random as random gets: 8.000 bits per byte. Whoever gets the bucket gets nothing without the passphrase.
  • The backup key is scoped. pgBackRest's S3 identity can read and write pg-backups and nothing else (AccessDenied on app-data), so a compromised database node can't read application files, and a leaked application key can't read backups.

On this page