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

Free lesson

A sudo rule that gives too much

After this lesson: I can judge whether a sudo rule hands an account more power than it should.

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

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 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 judge whether a sudo rule hands an account more power than it should.

Key words

WordWhat it meansExample
sudo rulea line saying what an account may run as root
NOPASSWDthe account runs it with no passwordNOPASSWD: /usr/bin/vim
Shell escaperunning a shell from inside another program
sudoeditthe safe way to edit a file as root
Least privilegegiving only the power a job needs

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

Read 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

Command
sudo -l -U intern

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

Try it: which rules are risky?

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

An 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

Command
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

Command
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-intern

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

The old escape is now refused

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

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

Command
sudo -l -U intern

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

Worked example: wide and narrow rules

RuleWhyBetter
NOPASSWD: /usr/bin/vimAn editor opens a shellsudoedit for the one file
NOPASSWD: ALLEvery command as rootName the exact commands
NOPASSWD: /usr/bin/systemctlCan edit and start any servicesystemctl 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.

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

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.