26.6. Auditing the System
Confinement limits what a program can do; auditing records what was actually done. The two disciplines answer different questions - "could it?" versus "did it?" - and an incident investigation lives or dies on the second. This section reviews the routine evidence a Debian host already produces, then adds targeted audit rules for the changes that matter most: accounts, sudo policy, and privilege escalation. It ends with the two protections that keep the evidence itself trustworthy - integrity verification of the software that produced it, and retention rules for the logs that carry it.
26.6.1. Reviewing Logins, Privilege Changes, and Service Exposure
The review posture worth building is a short list checked on a schedule: who logged in, who became root, and what is listening that was not listening last week. The first question has a dedicated tool. On sec1, as john:
$ sudo last -a -n 5
docker-r Mon Nov 23 14:04 - 14:04 (00:00)
reboot system boot Mon Nov 23 13:20 - still running 6.12.86+deb13-amd64
wtmpdb begins Mon Nov 23 13:20:14 2026
Debian 13 records login history in wtmpdb, and last reads it: one
line per session with the account, the source address (-a moves it to
the end of the line), the interval, and the boot record with the
running kernel. The practice host shows a system-account session and
the current boot - a server with real users shows the review's actual
material: sessions from accounts and addresses you can name, at times
that match a work pattern. Anything that does not fit that standard -
a session for an account with no reason to log in, an address that
belongs to no one - is the finding this review exists to surface. The
-n 5 keeps the listing to the five most recent sessions; drop it to
read the whole file.
The second question - who became root - has two standard sources.
sudo writes an entry to auth.log and the journal for every
invocation, successful or refused: the privilege side reads back as
sudo journalctl -t sudo --since '1 hour ago', the login sessions
around it as sudo journalctl -u ssh --since '1 hour ago', and the
same records can be grepped out of auth.log -
Section 20.2.3 covers the journal's
identity filters, and Section 26.6.2 below moves the same question onto
auditd, which records it even when logging has been tampered with. The
third question is the baseline comparison: re-run the
Section 26.1.1 listener and service listings, diff against the
recorded baseline, and investigate every difference before deciding it
is benign. Five minutes, three commands, on a schedule - that is the
whole posture.
26.6.2. Auditd Rules for Selected Administrative Events
The kernel's audit subsystem records system calls matching rules you
load, with the login UID attached - the identity that survives sudo,
which is what makes the trail credible. Debian's auditd package
provides the daemon, the rules directory /etc/audit/rules.d, and
augenrules, which merges that directory into the kernel at boot and
on demand. The chapter's rule set watches the account database, the
sudo policy, and the act of executing sudo itself. Save this as
99-handbook.rules in the workspace, for installation to
/etc/audit/rules.d/99-handbook.rules on sec1:
-w /etc/passwd -p wa -k identity
-w /etc/shadow -p wa -k identity
-w /etc/sudoers -p wa -k privilege
-w /etc/sudoers.d -p wa -k privilege
-a always,exit -F arch=b64 -S execve -F auid>=1000 -F auid!=-1 -F exe=/usr/bin/sudo -F key=privilege
The first four are watch rules - -w names a file or directory,
-p wa records writes and attribute changes to it, and -k tags
every resulting event with a searchable key. The last is a system-call
rule: on exit from execve, when the executed file is
/usr/bin/sudo and the login UID is a real user (auid>=1000,
excluding unset -1), always record an event - every privilege
escalation attempt, tagged privilege. Install and load. On sec1, as
john:
$ sudo install -m 640 99-handbook.rules /etc/audit/rules.d/99-handbook.rules
$ sudo augenrules --load
No rules
enabled 1
failure 1
pid 95776
rate_limit 0
backlog_limit 8192
lost 0
backlog 2
backlog_wait_time 60000
backlog_wait_time_actual 0
The install is silent; augenrules --load merges the rules directory
and reports the kernel's audit status as it applies the changes -
selected output; augenrules prints the status block again after each
change operation and the later identical blocks are omitted. Confirm
what the kernel now enforces, with the same daemon's rule listing:
$ sudo auditctl -l
-w /etc/passwd -p wa -k identity
-w /etc/shadow -p wa -k identity
-w /etc/sudoers -p wa -k privilege
-w /etc/sudoers.d -p wa -k privilege
-a always,exit -F arch=b64 -S execve -F auid>=1000 -F auid!=-1 -F exe=/usr/bin/sudo -F key=privilege
The five rules, loaded and live until the next boot rebuilds them from the same directory. Now generate the events the rules exist to catch - create a probe account and then modify it, exactly the kind of change the identity watches guard. On sec1, as john:
$ sudo useradd -m -c 'Audit probe' auditprobe
$ sudo usermod -c 'Audit probe updated' auditprobe
Both silent. The useradd lands in /etc/passwd (a write), and the
usermod rewrites it through the usual rename dance. Read the trail
back, keyed on identity, interpreted with -i:
$ sudo ausearch --input-logs -k identity -i | tail -8
type=PROCTITLE msg=audit(11/23/26 16:41:59.505:44995) : proctitle=usermod -c Audit probe updated auditprobe
type=PATH msg=audit(11/23/26 16:41:59.505:44995) : item=4 name=/etc/passwd inode=5244249 dev=fe:01 mode=file,644 ouid=root ogid=root rdev=00:00 nametype=CREATE cap_fp=none cap_fi=none cap_fe=0 cap_fver=0 cap_frootid=0
type=PATH msg=audit(11/23/26 16:41:59.505:44995) : item=3 name=/etc/passwd inode=5244299 dev=fe:01 mode=file,644 ouid=root ogid=root rdev=00:00 nametype=DELETE cap_fp=none cap_fi=none cap_fe=0 cap_fver=0 cap_frootid=0
type=PATH msg=audit(11/23/26 16:41:59.505:44995) : item=2 name=/etc/passwd+ inode=5244249 dev=fe:01 mode=file,644 ouid=root ogid=root rdev=00:00 nametype=DELETE cap_fp=none cap_fi=none cap_fe=0 cap_fver=0 cap_frootid=0
Selected output; the two PATH records for the parent directories, the
CWD record, and the final SYSCALL record are omitted. The omitted
SYSCALL record is the one an investigation reads first - it carries
the actor's login UID (auid), the effective identity at the time,
success=yes, and the key=identity tag. What the shown lines
establish: the exact
command (proctitle), and the file operations - usermod did not
edit /etc/passwd in place but created a new file and renamed it over
the old one, which is why the watch on the path caught a CREATE
and two DELETEs rather than a WRITE. That detail matters in an
investigation: a watch rule names a path, and tools that replace files
by rename still trip it.
The privilege rule's trail reads the same way, restricted to the
EXECVE record type:
$ sudo ausearch --input-logs -k privilege -m EXECVE -i | tail -8
----
type=PROCTITLE msg=audit(11/23/26 16:42:03.481:45042) : proctitle=sudo bash -lc ausearch --input-logs -k privilege -m EXECVE -i | tail -8
type=PATH msg=audit(11/23/26 16:42:03.481:45042) : item=1 name=/usr/bin/sudo inode=3956626 dev=fe:01 mode=file,suid,755 ouid=root ogid=root rdev=00:00 nametype=NORMAL cap_fp=none cap_fi=none cap_fe=0 cap_fver=0 cap_frootid=0
type=CWD msg=audit(11/23/26 16:42:03.481:45042) : cwd=/home/john/debian-handbook/security
type=EXECVE msg=audit(11/23/26 16:42:03.481:45042) : argc=4 a0=sudo a1=bash a2=-lc a3=ausearch --input-logs -k privilege -m EXECVE -i | tail -8
Selected output; the duplicate sudo PATH record (item 0), the
dynamic-loader PATH record, and the final SYSCALL record are omitted -
the SYSCALL record again carries the login UID, the effective root
identity, and key=privilege. The most recent privilege event at
capture time is the search command itself: the rule keys every
execution of sudo, including the one performing the search, and the
PATH line records the file's suid mode - the very property
Section 26.3.4 inventoried. One note on the command itself: on this
system the audit tools need --input-logs to read the daemon's
configured log location - if ausearch reports no events while
auditctl -l shows rules, check where your auditd.conf writes.
For the recurring review, aureport summarizes the whole audit trail
without a search key:
$ sudo aureport --input-logs --summary
Summary Report
======================
Range of time in logs: 11/23/26 13:48:23.419 - 11/23/26 16:42:05.785
Selected time for report: 11/23/26 13:48:23 - 11/23/26 16:42:05.785
Number of changes in configuration: 266
Number of changes to accounts, groups, or roles: 214
Number of logins: 0
Number of failed logins: 11
Number of authentications: 1
Number of failed authentications: 0
Number of users: 5
Number of terminals: 7
Number of host names: 4
Number of executables: 17
Number of commands: 15
Number of files: 16
Number of AVC's: 0
Number of MAC events: 0
Number of failed syscalls: 0
The counts that matter to a reviewer are on adjacent lines: 11 failed logins against zero successful ones in the log's window - a number a reviewer chases to its source, whatever it turns out to be - and the account-change count that includes this section's own probe operations. A summary like this, read weekly, is what turns "who became root?" from a forensic exercise into a glance. To retire the chapter's rules, remove the file and reload - the kernel follows the directory, not the individual rule:
$ sudo rm /etc/audit/rules.d/99-handbook.rules
$ sudo augenrules --load
No rules
enabled 1
failure 1
pid 95776
rate_limit 0
backlog_limit 8192
lost 0
backlog 4
backlog_wait_time 60000
backlog_wait_time_actual 0
Selected output; the repeated status blocks are omitted as before.
The rules are gone from the kernel; the audit log keeps everything
already recorded, which is the asymmetry an investigation depends on.
A periodic cleanup removes the probe account - sudo userdel -r
auditprobe, silent.
26.6.3. Package File Verification and Integrity Baselines
When software itself is the suspect, the question is whether the files
on disk still match what Debian shipped. The package database carries
the checksums, and two tools compare against them. hello, installed
in the chapter's package list, is the harmless subject. First put it in
a known state - a reinstall writes pristine files, silently:
$ sudo apt install --reinstall hello
The reinstall prints its normal package progress and ends quietly. Tamper with one file the way an intruder's backdoor would:
$ sudo sh -c 'printf tamper >> /usr/bin/hello'
Silent - the append lands at the end of the binary. Ask dpkg what it sees:
$ sudo dpkg -V hello
??5?????? /usr/bin/hello
One line, one verdict: ??5?????? is dpkg's nine-character code for
"this file differs from the packaged version", the 5 in the
third position naming the checksum as the failing check, and the
question marks the checks not performed. The command's exit status
deserves its own sentence, because it is the opposite of what a
script writer expects: dpkg -V reports the mismatch on output but
still exits 0 - it is a reporting tool, not a failing one.
debsums has the opposite contract, which is why monitoring scripts
prefer it:
$ sudo debsums -c hello
/usr/bin/hello
-c lists only changed files and the command exits nonzero when it
finds any - the property that makes debsums -c a checkable
assertion rather than a report to read. A full sweep
(sudo debsums -c with no package, or sudo dpkg -V for everything)
costs minutes and names every divergent file; config files you
deliberately changed will appear, so the skill is reading the list,
not assuming every line is a compromise. The fix for a tampered
packaged file is the same reinstall that started this exercise:
$ sudo apt install --reinstall hello
The reinstall runs exactly as the first one did, and a following
sudo dpkg -V hello prints nothing - the empty output is the
passing result.
Packaged files are the easy case: Debian ships their checksums. Your
own configuration and content need a baseline you create. The
pattern: hash the files while they are trusted, store the list where
it cannot be rewritten casually, and re-check on demand. A small
practice file, created in the workspace with this content and saved
as integrity.conf:
setting=1
Record the baseline, as john on sec1:
$ sha256sum integrity.conf > integrity.sha256
$ cat integrity.sha256
2bb264bf86e6547af86ce050ef56c3c569dea500d3f3512f528584aabc7f62d1 integrity.conf
The redirection writes the manifest; cat shows what was recorded.
The check that will be run over and over:
$ sha256sum -c integrity.sha256
integrity.conf: OK
Now the failure path. Change the file:
$ printf 'setting=2\n' > integrity.conf
Silent. The same check refuses:
$ sha256sum -c integrity.sha256
integrity.conf: FAILED
sha256sum: WARNING: 1 computed checksum did NOT match
One line on standard output naming the file, one warning on standard
error counting the failure, and a nonzero exit status - the assertion
shape that fits a monitoring loop. Restore the content (setting=1
in the file, silent) and the check passes again:
$ sha256sum -c integrity.sha256
integrity.conf: OK
A production baseline hashes the files that matter - service configurations, unit files, web roots - with a manifest stored off-host or on write-once media, because a baseline on the same disk as the files it describes is editable by whoever compromised the files. The manifest in this exercise stays in the workspace; treat "where does the manifest live" as the real design question.
26.6.4. Retaining and Protecting Security Logs
Evidence that rotates away is evidence you no longer have. Three
retention domains need deliberate settings: the journal, auditd's
logs, and any security log you keep by hand. The journal's defaults
are volatile or size-limited; make the practice host's persistent and
bounded. Save this as journald-99-handbook.conf in the workspace for
installation to /etc/systemd/journald.conf.d/99-handbook.conf on
sec1:
[Journal]
Storage=persistent
SystemMaxUse=200M
MaxRetentionSec=30day
Compress=yes
Storage=persistent writes the journal under /var/log/journal so
it survives reboots; SystemMaxUse caps it; MaxRetentionSec bounds
age. Install, restart, and confirm the effective values -
systemd-analyze cat-config prints the merged configuration across
the main file and every drop-in:
$ sudo install -d /etc/systemd/journald.conf.d
$ sudo install -m 644 journald-99-handbook.conf /etc/systemd/journald.conf.d/99-handbook.conf
$ sudo systemctl restart systemd-journald
$ sudo systemd-analyze cat-config systemd/journald.conf | grep -E 'Storage=|SystemMaxUse=|MaxRetentionSec=' | tail -3
Storage=persistent
SystemMaxUse=200M
MaxRetentionSec=30day
The three setup commands are silent; the restart activates the settings. The last matches in the merged output are the drop-in's - its values override any earlier ones. The disk cost of the policy:
$ sudo journalctl --disk-usage
Archived and active journals take up 72M in the file system.
72 MiB against a 200 MiB cap, with 30 days of retention - the three numbers that make the journal a usable evidence source rather than a best-effort hint. Section 8.4.2 covers the journal's retention vocabulary in depth.
auditd manages its own rotation, and its policy lives in
/etc/audit/auditd.conf. The settings that matter for evidence: log
files of 8 MiB, five kept before rotation continues without deletion,
and - the two lines that make the daemon fail loudly rather than
discard - disk_full_action and disk_error_action set to SUSPEND:
when the disk fills or errors, auditd stops processing rather than
silently dropping the record of what happened. Edit the file so these
five lines read:
max_log_file = 8
num_logs = 5
max_log_file_action = keep_logs
disk_full_action = SUSPEND
disk_error_action = SUSPEND
Then restart the daemon and read the effective policy back, on sec1, as john:
$ sudo systemctl restart auditd
Silent. The settings:
$ sudo grep -E '^(max_log_file|num_logs|max_log_file_action|disk_full_action|disk_error_action) ' /etc/audit/auditd.conf
max_log_file = 8
num_logs = 5
max_log_file_action = keep_logs
disk_full_action = SUSPEND
disk_error_action = SUSPEND
keep_logs is the evidence-preserving choice: after the five rotating
files, older ones are renamed and kept, never deleted - the opposite
of the stock rotate behavior, and the reason the setting belongs in
a security chapter. The directory and file permissions carry the same
intent:
$ sudo stat -c '%a %U:%G %n' /var/log/audit /var/log/audit/audit.log
750 root:adm /var/log/audit
640 root:adm /var/log/audit/audit.log
root:adm with no world access: the audit trail is readable by the
administrator's group and no one else. The listing's two lines follow
the order given on the command line - the directory at mode 750, the
log file at 640.
For logs you keep by hand, the filesystem offers a tamper-evidence mechanism stronger than permissions: the append-only attribute. Create a security log and set it. On sec1, as john:
$ sudo install -d -m 750 /var/log/handbook
$ sudo sh -c 'printf "retained security evidence\n" > /var/log/handbook/security.log'
$ sudo chmod 600 /var/log/handbook/security.log
$ sudo chattr +a /var/log/handbook/security.log
$ sudo lsattr /var/log/handbook/security.log
-----a--------e------- /var/log/handbook/security.log
The first four commands are silent; the lsattr line shows the
attribute set - the a in the flag string is append-only, the e
the extents format every ext4 file carries. Now the two attempts that
matter. Overwrite:
$ sudo sh -c 'printf tamper > /var/log/handbook/security.log'
bash: line 1: /var/log/handbook/security.log: Operation not permitted
Delete:
$ sudo rm /var/log/handbook/security.log
rm: cannot remove '/var/log/handbook/security.log': Operation not permitted
Both refused, as root, because the append-only attribute is enforced
by the kernel for every process, root included - appending still
works, and nothing else does. The attribute is honest about its own
limits: root can clear it with chattr -a and then write, so this is
tamper evidence - the modification attempt fails loudly in front of
witnesses - not tamper proof against a root-level attacker. Clear it
when the log's life is over:
$ sudo chattr -a /var/log/handbook/security.log
$ sudo lsattr /var/log/handbook/security.log
--------------e------- /var/log/handbook/security.log
Silent, then the flag string without the a. The last protection is
off-host: rsyslog forwarding to a collector that the compromised host
cannot reach back into - the mechanism Section 8.5.1
introduces, and the property that makes it decisive in an incident: a
log the attacker cannot edit is the one you actually consult.