通过粘滞键调用CreateProcess访问被拒绝

问题描述:

我的程序被设计为一个安全的粘滞键黑客。如果从登录时调用粘键,它将向本地帐户请求用户名和密码。如果它们是正确的,那么cmd.exe的实例将作为该用户被调用以避免损坏。当我从资源管理器中双击sethc程序,通过粘滞键调用CreateProcess访问被拒绝

它成功运行。

当我运行按shift五次同样的程序,

它失败,并拒绝错误5访问。

我可以验证sethc是否在winlogon下运行,当按下shift五次。

整个文件是:

// 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); 
    system("pause"); 
    exit(dwError); 
} 

int main() 
{ 
    LPTSTR szCmdline[] = {"cmd"}; 
    STARTUPINFOW si; 
    PROCESS_INFORMATION pi; 
    memset(&si, 0, sizeof(si)); 
    si.cb = sizeof(si); 
    TCHAR un[UNLEN+1]; 
    DWORD size = UNLEN + 1; 
    GetUserName(un, &size); 

    string una(un); 

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


    if(!sys) { 
     system("cls"); 
     if(!CreateProcessW(L"C:\\Windows\\System32\\cmd.exe", NULL, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS | CREATE_BREAKAWAY_FROM_JOB, NULL, NULL, &si, &pi)) ReportError(L"normal Create process"); 
     return 0; 
    } 



    wchar_t szUserName[INFO_BUFFER_SIZE] = {}; 
    wchar_t szPassword[INFO_BUFFER_SIZE] = {}; 
    wchar_t *pc = NULL; 
    HANDLE hToken = NULL; 
    HANDLE aToken = NULL; 
    BOOL dup = FALSE; 
    BOOL logon = FALSE; 


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

    cout << endl; 

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

    if(!LogonUserW(szUserName, NULL, szPassword, LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, &hToken)) ReportError(L"Logon"); 
    else logon = TRUE; 

    if(!DuplicateTokenEx(hToken, TOKEN_DUPLICATE | TOKEN_IMPERSONATE, NULL, SecurityImpersonation, TokenPrimary, &aToken)) ReportError(L"Impersonate"); 
    else dup = TRUE; 

    if(!CreateProcessAsUserW(aToken,L"C:\\Windows\\System32\\cmd.exe", NULL, NULL, NULL, FALSE, CREATE_BREAKAWAY_FROM_JOB, NULL, L"C:\\Windows\\System32\\", &si, &pi)){ 
     ReportError(L"Create Process"); 
    } 

    SecureZeroMemory(szPassword, sizeof(szPassword)); 
} 

我想知道为什么sethc是不允许的CreateProcess在所有如果它是从Winlogon中的。我正在运行Windows 7.奇怪的是system(command)工作正常。我用system("pause");一次。

+0

'system(“pause”);'不创建进程,因为'pause'是一个内置的shell命令。 – sashoalm

虽然很难说你的问题实际上是什么,但请注意,有一个特定的权限PROCESS_CREATE_PROCESS

从MSDN:

PROCESS_CREATE_PROCESS(0x0080)具有需要创建一个进程。

缺少此特权的进程无法产生子进程。 system("pause");不会创建子进程,因为暂停是一个内置的shell命令,但我理解您为什么会产生这种印象。

所以检查你的进程是否有这个特权。使用进程资源管理器或简单的WINAPI函数,必然会有一个。

如果你想知道为什么微软的开发者决定不把这个特权给sethc,你必须问他们。

+0

系统(“暂停”)产生一个命令'cmd/c ' – nimsson