gpt4 book ai didi

windows - 具有提升权限的 CreateProcessAsUser

转载 作者:可可西里 更新时间:2023-11-01 09:19:52 32 4
gpt4 key购买 nike

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

我得到的是:

  1. WTSGetActiveConsoleSessionID()
  2. WTSQueryUserToken 用于 session ID
  3. CreateProcessAsUser

问题是我需要以管理员身份运行该过程(第 3 步),而不要求用户提供管理员密码。

在 Linux 系统上我会简单地做一个“su”,但要在 Windows 系统上实现这个?

最佳答案

我终于找到了解决这个问题的方法:

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);
}
}

关于windows - 具有提升权限的 CreateProcessAsUser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33212984/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com