39. Security Auditing
This chapter covers authentication auditing, kernel event monitoring with Linux Audit Framework (auditd), permission reconnaissance, and network exposure auditing.
39.1 Login & Auth Logs
Section titled “39.1 Login & Auth Logs”Linux tracks all interactive logins, session durations, reboot events, and failed authentication attempts in binary accounting files.
1. Inspecting Authentication Logs
Section titled “1. Inspecting Authentication Logs”Raw text logs record authentication attempts in real-time:
- Debian / Ubuntu:
/var/log/auth.log - RHEL / Rocky / AlmaLinux:
/var/log/secure
# Real-time monitoring of authentication attemptssudo tail -f /var/log/auth.log # or /var/log/secure
# Identify brute-force SSH attempts (failed passwords)sudo grep "Failed password" /var/log/auth.log | awk '{print $(NF-3)}' | sort | uniq -c | sort -nr | head -n 15
# View successful sudo elevations and executed commandssudo grep "COMMAND=" /var/log/auth.log | tail -n 202. Historical Login Accounting: last, lastb & lastlog
Section titled “2. Historical Login Accounting: last, lastb & lastlog”| Utility | Source File | Purpose | Example |
|||||
| last | /var/log/wtmp | Shows historical record of successful user logins, logouts, TTYs, IP addresses, and system reboots | last -n 20 |
| lastb | /var/log/btmp | Shows all bad / failed login attempts (root privileges required) | sudo lastb -n 25 |
| lastlog | /var/log/lastlog | Displays the most recent login date and time for every user account on the system | lastlog |
# Check recent system rebootslast reboot
# Check failed login attempts grouped by IP to detect attack sourcessudo lastb | awk '{print $3}' | sort | uniq -c | sort -nr | head -n 10
# Audit accounts that have never logged inlastlog | grep "**Never logged in**"39.2 Linux Audit Framework (auditd)
Section titled “39.2 Linux Audit Framework (auditd)”While syslog captures application logs, the Linux Audit Framework (auditd) is an enterprise-grade kernel subsystem that logs low-level system calls, file access, permission alterations, and command executions with non-repudiable audit trails.
1. Architecture
Section titled “1. Architecture” User Space / System Calls (execve, open, unlink) ↓ Linux Kernel (Audit Subsystem Filter) ↓ auditd Daemon (/etc/audit/auditd.conf) ↓ Log File (/var/log/audit/audit.log) ↓ Analysis Tools (ausearch, aureport)2. Installing & Enabling auditd
Section titled “2. Installing & Enabling auditd”- Ubuntu / Debian:
Terminal window sudo apt install auditd audispd-plugins -ysudo systemctl enable --now auditd - RHEL / AlmaLinux:
Terminal window sudo dnf install audit -ysudo systemctl enable --now auditd
3. Writing Audit Rules (auditctl & /etc/audit/rules.d/)
Section titled “3. Writing Audit Rules (auditctl & /etc/audit/rules.d/)”Audit rules are defined dynamically via auditctl or persistently in /etc/audit/rules.d/audit.rules.
Rule 1: Watch Sensitive Configuration Files (-w)
Section titled “Rule 1: Watch Sensitive Configuration Files (-w)”Monitor any read, write, execute, or attribute changes to /etc/passwd and /etc/shadow:
# -w path, -p permissions (r=read, w=write, x=execute, a=attribute), -k tag/keysudo auditctl -w /etc/shadow -p wa -k shadow_modificationsudo auditctl -w /etc/sudoers -p wa -k sudoers_modificationRule 2: Watch Directory Changes
Section titled “Rule 2: Watch Directory Changes”Monitor all changes inside /etc/ssh/:
sudo auditctl -w /etc/ssh/ -p wa -k sshd_config_changeRule 3: Track Execution of Specific System Calls
Section titled “Rule 3: Track Execution of Specific System Calls”Audit every time an unprivileged user executes a command with elevated privileges via execve:
sudo auditctl -a always,exit -F arch=b64 -S execve -F euid=0 -F auid>=1000 -F auid!=4294967295 -k root_actionView Active Audit Rules:
Section titled “View Active Audit Rules:”sudo auditctl -l4. Querying Audit Logs: ausearch & aureport
Section titled “4. Querying Audit Logs: ausearch & aureport”Never grep /var/log/audit/audit.log manually; it contains raw hexadecimal and numeric timestamps. Use the native audit parsers:
ausearch (Search Events)
Section titled “ausearch (Search Events)”# Search for events matching our custom key (-k)sudo ausearch -k shadow_modification
# Search for all failed system calls todaysudo ausearch -m SYSCALL -sv no -ts today
# Search for commands executed by a specific user (UID 1001)sudo ausearch -ua 1001 -iThe -i flag automatically translates UIDs to usernames, syscall numbers to names, and timestamps to human-readable dates!
aureport (Summary Reports)
Section titled “aureport (Summary Reports)”Generates high-level compliance summaries:
# Generate report of all executable launchessudo aureport -x --summary
# Generate report of authentication attemptssudo aureport -au
# Generate report of failed eventssudo aureport --failed39.3 File & Permission Auditing
Section titled “39.3 File & Permission Auditing”Attackers maintain persistence by leaving hidden SUID binaries or writable cron jobs.
1. SUID / SGID Reconnaissance
Section titled “1. SUID / SGID Reconnaissance”Find all binaries that execute with root privileges:
# Audit SUID root filessudo find / -xdev -type f -perm -4000 -exec ls -la {} + 2>/dev/null
# Identify newly added SUID files compared to baselinesudo find / -xdev -type f -perm -4000 -mtime -3 2>/dev/null2. World-Writable Files & Directories
Section titled “2. World-Writable Files & Directories”Scan for files that anyone can overwrite:
# Search for world-writable filessudo find / -xdev -type f -perm -0002 -exec ls -ld {} + 2>/dev/null
# Search for world-writable directories missing the Sticky Bitsudo find / -xdev -type d \( -perm -0002 -a ! -perm -1000 \) 2>/dev/null3. Orphaned Files (No User or Group)
Section titled “3. Orphaned Files (No User or Group)”Files left behind after deleting users:
sudo find / -xdev \( -nouser -o -nogroup \) 2>/dev/null4. Automated Rootkit & Integrity Scanning
Section titled “4. Automated Rootkit & Integrity Scanning”- Lynis: Industry standard security auditing tool for Linux:
Terminal window sudo apt install lynis -y # or dnf install lynissudo lynis audit system - Rkhunter (Rootkit Hunter):
Terminal window sudo apt install rkhunter -ysudo rkhunter --check --skip-keypress
39.4 Network Exposure & Ports
Section titled “39.4 Network Exposure & Ports”An audit is incomplete without knowing what doors are open to the outside world.
1. Auditing Listening Ports (ss)
Section titled “1. Auditing Listening Ports (ss)”Confirm that only required services are exposed on network interfaces:
# Display all listening TCP and UDP sockets with process names and PIDssudo ss -tulpnWhat to Look For:
Section titled “What to Look For:”- Is a database (PostgreSQL
:5432or MySQL:3306) listening on0.0.0.0(all interfaces) instead of127.0.0.1? - Are insecure legacy services (Telnet
:23, FTP:21) running?
2. Auditing Running Services (systemctl)
Section titled “2. Auditing Running Services (systemctl)”List all services currently active in memory:
# List active running service daemonssystemctl list-units --type=service --state=running
# List all enabled services scheduled to start at bootsystemctl list-unit-files --type=service --state=enabled39.5 Security Auditing Checklist
Section titled “39.5 Security Auditing Checklist”| Audit Task | Frequency | Command / Tool | Success Baseline |
|||||
| Failed Logins | Daily | lastb \| head -n 20 | No distributed brute force |
| Sudo Actions | Weekly | grep "COMMAND=" /var/log/auth.log | Authorized team actions only |
| Auditd File Integrity | Continuous | ausearch -k shadow_modification | Zero unauthorized changes |
| SUID Binaries | Weekly | find / -xdev -perm -4000 | Matches baseline whitelist |
| Listening Ports | Monthly | ss -tulpn | Zero unintended public ports |
| System Audit Score | Quarterly | lynis audit system | Hardening index > 75 |