Sudo Binaries

Theory

On this page, we speak about vulnerabilities within a sudo binary/script itself. Custom or known binaries/scripts may be exploited and allow us to subvert sudo's intended functionality.

Practice

Known Sudo Binaries Exploits

If the binary is allowed to run as sudo, it does not drop the elevated privileges and may be used to access the file system, escalate or maintain privileged access.

If you find that a binary can be run as sudo, you can check on GTFOBins for known exploits.

$ sudo -l

User demo may run the following commands on crashlab:
    (root): /usr/bin/awk

No Command Path Exploit

If a sudo binary/script executes another command without specifying the path. We can abuse it and get a privilege escalation.

Note that env_reset and secure_path should not be set !

You may use strings to spot other binaries calls, or do some reverse engineering on the sudo binary.

$ sudo -l
User demo may run the following commands on crashlab:
    (root): /opt/bin/the-sudo-bin

$ strings ./the-sudo-bin
...
find
...

Shared Library Hijacking

If you find some binary with Sudo permissions, you can check if all the .so files are loaded correctly.

strace the-sudo-bin 2>&1 | grep -i -E "open|access|no such file"

You also could check if the Sudo binary is loading a library from a folder where we can write:

# Lets find a Sudo using a non-standard library
ldd the-sudo-bin
something.so => /lib/x86_64-linux-gnu/something.so

# The Sudo also loads libraries from a custom location where we can write
readelf -d the-sudo-bin | grep PATH
0x000000000000001d (RUNPATH)            Library runpath: [/development]

Alternatively, you could use the strings command to find used shared library

strings ./the-sudo-bin | grep -i '*.so*'

Weak File Permissions

If you find some binary/script with Sudo permissions, you could check if you have enought rights to overwrite it. If so, you can replace it by a malicious one.

sudo -l
        (root) NOPASSWD: /usr/bin/python3 /home/<username>/example.py

Check permissions:

ls -la /home/<username>/
ls -la /home/<username>/example.py

References

Last updated