Dr. Daniel, M.D.

Q: Security issue? Gaining root is ridiculously easy.

Okay, so I noticed the other day that an app (forget which) that usually prompts for my password to gain root privileges didn't do so — it just continued doing what it needed the privileges for.

 

I remembered that shortly before, I had used sudo in iTerm to run a command with root privileges, and I know that OS X doesn't ask for your password after the first time you run sudo unless a number of minutes have passed. I had assumed that this behavior would be local to the thread from which you initially provided the password, which would've been the zsh session in iTerm. However, it seemed that it was local to the zsh session, nor to zsh, nor to iTerm. A completely different app had apparently piggy-backed on my sudo "session" and gained root privileges without my approval.

 

I tested this by issuing a sudo command in zsh in iTerm, and then, after having provided my password, I opened up Terminal with bash and issued a sudo command there. No password prompt, instant root privileges.

 

Based on this, it's clear that any app which runs as a user who can run sudo to gain root privileges (which is any normal OS X user) can wait for the user to execute sudo, and immediately gain root access to the system. Knowing when the current user runs sudo is easy, as such an event is written to the syslog.

 

Proof of concept. Quick-n-dirty. Save as a script and run in a terminal window. Then, run sudo in a different terminal window. The script will catch the sudo event and write the empty file "kilroy-was-here", as root:wheel, to the root of the drive.

 

#!/bin/bash

 

tail -f -n 0 /var/log/system.log | grep -m 1 -E 'sudo\[[0-9]+\]:\s+'$USER

echo "Gonna play around with root privs ..."

sudo touch /kilroy-was-here

 

This seems... Wrong... Thoughts?

 

Daniel

MacBook Air, OS X El Capitan (10.11.6), null

Posted on Jul 26, 2016 2:42 AM

Close

Q: Security issue? Gaining root is ridiculously easy.

  • All replies
  • Helpful answers

