Linux System Hardening & Defensive Auditing
Hardening transforms a default, permissive Linux installation into a secure host by enforcing the principle of least privilege, reducing network attack surfaces, locking down remote management interfaces, and implementing active log auditing.
Defense in Depth Architecture
Section titled “Defense in Depth Architecture”flowchart TD Internet["External Network Traffic"] --> UFW["1. Kernel Packet Filter (UFW / Netfilter)"] UFW --> SSH["2. Cryptographic Access Boundary (OpenSSH Ed25519)"] SSH --> RBAC["3. OS Privilege Controls (Sudoers / Least Privilege)"] RBAC --> Kernel["4. Filesystem & Kernel Protections (AppArmor / Perms)"]Host Firewall Configuration (UFW)
Section titled “Host Firewall Configuration (UFW)”The Uncomplicated Firewall (UFW) provides an interface for managing Linux iptables and nftables netfilter rules.
Safe Baseline Deployment
Section titled “Safe Baseline Deployment”# 1. Reset rules to a clean state (optional for fresh systems)sudo ufw default deny incomingsudo ufw default allow outgoing
# 2. Whitelist remote administration (rate-limited SSH)sudo ufw limit 22/tcp comment 'Rate-limited SSH Access'
# 3. Whitelist web traffic if hosting public web serverssudo ufw allow 80/tcp comment 'HTTP Service'sudo ufw allow 443/tcp comment 'HTTPS Service'
# 4. Enable the firewallsudo ufw enable
# 5. Review active rules with numbered indexsudo ufw status verboseAdvanced UFW Rules
Section titled “Advanced UFW Rules”# Allow traffic exclusively from a trusted administrative bastion subnetsudo ufw allow from 10.10.0.0/24 to any port 22 proto tcp comment 'Bastion SSH'
# Delete an obsolete rule by its index numbersudo ufw status numberedsudo ufw delete 3OpenSSH Daemon Hardening
Section titled “OpenSSH Daemon Hardening”The OpenSSH daemon configuration file (/etc/ssh/sshd_config) is the primary gatekeeper for remote shell access.
Recommended Configuration Directives
Section titled “Recommended Configuration Directives”Edit /etc/ssh/sshd_config or deploy a drop-in configuration file to /etc/ssh/sshd_config.d/99-hardened.conf:
# Disallow root login directly via SSHPermitRootLogin no
# Disable legacy, brute-forceable password authenticationPasswordAuthentication noPubkeyAuthentication yes
# Disallow empty passwordsPermitEmptyPasswords no
# Disallow graphical forwarding and agent forwarding unless requiredX11Forwarding noAllowAgentForwarding no
# Limit failed authentication attempts before disconnectingMaxAuthTries 3
# Idle timeout: disconnect after 10 minutes of inactivityClientAliveInterval 300ClientAliveCountMax 2
# Accept modern cryptographic key exchange and ciphers onlyKexAlgorithms curve25519-sha256,curve25519-sha256@libssh.org,diffie-hellman-group16-sha512Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.comMACs hmac-sha2-512-etm@openssh.comConfiguration Validation & Reload
Section titled “Configuration Validation & Reload”Always validate the configuration syntax before restarting or reloading the daemon:
# Test configuration syntax (-t triggers test mode)sudo sshd -t
# If syntax is valid (no errors returned), reload the service safelysudo systemctl reload ssh || sudo systemctl reload sshdAuthentication & Audit Logging
Section titled “Authentication & Audit Logging”Auditing authentication logs enables early detection of password spraying, brute-force attempts, and unauthorized privilege escalation.
Inspecting System Logs
Section titled “Inspecting System Logs”# View real-time authentication events via systemd journalsudo journalctl -u ssh -f
# Filter for failed login attempts on Debian / Kali / Ubuntusudo grep "Failed password" /var/log/auth.log | tail -n 25
# On RHEL / Rocky / CentOS systemssudo grep "Failed password" /var/log/secure | tail -n 25Filesystem Permission Audits
Section titled “Filesystem Permission Audits”Misconfigured permissions—such as world-writable configuration files or unauthorized SUID binaries—frequently enable local privilege escalation.
Locating Dangerously Permissive Files
Section titled “Locating Dangerously Permissive Files”# Find all world-writable regular files across the systemfind / -type f -perm -0002 2>/dev/null
# Find world-writable files specifically within system configuration directoriesfind /etc /usr/local/etc -type f -perm -o+w 2>/dev/null
# Find world-writable directories lacking the Sticky Bit (drwxrwxrwt)find / -type d -perm -0002 ! -perm -1000 2>/dev/nullAuditing Sudo Privileges & Insecure Wildcards
Section titled “Auditing Sudo Privileges & Insecure Wildcards”Inspect the /etc/sudoers file and /etc/sudoers.d/ directory for insecure rules:
# Check current user sudo rightssudo -l
# Safe inspection of sudoers file (always use visudo to avoid syntax corruption)sudo visudo -cDangerous sudoers anti-patterns to eliminate:
NOPASSWD: ALL(allows root compromise without credential verification).NOPASSWD: /usr/bin/findor/usr/bin/vim(GTFOBins escape vectors).- Insecure wildcards like
/bin/cat /home/*allowing path traversal (/home/../root/flag).