-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRunAsTrustedInstaller.cpp
140 lines (138 loc) · 4.15 KB
/
RunAsTrustedInstaller.cpp
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
#include <windows.h>
#include <stdlib.h>
#include <tlhelp32.h>
#include <stdio.h>
#include <psapi.h>
#include <AclApi.h>
#include <WtsApi32.h>
#include <WinUser.h>
#include <winternl.h>
#include <wincodec.h>
#include <PropIdl.h>
#include "privilege.h"
EXTERN_C NTSYSAPI ULONG NTAPI CsrGetProcessId();
void PrintLastError(DWORD error = -114514)
{
DWORD errorCode = error == -114514 ? GetLastError() : error;
LPSTR errorMessage = NULL;
DWORD flags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;
DWORD languageId = MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT);
if (FormatMessageA(flags, NULL, errorCode, languageId, (LPSTR)&errorMessage, 0, NULL) != 0)
{
printf("Error: %lu %s\n", errorCode, errorMessage);
LocalFree(errorMessage);
}
else
{
printf("Failed to retrieve error message. Error code: %lu\n", errorCode);
}
}
HANDLE getSysToken()
{
HANDLE hToken, hProcess;
DWORD pid = CsrGetProcessId();
hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, 0, pid);
if (!hProcess)
{
PrintLastError();
MessageBoxW(0, L"OpenProcess csrss", 0, 0);
return 0;
}
if (!OpenProcessToken(hProcess, TOKEN_DUPLICATE, &hToken))
{
PrintLastError();
MessageBoxW(0, L"OpenProcessToken csrss", 0, 0);
return 0;
}
HANDLE hDuplicateToken;
if (!DuplicateTokenEx(hToken, MAXIMUM_ALLOWED, NULL, SecurityImpersonation, TokenPrimary, &hDuplicateToken))
{
PrintLastError();
MessageBoxW(0, L"DuplicateTokenEx csrss", 0, 0);
return 0;
}
return hDuplicateToken;
}
int main()
{
wchar_t *cmdline = GetCommandLineW();
int argc;
LPWSTR *argv = CommandLineToArgvW(cmdline, &argc);
BOOLEAN e;
RtlAdjustPrivilege(SE_DEBUG_PRIVILEGE, 1, 0, &e);
SC_HANDLE hScm, hSvc;
hScm = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
if (!hScm)
{
PrintLastError();
MessageBoxW(0, L"OpenSCManager", 0, 0);
return 0;
}
hSvc = OpenServiceW(hScm, L"TrustedInstaller", SERVICE_START | SERVICE_QUERY_STATUS);
if (!hSvc)
{
PrintLastError();
MessageBoxW(0, L"OpenServiceW", 0, 0);
return 0;
}
if (!StartServiceW(hSvc, 0, NULL))
{
DWORD err = GetLastError();
if (err != 1056)
{
PrintLastError(err);
MessageBoxW(0, L"StartServiceW", 0, 0);
return 0;
}
}
HANDLE hToken, hProcess;
SERVICE_STATUS_PROCESS serviceStatus;
DWORD bytesNeeded;
while (!QueryServiceStatusEx(hSvc, SC_STATUS_PROCESS_INFO, (LPBYTE)&serviceStatus, sizeof(SERVICE_STATUS_PROCESS), &bytesNeeded))
{
PrintLastError();
Sleep(100);
}
DWORD pid = serviceStatus.dwProcessId;
HANDLE sysToken = getSysToken();
if (!ImpersonateLoggedOnUser(sysToken))
{
PrintLastError();
MessageBoxW(0, L"ImpersonateLoggedOnUser", 0, 0);
return 0;
}
RtlAdjustPrivilege(SE_DEBUG_PRIVILEGE, 1, 0, &e);
hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, 0, pid);
if (!hProcess)
{
PrintLastError();
MessageBoxW(0, L"OpenProcess TrustedInstaller", 0, 0);
return 0;
}
if (!OpenProcessToken(hProcess, TOKEN_DUPLICATE, &hToken))
{
PrintLastError();
MessageBoxW(0, L"OpenProcessToken TrustedInstaller", 0, 0);
return 0;
}
HANDLE hDuplicateToken;
if (!DuplicateTokenEx(hToken, MAXIMUM_ALLOWED, NULL, SecurityImpersonation, TokenPrimary, &hDuplicateToken))
{
PrintLastError();
MessageBoxW(0, L"DuplicateTokenEx TrustedInstaller", 0, 0);
return 0;
}
STARTUPINFOW si;
ZeroMemory(&si, sizeof(STARTUPINFOW));
si.cb = sizeof(STARTUPINFOW);
PROCESS_INFORMATION pi;
ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
si.lpDesktop = (LPWSTR)L"WinSta0\\Default";
if (!CreateProcessWithTokenW(hDuplicateToken, 0, argc < 2 ? L"C:\\Windows\\system32\\cmd.exe" : argv[1], 0, 0, NULL, NULL, &si, &pi))
{
PrintLastError();
MessageBoxW(0, L"CreateProcessWithTokenW", 0, 0);
return 0;
}
return 0;
}