Kahibaro
Discord Login Register

Database hardening

Why Database Hardening Matters

Databases store the most sensitive data on a server (credentials, customer data, logs, financial info). Because of that, they are prime targets. Hardening is everything you do to reduce the attack surface and limit damage if an attacker gets in.

In this chapter, “database” mainly means MySQL/MariaDB and PostgreSQL, but the principles apply to most RDBMS.

Key goals:

Threat Model and Principles

Before tweaking configs, think about typical threats:

Basic principles you’ll follow:

Network Exposure and Access Control

Bind Addresses and Listening Ports

Databases should not listen on all interfaces unless absolutely required.

MySQL/MariaDB example (usually /etc/mysql/my.cnf or /etc/my.cnf):

[mysqld]
bind-address = 127.0.0.1
port = 3306

PostgreSQL example (postgresql.conf):

listen_addresses = 'localhost'
port = 5432

Guidelines:

Firewalls and Network Segmentation

Use the host firewall and, where possible, network segmentation:

Example with ufw:

sudo ufw allow from 10.0.1.20 to any port 5432 proto tcp   # app server
sudo ufw deny 5432/tcp

Authentication Hardening

Account Design

Avoid using a single all-powerful account for everything.

Create:

Never let the application use:

These should be reserved for maintenance and emergency tasks.

Password Policies and Authentication Methods

Basic password hardening:

MySQL/MariaDB (plugin-based password policies):

INSTALL PLUGIN validate_password SONAME 'validate_password.so';
SET GLOBAL validate_password.length = 14;
SET GLOBAL validate_password.policy = MEDIUM;

PostgreSQL authentication is controlled in pg_hba.conf; choose strong methods (scram-sha-256 where available) instead of md5 or weaker.

Example pg_hba.conf line:

# TYPE  DATABASE USER    ADDRESS        METHOD
host    appdb    appusr  10.0.1.20/32   scram-sha-256

Limiting Login Sources

Restrict accounts to specific hosts or networks.

MySQL/MariaDB:

Example:

CREATE USER 'appusr'@'10.0.1.20' IDENTIFIED BY 'StrongPassword';
GRANT SELECT, INSERT, UPDATE, DELETE ON appdb.* TO 'appusr'@'10.0.1.20';

PostgreSQL:

Authorization and Least Privilege

Principle of Least Privilege

Grant only what the role needs:

MySQL/MariaDB:

GRANT SELECT, INSERT, UPDATE, DELETE
ON appdb.*
TO 'appusr'@'10.0.1.20';

PostgreSQL (using roles):

CREATE ROLE app_readwrite LOGIN PASSWORD 'StrongPassword';
GRANT CONNECT ON DATABASE appdb TO app_readwrite;
GRANT USAGE ON SCHEMA public TO app_readwrite;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO app_readwrite;

Avoiding Superusers

Only dedicated DBA accounts should have:

Check for overly-privileged accounts regularly:

MySQL/MariaDB:

SELECT user, host, Super_priv, Grant_priv
FROM mysql.user
ORDER BY Super_priv DESC;

PostgreSQL:

SELECT rolname, rolsuper, rolreplication, rolcreaterole, rolcreatedb
FROM pg_roles
ORDER BY rolsuper DESC;

Remove or reduce privileges where possible.

Separation of Duties

In larger environments, use separate roles:

Secure Configuration

Disabling Unused Features

Review and disable:

MySQL/MariaDB example:

[mysqld]
local_infile = 0

PostgreSQL:

Secure Defaults and Limits

Set conservative limits to reduce impact of abuse:

PostgreSQL examples (postgresql.conf):

max_connections = 200
statement_timeout = '1min'
idle_in_transaction_session_timeout = '5min'

MySQL/MariaDB examples:

[mysqld]
max_connections = 200
wait_timeout = 600

Ensure error messages do not leak sensitive information (schema names, internal details) to untrusted clients; handle detailed error logging server-side.

Encryption: In Transit and At Rest

Encryption in Transit (TLS)

Enable TLS between clients and the database:

MySQL/MariaDB (my.cnf):

[mysqld]
ssl-ca   = /etc/mysql/certs/ca.pem
ssl-cert = /etc/mysql/certs/server-cert.pem
ssl-key  = /etc/mysql/certs/server-key.pem

Then, for users:

CREATE USER 'appusr'@'10.0.1.20'
  IDENTIFIED BY 'StrongPassword'
  REQUIRE SSL;

PostgreSQL (postgresql.conf):

ssl = on
ssl_cert_file = 'server.crt'
ssl_key_file  = 'server.key'

pg_hba.conf can require hostssl (SSL-only connections):

hostssl appdb appusr 10.0.1.20/32 scram-sha-256

Use current TLS versions and secure cipher suites.

Encryption at Rest

Common approaches:

Hardening points:

Logging, Auditing, and Monitoring

Database Logs

Enable and protect logs:

MySQL/MariaDB examples:

[mysqld]
general_log = OFF
slow_query_log = ON
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 2
log_error = /var/log/mysql/error.log

Consider:

Auditing Data Access

Audit capabilities vary by engine, but you aim to track:

For engines lacking built-in auditing, you can:

Monitoring and Alerting

Hook the DB into your monitoring stack:

Combine DB metrics with system-level monitoring from the rest of your Linux stack.

Backup, Restore, and Replication Security

Securing Backup Processes

Backups often contain full database contents and must be hardened:

Example MySQL/MariaDB backup account:

CREATE USER 'backup'@'10.0.2.10' IDENTIFIED BY 'StrongBackupPassword';
GRANT SELECT, SHOW VIEW, EVENT, TRIGGER, LOCK TABLES
  ON *.* TO 'backup'@'10.0.2.10';

Securing Replication

Replication users often have broad privileges:

MySQL/MariaDB example:

CREATE USER 'repl'@'10.0.2.%' IDENTIFIED BY 'StrongReplPassword';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'10.0.2.%';

Protect configuration files that contain these credentials (chmod 600 where appropriate).

OS-Level Hardening Around the Database

Although this chapter is about the DB layer, a few OS-level measures are critical specifically for databases:

Example /etc/passwd entry for a service account:

mysql:x:27:27:MySQL Server:/var/lib/mysql:/usr/sbin/nologin

Secure Deployment and Maintenance Practices

Configuration Management

Use configuration management and version control for:

Benefits:

Patching and Upgrades

Keep your database engine patched:

Baseline and Regular Reviews

Hardening is not “set and forget”:

Testing Your Hardening

To validate hardening:

Testing should be done in non-production first, then carefully in production with clear rollback steps.


Database hardening is about layering controls: network, authentication, authorization, configuration, encryption, logging, and operational discipline. Properly applied, these controls significantly reduce both the likelihood and impact of database compromise.

Views: 26

Comments

Please login to add a comment.

Don't have an account? Register now!