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 questionsBy the end of this lesson
Note
I can fix the common privilege-escalation gaps and detect when one returns.
Key words
| Word | What it means |
|---|---|
| Baseline | a record of a clean machine to compare against |
| File integrity | checking files have not changed |
| Hardening | closing gaps before an attacker finds them |
| Audit log | the kernel's record of who did what |
| Least privilege | giving only the power a job needs |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsThe 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
| Gap | Spot it | Fix it |
|---|---|---|
| Odd setuid file | find / -perm -4000 | chmod u-s, or delete it |
| Dangerous capability | getcap -r / | setcap -r on the file |
| Wide sudo rule | sudo -l -U account | sudoedit, or an exact command |
| Writable cron script | find -perm -o+w | root owner, 755, safe folder |
| Writable service | systemctl cat, check modes | root owner, 644 unit, 755 script |
The cells students fill in are left blank in this preview.
Check
Have a go
Check
3 questionsA 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
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.shShown here, not run. Press Start lab to type it into your own lab’s terminal.
Take the baseline
sudo /usr/local/sbin/privcheck.sh | sudo tee /root/priv.baseline | wc -lShown here, not run. Press Start lab to type it into your own lab’s terminal.
An attacker adds a setuid copy
sudo cp /usr/bin/awk /usr/local/bin/sysawk && sudo chmod 4755 /usr/local/bin/sysawkShown here, not run. Press Start lab to type it into your own lab’s terminal.
What is new since the baseline?
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
# 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
| Gap | In a baseline | In the logs |
|---|---|---|
| New setuid file | A new line | An audit write to the folder |
| sudo escape | No change | A sudo command in the auth log |
| Changed cron script | A new hash | An audit write to the file |
The cells students fill in are left blank in this preview.
Check
Have a go
Check
3 questionsAssignment: 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.
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