Sub topic 17
File integrity monitoring with AIDE
This chapter assumes you have finished the filesystem, logging and auditd chapters. You should be comfortable with systemd units and timers, and with reading file permissions. Commands use RHEL 9 paths. Debian and Ubuntu differences are noted where they matter.
What AIDE does and why CIS asks for it
AIDE (Advanced Intrusion Detection Environment) is a file integrity checker. It reads a configuration that lists paths and the attributes to record for each one. It stores the results in a database, and later compares the live filesystem to that database.
The CIS benchmarks have a filesystem integrity section with two requirements:
- AIDE, or an equivalent tool, is installed.
- Integrity is checked regularly, usually through cron or a systemd timer.
AIDE detects changes. It does not prevent them. If an attacker replaces /usr/sbin/sshd or edits /etc/pam.d/system-auth, AIDE reports it at the next check. Auditd records who changed a file as it happens, and AIDE records that the file differs from a known-good state. Use both.
The AIDE lifecycle
Install and initialize
# RHEL 9 and derivatives
sudo dnf install aide
# Build the first database. This reads every file in scope and can take minutes.
sudo aide --init
# The new database is written under a different name so it cannot overwrite the live one
ls -l /var/lib/aide/
sudo mv /var/lib/aide/aide.db.new.gz /var/lib/aide/aide.db.gz
# Compare the system to the database
sudo aide --checkOn Debian and Ubuntu, install aide aide-common and run sudo aideinit. It writes /var/lib/aide/aide.db.new. Copy that file to /var/lib/aide/aide.db. Configuration lives in /etc/aide/aide.conf and /etc/aide/aide.conf.d/. The aide.wrapper command reads that layout.
Initialize on a system you trust. If the host is already compromised, the baseline records the compromise as normal.
Reading aide.conf
An AIDE rule has three parts: a path, a regular expression that selects files, and a group of attributes to compare. The group is a set of letters joined by +.
| Letter | Meaning |
|---|---|
| `p` | permissions |
| `i` | inode |
| `n` | number of links |
| `u`, `g` | owner, group |
| `s` | size |
| `m`, `c` | modification time, change time |
| `acl`, `xattrs`, `selinux` | ACLs, extended attributes, SELinux context |
| `sha256`, `sha512` | content hash |
Without a content hash, AIDE can only see metadata changes. An attacker can change a file and reset its size and timestamps. Always include a strong hash for files that matter.
# /etc/aide.conf (excerpt)
# Define your own groups and reuse them
CIS_FULL = p+i+n+u+g+s+m+c+acl+selinux+xattrs+sha512
LOGS_ONLY = p+u+g+n+acl+selinux+xattrs
# Binaries, libraries and configuration: full comparison
/boot CIS_FULL
/bin CIS_FULL
/sbin CIS_FULL
/lib64 CIS_FULL
/usr CIS_FULL
/etc CIS_FULL
# Audit tools get the same hash-based protection
/usr/sbin/auditctl CIS_FULL
/usr/sbin/auditd CIS_FULL
/usr/sbin/augenrules CIS_FULL
# Logs grow constantly, so compare only ownership and permissions
/var/log LOGS_ONLY
# Exclude data that changes legitimately, and the AIDE database itself
!/var/lib/aide
!/var/spool
!/var/cache
!/etc/mtabThe ! prefix excludes a path. Tune exclusions to your workload. A database server's data directory, container storage under /var/lib/containers, and /var/lib/docker all change constantly, and including them buries real findings in noise. Before you exclude anything, check that the rule above it does not already cover the path in a more specific way. Rule order matters when patterns overlap.
Run sudo aide --config-check after every edit. This is the AIDE equivalent of sshd -t.
Verify that it detects changes
Check the tool before you rely on it.
sudo touch /etc/aide-test.conf
sudo aide --check; echo "exit code: $?"
sudo rm /etc/aide-test.confThe check should report one added file under /etc and one changed directory (/etc, because its modification time changed). The report ends with a summary of added, removed and changed counts. The exit code is a bitmask: 1 means files were added, 2 means removed, 4 means changed. A run with one addition and one change exits with 5. Codes of 14 and above mean AIDE itself failed, for example a missing database or a configuration error.
Schedule the check
CIS accepts cron or a systemd timer. A timer logs to the journal and shows failures in systemctl --failed.
# /etc/systemd/system/aidecheck.service
[Unit]
Description=AIDE integrity check
ConditionPathExists=/var/lib/aide/aide.db.gz
[Service]
Type=oneshot
ExecStart=/usr/sbin/aide --check
Nice=19
IOSchedulingClass=idle
# /etc/systemd/system/aidecheck.timer
[Unit]
Description=Run AIDE check daily
[Timer]
OnCalendar=*-*-* 05:00:00
RandomizedDelaySec=1h
Persistent=true
[Install]
WantedBy=timers.targetThe two units above are separate files. Save each under its own path, then enable the timer:
sudo systemctl daemon-reload
sudo systemctl enable --now aidecheck.timer
systemctl list-timers aidecheck.timerA non-zero exit marks the service as failed when differences exist. Forward the output with your log shipping or alerting tool. A check that runs and that nobody reads gives you no protection.
Handling legitimate changes
Patching changes thousands of files. After a planned change window:
- Run
sudo aide --checkand read the report. - If every difference is explained by the update, run
sudo aide --update. This writesaide.db.new.gz. - Replace the baseline with the new file, and save an off-host copy.
sudo aide --update
sudo cp /var/lib/aide/aide.db.gz /root/aide.db.gz.$(date +%F)
sudo mv /var/lib/aide/aide.db.new.gz /var/lib/aide/aide.db.gzRun --update immediately after the --check that you reviewed. Files can change between the two runs, and --update accepts them without review.
Protecting the database
An attacker with root can edit the AIDE database and the AIDE binary as easily as any other file. Reduce that risk:
- Copy the database to read-only or off-host storage after each accepted update, and compare a stored SHA-256 of it before trusting results.
- Keep the
aidebinary andaide.confin a rule that is checked, and store a hash of the configuration off the host. - Run checks from a central tool that pulls the database from remote storage, where you can.
Without that separation, AIDE detects careless changes and opportunistic tampering. It does not detect a determined attacker who knows it is there.
Common errors
aide: Couldn't open file /var/lib/aide/aide.db.gz: you ran--checkbefore movingaide.db.new.gzinto place.- Hundreds of changes after every run: a busy directory is in scope. Find it in the report and add an exclusion or a lighter group.
- Version mismatch (exit 19): the database was built by a different AIDE version. Re-initialize after a trusted upgrade.
- Nothing is reported after a change: the path lacks a rule, or the group has no hash and the metadata was restored.
Exercise
You run sudo aide --check after a planned dnf update and the report lists 412 changed files, all under /usr/lib64 and /usr/bin, plus one added file, /etc/cron.d/.hidden. Which step do you take for each, and why must you not simply run aide --update right away?
Show answer
The changes under /usr/lib64 and /usr/bin can be matched against the transaction in dnf history info (or rpm -V on the affected packages) and then accepted. The /etc/cron.d/.hidden file is not explained by the update, so investigate it as a possible compromise: inspect its contents, owner and timestamps, and check auditd records for who created it. Running aide --update first would write the unexplained file into the new baseline, so later checks would treat it as normal.