Pourquoi la création de création est-ce que l'échec avec l'erreur_access_deniée
-
12-11-2019 - |
Question
J'ai un appel à la création de création avec un échec avec l'accès refusé. Des idées comment déboguer cela?
L'appel à la création de la création est là: https://github.com/fschwiet/pshochu/blob/master/pshochu/pinvoke/netwrappers/processutil.cs
Pour l'instant, j'utilise un jeton d'accès pour le processus actuel, je vais finalement utiliser un jeton d'un autre utilisateur. Pour l'instant alors j'utilise https://github.com/fschwiet/pshochu/blob/master/pshochu/pinvoke/netwrappers/accesstoken.cs Pour obtenir le jeton d'accès.
Si vous souhaitez déboguer, baissez le SourceCode et exécutez Build_and_test.PS1. La pile d'erreur est:
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
Mise à jour ultérieure: j'ai vu dans certains documents que des privilèges supplémentaires sont nécessaires (http://msdn.microsoft.com/en-us/library/aa374905%28v=vs.85%29.aspx). J'ai du mal à obtenir des tests pour vérifier que j'ai ces titres individuels (ils sont définis dans Secpol.msc pré-Recoot)
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"
Ces tests continuent de me dire que je n'ai pas les autorisations que j'ai fixées dans l'interface utilisateur, https://github.com/fschwiet/pshochu/blob/master/pshochu.tests/verify_privileges.cs
La solution
Grâce à des essais et à des erreurs, j'ai compris que le jeton que vous transmettez à CreateProcess withTokenw () a besoin des drapeaux d'accès suivants (au moins sur Windows 7 SP1 64 bits):
- Token_assign_primary
- Token_duplicate
- Token_query
- Token_adjust_default
- Token_adjust_sessionid
Les deux derniers en gras ne sont pas très utilement mentionnés du tout dans la documentation pour CreateProcess withTokenw ().
ÉDITER: Le code suivant fonctionne bien pour moi (en cours d'exécution élevé):
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;
}