对于 UIAccess="true" 的进程,CreateProcessAsUser 失败并显示 ERROR_ELEVATION_REQUIRED

c00*_*0fd 5 c c++ service winapi uac

我正在尝试使用以下代码从我的服务应用程序运行用户模式进程(作为local system.)

用户模式进程的要求是在没有提升的情况下运行,但UIAccess="true"在其清单中必须能够在 Windows 8 下正确显示top-most窗口

所以我这样做(从我的服务)来运行我的用户模式进程:

//NOTE: Error checking is omitted for readability
//'dwSessionID' = user session ID to run user-mode process in
//'pUserProcPath' = L"C:\\Program Files (x86)\\Company\\Software\\user_process.exe"

HANDLE hToken = NULL;
WTSQueryUserToken(dwSessionID, &hToken);

HANDLE hToken2;
DuplicateTokenEx(hToken, MAXIMUM_ALLOWED, NULL, SecurityIdentification, TokenPrimary, &hToken2);

LPVOID pEnvBlock = NULL;
CreateEnvironmentBlock(&pEnvBlock, hToken2, FALSE);

STARTUPINFO si;
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.lpDesktop = L"winsta0\\default";

PROCESS_INFORMATION pi;
ZeroMemory(&pi, sizeof(pi));

//ImpersonateLoggedOnUser(hToken2);   //Not necessary as suggested below

PVOID OldRedir;
Wow64DisableWow64FsRedirection(&OldRedir);

BOOL bSuccess = CreateProcessAsUser(
    hToken2,           // client's access token
    pUserProcPath,     // file to execute
    NULL,              // command line
    NULL,              // pointer to process SECURITY_ATTRIBUTES
    NULL,              // pointer to thread SECURITY_ATTRIBUTES
    FALSE,             // handles are not inheritable
    NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE | CREATE_UNICODE_ENVIRONMENT,   // creation flags
    pEnvBlock,         // pointer to new environment block 
    NULL,              // name of current directory 
    &si,               // pointer to STARTUPINFO structure
    &pi                // receives information about new process
    );

int nOSError = ::GetLastError();

Wow64RevertWow64FsRedirection(OldRedir);
//RevertToSelf();       //Not necessary as suggested below

CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
DestroyEnvironmentBlock(pEnvBlock);
CloseHandle(hToken2);
CloseHandle(hToken);
Run Code Online (Sandbox Code Playgroud)

如果UIAccess="false"user_process.exe.

但是,如果我执行以下操作:

  • 启用UIAccess="true"user_process.exe

在此处输入图片说明

  • 用我的代码签名证书签名,然后

  • 将其放在"C:\Program Files (x86)\Company\Software"文件夹中。

我得到的是CreateProcessAsUser失败并出现错误ERROR_ELEVATION_REQUIRED

有人可以建议如何使它工作吗?

附注。我尝试调整我的服务的权限以启用SE_DEBUG_NAMESE_TCB_NAME,但都没有帮助。

PS2。如果我只是双击我用user_process.exe编译UIAccess="true"、代码签名并放置在C:\Program Files (x86)\Company\Software文件夹中的进程,它就会启动并运行得很好(没有提升。)。

c00*_*0fd 6

我找到了答案。这也适用于遇到这种情况的人:

调用后添加以下内容DuplicateTokenEx

HANDLE hToken2;
DuplicateTokenEx(hToken, MAXIMUM_ALLOWED, NULL, SecurityIdentification, TokenPrimary, &hToken2);

//ONLY if requiring UIAccess!
DWORD dwUIAccess = 1;
::SetTokenInformation(hToken2, TokenUIAccess, &dwUIAccess, sizeof(dwUIAccess));
Run Code Online (Sandbox Code Playgroud)