If a Windows cmd.exe is running with elevated privileges does anything I execute from its prompt also run with elevated privileges?

17
2014-03
  • Ian C.

    If my cmd.exe Window says "Administrator" in the title bar, indicating it was started with elevated privileges, does this mean anything I execute from this command window is also run with elevated privileges?

    Specifically, if I run something like:

    msiexec SomeProgram.msi
    

    is my installer being run with elevated privileges because it was executed from a cmd.exe that was running with elevated privileges?

    More specifically: I'm wondering if applications that present a UI and return the prompt in the cmd.exe window right away, like the msiexec call up above, are executing with elevated privileges.

  • Answers
  • Tanner

    Yes, it does execute with elevated privileges.

    Simple test:

    You can test this quite easily by opening one elevated and one non-elevated command prompt. Run the command notepad.exe in both, and try saving a blank text file to C:\Windows. One will save, one will throw a permissions error.

    Thorough test:

    If that's not enough to confirm it for you (it didn't really satisfy me) you can use AccessChk from SysInternals. You'll need to run this from an elevated command prompt.

    Lets start by checking out the two Notepad processes that are running:

    Notepad: (accesschk.exe -v -p notepad)

    [11140] notepad.exe
      Medium Mandatory Level [No-Write-Up, No-Read-Up]
      RW EAGLEGROUP\Tannerf
            PROCESS_ALL_ACCESS
      RW NT AUTHORITY\SYSTEM
            PROCESS_ALL_ACCESS
    [11004] notepad.exe
      High Mandatory Level [No-Write-Up, No-Read-Up]
      RW BUILTIN\Administrators
            PROCESS_ALL_ACCESS
      RW NT AUTHORITY\SYSTEM
            PROCESS_ALL_ACCESS
    

    One is running under my domain username, the other is running under the Administrators built-in group. It also has a high mandatory level. You can also run with the -f flag for a breakdown of the privileges and tokens.

    MSIExec and MSI files

    I thought things might get a little more complicated when running msiexec. I have a Google Chrome standalone installer that was handy to test.

    msiexec.exe launching Chrome installer from elevated prompt:

    D:\Users\tannerf>accesschk.exe -p msiexec.exe
    
    [10540] msiexec.exe
      RW BUILTIN\Administrators
      RW NT AUTHORITY\SYSTEM
    

    chrome_installer.exe spawned by MSI:

    D:\Users\tannerf>accesschk.exe -p chrome_installer.exe
    
    [5552] chrome_installer.exe
         NT AUTHORITY\SYSTEM
         OWNER RIGHTS
      RW NT SERVICE\msiserver
    

    Not so cut and dry anymore! Looks like a chrome_installer.exe processes was run through the MSIServer service.


    This makes me wonder what behavior other installers might have, so I ran an Evernote.msi I had handy:

    Elevated msiexec.exe launching an Evernote installer:

    [6916] msiexec.exe
      High Mandatory Level [No-Write-Up, No-Read-Up]
      RW BUILTIN\Administrators
            PROCESS_ALL_ACCESS
      RW NT AUTHORITY\SYSTEM
            PROCESS_ALL_ACCESS
    [4652] msiexec.exe
      System Mandatory Level [No-Write-Up, No-Read-Up]
      R  BUILTIN\Administrators
            PROCESS_QUERY_INFORMATION
            PROCESS_QUERY_LIMITED_INFORMATION
    

    Interesting; there's an msiexec.exe that's run under system level this time. I used Process Monitor to find that the actual install window that pops up comes from the system level msiexec process. Killing the high mandatory level also killed the system level process.

    Non-elevated msiexec.exe launching an Evernote installer:

    [7472] msiexec.exe
      Medium Mandatory Level [No-Write-Up, No-Read-Up]
      RW EAGLEGROUP\Tannerf
            PROCESS_ALL_ACCESS
      RW NT AUTHORITY\SYSTEM
            PROCESS_ALL_ACCESS
    [4404] msiexec.exe
      System Mandatory Level [No-Write-Up, No-Read-Up]
      R  BUILTIN\Administrators
            PROCESS_QUERY_INFORMATION
            PROCESS_QUERY_LIMITED_INFORMATION
    

    Looks like Evernote will get system level access either way. Double-clicking the installer has the same result.


    Conclusion:

    I think it's pretty well demonstrated that a processes will inherit permissions unless otherwise specified. That doesn't guarantee msiexec SomeProgram.msi will run with a high mandatory level across all processes processes; it could run under system level or under MSIServer. Your mileage may vary, and I wouldn't be surprised to see many instances where these rules seem to be "broken".

  • Bob

    By default, Windows processes will inherit their security context from the parent:

    The ACLs in the default security descriptor for a process come from the primary or impersonation token of the creator.

    MSDN on process security and access rights

    It is, however, possible to spawn processes with less privileges:

    While processes inherit the integrity level of the process that spawned it, the integrity level can be customized at the time of process creation. As well as for defining the boundary for window messages in the User Interface Privilege Isolation technology, Mandatory Integrity Control is used by applications like Windows Explorer, Internet Explorer, Google Chrome and Adobe Reader to isolate documents from vulnerable objects in the system.

    Wikipedia on Mandatory Integrity Control relating to this other MSDN page, also mentioned here. Another presentation also mentions process inheritance.

    However, I believe cmd.exe will launch child processes with the greatest level of privilege inheritance possible, as @Tanner's testing and answer shows.

  • Tobias Kienzler

    There may be two ways to un-elevate the priviledges of the executed command:

    • runas /trustlevel:0x20000 "msiexec SomeProgram.msi" (run runas /showtrustlevels to learn that 0x20000 is the default user trustlevel - this even works for installing/running programs that "require" elevated privileges - without actually granting them when run as admin. This passes Tanner's notepad test) as per this SU answer
    • psexec -l -d msiexec SomeProgram.msi per this SU answer (maybe some "" are required as well, I didn't test this since runas works fine enough for me)

  • Related Question

    windows 7 - Does "Run as administator" really do anything if UAC is off?
  • cowgod

    I know that User Account Control should not be turned off, but let's just say it's off for the sake of argument. With UAC off, I still have the option to "Run as administrator" on executable files, even though I have an Administrator account type. Additionally, there is a checkbox under the Compatability tab (via Properties) labeled Run this program as an administrator. Do either of these options actually do anything if UAC is off?


  • Related Answers
  • 8088

    Yes. When you use the slider to set UAC to "Never notify", it's not really turning UAC off - it's just turning the UAC notification level down to zero. UAC is still on and UAC events still appear in the Windows event log. If you have the slider all the way down so that you get no UAC notifications and then do a "Run this program as an administrator", UAC will still elevate the user's privileges in order to perform the action. It will just be done silently.

    enter image description here

  • William Hilsum

    By default, UAC removes certain abilities from a administrator. These include the ability to write to restricted paths amongst other things. Run as administrator gives the process the additional security privileges needed to accomplish theses tasks.

    Turning of UAC basically does the same job as running as administrator (when using an account with administrator privileges) and the reason for it being on the menu is probably the same reason as having Paste always visible but sometimes disabled - it is more trouble to remove it...

    The reason for it being there in properties is so the setting gets saved - if you ever turn UAC back on, it will remember that setting.

  • A Dwarf

    Running as Administrator with UAC turned off is useful for those situations when you log to Windows on a non administrator account. As you might expect certain applications or certain actions require administrator privileges.

    With UAC turned off, user level permissions at the file and directory level are still in effect and Run As Administrator becomes a useful tool in order to gain access.

  • 8088

    I still have the option to "Run as administrator" on executable files, even though I have an Administrator account type.

    To clarify this: "Run as administrator" is not the same as running a program from an account with administrator rights - it means to execute a program from the built-in Administrator account which in Windows Vista/7 is disabled by default (not visible).

    You really shouldn't use this account for anything other than troubleshooting. In fact, you probably shouldn't use it at all.

    For some reason, good old "Run as ..." is now hidden; to access this option, press Shift and right click the shortcut/executable to get this:

    enter image description here

    enter image description here

    UAC is in no way related to "Run as administrator" except you will not receive a warning when using the built-in Administrator account, if UAC is disabled or set to "Never notify".