Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Hardening a server against a benchmark

After this lesson: I can audit a Linux server against benchmark items and record each result.

60 min lesson8 commandsLinux administration lab: users, permissions, cron and systemd in your browser

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

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

WordWhat it meansExample
Benchmarka published list of recommended secure settings
Recommendationone item of a benchmark, with its reason, audit and fix
Auditreading the current value and comparing it with the expected one
Remediationthe change that makes a failed item pass
Exceptiona failed item you keep on purpose, with a written reason
Kernel parametera setting of the Linux kernel, read and set with sysctlkernel.randomize_va_space

The cells students fill in are left blank in this preview.

Starter

Have a go

Starter

4 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

A 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

PartWhat it saysAn example of our own
TitleThe setting to haveThe server does not send network redirects
ProfileLevel 1 or Level 2Level 1: safe on almost any server
RationaleWhy it mattersOnly a router needs to send redirects
AuditHow to read the value nowsysctl net.ipv4.conf.all.send_redirects
RemediationHow to fix itA 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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Level 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?

Command
sysctl net.ipv4.conf.all.send_redirects

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Is memory layout randomised?

Command
sysctl kernel.randomize_va_space

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Who may read the crontab and /tmp?

Command
stat -c '%a %U %n' /etc/crontab /tmp

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

How long may a password last?

Command
grep '^PASS_MAX_DAYS' /etc/login.defs

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Worked example: the audit record

ItemExpectedFoundResult
send_redirects01Fail
randomize_va_space22Pass
/etc/crontab mode600644Fail
/tmp mode17771777Pass
PASS_MAX_DAYS365 or less99999Fail, 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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Fix 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

Command
printf 'net.ipv4.conf.all.send_redirects = 0\n' | sudo tee /etc/sysctl.d/60-hardening.conf

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Load that file now

Command
sudo sysctl -p /etc/sysctl.d/60-hardening.conf

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Only root may read the crontab

Command
sudo chmod 600 /etc/crontab

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Audit both items again

Command
stat -c '%a %U %n' /etc/crontab

Shown 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

Code: python
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.

Code: python
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

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.