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

Free lesson

Fixing and detecting it all

After this lesson: I can fix the common privilege-escalation gaps and detect when one returns.

55 min lesson4 commandsLinux privilege escalation lab: a setuid binary, a sudo rule and a cron job to break

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

Already have an account? Sign in

Now play the defender

You have reached root five ways on your own machine. The valuable half of the skill is the other direction: closing each gap, and spotting the next one.

A good security report does both. It says what was wrong, how to fix it, and how to notice if it comes back.

The one rule

Warning

Everything here stays on your own lab machine. The skills here are for systems you own or may test with written permission. In the UK that is the Computer Misuse Act 1990. Defending is where they belong.

Quick check: the rule

Have a go

Quick check: the rule

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

By the end of this lesson

Note

I can fix the common privilege-escalation gaps and detect when one returns.

Key words

WordWhat it means
Baselinea record of a clean machine to compare against
File integritychecking files have not changed
Hardeningclosing gaps before an attacker finds them
Audit logthe kernel's record of who did what
Least privilegegiving only the power a job needs

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

The fixes, in one place

Each weakness in this module has a one-line fix. Together they are a hardening checklist you can run on any machine.

The table gathers them. The point is the pattern: remove the grant, or narrow it to exactly the job.

The five gaps and their fixes

GapSpot itFix it
Odd setuid filefind / -perm -4000chmod u-s, or delete it
Dangerous capabilitygetcap -r /setcap -r on the file
Wide sudo rulesudo -l -U accountsudoedit, or an exact command
Writable cron scriptfind -perm -o+wroot owner, 755, safe folder
Writable servicesystemctl cat, check modesroot owner, 644 unit, 755 script

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

A baseline finds the next one

You cannot watch every file by hand. Record the setuid files, capabilities and writable scheduled scripts on a clean machine, and compare later.

The Run writes a small check, takes a baseline, then an attacker adds a setuid copy and the check spots it.

Write the baseline check

Command
sudo tee /usr/local/sbin/privcheck.sh >/dev/null <<'EOS'
#!/bin/bash
{ find / -xdev -perm -4000 -type f 2>/dev/null
  getcap -r / 2>/dev/null | cut -d' ' -f1
  find /opt /etc/cron.d /etc/systemd -perm -o+w -type f 2>/dev/null
} | sort -u
EOS
sudo chmod 755 /usr/local/sbin/privcheck.sh

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

Take the baseline

Command
sudo /usr/local/sbin/privcheck.sh | sudo tee /root/priv.baseline | wc -l

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

An attacker adds a setuid copy

Command
sudo cp /usr/bin/awk /usr/local/bin/sysawk && sudo chmod 4755 /usr/local/bin/sysawk

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

What is new since the baseline?

Command
diff <(sudo /usr/local/sbin/privcheck.sh) /root/priv.baseline | grep '^<'

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

Try it: new and gone

Code: python
# A baseline of risky files, and the machine today. What is new?
baseline = {"/usr/bin/passwd", "/usr/bin/sudo", "/usr/local/bin/sysfind"}
today = {"/usr/bin/passwd", "/usr/bin/sudo", "/usr/local/bin/sysfind", "/usr/local/bin/sysawk"}

added = sorted(today - baseline)
removed = sorted(baseline - today)
print("new since the baseline:", added)
print("gone since the baseline:", removed)

# Remove sysfind from today (you fixed it). What does removed show, and is that good?

Make a free account to save your work and track your progress.

A common mistake

Warning

Many people take the baseline after the machine has run for a while, so it already holds the attacker's file. A baseline is only worth taking on a machine you trust, right after it is built and hardened.

The logs and the audit system

A baseline finds a changed file. Logs find an action as it happens. sudo records every command it runs to the authentication log, so a sudo escape leaves a trail.

The Linux audit system can watch /etc/sudoers, the setuid folders and the service files, and record every write. Endpoint hardening sets it up.

Where each gap shows up

GapIn a baselineIn the logs
New setuid fileA new lineAn audit write to the folder
sudo escapeNo changeA sudo command in the auth log
Changed cron scriptA new hashAn audit write to the file

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

Assignment: compare to a baseline

Compare a machine to its baseline. Write added(baseline, today), returning the sorted list of entries in today but not in baseline (both are lists of file paths). Write report(baseline, today), returning a dictionary with keys "new" (the sorted added entries) and "gone" (the sorted entries in baseline but not in today). A new setuid file is a worry; a gone one is usually a fix.

Code: python
def added(baseline, today):
    return []


def report(baseline, today):
    return {"new": [], "gone": []}

Make a free account to save your work and track your progress.

Exit quiz

Have a go

Exit quiz

6 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.