Page 1 of 3 last Next
  • by Barney-15E,

    Barney-15E Barney-15E Jul 26, 2016 7:22 AM in response to Dr. Daniel, M.D.
    Level 9 (50,094 points)
    Mac OS X
    Jul 26, 2016 7:22 AM in response to Dr. Daniel, M.D.

    Can you show a "Normal" (Standard) user gain root after an admin has used sudo?

  • by Jesse Trucks,

    Jesse Trucks Jesse Trucks Jul 26, 2016 7:41 AM in response to Dr. Daniel, M.D.
    Level 1 (21 points)
    Mac OS X
    Jul 26, 2016 7:41 AM in response to Dr. Daniel, M.D.

    That does seem like odd behavior from a normal unix, sudo, and security viewpoint.

     

    This is baffling after looking at it.

     

    sudo -V on my stock 10.11.6 system shows:

     

    Sudo version 1.7.10p9

     

     

    Configure args: --with-password-timeout=0 --disable-setreuid --with-env-editor --with-pam --with-libraries=bsm --with-noexec=no --sysconfdir=/private/etc --with-timedir=/var/db/sudo

    ...

    Authentication timestamp timeout: 5.0 minutes

    ...

    Path to authentication timestamp dir: /var/db/sudo

     

    This, in theory, should be fairly normal behavior. However, I have no timestamp file in /var/db/sudo/$username when I tested sudo. I cannot find any place where this timestamp file may reside, so far.

     

    This is both a mystery and disconcerting.

  • by Dr. Daniel, M.D.,

    Dr. Daniel, M.D. Dr. Daniel, M.D. Jul 26, 2016 9:00 AM in response to Barney-15E
    Level 1 (5 points)
    Mac OS X
    Jul 26, 2016 9:00 AM in response to Barney-15E

    Well, this is a standard user. The first user created when installing OS X has the ability to use sudo to elevate privileges.

  • by Dr. Daniel, M.D.,

    Dr. Daniel, M.D. Dr. Daniel, M.D. Jul 26, 2016 9:02 AM in response to Jesse Trucks
    Level 1 (5 points)
    Mac OS X
    Jul 26, 2016 9:02 AM in response to Jesse Trucks

    Jesse, did you try reproducing this? AFAICT, this was not due to me having brewed coreutils or anything, but I do have a ton of custom stuff on my system, so it might just be my bad OS.

     

    Judging from your response, you're a power user, so if you could replicate my findings, that'd be awesome

     

    Thanks!

  • by Barney-15E,

    Barney-15E Barney-15E Jul 26, 2016 9:07 PM in response to Dr. Daniel, M.D.
    Level 9 (50,094 points)
    Mac OS X
    Jul 26, 2016 9:07 PM in response to Dr. Daniel, M.D.

    Dr. Daniel, M.D. wrote:

     

    Well, this is a standard user. The first user created when installing OS X has the ability to use sudo to elevate privileges.

    Not what I meant, but the first user created has Admin privileges. A plain, Standard user cannot elevate their privileges using sudo unless you add them to the sudoers file (which can be done in the GUI by marking the allow user to administer the computer).

  • by etresoft,

    etresoft etresoft Jul 26, 2016 9:19 PM in response to Dr. Daniel, M.D.
    Level 7 (29,198 points)
    Mac OS X
    Jul 26, 2016 9:19 PM in response to Dr. Daniel, M.D.

    Hello Dr. Daniel,

    The sudo command is for advanced users. The expectation is that they know what else is running on their system at all times. If you are concerned about this, you can always run as a standard user and then you won't have sudo privileges. This what I use. If I need to use sudo, I have to su into my admin user and then run sudo from there.

     

    There are timestamp directories in /var/db/sudo as expected. There just isn't any $username variable.

  • by Dr. Daniel, M.D.,

    Dr. Daniel, M.D. Dr. Daniel, M.D. Jul 28, 2016 1:05 AM in response to Barney-15E
    Level 1 (5 points)
    Mac OS X
    Jul 28, 2016 1:05 AM in response to Barney-15E

    Okay, my bad for improper semantics. What I meant by "standard" was that it's the type of user that is created whenever someone starts using their Mac.

     

    So, to put it properly: Everyone will have this type of user after setting up their computer, and unless they're extremely vigilant and want to jump through hoops on a daily basis, they'll also be logged in as this user

  • by Dr. Daniel, M.D.,

    Dr. Daniel, M.D. Dr. Daniel, M.D. Jul 28, 2016 1:14 AM in response to etresoft
    Level 1 (5 points)
    Mac OS X
    Jul 28, 2016 1:14 AM in response to etresoft

    Hi etresoft,

     

    The sudo command isn't just for advanced users. It is also indirectly called by apps that require root access to do stuff (e.g. write images to USB sticks, mount encrypted partitions, etc.). The fact that it does this via a graphical user interface doesn't change the fact that it triggers a sudo session in the background which can be hijacked very easily (if you're logged in the way that arguably 99+% of Mac users are).

     

    Also, I don't agree with you that, "the expectation is that they know what else is running on their system at all times." That would just be a blanket excuse someone could apply anywhere to not bother dealing with security in one's own code. I don't remember sudo behaving this way when I used to run Linux as a desktop OS.

     

    Don't know what you mean by, "There are timestamp directories in /var/db/sudo as expected. There just isn't any $username variable."?

     

    Cheers

  • by Barney-15E,

    Barney-15E Barney-15E Jul 28, 2016 4:42 AM in response to Dr. Daniel, M.D.
    Level 9 (50,094 points)
    Mac OS X
    Jul 28, 2016 4:42 AM in response to Dr. Daniel, M.D.

    So, to put it properly: Everyone will have this type of user after setting up their computer, and unless they're extremely vigilant and want to jump through hoops on a daily basis, they'll also be logged in as this user

     

    No hoops to jump through at all. I'm not sure why you think there are some.

     

    However, how does this become an exploit. You have the ability to elevate your user's privileges. How does another user (i.e. Hacker) use your user to exploit this. Another user (i.e. Another process running on the Mac) can't exploit this so I'm not sure how it is a problem. If someone hacks into your Mac and logs in as your user, it doesn't matter whether there is a timeout or not.

     

    In your example script, your user is still running that script, it is not some rogue process.

  • by John Lockwood,

    John Lockwood John Lockwood Jul 28, 2016 5:10 AM in response to Dr. Daniel, M.D.
    Level 6 (9,309 points)
    Servers Enterprise
    Jul 28, 2016 5:10 AM in response to Dr. Daniel, M.D.

    If a Mac is owned and used in a home one could argue security is less of an issue. Therefore as you have observed the normal behaviour is that the first user account created is automatically given Administrator level privileges and hence is authorised to be a sudoer.

     

    However the proper practice in a business or education environment is the normal users e.g. staff, students, etc. are not given Administrator level accounts only 'standard' level accounts. As a result they do not have sudoer privileges. One could therefore argue the issue is not as big a problem as you perceive.

     

    It would still be worth you reporting it as a security concern because I agree with one aspect of your post, the time window where it caches sudo privileges should be as far as you and I are concerned limited to the original thread and not shared with other processes.

     

    See https://www.apple.com/uk/support/security/

  • by Barney-15E,

    Barney-15E Barney-15E Jul 28, 2016 5:43 AM in response to Dr. Daniel, M.D.
    Level 9 (50,094 points)
    Mac OS X
    Jul 28, 2016 5:43 AM in response to Dr. Daniel, M.D.

    If you wish to further secure sudo, see here: http://www.cnet.com/news/adjust-the-sudo-time-out-behavior-in-os-x/

  • by Tech198,

    Tech198 Tech198 Jul 28, 2016 6:03 AM in response to Dr. Daniel, M.D.
    Level 1 (48 points)
    Apple Pay
    Jul 28, 2016 6:03 AM in response to Dr. Daniel, M.D.

    haha... u call that "easy" ?  I would have a hard time doing that syntax, and i like to know more than the normal user.

     

    If i can't understand that, how do u expect a average user to know ? This is why Apple does things via Terminal

     

    to prevent most users getting access, but its there it they need it..

     

    I wouldn't call a "smart user" who can get "easily get root access" easy on the Mac ... Easy for you yes.. but hard for others.. if they must figure it out on themselves.

  • by BobHarris,

    BobHarris BobHarris Jul 28, 2016 6:07 AM in response to Dr. Daniel, M.D.
    Level 6 (19,432 points)
    Mac OS X
    Jul 28, 2016 6:07 AM in response to Dr. Daniel, M.D.

    When you issued the 'sudo' command, in your terminal session, you started a 5 minute timer for your 'admin' account to be able to issue the 'sudo' command again without a password.

     

    And when I say your 'admin' account, I mean every instance of your account.  So any other terminal sessions, your GUI login, etc... have 5 minutes to use the 'sudo' facilities without needing to specify a password.

     

    And the first account created on your Mac is always an 'admin' account.

     

    GUI applications running in your 'admin' account that need elevated privileges are using 'sudo' facilities under the covers.

     

    So your Terminal session's use of 'sudo' enabled your GUI apps use of 'sudo' because it is all you.  And you are the 'admin'

     

    If you wish the 'sudo' command to have a shorter timeout (not advised), you can change the /etc/sudoers file to include the 'passwd_timeout' parameter (see "man sudoers").  Note: mess up the /etc/sudoers file and you will not be able to get elevated privileges again, without restoring it, so be very careful about any changes you make to this file.

  • by Dr. Daniel, M.D.,

    Dr. Daniel, M.D. Dr. Daniel, M.D. Jul 28, 2016 6:23 AM in response to Barney-15E
    Level 1 (5 points)
    Mac OS X
    Jul 28, 2016 6:23 AM in response to Barney-15E

    Okay, first of all, let's get real here. I know of zero Mac users (including everyone at every place I've ever worked) who have created an extra, underprivileged user after having set up their Mac and logged in with the sudo-enabled user that is created by default, and is currently using that user to log in with. The majority of people don't know how to do that, let alone why they might want to.

     

    I know how to do that, but seriously can't be bothered having to deal with user switching throughout the day while developing and whatnot. And I shouldn't have to to avoid the issue I'm talking about right here. We can agree all day that your using a less privileged user for your sessions is more secure. But the reality is, that pretty much no one is. And if I'm interested in exploiting a vulnerability to get access to sensitive data en masse, I'm much more interested in the majority and the defaults than I am about seeking out people utilizing better practices.

     

    Now, back to the actual point.

     

    Barney-15E wrote:

     

    However, how does this become an exploit. You have the ability to elevate your user's privileges. How does another user (i.e. Hacker) use your user to exploit this. Another user (i.e. Another process running on the Mac) can't exploit this so I'm not sure how it is a problem. If someone hacks into your Mac and logs in as your user, it doesn't matter whether there is a timeout or not.

     

    No — that's exactly what I'm describing. The way sudo works (at least on my Capitan installation, which is why I'm posting here, since I haven't ruled out that it's just my system that's broken (please replicate if you can, and report back )) is that the "no-password-reprompt timeout period" is global. It's not local to whichever process owns the initial sudo elevation.

    In your example script, your user is still running that script, it is not some rogue process.

    No. My user is running the script, but once it detects that that user has successfully executed sudo anywhere else, it immediately issues a now password-prompt-free sudo elevated command which writes, as root:wheel, to the root of the filesystem. Any running app can monitor the syslog and issue shell commands as the user it runs under with the user being none the wiser. By waiting for the right time, it can gain root control without triggering any flags.

     

    I haven't actually tested if a privilege elevation in a GUI app might somehow be exempt from this behavior, but I suspect not. I'll try right now with TrueCrypt.

     

    Please, if you don't mind, do try to replicate the behavior from my original post on your system to see if it's just local to my installation. I really hope it is

     

    EDIT: I just tried to piggy-back from Terminal on a sudo which is executed through the TrueCrypt GUI. If you tell TC to mount an encrypted device, it'll prompt for your password in order to get the required root privileges to access block devices. Interesting result (although it makes perfect sense):

     

    TrueCrypt will first try to use sudo without a password. This generates a syslog sudo incident about "1 incorrect password attempt". TrueCrypt reacts on that by showing the dialog box prompt to input your password. Once you do that, I can steal root privileges from my script running in Terminal in the background without providing the password.

     

    My little script did react on the failed sudo attempt in syslog, which triggered a password prompt, but working around that is trivial — just do it properly using more than 30 seconds to write your code, or add a grep -v 'incorrect password attempt' filter to the watch command.

Page 1 of 3 last Next