有什么方法可以 "seal" 一个令牌,这样子进程就不能继承它了吗?

Is there any way to "seal" a token so child processes can't inherit it?

我正在尝试找到一种方法,允许单个进程作为高级用户启动,但禁止此进程使用它的令牌启动任何子进程——在某种程度上,有点像 "sealing" 令牌.这背后的原因是为了防止用户启动 cmd.exe 并获得对系统的完全访问权限。

我查看了进程权限常量,但没有看到任何可用于实现此功能的东西。

我认为您不能密封令牌,但您可以使用作业对象控制子进程的创建:

static BOOL SpawnProcessAndTerminateGrandchildren(PTSTR Cmdline)
{
    HANDLE hJob = CreateJobObject(0, 0);
    if (!hJob) return false;

    JOBOBJECT_BASIC_LIMIT_INFORMATION jobli;
    jobli.LimitFlags = JOB_OBJECT_LIMIT_ACTIVE_PROCESS;
    jobli.ActiveProcessLimit = 1;
    BOOL retval = SetInformationJobObject(hJob, JobObjectBasicLimitInformation, &jobli, sizeof(jobli));

    PROCESS_INFORMATION pi;
    if (retval)
    {
        STARTUPINFO si;
        ZeroMemory(&si, sizeof(si)), si.cb = sizeof(si);
        retval = CreateProcess(0, Cmdline, 0, 0, false, CREATE_SUSPENDED|CREATE_BREAKAWAY_FROM_JOB|CREATE_NEW_CONSOLE, 0, 0, &si, &pi);
    }
    if (retval)
    {
        if (AssignProcessToJobObject(hJob, pi.hProcess)) // This can fail if we are already in a job
        {
            ResumeThread(pi.hThread);
            WaitForSingleObject(pi.hProcess, INFINITE);
        }
        else
            TerminateProcess(pi.hProcess, ERROR_OPERATION_ABORTED);
        CloseHandle(pi.hThread);
        CloseHandle(pi.hProcess);
    }
    CloseHandle(hJob);
    return retval;
}


int main(int argc, ...)
{
    TCHAR cmd[] = TEXT("cmd.exe /k regedit"); // cmd.exe is our child, regedit is the grandchild spawned by cmd.exe
    SpawnProcessAndTerminateGrandchildren(cmd);
    return 0;
}

如果您想要更多的控制权,您可以使用 JobObjectAssociateCompletionPortInformation,这样每次创建新的子进程时您都会收到 JOB_OBJECT_MSG_NEW_PROCESS 消息。