2011-03-27 136 views
5

我有一个CreateProcessWithTokenW调用失败,拒绝访问。任何想法如何调试?为什么CreateProcessWithTokenW失败,ERROR_ACCESS_DENIED

到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

+0

没有发生?是的,我也不想碰它。 :P – 2011-03-28 22:16:33

+0

它是什么操作系统?您可以尝试运行Process Monitor并查看它是否与文件或注册表访问有关(考虑到它是同一用户,似乎不太可能)。 – Luke 2011-03-28 23:22:46

+0

Windows 7.我不知道如何在进程资源管理器中看到它,因为我明白错误,进程没有启动。 – 2011-03-29 00:25:59

回答

12

经过反复试验,我想通了,你传递给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; 
} 
+0

嗯,非常好,我期待今晚,当我可以给这个尝试验证。 – 2011-03-29 16:59:10

+0

嗯,这可能是一个问题,但不是最后一个。我看到相同的错误 – 2011-03-30 01:36:29

+0

您的应用程序运行是否升高? – Luke 2011-03-30 16:59:43