63

Can anyone explain (or provide a link to a simple explanation) of what the Windows "Secure Desktop" mode is and how it works?

I just heard about it in the KeePass documentation (KeePass - Enter Master Key on a Secure Desktop) and would like to understand it better.

snth
  • 965
  • 1
  • 9
  • 10
  • 3
    Secure Desktop link was broken. FTFY. Also, FTR: Even though a "Secure Desktop" similar to UAC, *might* be able to protect you against software keyloggers, they will be ineffective against hardware keyloggers. You still should not enter in any passwords (or other data) that you value on a system you do not trust. – Iszi May 12 '11 at 13:16
  • @Iszi thanks for pointing that out. What are hardware keyloggers? Would that be like a specially engineered keyboard in an internet cafe? I'm mostly concerned about my Windows PC so I'm willing to trust that the hardware is fine but I am concerned about Malware installing itself. – snth May 16 '11 at 07:09
  • 1
    Hardware keyloggers come in many forms. It could be a dongle plugged in between the keyboard and the USB/PS2 port (anyone still using PS2?) or it could be an entire keyboard. There's been many a story told about people at security conventions having their passwords snatched up by the cleaning lady. – Iszi May 16 '11 at 07:49
  • I agree with the security comments of @iszi. The security of the keyboard in UAC mode vs keyloggers is not well documented. IF you have not falled prey to phishing or a Trojan horse program that has side effects, any LIMITED USER/Base user process will not be able to read the UAC Secure desktop keyboard input. BUT. beware. If a program sets the registry key disabling SecureDesktop when it runs its install (hopefully not possible with Install Sheild) UAC runs in the user context and any non-admin key logger grabs UAC content. (GOOD behavior detection AV catches keyloggers though) – BenPen Feb 27 '18 at 17:21

3 Answers3

70

Short answer

