4

Is there any way to positively identify that sudo is being used to execute a command other than to identify the logs? I phrase this in the present tense because I would like to make a command fail if it detects that someone else is using sudo to change users to run a command.

I've been looking at using ps to trace parent process IDs, but it doesn't appear to be reliable with symbolic links.

7
  • 4
    This feels like an XY problem. If using sudo is inappropriate why enable it? However, if you must, look at env | grep SUDO_ and see if any of those variables help. No guarantee, but may be a start. Unless you've users who sudo su - Commented May 4 at 15:54
  • The server I am working on is one which I do not have control over sudoers. I'll have to look at those environment variables though, those look promising at the first glance. (and as for sudo su, my present opinion is that I will have to deal with that at another layer, as I think root can fake out anything I might look at)
    – Cort Ammon
    Commented May 4 at 17:07
  • 1
    By the way, @ChrisDavies, even if using sudo -i or the awful sudo su, the child processes still have the SUDO_USER variable set and can be caught.
    – terdon
    Commented May 4 at 17:51
  • Do you only want commands run with sudo or anything that is running as root but owned by someone else? And only things using sudo to become root or any user? The more detail you give, the better we can tailor our answers to your needs.
    – terdon
    Commented May 4 at 18:02
  • 1
    Check out execsnoop.
    – Travis
    Commented May 10 at 0:34

1 Answer 1

8

You can play around with the environ file under /proc/$pid. This contains the environment of the process with PID $pid and for commands launched with sudo, that file should contain the SUDO_USER variable. So you can look for all processes being run as root whose environ files contain the string SUDO_USER=. Like this (note that the command below needs to run as root to have access to the environ files):

# pgrep -u root | 
  while read pid; do 
     if grep -aq SUDO_USER= /proc/$pid/environ; then
       printf "PID %s was launched via sudo\n" "$pid"
     fi
done

On my system, this looks like:

# pgrep -u root | while read pid; do if grep -aq SUDO_USER= /proc/$pid/environ; then printf "PID %s was launched via sudo\n" "$pid"; fi; done
PID 70650 was launched via sudo
PID 561158 was launched via sudo
PID 1841478 was launched via sudo
PID 2923409 was launched via sudo
PID 2925264 was launched via sudo
PID 2968491 was launched via sudo
PID 2974216 was launched via sudo

Note that this will also catch cases where a user has started a shell with sudo -i or even the horrible sudo su. Any commands launched in such shells will inherit the various SUDO_* variable from the parent.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .