You run the SUID search on a Linux host and get forty lines back. Thirty-nine of them are things you have seen before. This post is about the fortieth.

Find the list

find / -perm -4000 -type f 2>/dev/null 

This prints every file that runs with its owner's privileges. On a normal system you will see passwd, sudo, mount, su and similar. Compare the output with what the distribution ships. A binary in /usr/local/bin, /opt or a user's home directory is a custom build, and custom code is where mistakes live.

Read it before you run it

strings /usr/local/bin/backup-helper 

You are looking for command names inside the output. Suppose you see service apache2 restart. If that string is passed to system(), the program asks the shell to locate service by searching PATH. A full path such as /usr/sbin/service would be safe. A bare service is not.

Why that gives you root

PATH is part of the environment, and the person running the program controls their own environment. So you can do this:

  1. Create a directory you can write to, such as /tmp/x.
  2. Put an executable file named service in it that launches a shell.
  3. Run PATH=/tmp/x:$PATH /usr/local/bin/backup-helper.

The SUID program looks up service, finds yours first, and runs it with the file owner's rights. If root owns the file, you now have a root shell.

Some programs clear the environment or drop privileges first, and some shells refuse to keep elevated rights in certain setups, so test what the binary does rather than assuming.

A worked example of what you might see

Say strings prints a line such as tar czf /var/backups/site.tgz /var/www. The tar there has no leading path. That one line is the finding. Everything else in the file is context, and you can usually confirm it by running the binary and watching which commands it starts with ps or strace -f -e execve.

If instead the string reads /usr/bin/tar, move on. The author did it right, and your time is better spent on the next entry in the list. Reading a handful of strings per binary is quick, and it filters out most dead ends before you try anything.

The defensive side

The fix is short. Call helpers by absolute path, or do not shell out at all. Set the environment explicitly if you must. And once a quarter, run the same find on your own servers and ask who owns each custom result and why it is SUID.

Practise it

Codelivly has a free SUID treasure hunt CTF where you do the enumeration yourself. If you want a longer reference that carries on from enumeration through container escape and hardening, the Linux privilege escalation playbook is the book that continues this topic.