Two hundred settings, one afternoon
A hardening benchmark is a published list of settings that make a system harder to attack. Each comes with a reason, a way to check it and a way to fix it. It turns "make this server secure" into a list somebody can work through.
A college IT team is told to harden its new file server before it goes live. Nobody agrees what "hardened" means. A benchmark settles it. Work only on your own lab copy, and change a real server only with written permission from its owner.
By the end of this lesson
Note
I can audit a Linux server against benchmark items and record each result.
Key words
| Word | What it means | Example |
|---|---|---|
| Benchmark | a published list of recommended secure settings | |
| Recommendation | one item of a benchmark, with its reason, audit and fix | |
| Audit | reading the current value and comparing it with the expected one | |
| Remediation | the change that makes a failed item pass | |
| Exception | a failed item you keep on purpose, with a written reason | |
| Kernel parameter | a setting of the Linux kernel, read and set with sysctl | kernel.randomize_va_space |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsA benchmark is a list of checked settings
The best-known benchmarks are the CIS Benchmarks, written by the Center for Internet Security, a non-profit, with volunteers from industry. There is one for each common system, such as Ubuntu, Windows or a web server.
Each recommendation has the same parts: what to set, why, how to audit it and how to fix it. The text belongs to CIS, so this lesson describes it and writes its own checks.
The parts of one recommendation
| Part | What it says | An example of our own |
|---|---|---|
| Title | The setting to have | The server does not send network redirects |
| Profile | Level 1 or Level 2 | Level 1: safe on almost any server |
| Rationale | Why it matters | Only a router needs to send redirects |
| Audit | How to read the value now | sysctl net.ipv4.conf.all.send_redirects |
| Remediation | How to fix it | A file in /etc/sysctl.d that sets it to 0 |
The cells students fill in are left blank in this preview.
Check
Have a go
Check
3 questionsLevel 1 for all, Level 2 for more
A Level 1 item is sensible on almost every machine and rarely breaks anything. A Level 2 item suits a server that holds more sensitive data, and may stop something working.
Items are also automated or manual. An automated item can be checked by a script. A manual item needs a person to judge it, such as "only the services you need are running".
Audit first, and change nothing yet
An audit only reads. You run the check, write down what you found and compare it with the expected value. Nothing on the server changes.
That order matters. A team that fixes while it audits cannot say what the server was like before, and cannot undo a change that broke something. Run the four checks below.
Does the server send redirects?
sysctl net.ipv4.conf.all.send_redirectsShown here, not run. Press Start lab to type it into your own lab’s terminal.
Is memory layout randomised?
sysctl kernel.randomize_va_spaceShown here, not run. Press Start lab to type it into your own lab’s terminal.
Who may read the crontab and /tmp?
stat -c '%a %U %n' /etc/crontab /tmpShown here, not run. Press Start lab to type it into your own lab’s terminal.
How long may a password last?
grep '^PASS_MAX_DAYS' /etc/login.defsShown here, not run. Press Start lab to type it into your own lab’s terminal.
Worked example: the audit record
| Item | Expected | Found | Result |
|---|---|---|---|
| send_redirects | 0 | 1 | Fail |
| randomize_va_space | 2 | 2 | Pass |
| /etc/crontab mode | 600 | 644 | Fail |
| /tmp mode | 1777 | 1777 | Pass |
| PASS_MAX_DAYS | 365 or less | 99999 | Fail, or an exception |
The cells students fill in are left blank in this preview.
What did the terminal print?
Have a go
What did the terminal print?
3 questionsFix it, or write down why not
Each failed item gets one of two answers. Either you apply the remediation and audit again, or you record an exception: who decided, why, and when it is looked at again.
Password ageing is a good example. Forcing a change every year is in many benchmarks, but current UK guidance advises against it. A written exception is a fair answer.
Write the fix in a file
printf 'net.ipv4.conf.all.send_redirects = 0\n' | sudo tee /etc/sysctl.d/60-hardening.confShown here, not run. Press Start lab to type it into your own lab’s terminal.
Load that file now
sudo sysctl -p /etc/sysctl.d/60-hardening.confShown here, not run. Press Start lab to type it into your own lab’s terminal.
Only root may read the crontab
sudo chmod 600 /etc/crontabShown here, not run. Press Start lab to type it into your own lab’s terminal.
Audit both items again
stat -c '%a %U %n' /etc/crontabShown here, not run. Press Start lab to type it into your own lab’s terminal.
A common mistake
Warning
Many people think a hardened server scores 100 per cent on its benchmark. A Level 2 item can break a service the server exists to run. A failed item with a written reason is a decision, not a failure. Aim for every item fixed or excepted.
Score an audit, with exceptions
expected = {"send_redirects": "0", "crontab_mode": "600", "tmp_mode": "1777", "aslr": "2"}
found = {"send_redirects": "1", "crontab_mode": "644", "tmp_mode": "1777", "aslr": "2"}
exceptions = []
passed = 0
counted = 0
for name in expected:
if name in exceptions:
continue
counted = counted + 1
if found[name] == expected[name]:
passed = passed + 1
else:
print("FAIL", name, "found", found[name], "expected", expected[name])
print(passed, "of", counted, "pass")
# Put "crontab_mode" in exceptions and press Run again. What changes?
Make a free account to save your work and track your progress.
Match each word to its meaning
Have a go
Loading…
Your answers are marked here and are not kept. An account keeps your marks.
Assignment: score a benchmark audit
An audit reads settings from a server and compares them with a benchmark. expected is a dictionary from a setting's name to the value it should have; found is what the server gave, with spaces around some values. Write failed(expected, found). It returns the names, in the order of expected, whose value was not found or differs once spaces are stripped. Then write score(expected, found, exceptions). Leave out every name in exceptions, and return the whole percentage of the rest that pass, rounded down. If nothing is left to count, return 100.
def failed(expected, found):
# A missing setting fails too.
return []
def score(expected, found, exceptions):
# Exceptions are not counted at all.
return 0
Make a free account to save your work and track your progress.
Tools that audit for you
Nobody checks two hundred items by hand. CIS publishes its own checking tool. Ubuntu Pro has the Ubuntu Security Guide, which audits and fixes against the CIS profile. OpenSCAP and Lynis are free tools that audit against their own lists.
Each one runs checks like yours and writes a report. Lesson 6 turns your fixes into code.
Exit quiz
Have a go