Skip to content

39. Security Auditing

This chapter covers authentication auditing, kernel event monitoring with Linux Audit Framework (auditd), permission reconnaissance, and network exposure auditing.

Linux tracks all interactive logins, session durations, reboot events, and failed authentication attempts in binary accounting files.

Raw text logs record authentication attempts in real-time:

  • Debian / Ubuntu: /var/log/auth.log
  • RHEL / Rocky / AlmaLinux: /var/log/secure
Terminal window
# Real-time monitoring of authentication attempts
sudo 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 commands
sudo grep "COMMAND=" /var/log/auth.log | tail -n 20

2. 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 |

Terminal window
# Check recent system reboots
last reboot
# Check failed login attempts grouped by IP to detect attack sources
sudo lastb | awk '{print $3}' | sort | uniq -c | sort -nr | head -n 10
# Audit accounts that have never logged in
lastlog | grep "**Never logged in**"

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.

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)
  • Ubuntu / Debian:
    Terminal window
    sudo apt install auditd audispd-plugins -y
    sudo systemctl enable --now auditd
  • RHEL / AlmaLinux:
    Terminal window
    sudo dnf install audit -y
    sudo 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:

Terminal window
# -w path, -p permissions (r=read, w=write, x=execute, a=attribute), -k tag/key
sudo auditctl -w /etc/shadow -p wa -k shadow_modification
sudo auditctl -w /etc/sudoers -p wa -k sudoers_modification

Monitor all changes inside /etc/ssh/:

Terminal window
sudo auditctl -w /etc/ssh/ -p wa -k sshd_config_change

Rule 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:

Terminal window
sudo auditctl -a always,exit -F arch=b64 -S execve -F euid=0 -F auid>=1000 -F auid!=4294967295 -k root_action
Terminal window
sudo auditctl -l

4. 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:

Terminal window
# Search for events matching our custom key (-k)
sudo ausearch -k shadow_modification
# Search for all failed system calls today
sudo ausearch -m SYSCALL -sv no -ts today
# Search for commands executed by a specific user (UID 1001)
sudo ausearch -ua 1001 -i

The -i flag automatically translates UIDs to usernames, syscall numbers to names, and timestamps to human-readable dates!

Generates high-level compliance summaries:

Terminal window
# Generate report of all executable launches
sudo aureport -x --summary
# Generate report of authentication attempts
sudo aureport -au
# Generate report of failed events
sudo aureport --failed

Attackers maintain persistence by leaving hidden SUID binaries or writable cron jobs.

Find all binaries that execute with root privileges:

Terminal window
# Audit SUID root files
sudo find / -xdev -type f -perm -4000 -exec ls -la {} + 2>/dev/null
# Identify newly added SUID files compared to baseline
sudo find / -xdev -type f -perm -4000 -mtime -3 2>/dev/null

Scan for files that anyone can overwrite:

Terminal window
# Search for world-writable files
sudo find / -xdev -type f -perm -0002 -exec ls -ld {} + 2>/dev/null
# Search for world-writable directories missing the Sticky Bit
sudo find / -xdev -type d \( -perm -0002 -a ! -perm -1000 \) 2>/dev/null

Files left behind after deleting users:

Terminal window
sudo find / -xdev \( -nouser -o -nogroup \) 2>/dev/null
  • Lynis: Industry standard security auditing tool for Linux:
    Terminal window
    sudo apt install lynis -y # or dnf install lynis
    sudo lynis audit system
  • Rkhunter (Rootkit Hunter):
    Terminal window
    sudo apt install rkhunter -y
    sudo rkhunter --check --skip-keypress

An audit is incomplete without knowing what doors are open to the outside world.

Confirm that only required services are exposed on network interfaces:

Terminal window
# Display all listening TCP and UDP sockets with process names and PIDs
sudo ss -tulpn
  • Is a database (PostgreSQL :5432 or MySQL :3306) listening on 0.0.0.0 (all interfaces) instead of 127.0.0.1?
  • Are insecure legacy services (Telnet :23, FTP :21) running?

List all services currently active in memory:

Terminal window
# List active running service daemons
systemctl list-units --type=service --state=running
# List all enabled services scheduled to start at boot
systemctl list-unit-files --type=service --state=enabled

| 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 |