You are in, as nobody important
An attacker who lands on a Linux machine rarely lands as root. They land as some ordinary account and look around.
Privilege escalation is going from that account to root. The first step is never a clever trick. It is a careful look at what the machine already tells you.
The one rule
Warning
Everything here happens inside your own lab machine, which has no route to anyone else. Doing this to a machine you do not own is a crime without written permission. In the UK that is the Computer Misuse Act 1990. Each country has its own such law.
Quick check: the rule
Have a go
Quick check: the rule
3 questionsBy the end of this lesson
Note
I can enumerate a Linux machine for the ways an ordinary account might reach root.
Key words
| Word | What it means | Example |
|---|---|---|
| Enumeration | listing what a machine shows about itself | |
| Privilege escalation | reaching an account with more power | |
| Setuid | a file that runs as its owner | -rwsr-xr-x |
| Capability | one slice of root's power on a file | cap_setuid |
| Finding | something worth reporting |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsStart with who you are
Before anything, learn the account you hold. id shows its user, its groups and its numbers. A group such as sudo, docker or lxd is a finding on its own.
Then ask what sudo lets it do. In the lab you can ask about the low-privilege account from your own shell.
Who is the low-privilege account?
sudo -u intern idShown here, not run. Press Start lab to type it into your own lab’s terminal.
What may it run with sudo?
sudo -l -U internShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsSetuid files and capabilities
A setuid file runs as its owner, so one owned by root runs as root. find / -perm -4000 lists them. Most are meant to be there, such as passwd; an odd one is a finding.
Capabilities are smaller slices of root's power pinned to a file. getcap -r / lists them.
List the setuid files
find / -perm -4000 -type f 2>/dev/nullShown here, not run. Press Start lab to type it into your own lab’s terminal.
List files with capabilities
getcap -r / 2>/dev/nullShown here, not run. Press Start lab to type it into your own lab’s terminal.
Try it: safe or worth a look?
# Each finding from the sweep, and whether it needs a second look.
suid = ["/usr/bin/passwd", "/usr/bin/sudo", "/usr/local/bin/sysfind"]
expected = {"/usr/bin/passwd", "/usr/bin/sudo", "/usr/bin/mount"}
for path in suid:
if path in expected:
print(path, "ships with the system")
else:
print(path, "is not a standard setuid file: look closer")
# Add "/usr/local/bin/sysfind" to expected. What changes, and should it?
Make a free account to save your work and track your progress.
A common mistake
Warning
Many people see a long list of setuid files and panic. Most are standard parts of the system and are meant to be setuid. The skill is spotting the one that is not: a copy in /usr/local, an interpreter, or a tool with a known escape.
Scheduled jobs and the kernel
Root often runs scripts on a timer. If an ordinary account can edit one, it can run code as root. The writable sweep finds those files.
Last, note the kernel and release. An old kernel is a finding you report, never one you exploit on a lab.
Find scheduled files anybody can write
find /opt /etc/cron.d /etc/systemd -perm -o+w -type f 2>/dev/nullShown here, not run. Press Start lab to type it into your own lab’s terminal.
Read root's own cron
sudo crontab -l -u rootShown here, not run. Press Start lab to type it into your own lab’s terminal.
Note the kernel and release
uname -r && grep PRETTY /etc/os-releaseShown here, not run. Press Start lab to type it into your own lab’s terminal.
What did the sweep show?
Have a go
What did the sweep show?
3 questionsHow a defender sees enumeration
A single find or getcap run looks like ordinary admin work, so enumeration is quiet. What a defender can do is remove the findings before an attacker lists them.
The fix for this lesson is a clean machine: no odd setuid files, no stray capabilities, no writable jobs. The rest of the module closes each one.
Assignment: sort the findings
An enumeration sweep gives you three lists. Write two functions.
odd_suid(paths, known) takes a list of setuid file paths and a set of paths known to ship with the system. It returns, in the order given, the paths not in known and not under /usr/bin/ or /usr/sbin/ (those two folders hold the system's own tools). writable_jobs(files) takes a list of dictionaries with path and mode (ten characters from ls -l, such as -rwxrwxrwx). It returns the sorted list of paths whose mode gives others write, that is, the ninth character is w.
def odd_suid(paths, known):
return []
def writable_jobs(files):
return []
Make a free account to save your work and track your progress.
Exit quiz
Have a go