【问题标题】:Create nonelevated process from elevated process with CreateProcessAsUser function使用 CreateProcessAsUser 函数从提升的进程创建非提升的进程
【发布时间】:2014-04-30 05:56:57
【问题描述】:

我正在运行一个具有SYSTEM 权限的程序。我向LogonUser(szUserName, NULL, szPassword, LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, &hToken) 询问用于本地登录的句柄的用户名和密码。我想运行cmd.exe 程序作为登录提供的用户凭据。登录成功,但是当我尝试运行该进程时,没有任何反应。程序中止。我的代码是...

// cmd.cpp : Defines the entry point for the console application.
//

#include <Windows.h>
#include <Lmcons.h>
#include <iostream>
#include <ctype.h>
#include <string>
#include <stdio.h>

#define winstring LPWSTR
#define stcas(x) static_cast<x>
#define INFO_BUFFER_SIZE    260 

using namespace std;

void ReportError(LPCWSTR pszFunction, DWORD dwError = GetLastError()) 
{ 
    wprintf(L"%s failed w/err 0x%08lx\n", pszFunction, dwError); 
} 

int main()
{
    TCHAR un[UNLEN+1];
    DWORD size = UNLEN + 1;
    GetUserName(un, &size);

    string una(un);

    bool sys = !una.compare("SYSTEM");

    if(!sys) {
        system("cls");
        system("title Command Prompt");
        system("cmd");
        return 0;
    }


    char szUserName[INFO_BUFFER_SIZE] = {}; 
    char szPassword[INFO_BUFFER_SIZE] = {}; 
    char *pc = NULL; 
    HANDLE hToken = NULL; 
    BOOL fSucceeded = FALSE; 
    BOOL logon = FALSE;


    printf("Enter the username: "); 
    fgets(szUserName, ARRAYSIZE(szUserName), stdin); 
    pc = strchr(szUserName, '\n'); 
    if (pc != NULL) *pc = '\0';  // Remove the trailing L'\n' 

    cout << endl;
    //string un(szUserName);

    printf("Enter the password: "); 
    fgets(szPassword, ARRAYSIZE(szPassword), stdin); 
    pc = strchr(szPassword, '\n'); 
    if (pc != NULL) *pc = '\0';  // Remove the trailing L'\n'



    if (!LogonUser(szUserName, NULL, szPassword,  LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, &hToken)) 
    {
        ReportError(L"Logon");
        goto Cleanup; 
    } 
    else logon = true;

    // Impersonate the logged on user. 
    if (!ImpersonateLoggedOnUser(hToken)) 
    { 

        ReportError(L"imp");
        goto Cleanup; 
    } 
    fSucceeded = true;

    Cleanup: 

    // Clean up the buffer containing sensitive password. 
    SecureZeroMemory(szPassword, sizeof(szPassword)); 

    LPTSTR szCmdline[] = {"C:\\windows\\system32\\cmd.exe"};
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    TCHAR uni[UNLEN+1];
    DWORD sizei = UNLEN + 1;
    GetUserName(uni, &sizei);

    string unai(uni);
    cout << unai << endl;

    memset(&si, 0, sizeof(si));
    si.cb = sizeof(si);

    HANDLE phToken = NULL;

    BOOL dup = FALSE;

    if(!DuplicateTokenEx(hToken, TOKEN_DUPLICATE | TOKEN_IMPERSONATE, NULL, SecurityImpersonation, TokenImpersonation, &phToken)){
        ReportError(L"DUPLICATE TOKEN");
    }

    else dup = TRUE;

    system("pause");

    // If the impersonation was successful, undo the impersonation. 
    if (fSucceeded && logon && dup) 
    { 
        system("cls");
        system("title Command Prompt");
        //system("cmd");

        if(!CreateProcessAsUser(
        phToken,            // client's access token
        NULL,              // file to execute
        *szCmdline,     // command line
        NULL,              // pointer to process SECURITY_ATTRIBUTES
        NULL,              // pointer to thread SECURITY_ATTRIBUTES
        FALSE,             // handles are not inheritable
        NORMAL_PRIORITY_CLASS,   // creation flags
        NULL,              // pointer to new environment block 
        NULL,              // name of current directory 
        &si,               // pointer to STARTUPINFO structure
        &pi                // receives information about new process
        )){
            ReportError(L"Create Process");
        }
        if (!RevertToSelf()) 
        {  
            ReportError(L"Undo Imp");
        } 

    }
    system("pause");
}

我真的希望非提升过程能够避免安全问题。

请不要告诉我输入的密码已显示。我知道,稍后会解决。

【问题讨论】:

  • DuplicateTokenEx 的参数错误。您需要创建主令牌,而不是模拟令牌,并且您至少需要 TOKEN_QUERY、TOKEN_DUPLICATE 和 TOKEN_ASSIGN_PRIMARY 访问权限;我建议使用 GENERIC_ALL。但是,在这种情况下,您根本不需要复制令牌。只需将 LogonUser 返回的令牌直接传递给 CreateProcessAsUser。
  • @HarryJohnston,我决定使用 CreateProcessWithLogonW 函数。

标签: c++ windows winapi system elevated-privileges


【解决方案1】:

尝试使用 CreateProcessWithLogonUser 函数。这应该为您完成所有这些,没有错误。

【讨论】:

【解决方案2】:

你没有初始化si。您需要在调用CreateProcessAsUser 之前执行此操作:

memset(&si, 0, sizeof(si));
si.cb = sizeof(si);

【讨论】:

  • 现在我得到 0x00000552... ERROR_NOT_LOGON_PROCESS... 请求的操作仅限于登录进程使用。调用进程尚未注册为登录进程。
  • 您的进程需要TOKEN_DUPLICATETOKEN_IMPERSONATE访问权限才能调用CreateProcessAsUser
  • 我该怎么做?我以NT AUTHORITY/SYSTEM 的身份运行
  • 我相信DuplicateTokenEx 会解决你的问题。看看this question
  • 现在我添加了DuplicateTokenEx,我收到错误0x00000005 ACCESS DENIED,请查看我更新的代码。
猜你喜欢
  • 2014-08-20
  • 1970-01-01
  • 2011-04-25
  • 1970-01-01
  • 2016-08-13
  • 2015-07-20
  • 2011-01-31
  • 2022-11-22
  • 1970-01-01
相关资源
最近更新 更多