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 questionsBy the end of this lesson
Note
I can tell whether a setuid file or a capability lets an ordinary account reach root.
Key words
| Word | What it means | Example |
|---|---|---|
| Setuid bit | the s that makes a file run as its owner | chmod 4755 |
| Shell escape | starting a shell from inside a program | |
| Capability | one slice of root's power on a file | cap_setuid |
| getcap | the command that lists capabilities | |
| setcap | the command that sets or removes them | setcap -r file |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsSetuid 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
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 questionsTake 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
sudo chmod u-s /usr/local/bin/sysfind && stat -c "%a %n" /usr/local/bin/sysfindShown here, not run. Press Start lab to type it into your own lab’s terminal.
The escape no longer reaches root
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
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/pyadminShown here, not run. Press Start lab to type it into your own lab’s terminal.
Find it with getcap
getcap -r / 2>/dev/nullShown here, not run. Press Start lab to type it into your own lab’s terminal.
Try it: which capability reaches root?
# 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
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
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 questionsHow 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
| Weakness | How to spot it | How to close it |
|---|---|---|
| Setuid copy of a tool | find / -perm -4000 | chmod u-s, or delete the copy |
| cap_setuid on a file | getcap -r / | setcap -r on the file |
| A tool with a shell escape | check what it can do | remove 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.
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