有没有办法“密封”令牌,以便子进程无法继承它?

时间:2015-05-08 18:32:36

标签: windows winapi uac elevated-privileges

我正在尝试找到一种方法来允许单个进程作为提升用户启动,但是禁止此进程使用它的令牌启动任何子进程 - 在某种程度上,类似于“密封”令牌。这背后的原因是阻止用户启动cmd.exe并获得对系统的完全访问权。

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

1 个答案:

答案 0 :(得分:3)

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

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消息。