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

Free lesson

Setuid binaries and capabilities

After this lesson: I can tell whether a setuid file or a capability lets an ordinary account reach root.

60 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

One copied tool, one slip

Someone wanted the intern to search the logs without the root password, so they made a copy of find that runs as root.

But find can run other programs. A copy of find that runs as root is a root shell for anyone who can start it.

The one rule

Warning

This is your own lab machine. Using a setuid escape on a machine you do not own is a crime without written permission. In the UK that is the Computer Misuse Act 1990. The tools and the account here are the lab's.

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 tell whether a setuid file or a capability lets an ordinary account reach root.

Key words

WordWhat it meansExample
Setuid bitthe s that makes a file run as its ownerchmod 4755
Shell escapestarting a shell from inside a program
Capabilityone slice of root's power on a filecap_setuid
getcapthe command that lists capabilities
setcapthe command that sets or removes themsetcap -r file

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

Setuid find runs code as root

From lesson 1 you found /usr/local/bin/sysfind, a setuid copy of find. find has a -exec option that runs a command for each match. Started as root, that command runs as root.

The Run matches one thing and runs id. It proves root and reads nothing it should not.

Run id as root through sysfind

Command
sudo -u intern /usr/local/bin/sysfind . -maxdepth 0 -exec id \;

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

Take the setuid bit off

The fix is to remove the setuid bit with chmod u-s, or to delete the copy. The real find in /usr/bin is not setuid and loses nothing.

The Run clears the bit, then runs the escape again: id now shows only intern.

Clear the setuid bit

Command
sudo chmod u-s /usr/local/bin/sysfind && stat -c "%a %n" /usr/local/bin/sysfind

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

The escape no longer reaches root

Command
sudo -u intern /usr/local/bin/sysfind . -maxdepth 0 -exec id \;

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

A common mistake

Warning

Many people think only a few named programs are dangerous when setuid. Any program that can run another program, read any file or write any file is dangerous setuid. Check each odd one against what it can do, not against a short list.

Capabilities: smaller slices of root

Modern Linux can give a file one slice of root's power instead of the whole setuid bit. cap_setuid, for one, lets a program change its own user id to root.

The lab has none, so the Run sets up a practice copy of python with cap_setuid, then lists it. This is on your own machine.

Set up a practice capability

Command
sudo apt-get install -y -qq libcap2-bin && sudo cp /usr/bin/python3 /usr/local/bin/pyadmin && sudo setcap cap_setuid+ep /usr/local/bin/pyadmin

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

Find it with getcap

Command
getcap -r / 2>/dev/null

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

Try it: which capability reaches root?

Code: python
# A capability pins one slice of root to a file. Which of these lets an account become root?
caps = {
    "/usr/bin/ping": "cap_net_raw",
    "/usr/local/bin/pyadmin": "cap_setuid",
    "/usr/bin/tar": "cap_dac_read_search",
}
dangerous = {"cap_setuid", "cap_setgid", "cap_dac_override", "cap_sys_admin"}

for path, cap in caps.items():
    mark = "becomes root" if cap in dangerous else "limited"
    print(path, cap, "->", mark)

# cap_dac_read_search reads any file but does not become root. Is that still a finding?

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

Use cap_setuid to become root

Command
sudo -u intern /usr/local/bin/pyadmin -c 'import os; os.setuid(0); os.system("id")'

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

Remove the capability

Command
sudo setcap -r /usr/local/bin/pyadmin && (getcap /usr/local/bin/pyadmin || echo "no capabilities")

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

What did the capability show?

Have a go

What did the capability 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 it

Keep a baseline. Record the setuid files and capabilities on a clean machine, and compare it later; a new line is a finding.

The next lessons do the same for scheduled jobs, and the last lesson builds the baseline into one check.

The two weaknesses and the fix

WeaknessHow to spot itHow to close it
Setuid copy of a toolfind / -perm -4000chmod u-s, or delete the copy
cap_setuid on a filegetcap -r /setcap -r on the file
A tool with a shell escapecheck what it can doremove the grant

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

Assignment: flag the risky files

Flag the risky setuid files and capabilities. Write risky_suid(files), where files is a list of dictionaries with path and owner. Return the sorted paths owned by "root" whose program name (the last part of the path) can open a shell or read or write any file: one of find, vim, awk, less, python3, cp, dd. Also write risky_caps(caps), where caps is a dictionary from a path to its capability name. Return the sorted paths whose capability is one of cap_setuid, cap_setgid, cap_dac_override or cap_sys_admin.

Code: python
shell_or_file = {"find", "vim", "awk", "less", "python3", "cp", "dd"}
dangerous_caps = {"cap_setuid", "cap_setgid", "cap_dac_override", "cap_sys_admin"}


def risky_suid(files):
    return []


def risky_caps(caps):
    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.