我有 3 个应用程序正在运行:
应用程序 A: 在本地系统下作为后台服务运行的本地服务器,具有与桌面交互的权限(至少这是高级安装程序所说的)
应用 B: 桌面捕获软件
应用程序 C: 作为登录用户运行的托盘图标。
我想要实现的是能够捕获桌面 UAC 提示和登录屏幕,所以这是我试图实现它的方法:
1.应用程序 C 调用 WTSGetActiveConsoleSessionId() (返回 2),获取桌面指针:
OpenInputDesktop(0, false, (uint)(ACCESS_MASK.DESKTOP_CREATEMENU | ACCESS_MASK.DESKTOP_CREATEWINDOW | ACCESS_MASK.DESKTOP_ENUMERATE | ACCESS_MASK.DESKTOP_HOOKCONTROL | ACCESS_MASK.DESKTOP_WRITEOBJECTS | ACCESS_MASK.DESKTOP_READOBJECTS | ACCESS_MASK.DESKTOP_SWITCHDESKTOP))
并将两者都发送到应用程序 A(本地服务器)
2.服务器运行如下代码:
static void CreateApplicationBProcess()
{
IntPtr hToken = IntPtr.Zero;
IntPtr P = IntPtr.Zero;
/*if (!OpenProcessToken(OpenProcess(Process.GetProcessesByName("winlogon").First(), ProcessAccessFlags.All), 0x2000000, out hToken))
{
throw new Exception("OpenProcessToken error #" + Marshal.GetLastWin32Error());
}*/
if (!WTSQueryUserToken(WTSGetActiveConsoleSessionId(), out hToken))
{
throw new Exception("WTSQueryUserToken error #" + Marshal.GetLastWin32Error());
}
IntPtr duplicatedTokenHandle = IntPtr.Zero;
if(!DuplicateTokenEx(hToken, 0, IntPtr.Zero, (int)SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation, (int)TOKEN_TYPE.TokenPrimary, ref duplicatedTokenHandle))
{
throw new Exception("DuplicateTokenEx error #" + Marshal.GetLastWin32Error());
}
uint sessionId = SessionId;
if(!SetTokenInformation(duplicatedTokenHandle, TOKEN_INFORMATION_CLASS.TokenSessionId, ref sessionId, (uint)IntPtr.Size))
{
throw new Exception("SetTokenInformation error #" + Marshal.GetLastWin32Error());
}
if (CreateEnvironmentBlock(out P, duplicatedTokenHandle, false))
{
Win32.PROCESS_INFORMATION processInfo = new Win32.PROCESS_INFORMATION();
Win32.STARTUPINFO startInfo = new Win32.STARTUPINFO();
bool bResult = false;
uint uiResultWait = Win32.WAIT_FAILED;
try
{
// Create process
startInfo.cb = Marshal.SizeOf(startInfo);
startInfo.lpDesktop = "winsta0\\default";
startInfo.dwFlags = 0x00000001;
startInfo.wShowWindow = (short)SW.SW_HIDE;
SetCurrentDirectory(GetDirectory());
Environment.CurrentDirectory = GetDirectory();
bResult = Win32.CreateProcessAsUser(duplicatedTokenHandle, Path.Combine(GetDirectory(), "ApplicationB.exe"), string.Format("{0}", (uint)DesktopPtr), IntPtr.Zero, IntPtr.Zero, false,
CREATE_UNICODE_ENVIRONMENT | CREATE_NO_WINDOW, P, GetDirectory(), ref startInfo, out processInfo);
if (!bResult)
{
throw new Exception("CreateProcessAsUser error #" + Marshal.GetLastWin32Error());
}
else
{
//IntPtr pOpenThread = OpenThread(ThreadAccess.SUSPEND_RESUME, false, (uint)processInfo.dwThreadId);
ResumeThread(processInfo.hThread);
}
// Wait for process to end
uiResultWait = Win32.WaitForSingleObject(processInfo.hProcess, Win32.INFINITE);
if (uiResultWait == Win32.WAIT_FAILED)
{
throw new Exception("WaitForSingleObject error #" + Marshal.GetLastWin32Error());
}
GetExitCodeProcess(processInfo.hProcess, out uint ExitCode);
Console.WriteLine("Exit code: " + ExitCode);
}
finally
{
// Close all handles
Win32.CloseHandle(hToken);
Win32.CloseHandle(processInfo.hProcess);
Win32.CloseHandle(processInfo.hThread);
DestroyEnvironmentBlock(P);
}
}
else
{
throw new Exception("CreateEnvironmentBlock error #" + Marshal.GetLastWin32Error());
}
}
private static string GetDirectory()
{
return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
}
3.现在 CreateProcessAsUser 返回成功,但进程正在退出,错误代码为 3228369022 (C06D007E)。
我使用 ProcMon 进行监视,并且加载图像没有任何错误,但是并非我的所有 dll 都已加载。