¿Por qué creeProcesswithtokenw fallando con Error_Access_denied?
-
12-11-2019 - |
Pregunta
Tengo una llamada a CreateProcessWithtokenW que está fallando con el acceso denegado. ¿Alguna idea de cómo depurar esto?
La llamada a CreateProcessWithtokenw está aquí: https: / /github.com/fschwiet/pshochu/blob/master/pshochu/pinvoke/netwispers/processutil.cs
Por ahora estoy usando un token de acceso para el proceso actual, eventualmente usaré un token de otro usuario. Por ahora, entonces estoy usando https:// github. COM / FSCHWIET / PSHOCHU / BLOB / MASTER / PSHOCHU / PINVOKE / NETWAPMERS / ACCESSTOGEN.CS Para obtener el token de acceso.
Si desea depurar, tire del código SourceCode y ejecute build_and_test.ps1. La pila de errores es:
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
Actualización posterior: vi en algunos documentos que se necesitan privilegios adicionales ( http://msdn.microsoft.com/en-us/library/aa374905%28v=vs.85%29.aspx ). Tengo problemas para obtener pruebas para verificar que tengo estos valores individuales (se establecen en Secpol.MSC Pre-Reinabo)
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"
Estas pruebas siguen diciéndome que no tengo los permisos que he establecido en la UI, https://github.com/fschwiet/pshochu/blob/master/pshochu.tests/verify_privileges.cs
Solución
A través de la prueba y el error. Me di cuenta de que el token que pasa a CreateProcessWithtokenW () necesita las siguientes banderas de acceso (al menos en Windows 7 SP1 de 64 bits):
- token_assign_primary
- token_duplicado
- token_query
- token_adjust_default
- token_adjust_sessionid
Los últimos dos en negrita no se mencionan con mucha audacia en la documentación de CREATEPROCESSWITHTOENW ().
Editar : el siguiente código funciona bien para mí (cuando se ejecuta elevado):
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; }