The winlogon secure desktop is really just another desktop on winsta0, and I came across a utility - RemoteUnlock.exe - which injects itself as a thread in the console winlogon process of a remote machine and switches from the secure winlogon desktop to the logged on user’s winsta0\default desktop.
This allows you to skip the SAS process requiring the account password of the currently logged on user, and just shows the desktop, allowing full console interaction as the logged on user.
This doesn't really have very many practical uses, but the concept is great and I thought I would share the utility I came across plus some additional thoughts on the topic.
The utility and source code:
http://www.codeproject.com/KB/system/RemoteUnlock.aspx
I've previously toyed with creating a command shell on a remote winlogon desktop, which can be done with the following command:
- psexec /s \\%computer% cmd /c c:\windows\temp\psexec /accepteula /x /d /s cmd
After another discussion regarding secure passwords with PowerShell, I was curious just what might be possible from the Winlogon desktop.
One thing led to another, and after trying unsuccessfully using a PowerShell script run from a command shell on the Winlogon desktop (using psexec) calling GetProcessWindowStation/UnlockWindowStation (an undocumented API, presumably unlocking a window station) and OpenDesktop/SwitchDesktop , I came across a reference to UnlockWindowStation which mentioned that this would only work when running as part of winlogon.exe, explaining why the PowerShell script did nothing.
I then successfully tested remoteunlock.exe running from one XPSP2 workstation against another. RemoteUnlock uses switchdesktop which doesn’t actually unlock the desktop, I was going to recompile and try unlockwindowstation, but I don’t have Visual Studio. It would also be interesting to modify this to work with TS winlogon processes, rather than only the interactive console (I presume you could similarly ‘unlock’ an in-use TS session).
As an aside, below is the PowerShell script calling APIs through VB.Net embedded code, which doesn’t work in this case but it's still a valid example of how to call APIs from PowerShell (which I essentially just copied from http://monadblog.blogspot.com/2005_12_01_archive.html):
$provider = new-object Microsoft.VisualBasic.VBCodeProvider
$params = new-object System.CodeDom.Compiler.CompilerParameters
$params.GenerateInMemory = $True
$refs = "System.dll","Microsoft.VisualBasic.dll"
$params.ReferencedAssemblies.AddRange($refs)
# VB.NET EXAMPLE
$txtCode = @'
Class FindProcessWinStation
Declare Auto Function GetProcWinStation Lib “user32.dll” Alias "GetProcessWindowStation" () As Integer
Declare Auto Function UnlockWinStation Lib “user32.dll” Alias "UnlockWindowStation" (ByVal WinSta As Integer) As Integer
Declare Auto Function OpenWinStation Lib “user32.dll” Alias "OpenWindowStation" (ByVal lpszWinSta As String, ByVal fInherit as Boolean, ByVal ACCESS_MASK as Integer) As Integer
Declare Auto Function OpenDesktop Lib “user32.dll” Alias "OpenDesktop" (ByVal lpszDesktop As String, ByVal dwFlags as Integer, ByVal fInherit as Boolean, ByVal ACCESS_MASK as Integer) As Integer
Declare Auto Function SwitchDesktop Lib “user32.dll” Alias "SwitchDesktop" (ByVal hDesktop As Integer) As Integer
Function Main()
main = GetProcWinStation()
' UnlockWinStation(main)
' main = OpenWinStation("winsta0\\desktop", True, 895)
main = OpenDesktop("Default", 0, True, 256)
SwitchDesktop(main)
End Function
end class
'@
$results = $provider.CompileAssemblyFromSource($params, $txtCode)
$mAssembly = $results.CompiledAssembly
$i = $mAssembly.CreateInstance("FindProcessWinStation")
$r = $i.main()
write-host $r
Wayne's World of IT (WWoIT), Copyright 2008 Wayne Martin.
Information regarding Windows Infrastructure, centred mostly around commandline automation and other useful bits of information.
3 comments:
I found your post while researching a way to create a desktop lock/unlock Powershell script.
I first wrote a single script to lock the computer using a run dll call [1] and then wait as long as the screen saver was running [2]. The only problem is when a screen saver is not running it simply switches desktops as you mention in your article.
I was wondering if you could point me in the right direction to detect when a workstation is locked/switched to the default desktop.
Thanks,
Nathan
[1] rundll32.exe "user32.dll,GetProcessWindowStation"
[2]
$ScreenSaverPath = (get-item "HKCU:\control panel\desktop\").GetValue("scrnsave.exe")
while ([boolean] (get-process | where-object {$_.Path -like $ScreenSaverPath})) {
Sleep .5
}
Hi Nathan,
The OpenInputDesktop function also provides a handle to the current desktop receiving user input, which I assume would be either winlogon or default, depending on whether workstation was locked or not.
http://msdn.microsoft.com/en-us/library/ms684309(VS.85).aspx
You could try using my example vb.net-in-powershell example and run EnumDesktops and OpenInputDesktop. I have no idea whether this will work, you'd have to try.
Some other information:
I also noticed while looking with procexp.exe that when the winlogon desktop is active (which I saw with psexec /x /i /s procexp.exe), it creates another handle to the \default desktop in the winlogon process, and when the desktop is unlocked and control returns to \default the handle is destroyed.
The win32_process WMI class has information that inclues the sessionID, and this might provide some of the info you want. For example, you can see the winlogon process and it's associated session, indicating the session that winsta0\winlogon is using. And when I ran it against a machine I was RDP'ing into, I could see that logon.scr was running in the same session ID as winlogon.
wmic path win32_process get name,sessionid
Query.exe enumerates sessions (query is a windows server 2003 utility)
query.exe session
I also saw a reference to a command-line utility called objdir.exe in the vista wdk to enumerate winstations, but it doesn't seem to return anything more than the GUI winobj.exe equivalent. I found this utility in the 2003 DDK:
in the TOOL_otherx.cab called 'TOOL_otherx_FILE_5' (rename to objdir.exe if you don't want to install the DDK).
http://download.microsoft.com/download/9/0/f/90f019ac-8243-48d3-91cf-81fc4093ecfd/1830_usa_ddk.iso
http://download.microsoft.com/download/f/3/9/f3900e1e-a45c-45a4-b716-740e553e1f62/SPTCF_SYS.doc
There's a TS API called RpcWinStationQueryInformation that has a property called WinStationLockedState
http://msdn.microsoft.com/en-us/library/cc248834.aspx
I hope this gives you something more to go on.
I have do have Visual Studio and tried recompiling it with UnlockWindowStation but nothing changed. In case you are interested here is the code I added.
HWINSTA wdesk = 0;
wdesk = GetProcessWindowStation();
HINSTANCE hGetProcIDDLL = LoadLibraryA("user32.dll");
FARPROC lpfnGetProcessID = GetProcAddress(HMODULE (hGetProcIDDLL),"UnlockWindowStation");
typedef int (__stdcall * pICFUNC)(HWINSTA winSta);
pICFUNC UnlockWindows;
UnlockWindows = pICFUNC(lpfnGetProcessID);
UnlockWindows(wdesk);
FreeLibrary(hGetProcIDDLL);
Also there is a Visual Studio express edition which is free and has practically the same functionality as the full version.
Post a Comment