Skip to content

Lesson 2 of 9 · 21m

Reading sudo Rules Like an Auditor

Parse sudoers output precisely, understand aliases and ordering, and separate benign administration access from exploitable ambiguity.

Most sudo abuse begins with a misunderstanding. The tester sees NOPASSWD and reaches for GTFOBins before reading the rule. Sudo is a policy engine with ordering, aliases, tags, command matching, and shell expansion traps. If you cannot explain which exact command line is permitted, you are not exploiting sudo — you are guessing at it.

Start with the policy as the machine evaluates it

sudo -l

A realistic result might look like this:

User analyst may run the following commands on web01:
    (root) NOPASSWD: /usr/bin/systemctl restart nginx
    (root) /usr/bin/journalctl -u nginx
    (deploy) NOPASSWD: /usr/bin/git -C /srv/www pull

Read every field. The run-as user matters. NOPASSWD matters. The exact binary path matters. Arguments matter when present. A rule for /usr/bin/systemctl restart nginx is not permission to run arbitrary systemctl subcommands unless matching is sloppy.

Ordering and aliases hide surprises

In sudoers, later matching entries can override earlier ones. Aliases compress policy and hide breadth:

User_Alias OPS = alice, bob
Cmnd_Alias SERVICES = /usr/bin/systemctl * nginx, /usr/bin/systemctl * php-fpm
OPS ALL=(root) NOPASSWD: SERVICES

The wildcard in * nginx is not a regular expression. It is shell-style command-line matching. Ask what whitespace and additional arguments can do. Ask whether paths can be influenced. Ask whether the command invokes other programs that inherit elevated trust.

Test without breaking production

You can often use --list with another user or inspect the exact command shape before executing:

sudo -l -U alice 2>/dev/null
sudo -n /usr/bin/systemctl restart nginx
sudo -u deploy /usr/bin/git -C /srv/www pull

-n prevents an interactive password prompt from hanging automation. A failure is information: perhaps a password is required, perhaps the terminal is required, perhaps the command line does not match.

Environment and execution context are part of the rule

Important sudoers controls include:

Defaults env_reset
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
Defaults !env_keep

If env_reset is missing or broad env_keep entries exist, variables such as LD_PRELOAD, LD_LIBRARY_PATH, PYTHONPATH, PERL5LIB, or EDITOR can become execution paths. Modern sudo strips many dangerous linker variables, but application-specific variables still matter.

Defense side: write commands that cannot become shells

Good sudo policy uses absolute paths, narrow arguments, non-interactive programs, and no broad wildcards. Prefer wrapper scripts owned by root, in root-owned directories, with fixed logic and strict input validation. Better still, avoid privileged shelling out entirely and use a service API with its own authorization model.

analyst ALL=(root) NOPASSWD: /usr/local/sbin/restart-nginx-safe

The wrapper must not trust user-controlled paths, environment variables, or unvalidated filenames. Moving the complexity into a script does not remove it; it concentrates it where review can happen.

Field notes

  • sudo -l output can differ by host because sudoers supports host matching. Re-check after pivoting.
  • A passwordless rule is not automatically a vulnerability; operational function and blast radius decide that.
  • Never edit sudoers with a plain text editor on a production machine. Use visudo so syntax checking can save you.
  • sudo logging is useful only if command arguments are captured and shipped somewhere the user cannot erase.
  • A root shell through sudo is authorized in some administrations and catastrophic in others. Scope and intent matter.

Was this lesson helpful?

Sign in to rate