Sub topic 19
Automating remediation and detecting drift
This chapter assumes you can run an OpenSCAP scan and read its report (see the scanning chapter). It also assumes you are comfortable with systemd units and editing files under /etc. You should have a test server or virtual machine with a snapshot to roll back to.
A hardened server does not stay hardened by itself. Packages update, administrators make quick fixes, and a configuration tool may overwrite a setting. Drift is any difference between the state you approved and the state the server is in now. Automation addresses two problems: applying the settings consistently, and noticing when they change.
The remediation and drift loop
The loop has four steps. You scan, remediate what failed, scan again to confirm, and then keep scanning on a schedule so you see drift.
Generating remediation from a scan
OpenSCAP can produce a remediation script from the same SCAP content you used for scanning. The --fix-type option selects the format. Generate the script from a results file so it contains only the fixes for rules that failed.
# Create a Bash remediation script for the rules that failed in results.xml
sudo oscap xccdf generate fix \
--fix-type bash \
--result-id "" \
--output remediate.sh \
results.xml
# Read it before running anything
less remediate.shAn empty --result-id selects the single result in the file. Use --fix-type ansible to get a playbook instead. You can also generate a script for a whole profile, without scan results, using --profile and the data stream file. That script covers every rule in the profile, not only the failures.
You can also let oscap apply fixes during a scan with --remediate. This is convenient on a disposable machine. On a production server it applies changes you have not read, so treat it as a last resort.
sudo oscap xccdf eval --remediate \
--profile xccdf_org.ssgproject.content_profile_cis_server_l1 \
--results results-after.xml \
/usr/share/xml/scap/ssg/content/ssg-rhel9-ds.xmlThe SCAP Security Guide also ships prebuilt scripts and playbooks, usually under /usr/share/scap-security-guide/. The exact paths and file names vary by distribution and package version, so list the directory on your system rather than assuming a name.
Why you must read generated fixes
Generated fixes are correct for the generic rule, not for your workload. Common problems:
- A rule that sets
noexecon/tmpcan break installers or build tools that run from/tmp. - A firewall rule set can lock out your own SSH session if your management network is not allowed.
- Rules that change partitions cannot be fixed by a script on a running system. They need a rebuild or a manual resize.
- Some rules are manual in the benchmark. Automation reports them as not checked, not as passed.
Apply fixes to a test host first, in small groups (for example filesystem rules, then SSH rules), and run the verification scan after each group.
Keeping the fix in configuration management
A one-time script fixes a server once. A configuration management tool such as Ansible re-applies the setting every time it runs, which also corrects drift. The task below enforces one of the SSH settings from the CIS drop-in and validates the file before the daemon reloads.
- name: Enforce CIS SSH settings
hosts: servers
become: true
tasks:
- name: Install the CIS SSH drop-in
ansible.builtin.copy:
dest: /etc/ssh/sshd_config.d/60-cis.conf
owner: root
group: root
mode: "0600"
content: |
PermitRootLogin no
MaxAuthTries 4
X11Forwarding no
validate: /usr/sbin/sshd -t -f %s
notify: Reload sshd
handlers:
- name: Reload sshd
ansible.builtin.service:
name: sshd
state: reloadedThe validate option runs sshd -t on a temporary copy before the real file is replaced. A syntax error therefore fails the task instead of breaking SSH. The service name is sshd on RHEL-family systems and ssh on Debian and Ubuntu. Note that sshd -t -f checks the given file on its own, so it does not catch an ordering conflict with other drop-ins. Run sshd -T on the host afterward to see the effective values.
Detecting drift on a schedule
Run the scan regularly and keep the results. A systemd timer is a good fit because it logs to the journal and can catch up after downtime.
# /etc/systemd/system/cis-scan.service
[Unit]
Description=CIS benchmark scan
[Service]
Type=oneshot
ExecStart=/usr/bin/oscap xccdf eval \
--profile xccdf_org.ssgproject.content_profile_cis_server_l1 \
--results /var/log/cis/results-latest.xml \
--report /var/log/cis/report-latest.html \
/usr/share/xml/scap/ssg/content/ssg-rhel9-ds.xml
SuccessExitStatus=0 2
# /etc/systemd/system/cis-scan.timer
[Unit]
Description=Weekly CIS scan
[Timer]
OnCalendar=weekly
Persistent=true
RandomizedDelaySec=1h
[Install]
WantedBy=timers.targetCreate the output directory and enable the timer:
sudo mkdir -p /var/log/cis
sudo systemctl daemon-reload
sudo systemctl enable --now cis-scan.timer
systemctl list-timers cis-scan.timeroscap returns exit code 0 when all rules pass, 2 when at least one rule fails, and 1 on an error. Without SuccessExitStatus=0 2, systemd would mark the service failed whenever a rule fails. You may prefer that, because a failed unit is easy to alert on. If so, drop the line and monitor unit failures instead.
Overwriting results-latest.xml loses history. Copy each result to a dated name, or ship it to central storage, so you can see when a rule started failing.
Comparing two scans
To see what changed, extract the failing rule IDs from two result files and compare them.
extract_fails() {
xmllint --xpath \
'//*[local-name()="rule-result"][*[local-name()="result"]="fail"]/@idref' \
"$1" | tr ' ' '\n' | sed 's/idref=//; s/"//g' | sort
}
diff <(extract_fails results-last-week.xml) <(extract_fails results-latest.xml)Lines starting with > are rules that fail now but passed before. Those are your drift findings. Check them against change records to see whether someone changed the setting on purpose.
Using audit and integrity tools for drift
Scans tell you the state at scan time. Two other controls from earlier chapters tell you when a change happens. The auditd rules on /etc/passwd and /etc/sudoers record who modified them, and AIDE reports changed files at its next check. Use scans for configuration state and these tools for change events.
Common errors
- Remediating production first, then discovering a service broke.
- Running a profile-wide fix script after a scan, which re-applies rules you had approved as exceptions.
- Forgetting that a configuration management run and a hand-written script can disagree. Pick one source of truth.
- Not rebooting or reloading after changes that need it, such as kernel module and bootloader settings, so the verification scan fails.
- Scanning with a different SCAP content version than last time, which changes the rule list and makes comparisons misleading.
Exercise
Your weekly scan diff shows that sshd_disable_root_login passed last week and fails now. What are the first three things you check, and which tool tells you who changed the file?
Show answer
First, run sshd -T | grep -i permitrootlogin to confirm the effective value and check the drop-in files in /etc/ssh/sshd_config.d/ for a file read earlier than 60-cis.conf (sshd keeps the first value it reads). Second, check the change records and your configuration management run history to see whether the change was planned. Third, look at the package log for an update that replaced sshd_config. If you have auditd watch rules on the SSH configuration files, ausearch -k with your rule key shows which account made the change. Then re-apply the setting through configuration management and rerun the scan.
Exercise
Why should a generated remediation script be run on a test host first, and why is --remediate risky on production?
Show answer
The script applies the generic rule, which may conflict with your workload (for example noexec on /tmp breaking an installer) or lock you out (a firewall without your management network). --remediate applies all of these changes during the scan with no chance to review them, so a failure can affect a live service.