CreateProcessAsUser具有提升的权限(CreateProcessAsUser with elevated priviledges)

我的服务在本地系统权限下运行,需要在用户会话中启动具有管理员权限的应用程序。

我得到的是:

WTSGetActiveConsoleSessionID() 会话ID的WTSQueryUserToken CreateProcessAsUser

问题是我需要以管理员身份运行流程(步骤3),而不要求用户输入管理员密码。

在Linux系统上,我只是做一个“su”,但要在Windows系统上实现这一点?

My service is running under local system permissions, and needs to start an application with administrator permissions in the user session.

What I got is:

WTSGetActiveConsoleSessionID() WTSQueryUserToken for session ID CreateProcessAsUser

The problem is I need to run the process (Step 3) as administrator, without asking the user for the administrator's password.

On Linux systems I would simply do a "su ", but to achive this on a windows system?

最满意答案

我需要以管理员身份运行该过程(步骤3),而不要求用户输入管理员密码。

如果低权限用户可以作为特权用户执行代码,那么系统的安全模型将被破坏。 如果要使用管理员权限执行代码,则需要在某些时候提供适当的凭据。

您提议的行动计划是您调用CreateProcessAsUser为低权限用户传递用户令牌。 该计划在问题中逐项列出,不能成功。 由于您将提供的用户令牌是低权限用户的用户令牌,因此该进程将无法执行管理任务。

您需要以这种或那种方式为具有管理权限的用户提供凭据。

I've finally found the solution to manage this:

public void launchProcessInUserSession(String process) throws WindowsAPIException { final DWORD interactiveSessionId = kernel32.WTSGetActiveConsoleSessionId(); final DWORD serviceSessionId = getCurrentSessionId(); final HANDLEByReference pExecutionToken = new HANDLEByReference(); final HANDLE currentProcessToken = getCurrentProcessToken(); try { final HANDLE interactiveUserToken = getUserToken(interactiveSessionId); checkAPIError(advapi32.DuplicateTokenEx(currentProcessToken, WinNT.TOKEN_ALL_ACCESS, null, SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation, WinNT.TOKEN_TYPE.TokenPrimary, pExecutionToken)); } finally { kernel32.CloseHandle(currentProcessToken); } final HANDLE executionToken = pExecutionToken.getValue(); try { checkAPIError(advapi32.SetTokenInformation(executionToken, TOKEN_INFORMATION_CLASS.TokenSessionId, new IntByReference(interactiveSessionId.intValue()), DWORD.SIZE)); final WinBase.STARTUPINFO si = new WinBase.STARTUPINFO(); final PROCESS_INFORMATION processInfo = new WinBase.PROCESS_INFORMATION(); final int dwFlags = WinBase.DETACHED_PROCESS; checkAPIError(advapi32.CreateProcessAsUser(executionToken, null, process, null, null, false, dwFlags, null, null, si, processInfo)); LOGGER.debug("Execution done. Process ID is {}", processInfo.dwProcessId); } finally { kernel32.CloseHandle(executionToken); } }

更多推荐