There are three, separate issues claiming the name of "Secure Desktop":

  • Windows builtin functions like GINA and the Credential Provider Model.
  • Separation of privileged vs unprivileged applications running as the same user (nominally prevent privilege escalation), which may or may not be related to:
  • SwitchDesktop(), which is what KeePass is using and may or may not (I'm not sure) be resistant to DLL Injection.

Detailed answer

As a quick primer to how Windows GUIs are built, basically everything runs through a function called CreateWindow() (I mean everything, every button, every menu, everything) and is given a hWnd or Window Handle. Modifying these Windows is done via another function, SendMessage().

Here's the catch. As a user mode application, making the right API calls I can fairly easily send messages to other Windows. It's fairly trivial to make buttons disappear from other people's forms. It is a little harder to perform DLL injection and hook the message loop that receives messages (the OS sends Windows messages when things happen to them) but not that much harder. If I can hook those events, I could automatically submit your "yes/no" form. Or, I could change the label from ReallyDodgyVirus.exe to explorer.exe and you'd be none the wiser.

Insert: A really good article on the various techniques of getting your code into the address space of a running process.

Now, what are KeePass doing?

A very brief perusal of the source shows they are using CreateDesktop(), SwitchDesktop() and CloseDesktop() to create a second desktop connected to the physical viewing device you're on. In English, they're asking the kernel to create for them an isolated desktop whose hWnd objects are outside of the findable range of any other application's SendMessage().

I should point out that SwitchDesktop suspends the updating of the UI of the default desktop. I'm not sure if the message loops are also frozen - I suspect not since the desktop is created as a new thread.

In this instance, KeePass is drawing the UI, so the execution is not, as I understand it, as NT AUTHORITY/SYSTEM. Instead, the new desktop is created in isolation from basically the rest of the current desktop, which protects it. I'll be happy to be corrected on that. However, see the MSDN for SwitchDesktop:

The SwitchDesktop function fails if the desktop belongs to an invisible window station. SwitchDesktop also fails when called from a process that is associated with a secured desktop such as the WinLogon and ScreenSaver desktops. Processes that are associated with a secured desktop include custom UserInit processes. Such calls typically fail with an "access denied" error.

I believe this means that these dialogs (screensavers, Windows Logon) are built more deeply into Windows such that they always execute as NT AUTHORITY\SYSTEM and the UserInit process creates the sub processes on valid authentication at the required privilege level.

The reason I bring this up is because I believe there are two issues: different desktops and privilege separation. From Mark Russinovich's discussion of the topic of Secure Desktop:

The Windows Integrity Mechanism and UIPI were designed to create a protective barrier around elevated applications. One of its original goals was to prevent software developers from taking shortcuts and leveraging already-elevated applications to accomplish administrative tasks. An application running with standard user rights cannot send synthetic mouse or keyboard inputs into an elevated application to make it do its bidding or inject code into an elevated application to perform administrative operations.

As SteveS says, UAC runs a separate desktop process as NT AUTHORITY/SYSTEM. If you can catch UAC in action (consent.exe) via process explorer, it looks like this:

UAC under process explorer

Escalating privileges as a process I don't have the specifics of, but here is what I think I understand: I believe the process of privilege escalation in the Windows API causes a process running as NT AUTHORITY/SYSTEM (therefore able to execute the new process under whatever privileges it wants to, in this case an Administrator). When an application asks for higher privileges, that question is asked to you on a new desktop locally, to which none of your applications can get either the Desktop Handle or any of the GUI element handles. When you consent, consent.exe creates the process as the privileged user. Thus, the process running as NT AUTHORITY\SYSTEM is a consequence of the need to create a new privileged process, not as a method of creating a secure desktop. The fact the desktop is different to the default is what adds security in both cases.

I believe what Mark means above is that, in addition to these secure desktops, two things are happening:

  • Your default administrator desktop is in fact running unprivileged, contrary to Windows XP and earlier and
  • Unprivileged and privileged applications now exist on separate desktops (disclaimer: could just be ACLs on the objects in memory, I'm not sure), ensuring that unprivileged code can't access privileged objects.

The Windows Logon UI is different again in Vista/7.

Clearly, none of these methods will defend you against kernel mode rootkits, but they do prevent privilege escalation and UI integrity compromise by isolating privileged applications, or in the case of KeePass, the sensitive dialog.

Edit

Having looked harder at the KeePass code, I saw this handy piece of C#:

Bitmap bmpBack = UIUtil.CreateScreenshot();
if(bmpBack != null) UIUtil.DimImage(bmpBack);
/* ... */

SecureThreadParams stp = new SecureThreadParams();
stp.BackgroundBitmap = bmpBack;
stp.ThreadDesktop = pNewDesktop;

From this you can see that in fact in order to mimic consent.exe, KeePass takes a screenshot of the background, dims it and creates its new desktop with the background of the old desktop. I therefore suspect the old desktop continues running even while it isn't being rendered. This I think confirms that no magic NT AUTHORITY\SYSTEM action is happening both with KeePass and consent.exe (I suspect consent.exe is doing the same thing UI-wise, it just happens to be launched in the context of NT AUTHORITY\SYSTEM).

Edit 2

When I say DLL Injection, I'm specifically thinking of DLL injection to corrupt the UI. DLL Injection remains possible on KeePass as a process, I'm just not sure whether it could be used to influence that secure UI. It could, however, be used to access the memory of the process and its threads, thereby grabbing the entered password pre-encryption. Hard, but I think possible. I'd appreciate someone advising on this if they know.

  • 1
    I don't know if that qualifies as a "simple explanation", but this is one of the better explanations I've read. :) I will edit my post as it's not entirely accurate with regards to only SYSTEM can create secure desktops. – Steve May 12 '11 at 16:21
  • @SteveS I think, if my understanding is right, there are two different types of desktop, "Secure" as in "part of Windows and only customisable via GINA, say" and "Secure" as in "isolated". I'm not 100% that separate desktops are created for unprivileged and privileged applications run by the same user but I think that is what Mark R is alluding to. Unfortunately, separating out these concepts becomes non trivial fairly quickly...! Your answer is fine I think, as for all intents and purposes the same ideas (at a high level) are used. –  May 12 '11 at 16:25
  • @Ninefingers - This is a great answer. However, could you perhaps provide a one-paragraph "TL;DR" summary at the top? – Iszi May 12 '11 at 17:20
  • @Iszi I edited in a `tl;dr` but have now put it to the top. –  May 12 '11 at 17:25
  • I really wish I could +1 this again, for the added detail in edits. @Ninefingers - Hope you don't mind, I edited the third "short answer" bullet to hopefully make it more clear - it appeared a little confusing to me. – Iszi May 12 '11 at 18:08
  • @Iszi I don't mind at all - I agree, moving the wording around is an improvement. –  May 12 '11 at 18:50
  • Nice answer!! One tiny confusion: what's a "message loop"? I'm guessing it is a typical windows term for what I'd call an event loop? – nealmcb May 12 '11 at 20:58
  • @nealmcb yes, basically. Every Window (usually top level actual Windows) has a function that is a call back that basically is continually called with all the events that happen on that window, like `WM_CLOSE` if the user clicks the `x` for example. Dll Injection allows you to hook in your own listeners, so you could hook `WM_KEYDOWN` and grab every key the user types in the context of an application... –  May 12 '11 at 21:02
  • Wow, I learned more about the Windows GUI than I had bargained for. Great answer! Thank you very much! – snth May 12 '11 at 21:30
  • 1
    @snth no problem. If you're interested in Windows internals, Mark Russinovich is the author of the tool in the screenshot (Process Explorer) and his blog is very much worth a read. He's so good, Microsoft hired him a couple of years' back. –  May 12 '11 at 23:21
  • 2.0 uses actual SecureDesktop, but is that something that can be run as a mortal user too and just prevents window switching to normal programs? It would take a system routine that could start an actual SecureDesktop in System user context, which could happen, but it would take some research to determine what KeePass 2.0 does, there... – BenPen Feb 27 '18 at 17:47
17

A "Secure Desktop" is a desktop that can only be run by the system itself. That sounds a bit weird, and probably doesn't explain much.

In Windows, a desktop is a view that allows you to interact with processes. When you log into Windows (the log in prompt) you are on a desktop. When you are logged in, and see the start menu, you are on a seperate desktop. When you lock your PC, you are on yet another desktop. When UAC pops up you are on another desktop. There are quite a few different desktops in Windows.

A Secure Desktop is a desktop that is out of scope of other applications accessibility. The Log In desktop is a secure desktop (created by winlogon.exe), as is the UAC desktop. No other process can interact with the desktop, so therefore no other process can do things like activate a button, or read the contents of a textbox. This is why UAC is (in theory) useful.

Third party applications can create a secure desktop to request information (such as a master password) and then pass it into the application in question. This way no other process can, in theory, snoop the password.

A good starter on secure desktops is the first half of this article on how UAC works with on the secure desktop: http://blogs.msdn.com/uac/archive/2006/05/03/589561.aspx.

mirh
  • 127
  • 7
Steve
  • 15,155
  • 3
  • 37
  • 66
  • 2
    Link is dead, here's the updated URL: https://docs.microsoft.com/en-us/archive/blogs/uac/user-account-control-prompts-on-the-secure-desktop – Miscreant Jan 22 '20 at 08:24
1

secure desktop runs under local system account and no other process can interact with it except OSK,Narrator etc,it is started by winlogon.exe and you can disable it in registry HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System by changing the value of PromptOnSecureDesktop from 1 to 0 if you run cmd.exe under system account it still won't interact with secure desktop and the dim desktop you see when you click run as administrator below UAC prompt is secure desktop and when you press ctrl+Alt+Del and the skyblue screen you see with few options like lock this compter etc is also secure desktop,windows by default has three desktop 1 winlogon 2 screensaver 3 userdesktop

Mudasir
  • 29
  • 2