Hardening baselines as code
Secure settings you can write down, apply and check
Learn what a security baseline is, how CIS Benchmarks fit in, and how to apply, verify and keep settings from drifting using code and scanners.
beginner level, 18 minutes, 16 slides.
Hardening baselines as code
Welcome. Learn what a security baseline is, how CIS Benchmarks fit in, and how to apply, verify and keep settings from drifting using code and scanners. This talk runs about 18 minutes.
Secure settings, written down
Hardening means changing a system's settings to reduce risk. Doing it as code makes it repeatable and checkable.
Hardening is the work of tightening a system's settings so there is less for an attacker to use. NIST describes the goal of security-focused configuration management as managing and monitoring configurations to achieve adequate security and minimize risk, while still supporting the business. Doing this by hand once is fine. Doing it for a hundred servers, over years, needs code. That is what this talk is about.
What is a baseline?
Definition: A baseline is an agreed list of secure settings that every system of a given type should have.
- Hardening: changing settings to reduce attack surface.
- Attack surface: all the ways in that an attacker could try.
- NIST calls managing and monitoring configurations "security-focused configuration management".
A baseline is simply an agreed list of secure settings that every system of a type should have. Hardening is applying it. NIST SP 800-128 describes this as security-focused configuration management, the part of configuration management that concentrates on information security.
Where baselines come from
You do not need to invent your own list. CIS Benchmarks are prescriptive configuration recommendations, built by a worldwide community of experts.
- Consensus-based, not one vendor's opinion.
- Cover more than 25 vendor product families.
- Recommendations are prescriptive: they say what to set.
Define: CIS: CIS is the Center for Internet Security, which publishes the benchmarks.
The Center for Internet Security publishes CIS Benchmarks. CIS describes them as prescriptive configuration recommendations for more than 25 vendor product families, and as the consensus-based effort of cybersecurity experts around the world. Starting from one means you do not have to invent your own list.
Level 1 and Level 2
Level 1
- Base recommendations
- Can be done fairly promptly
- Aims to avoid heavy performance impact
Level 2
- Defense in depth
- For environments where security is paramount
- Can hurt if applied without care
- Test before you roll out
Start here: Begin with Level 1, measure, then decide where Level 2 is worth it.
Most CIS Benchmarks have profiles. Level 1 is the base: it can be implemented fairly promptly and aims to reduce attack surface without hurting usability. Level 2 is defense in depth for environments where security is paramount, and it can have an adverse effect if applied without due care. A sensible path is to begin with Level 1.
Configuration as code
The baseline is a file, not a memory.
- Baseline in git: Settings as files
- Review: A second person checks
- Apply: Automation sets it
- Verify: Scanner checks
- Every change has an author and a reason.
- Rollback is a git revert.
- New servers get the same settings.
Configuration as code means the baseline lives in files under version control. A change is reviewed like any other change. Automation applies it, and a scanner verifies it. Every change has an author and a reason, and if something breaks you can roll back. New servers start from the same file. This is a common practice rather than a quote from a standard.
Idempotent: safe to run again
Ansible's definition: An operation is idempotent if the result of performing it once is exactly the same as the result of performing it repeatedly without any intervening actions.
- Run the hardening twice, get the same state.
- Safe to schedule on repeat.
- A second run only fixes what changed.
Good configuration tools are idempotent. The Ansible glossary says an operation is idempotent if the result of performing it once is exactly the same as the result of performing it repeatedly without any intervening actions. For hardening, that means you can run the same code every night. It leaves correct settings alone and fixes only what has changed.
Drift: when reality moves
Drift is when the real system no longer matches the written configuration.
Detecting drift is part of the job.
- Apply: System matches code
- Manual change: Someone edits by hand
- Detect drift: Compare real to code
- Reconcile: Fix it or update the code
Terraform's warning: Manual changes to managed resources put the state file out of sync, or drifted, from real infrastructure.
Drift happens when the real system stops matching the written baseline, usually because someone made a quick manual change. Terraform's documentation warns that manual changes to resources it manages push the state out of sync from the real infrastructure. The fix is a loop: apply, detect drift, then either reset the system or update the code if the change was right.
Golden images and immutable servers
A golden image is a pre-hardened machine image that new servers start from. Packer creates identical machine images for multiple platforms from a single source configuration.
Patch in place
- Servers slowly differ
- Drift builds up
- Hard to know what is on each
Replace from image
- Every server starts identical
- Change the image, not the server
- Old server is thrown away
Another approach is the golden image. You build a hardened machine image once, from a single source configuration, and every new server starts from it. Packer is a tool that creates identical machine images for multiple platforms this way. Instead of patching servers in place and letting them slowly differ, you rebuild from a new image and replace the old server.
Verify with a scanner
OpenSCAP evaluates a system against a security profile and writes a report.
Syntax from the OpenSCAP User Manual (run it on a test system)
oscap xccdf eval \
--profile PROFILE_ID \
--results-arf results.xml \
--report report.html \
SOURCE_DATA_STREAM_FILEThe profile and data stream come from content such as the SCAP Security Guide.
Writing a baseline is not enough. You must check that systems really match it. OpenSCAP does this with the oscap xccdf eval command. You give it a profile, ask for a results file and an HTML report, and point it at a data stream file. The SCAP Security Guide project provides ready-made content that many Linux distributions ship.
Reading the results
- Each rule gets a result, such as pass or fail.
- Failures show what to change.
- Remediate means fix. OpenSCAP can do it when the rule has a fix script.
- Re-scan after any fix to confirm.
Make it routine: Run the scan on a schedule and track the failure count over time.
A scan gives each rule a result, and the failures are your to-do list. OpenSCAP can remediate systems automatically when the rules in the SCAP content have a remediation script attached, and the manual defines a fixed result for a rule that failed, was fixed by automated remediation, and now passes. Always re-scan after a fix, and run scans on a schedule so you can watch the number of failures fall.
Exceptions: when a rule does not fit
Some recommendations will break a real system. That is normal, if you handle it openly.
| Record | Why |
|---|---|
| Which rule and which system | So the scanner can skip it on purpose |
| Why it cannot be applied | So reviewers understand |
| Who owns the risk | So someone is accountable |
| Another control in its place | So the gap is reduced |
| A review date | So it does not last forever |
Sooner or later a recommendation will break something important. The answer is not to quietly ignore it. Write the exception down: which rule, which system, why, who owns the risk, what other control covers the gap, and when it will be reviewed. This way an exception is a decision, not a hole. The CIS FAQ itself says higher-level profiles can have an adverse effect if not applied with due care.
Check your understanding
Check yourself: Someone edits a setting by hand on a production server. What is this called when the system no longer matches the code?
Answer: Drift. Drift is when the real system diverges from its written configuration.
Check yourself: Which CIS profile is the sensible place to start?
Answer: Level 1. Level 1 is a base set intended to avoid heavy performance impact; Level 2 can have adverse effects if applied carelessly.
Two questions. Pause the video and answer before you reveal.
Recap
- A baseline is an agreed list of secure settings.
- CIS Benchmarks give you a starting point.
- Keep the baseline in git; apply it with idempotent automation.
- Detect drift, and rebuild from golden images where you can.
- Verify with a scanner and record exceptions.
To recap: choose a baseline, keep it as code, apply it with idempotent tools, watch for drift, verify with a scanner, and document every exception. The labs let you audit a machine against CIS and harden a server with an Ansible role.
Practise this
- Audit a Linux box against a CIS benchmark with OpenSCAP
- Harden a Linux server with an Ansible role (an idempotent CIS-style baseline) — automation, Part 2
- Track /etc configuration drift with git and a Python differ
- Harden a Windows machine toward a CIS Benchmark baseline
Slides only get you so far. Pick one of these hands-on labs and do it today while it is fresh. Every lab is free.
Sources
- NIST SP 800-128: Security-Focused Configuration Management (with 2019 updates)
- CIS Benchmarks
- CIS Benchmarks FAQ
- Ansible glossary
- Terraform: Manage resource drift
- Packer introduction
- OpenSCAP User Manual
Every claim in this deck came from these pages, fetched and checked for this release. Read the originals.
Hands-on labs for this deck
- Audit a Linux box against a CIS benchmark with OpenSCAP
- Harden a Linux server with an Ansible role (an idempotent CIS-style baseline) — automation, Part 2
- Track /etc configuration drift with git and a Python differ
- Harden a Windows machine toward a CIS Benchmark baseline
Open the interactive deck or see all slide decks.