【问题标题】:why is CreateProcessWithTokenW failing with ERROR_ACCESS_DENIED为什么 CreateProcessWithTokenW 因 ERROR_ACCESS_DENIED 而失败
【发布时间】:2011-07-23 18:37:20
【问题描述】:

我对 CreateProcessWithTokenW 的调用因访问被拒绝而失败。有什么想法可以调试吗?

此处调用 CreateProcessWithTokenW:https://github.com/fschwiet/PShochu/blob/master/PShochu/PInvoke/NetWrappers/ProcessUtil.cs

目前我正在为当前进程使用访问令牌,最终我将使用来自另一个用户的令牌。现在我使用https://github.com/fschwiet/PShochu/blob/master/PShochu/PInvoke/NetWrappers/AccessToken.cs 来获取访问令牌。

如果要调试,请下拉源代码并运行 build_and_test.ps1。错误堆栈是:

1) Test Error : PShochu.Tests.can_run_remote_interactive_tasks, given a psake script which writes the current process id to output, when that script is invoked interactively, then the script succeeds
   System.ComponentModel.Win32Exception : Access is denied
   at PShochu.PInvoke.NetWrappers.ProcessUtil.CreateProcessWithToken(IntPtr userPrincipalToken, String applicationName,
String applicationCommand, Boolean dontCreateWindow, Boolean createWithProfile, StreamReader& consoleOutput, StreamReader& errorOutput) in c:\src\PShochu\PShochu\PInvoke\NetWrappers\ProcessUtil.cs:line 52
   at PShochu.ProcessHandling.RunNoninteractiveConsoleProcessForStreams2(String command, String commandArguments, String& newLine) in c:\src\PShochu\PShochu\ProcessHandling.cs:line 36
   at PShochu.ProcessHandling.RunNoninteractiveConsoleProcess(String command, String commandArguments) in c:\src\PShochu\PShochu\ProcessHandling.cs:line 20
   at PShochu.Tests.can_run_remote_interactive_tasks.<>c__DisplayClass16.<>c__DisplayClass18.<Specify>b__2() in c:\src\PShochu\PShochu.Tests\can_run_remote_interactive_tasks.cs:line 27
   at NJasmine.Core.Execution.DescribeState.<>c__DisplayClass7`1.<visitBeforeEach>b__3() in c:\src\NJasmine\NJasmine\Core\Execution\DescribeState.cs:line 62

后来的更新:我在一些文档中看到需要额外的权限 (http://msdn.microsoft.com/en-us/library/aa374905%28v=vs.85%29.aspx)。我无法通过测试来验证我是否拥有这些单独的证券(它们在 secpol.msc 重启前设置)

SE_ASSIGNPRIMARYTOKEN_NAME  "Replace a process level token"
SE_TCB_NAME "Act as part of the operatin system"
SE_INCREASE_QUOTA_NAME  "Adjust memory quotas for a process"

这些测试不断告诉我我没有在 UI 中设置的权限,https://github.com/fschwiet/PShochu/blob/master/PShochu.Tests/verify_privileges.cs

【问题讨论】:

  • 不拍?是的,我也不想碰它。 :P
  • 它是什么操作系统?您可以尝试在其上运行 Process Monitor 并查看它是否与文件或注册表访问有关(考虑到它是同一用户,这似乎不太可能)。
  • Windows 7。我不确定我会如何在进程资源管理器中看到它,因为据我了解错误,进程没有启动。
  • 关于权限,即使您在 UI 中“启用”它们,您仍然必须通过 AdjustTokenPrivileges 显式启用它们。不过,我不记得在我的代码中这样做了。

标签: winapi pinvoke createprocessasuser


【解决方案1】:

通过反复试验,我发现您传递给 CreateProcessWithTokenW() 的令牌需要以下访问标志(至少在 Windows 7 SP1 64 位上):

  • TOKEN_ASSIGN_PRIMARY
  • TOKEN_DUPLICATE
  • TOKEN_QUERY
  • TOKEN_ADJUST_DEFAULT
  • TOKEN_ADJUST_SESSIONID

最后两个粗体字在 CreateProcessWithTokenW() 的文档中根本没有提及。

编辑:以下代码对我来说很好(运行提升时):

HANDLE hToken = NULL;
if(OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE, &hToken))
{
    HANDLE hDuplicate = NULL;
    if(DuplicateTokenEx(hToken, TOKEN_ASSIGN_PRIMARY | TOKEN_DUPLICATE | TOKEN_QUERY | TOKEN_ADJUST_DEFAULT | TOKEN_ADJUST_SESSIONID, NULL, SecurityImpersonation, TokenPrimary, &hDuplicate))
    {
        TCHAR szCommandLine[MAX_PATH];
        _tcscpy_s(szCommandLine, MAX_PATH, _T("C:\\Windows\\system32\\notepad.exe"));
        STARTUPINFO StartupInfo;
        ZeroMemory(&StartupInfo, sizeof(STARTUPINFO));
        StartupInfo.cb = sizeof(STARTUPINFO);
        PROCESS_INFORMATION ProcessInformation;
        ZeroMemory(&ProcessInformation, sizeof(PROCESS_INFORMATION));
        if(CreateProcessWithTokenW(hDuplicate, LOGON_WITH_PROFILE, NULL, szCommandLine, 0, NULL, NULL, &StartupInfo, &ProcessInformation))
        {
            WaitForSingleObject(ProcessInformation.hProcess, INFINITE);
            CloseHandle(ProcessInformation.hThread);
            ProcessInformation.hThread = NULL;
            CloseHandle(ProcessInformation.hProcess);
            ProcessInformation.hProcess = NULL;
        }
        CloseHandle(hDuplicate);
        hToken = hDuplicate;
    }
    CloseHandle(hToken);
    hToken = NULL;
}

【讨论】:

  • 嗯,非常好,期待今晚我可以尝试验证一下。
  • 嗯,这可能是个问题,但不是最后一个问题。我看到了同样的错误
  • 您的应用程序运行升级了吗?
  • 我在测试运行器中运行,需要将其拆分以查看是否是问题所在。感谢您的代码,我会尝试从那里返回。
  • GENERIC_ALL_ACCESS 不足以满足 DuplicateTokenEx 吗?
猜你喜欢
  • 2013-04-08
  • 1970-01-01
  • 1970-01-01
  • 2012-04-06
  • 2012-09-08
  • 2013-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多