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

Free lesson

Enumerating the machine

After this lesson: I can enumerate a Linux machine for the ways an ordinary account might reach root.

55 min lesson7 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

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 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 enumerate a Linux machine for the ways an ordinary account might reach root.

Key words

WordWhat it meansExample
Enumerationlisting what a machine shows about itself
Privilege escalationreaching an account with more power
Setuida file that runs as its owner-rwsr-xr-x
Capabilityone slice of root's power on a filecap_setuid
Findingsomething worth reporting

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

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

Command
sudo -u intern id

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

What may it run with sudo?

Command
sudo -l -U intern

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

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

Setuid 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

Command
find / -perm -4000 -type f 2>/dev/null

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

List files with capabilities

Command
getcap -r / 2>/dev/null

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

Try it: safe or worth a look?

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

Command
find /opt /etc/cron.d /etc/systemd -perm -o+w -type f 2>/dev/null

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

Read root's own cron

Command
sudo crontab -l -u root

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

Note the kernel and release

Command
uname -r && grep PRETTY /etc/os-release

Shown 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 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

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

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

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.