Sub topic 16
Auditing security-relevant events with auditd
This chapter covers the Linux Audit system. It assumes you can edit files under /etc, read journalctl output, and know which files hold accounts and sudo configuration (/etc/passwd, /etc/shadow, /etc/sudoers). It also assumes you have a test VM where you can safely change audit settings.
What auditd does and why CIS asks for it
The Linux Audit system records security-relevant events: who changed an account file, who ran a privileged command, who changed the system clock. The kernel generates the records. The auditd daemon receives them and writes them to /var/log/audit/audit.log. Ordinary logging (journald, rsyslog) records what programs choose to say. Auditing records what the kernel observes, so a program cannot leave an event out.
The CIS benchmarks have a whole section on auditd. It covers installing the package, sizing the log, and a set of rules for identity files, sudo configuration, time changes, mount operations, and login records. A scanner such as OpenSCAP checks each of these.
The working model
There are three kinds of rules:
- Control rules set behavior:
-Ddeletes existing rules,-bsets the backlog buffer,-eenables or locks auditing. - Watch rules (
-w) monitor a file or directory. The-poption lists the permissions to watch:r(read),w(write),x(execute),a(attribute change). - Syscall rules (
-a) match system calls, and can be filtered by architecture, user ID, and other fields.
Every rule should carry a key (-k). The key is a label stored in each matching event, and it is how you search later.
Rule files in /etc/audit/rules.d/ are read in filename order. augenrules concatenates them into /etc/audit/audit.rules, and augenrules --load pushes the result into the kernel. Number the files so the order is predictable: control rules first, watches in the middle, the lock last.
Writing and loading rules
This is the watch-rule example from the plan, covering sudo configuration and account files:
# /etc/audit/rules.d/50-cis.rules
-w /etc/sudoers -p wa -k scope
-w /etc/sudoers.d -p wa -k scope
-w /etc/passwd -p wa -k identity
-w /etc/group -p wa -k identity
-w /etc/shadow -p wa -k identityLoad the rules and list what the kernel is running:
sudo augenrules --load
sudo auditctl -lWatches only catch file changes. For events that are not tied to a file, use a syscall rule. This one records changes to the system clock, which CIS also requires. It applies to 64-bit calls only. A complete rule set adds a matching arch=b32 line.
# /etc/audit/rules.d/50-time.rules
-a always,exit -F arch=b64 -S adjtimex,settimeofday,clock_settime -k time-changeRead it left to right. always,exit means "record this on syscall exit, every time." -F arch=b64 limits the rule to 64-bit calls. -S names the syscalls.
Verifying that events are recorded
A rule that loads without errors can still fail to record. Trigger the event yourself and search for it:
sudo useradd audittest
sudo ausearch -k identity -ts recent -i
sudo userdel audittest-ts recent limits results to the last ten minutes. -i turns numeric IDs into names. You should see records of type PATH and SYSCALL that show /etc/passwd being written, with the auid (the login user who started the session) and the exe that did it. The auid stays the same after sudo, so the log shows who acted, not just "root".
To summarize rather than read raw records, use aureport:
sudo aureport --auth --summary
sudo aureport -k --summaryCheck the daemon's state with sudo auditctl -s. The lost counter should stay at 0. A growing lost value means the kernel dropped events because the backlog was full.
Daemon settings that CIS checks
The settings in /etc/audit/auditd.conf decide what happens as the log grows. Typical CIS values:
| Setting | Typical value | Effect |
|---|---|---|
| `max_log_file` | 8 (MB) | Size at which the log rotates |
| `max_log_file_action` | `keep_logs` | Rotate without deleting old logs |
| `space_left_action` | `email` | Warn when free space runs low |
| `admin_space_left_action` | `halt` | Stop the system when space is nearly gone |
CIS also asks you to enable auditing for processes that start before auditd. Add audit=1 to the kernel command line, and optionally audit_backlog_limit=8192. Without audit=1, early-boot activity is not audited.
Locking the rules
Setting -e 2 makes the rule set immutable until the next reboot. An attacker with root cannot silently remove your watches. It must be the last rule that loads, so give it the last filename:
# /etc/audit/rules.d/99-finalize.rules
-e 2Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `auditctl -l` shows "No rules" after a reboot | Rules were added with `auditctl` only, not saved in `rules.d` | Put the rules in a `.rules` file and run `augenrules --load` |
| `augenrules --load` reports an error | Typo in a rules file, or the system is locked with `-e 2` | Run `auditctl -R` on the file to find the line, or reboot if locked |
| A syscall rule never matches | Only `arch=b64` was written, and the program makes 32-bit calls | Add the `arch=b32` twin |
| `ausearch` returns nothing | Missing or different key, or the wrong time range | Use `-k` with the exact key and widen `-ts` |
| `lost` count rises | Backlog buffer too small | Raise `-b` (for example `-b 8192`) in a control file |
| Rules apply in the wrong order | Filenames don't sort the way you expect | Prefix every file with two digits |
A final point: the audit log holds sensitive details such as usernames and command arguments. CIS requires /var/log/audit to be readable only by root (mode 0700 on the directory, 0600 on the files). Check this with stat, and ship logs to a central server so a compromised host cannot erase its own history.
Exercise
You add a rule file /etc/audit/rules.d/50-cis.rules and run sudo augenrules --load. After a reboot, auditctl -l shows your rules, but you also added -e 2 in /etc/audit/rules.d/10-lock.rules, and now a later file's rules do not load. What went wrong, and how do you fix it?
Show answer
Files load in filename order, so 10-lock.rules set the immutable flag before 50-cis.rules was read. The kernel then refused the later rules. Rename the lock file so it sorts last (for example 99-finalize.rules), then reboot. Because the system is locked until reboot, augenrules --load cannot fix it without one.