Let the help desk edit one file
An admin wants the intern to edit a settings file as root without the root password. So they write a sudo rule for the editor.
It works. It also hands the intern a root shell, because an editor can run other programs. One rule, far more power than intended.
The one rule
Warning
This is your own lab machine. 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, and every country has a like law. The account, the rule and the goal here are all part of the lab.
Quick check: the rule
Have a go
Quick check: the rule
3 questionsBy the end of this lesson
Note
I can judge whether a sudo rule hands an account more power than it should.
Key words
| Word | What it means | Example |
|---|---|---|
| sudo rule | a line saying what an account may run as root | |
| NOPASSWD | the account runs it with no password | NOPASSWD: /usr/bin/vim |
| Shell escape | running a shell from inside another program | |
| sudoedit | the safe way to edit a file as root | |
| 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 questionsRead the rule first
From lesson 1 you know intern has a passwordless rule for vim. Read it again. A rule for a whole program, with no password, is wide: the account runs that program as root whenever it likes.
The question is always: what can that program do once it is root?
Read the sudo rule
sudo -l -U internShown here, not run. Press Start lab to type it into your own lab’s terminal.
Try it: which rules are risky?
# A sudo rule names what an account may run as root. Which of these is safe?
rules = ["/usr/bin/systemctl restart web", "/usr/bin/vim", "ALL", "/usr/bin/less /var/log/syslog"]
escapes = {"vim", "less", "more", "nano", "awk", "find", "env", "python3"}
for rule in rules:
name = rule.split("/")[-1].split()[0]
if rule == "ALL":
print(rule, "-> every command: never do this")
elif name in escapes:
print(rule, "-> this program can open a shell: risky")
else:
print(rule, "-> one named command: narrower")
# Add "/usr/bin/awk {}" to the rules. Which bucket does it fall in?
Make a free account to save your work and track your progress.
Check
Have a go
Check
3 questionsAn editor can run a shell
Most editors can run a command without leaving. In vim that is :!. Run as root, that command runs as root.
The Run below steps into intern and uses the rule to run id as root. It proves the escape without touching the flag, which is the lab's own task to reach.
Use the rule to run id as root
sudo -u intern bash -c "sudo -n vim -es -c ':! id' -c ':qa!' /etc/hostname"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
A common mistake
Warning
Many people think "sudo for one editor" is a small, safe grant. It is not. An editor, a pager, awk, find, env and most interpreters can all start a shell. A sudo rule for any of them is a sudo rule for everything.
Narrow the rule, or use sudoedit
The fix is least privilege. If the job is editing one file, use sudoedit, which opens a copy and never runs a shell. If the job is one command, name that exact command.
The Run replaces the wide rule with a sudoedit rule for one file and checks it parses.
Replace it with a sudoedit rule
printf 'intern ALL=(root) NOPASSWD: sudoedit /etc/motd\n' | sudo tee /etc/sudoers.d/90-lab-intern && sudo visudo -cf /etc/sudoers.d/90-lab-internShown here, not run. Press Start lab to type it into your own lab’s terminal.
The old escape is now refused
sudo -u intern bash -c "sudo -n vim -es -c ':! id' -c ':qa!' /etc/hostname"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
What did the fix do?
Have a go
What did the fix do?
3 questionsHow a defender sees it
Two things. First, review: sudo -l -U <account> lists what each account may run, so an audit finds a wide rule before an attacker does.
Second, watch: the Linux audit system can record every change to /etc/sudoers, and sudo itself logs each command it runs. Endpoint hardening sets both up.
Audit the rule after the fix
sudo -l -U internShown here, not run. Press Start lab to type it into your own lab’s terminal.
Worked example: wide and narrow rules
| Rule | Why | Better |
|---|---|---|
| NOPASSWD: /usr/bin/vim | An editor opens a shell | sudoedit for the one file |
| NOPASSWD: ALL | Every command as root | Name the exact commands |
| NOPASSWD: /usr/bin/systemctl | Can edit and start any service | systemctl restart one-service |
The cells students fill in are left blank in this preview.
Assignment: audit the sudo rules
Audit a set of sudo rules. Write risky(commands), where commands is a list of the command parts of sudo rules, such as "/usr/bin/vim" or "ALL". Return the sorted list of those that are risky. A rule is risky if it is exactly "ALL", or if the program (the last part of the path, before any space) is one of a set of programs that can open a shell: vim, vi, nano, less, more, awk, find, env, python3. Also write narrowest(rules), where rules is a dictionary from an account to its list of command parts. Return the account with the fewest risky rules, and on a tie the one that sorts first.
shell_out = {"vim", "vi", "nano", "less", "more", "awk", "find", "env", "python3"}
def risky(commands):
return []
def narrowest(rules):
return ""
Make a free account to save your work and track your progress.
Exit quiz
Have a go