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

Free lesson

Writable services and timers

After this lesson: I can find a systemd service an ordinary account can turn into a root shell.

60 min lesson8 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

The modern scheduled job

Most Linux machines no longer lean on cron alone. systemd runs services, and timers start them on a schedule.

The old bug follows. If a service runs a script an ordinary account can edit, or a unit file it can rewrite, root will run whatever that account wants.

The one rule

Warning

This is your own lab machine. Editing a service on a machine you do not own is a crime without written permission. In the UK that is the Computer Misuse Act 1990. The service here is a practice you build on your own copy.

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 find a systemd service an ordinary account can turn into a root shell.

Key words

WordWhat it meansExample
Servicea program systemd runs and manages
Unit filethe file describing a servicelabmon.service
ExecStartthe program line a service runs
Timera unit that starts a service on a schedule
systemctlthe command that reads and controls services

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

Build a practice service

The Run sets up a service, labmon, whose program is a world-writable script, and a timer that starts it every half minute. An admin might leave the script writable by mistake.

This is on your own machine, built so you can see the weakness safely.

Set up the practice timer

Command
sudo mkdir -p /opt/labmon && printf '#!/bin/bash\ndate >> /opt/labmon/log\n' | sudo tee /opt/labmon/run.sh && sudo chmod 777 /opt/labmon/run.sh && printf '[Unit]\nDescription=Lab monitor\n[Service]\nType=oneshot\nExecStart=/opt/labmon/run.sh\n' | sudo tee /etc/systemd/system/labmon.service

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

Add the timer and start it

Command
printf '[Unit]\nDescription=Run lab monitor\n[Timer]\nOnBootSec=10s\nOnUnitActiveSec=30s\n[Install]\nWantedBy=timers.target\n' | sudo tee /etc/systemd/system/labmon.timer && sudo systemctl daemon-reload && sudo systemctl enable --now labmon.timer

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

Read the service like an attacker

systemctl cat prints a unit and its ExecStart line. systemctl list-timers shows what is scheduled. The question, as ever: can an ordinary account edit what root runs?

Read the ExecStart line, then check the mode of the file it names.

Read the service and its script's mode

Command
systemctl cat labmon.service --no-pager | grep ExecStart && stat -c "%a %U %n" /opt/labmon/run.sh

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

List the timers

Command
systemctl list-timers labmon.timer --no-pager

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

Try it: which file lets others win?

Code: python
# A unit file and its ExecStart script. Which file lets an ordinary account win?
files = {
    "/etc/systemd/system/labmon.service": "644 root",
    "/opt/labmon/run.sh": "777 root",
}

for path, info in files.items():
    mode, owner = info.split()
    others_write = mode[2] in "2367"
    if others_write:
        print(path, "-> anybody can edit it: root runs this")
    else:
        print(path, "-> only root can edit it")

# Change the unit file to "646 root". Does that open a second door?

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

Edit the script, let the timer run it

intern can write the script, so it adds a line. The timer starts the service as root within half a minute, running intern's line as root.

The Run adds the line and waits for the next tick.

intern edits the service's script

Command
sudo -u intern bash -c "echo 'id > /tmp/svc-proof.txt' >> /opt/labmon/run.sh"

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

Wait for the timer, then read the proof

Command
for i in $(seq 10); do [ -s /tmp/svc-proof.txt ] && break; sleep 5; done; cat /tmp/svc-proof.txt

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

A common mistake

Warning

Many people guard the unit file and forget the ExecStart script, or the other way round. Both matter. If an account can edit the unit, it changes what runs; if it can edit the script, it changes what the service does. Lock both.

Lock the unit and the script

The fix is ownership and mode. The unit file is root-owned at 644. The script is root-owned and not writable by others, 755, so the service still runs.

The Run tightens the script; intern can no longer edit it, and the timer still runs.

Lock the script, keep the service working

Command
sudo chmod 644 /etc/systemd/system/labmon.service && sudo chmod 755 /opt/labmon/run.sh && sudo -u intern bash -c "echo x >> /opt/labmon/run.sh" 2>&1

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

The timer still works

Command
systemctl is-active labmon.timer

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

systemd-analyze security <service> scores how exposed a service is, and endpoint hardening uses it. Watch /etc/systemd for changes, and keep a baseline of the ExecStart scripts.

The next lesson rolls the setuid, capability, cron and service checks into one baseline.

Assignment: audit the units

Audit a set of systemd units. Each is a dictionary with unit (the unit file path), unit_mode (ten characters), exec (the ExecStart path) and exec_mode (ten characters). Write others_write(mode), a helper returning True when the ninth character of a ten-character mode is w. Write weak_units(units): the sorted unit names (the last part of the unit path) whose unit file or ExecStart script is writable by others. Use others_write.

Code: python
def others_write(mode):
    return False


def weak_units(units):
    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.