1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
| #include <UserEnv.h> #include <WtsApi32.h> #include <tchar.h> #pragma comment(lib, "UserEnv.lib") #pragma comment(lib, "WtsApi32.lib") VOID APIENTRY ServiceMain(DWORD dwArgc, PCHAR lpszArgv); VOID APIENTRY ServiceCtrlHandle(DWORD dwOperateCode); VOID DoTask(VOID);
VOID ShowMessage(PTCHAR lpszMessage, PTCHAR lpszTitle);
BOOL CreateUserProcess(PWCHAR lpszFileName);
WCHAR g_szServiceName[MAX_PATH] = TEXT("CreateProcessAsUser_Test.exe"); SERVICE_STATUS g_ServiceStatus = { 0 }; SERVICE_STATUS_HANDLE g_ServiceStatusHandle = { 0 }; DWORD _tmain(IN DWORD argc, IN PTCHAR argv[]) { UNREFERENCED_PARAMETER(argc); UNREFERENCED_PARAMETER(argv); SERVICE_TABLE_ENTRY stDispatchTable[] = { {g_szServiceName, (LPSERVICE_MAIN_FUNCTION)ServiceMain }, { NULL, NULL } }; ::StartServiceCtrlDispatcher(stDispatchTable); return 0; }; VOID APIENTRY ServiceMain(DWORD dwArgc, PCHAR lpszArgv) { g_ServiceStatus.dwServiceType = SERVICE_WIN32; g_ServiceStatus.dwCurrentState = SERVICE_START_PENDING; g_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP; g_ServiceStatus.dwWin32ExitCode = 0; g_ServiceStatus.dwServiceSpecificExitCode = 0; g_ServiceStatus.dwCheckPoint = 0; g_ServiceStatus.dwWaitHint = 0; g_ServiceStatusHandle = ::RegisterServiceCtrlHandler(g_szServiceName, ServiceCtrlHandle); g_ServiceStatus.dwCurrentState = SERVICE_RUNNING; g_ServiceStatus.dwCheckPoint = 0; g_ServiceStatus.dwWaitHint = 0; ::SetServiceStatus(g_ServiceStatusHandle, &g_ServiceStatus); DoTask(); return; }; VOID APIENTRY ServiceCtrlHandle(DWORD dwOperateCode) { switch (dwOperateCode) { case SERVICE_CONTROL_PAUSE: { g_ServiceStatus.dwCurrentState = SERVICE_PAUSED; break; }; case SERVICE_CONTROL_CONTINUE: { g_ServiceStatus.dwCurrentState = SERVICE_RUNNING; break; }; case SERVICE_CONTROL_STOP: { g_ServiceStatus.dwWin32ExitCode = 0; g_ServiceStatus.dwCurrentState = SERVICE_STOPPED; g_ServiceStatus.dwCheckPoint = 0; g_ServiceStatus.dwWaitHint = 0; ::SetServiceStatus(g_ServiceStatusHandle, &g_ServiceStatus); break; }; case SERVICE_CONTROL_INTERROGATE: break; default: break; }; return; }; VOID DoTask(VOID) { ShowMessage((PTCHAR)"Hi Demon·Gan\nThis Is From Session 0 Service!\n", (PTCHAR)"HELLO"); CreateUserProcess((PTCHAR)"C:\\Users\\DemonGan\\Desktop\\520.exe");
return; }; VOID ShowMessage(PTCHAR lpszMessage, PTCHAR lpszTitle) { DWORD dwSessionId = ::WTSGetActiveConsoleSessionId(); DWORD dwResponse = 0; ::WTSSendMessage(WTS_CURRENT_SERVER_HANDLE, dwSessionId, lpszTitle, (1 + ::lstrlen(lpszTitle)), lpszMessage, (1 + ::lstrlen(lpszMessage)), 0, 0, &dwResponse, FALSE); return; };
BOOL CreateUserProcess(PWCHAR lpszFileName) { BOOL bRet = TRUE; DWORD dwSessionID = 0; HANDLE hToken = NULL; HANDLE hDuplicatedToken = NULL; LPVOID lpEnvironment = NULL; STARTUPINFO si = { 0 }; PROCESS_INFORMATION pi = { 0 }; si.cb = sizeof(si); do { dwSessionID = ::WTSGetActiveConsoleSessionId(); if (FALSE == ::WTSQueryUserToken(dwSessionID, &hToken)) { ShowMessage((PTCHAR)"WTSQueryUserToken", (PTCHAR)"ERROR"); bRet = FALSE; break; }; if (FALSE == ::DuplicateTokenEx(hToken, MAXIMUM_ALLOWED, NULL, SecurityIdentification, TokenPrimary, &hDuplicatedToken)) { ShowMessage((PTCHAR)"DuplicateTokenEx", (PTCHAR)"ERROR"); bRet = FALSE; break; }; if (FALSE == ::CreateEnvironmentBlock(&lpEnvironment, hDuplicatedToken, FALSE)) { ShowMessage((PTCHAR)"CreateEnvironmentBlock", (PTCHAR)"ERROR"); bRet = FALSE; break; }; if (FALSE == ::CreateProcessAsUser(hDuplicatedToken, lpszFileName, NULL, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE | CREATE_UNICODE_ENVIRONMENT, lpEnvironment, NULL, &si, &pi)) { ShowMessage((PTCHAR)"CreateProcessAsUser", (PTCHAR)"ERROR"); bRet = FALSE; break; }; } while (FALSE); if (lpEnvironment) ::DestroyEnvironmentBlock(lpEnvironment); if (hDuplicatedToken) ::CloseHandle(hDuplicatedToken); if (hToken) ::CloseHandle(hToken); return bRet; };
|