-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathtokens.c
More file actions
278 lines (233 loc) · 6.77 KB
/
tokens.c
File metadata and controls
278 lines (233 loc) · 6.77 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
superUser 6.2
Copyright 2019-2026 https://github.com/mspaintmsi/superUser
tokens.c
Tokens and privileges management functions
*/
#include "tokens.h"
#define CUSTOM_ERROR_PROCESS_NOT_FOUND 0xA0001000
#define CUSTOM_ERROR_SERVICE_START_FAILED 0xA0001001
#include <wchar.h>
#include <windows.h>
#include <wtsapi32.h>
#ifdef __GNUC__
#include "winnt2.h"
#endif
#include "output.h" // Display functions
const wchar_t* apcwszTokenPrivileges[ 36 ] = {
SE_ASSIGNPRIMARYTOKEN_NAME,
SE_AUDIT_NAME,
SE_BACKUP_NAME,
SE_CHANGE_NOTIFY_NAME,
SE_CREATE_GLOBAL_NAME,
SE_CREATE_PAGEFILE_NAME,
SE_CREATE_PERMANENT_NAME,
SE_CREATE_SYMBOLIC_LINK_NAME,
SE_CREATE_TOKEN_NAME, // Most users won't have that.
SE_DEBUG_NAME,
SE_DELEGATE_SESSION_USER_IMPERSONATE_NAME,
SE_ENABLE_DELEGATION_NAME, // Most users won't have that.
SE_IMPERSONATE_NAME,
SE_INC_BASE_PRIORITY_NAME,
SE_INC_WORKING_SET_NAME,
SE_INCREASE_QUOTA_NAME,
SE_LOAD_DRIVER_NAME,
SE_LOCK_MEMORY_NAME,
SE_MACHINE_ACCOUNT_NAME, // Most users won't have that.
SE_MANAGE_VOLUME_NAME,
SE_PROF_SINGLE_PROCESS_NAME,
SE_RELABEL_NAME, // Most users won't have that.
SE_REMOTE_SHUTDOWN_NAME, // Most users won't have that.
SE_RESTORE_NAME,
SE_SECURITY_NAME,
SE_SHUTDOWN_NAME,
SE_SYNC_AGENT_NAME, // Most users won't have that.
SE_SYSTEM_ENVIRONMENT_NAME,
SE_SYSTEM_PROFILE_NAME,
SE_SYSTEMTIME_NAME,
SE_TAKE_OWNERSHIP_NAME,
SE_TCB_NAME,
SE_TIME_ZONE_NAME,
SE_TRUSTED_CREDMAN_ACCESS_NAME, // Most users won't have that.
SE_UNDOCK_NAME,
SE_UNSOLICITED_INPUT_NAME // Most users won't have that.
};
static BOOL enableTokenPrivilege( HANDLE hToken, const wchar_t* pcwszPrivilege )
{
LUID luid;
if (! LookupPrivilegeValue( NULL, pcwszPrivilege, &luid ))
return FALSE; // Cannot lookup privilege value
TOKEN_PRIVILEGES tp = {
.PrivilegeCount = 1,
.Privileges[ 0 ].Luid = luid,
.Privileges[ 0 ].Attributes = SE_PRIVILEGE_ENABLED
};
AdjustTokenPrivileges( hToken, FALSE, &tp, 0, NULL, NULL );
return (GetLastError() == ERROR_SUCCESS);
}
void setAllPrivileges( HANDLE hToken, MissingPrivilegeFunc fnMPCb )
{
// Iterate over apcwszTokenPrivileges to add all privileges to a token
for (int i = 0; i < (sizeof( apcwszTokenPrivileges ) /
sizeof( *apcwszTokenPrivileges )); i++)
if (! enableTokenPrivilege( hToken, apcwszTokenPrivileges[ i ] ) && fnMPCb)
fnMPCb( apcwszTokenPrivileges[ i ] );
}
int acquireSeDebugPrivilege( void )
{
DWORD dwLastError = 0;
int iStep = 1;
BOOL bSuccess = FALSE;
HANDLE hToken = NULL;
if (OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken )) {
iStep++;
bSuccess = enableTokenPrivilege( hToken, SE_DEBUG_NAME );
if (! bSuccess) dwLastError = GetLastError();
CloseHandle( hToken );
}
else dwLastError = GetLastError();
if (! bSuccess) {
showError( L"Failed to acquire SeDebugPrivilege", dwLastError, iStep );
return 2;
}
return 0;
}
int createSystemContext( void )
{
DWORD dwLastError = 0;
int iStep = 1;
DWORD dwSysPid = (DWORD) -1;
PWTS_PROCESS_INFOW pProcList = NULL;
DWORD dwProcCount = 0;
// Get the process id
if (WTSEnumerateProcessesW( WTS_CURRENT_SERVER_HANDLE, 0, 1,
&pProcList, &dwProcCount )) {
PWTS_PROCESS_INFOW pProc = pProcList;
while (dwProcCount > 0) {
if (! pProc->SessionId && pProc->pProcessName &&
! _wcsicmp( L"services.exe", pProc->pProcessName ) &&
pProc->pUserSid &&
IsWellKnownSid( pProc->pUserSid, WinLocalSystemSid )) {
dwSysPid = pProc->ProcessId;
break;
}
pProc++;
dwProcCount--;
}
WTSFreeMemory( pProcList );
}
else dwLastError = GetLastError();
HANDLE hToken = NULL;
if (dwSysPid != (DWORD) -1) {
iStep++;
HANDLE hSysProcess = OpenProcess( PROCESS_QUERY_LIMITED_INFORMATION, FALSE,
dwSysPid );
if (hSysProcess) {
iStep++;
// Get the process token
HANDLE hSysToken = NULL;
if (OpenProcessToken( hSysProcess, TOKEN_DUPLICATE, &hSysToken )) {
iStep++;
if (! DuplicateTokenEx( hSysToken,
TOKEN_ADJUST_PRIVILEGES | TOKEN_IMPERSONATE, NULL,
SecurityImpersonation, TokenImpersonation, &hToken )) {
dwLastError = GetLastError();
hToken = NULL;
}
CloseHandle( hSysToken );
}
else dwLastError = GetLastError();
CloseHandle( hSysProcess );
}
else dwLastError = GetLastError();
}
else dwLastError = CUSTOM_ERROR_PROCESS_NOT_FOUND; // Process not found
BOOL bSuccess = FALSE;
if (hToken) {
iStep++;
if (enableTokenPrivilege( hToken, SE_ASSIGNPRIMARYTOKEN_NAME )) {
iStep++;
bSuccess = SetThreadToken( NULL, hToken );
}
if (! bSuccess) dwLastError = GetLastError();
CloseHandle( hToken );
}
if (! bSuccess) {
showError( L"Failed to create system context", dwLastError, iStep );
return 5;
}
return 0;
}
int getTrustedInstallerProcess( HANDLE* phTIProcess )
{
DWORD dwLastError = 0;
int iStep = 1;
HANDLE hSCManager, hTIService;
SERVICE_STATUS_PROCESS serviceStatusBuffer = {0};
SetLastError( 0 );
hSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_CONNECT );
hTIService = OpenService( hSCManager, L"TrustedInstaller",
SERVICE_QUERY_STATUS | SERVICE_START );
// Start the TrustedInstaller service
BOOL bStopped = TRUE;
if (hTIService) {
iStep++;
int retry = 1;
DWORD dwBytesNeeded;
while (
QueryServiceStatusEx( hTIService, SC_STATUS_PROCESS_INFO,
(LPBYTE) &serviceStatusBuffer, sizeof( SERVICE_STATUS_PROCESS ),
&dwBytesNeeded ) &&
(bStopped = (serviceStatusBuffer.dwCurrentState == SERVICE_STOPPED)) &&
retry &&
StartService( hTIService, 0, NULL )
) {
retry = 0;
}
}
if (bStopped) {
dwLastError = GetLastError();
if (dwLastError == 0) dwLastError = CUSTOM_ERROR_SERVICE_START_FAILED;
}
CloseServiceHandle( hSCManager );
CloseServiceHandle( hTIService );
*phTIProcess = NULL;
if (! bStopped) {
iStep++;
// Get the TrustedInstaller process handle
*phTIProcess = OpenProcess( PROCESS_CREATE_PROCESS | PROCESS_QUERY_INFORMATION,
FALSE, serviceStatusBuffer.dwProcessId );
if (! *phTIProcess) dwLastError = GetLastError();
}
if (! *phTIProcess) {
showError( L"Failed to open TrustedInstaller process", dwLastError, iStep );
return 3;
}
return 0;
}
int createChildProcessToken( HANDLE hBaseProcess, HANDLE* phNewToken )
{
DWORD dwLastError = 0;
int iStep = 1;
*phNewToken = NULL;
// Get the base process token
HANDLE hBaseToken = NULL;
if (OpenProcessToken( hBaseProcess, TOKEN_DUPLICATE, &hBaseToken )) {
iStep++;
if (! DuplicateTokenEx( hBaseToken,
TOKEN_ADJUST_DEFAULT | TOKEN_ADJUST_PRIVILEGES | TOKEN_ADJUST_SESSIONID |
TOKEN_ASSIGN_PRIMARY | TOKEN_QUERY,
NULL,
SecurityIdentification, TokenPrimary, phNewToken )) {
dwLastError = GetLastError();
*phNewToken = NULL;
}
CloseHandle( hBaseToken );
}
else dwLastError = GetLastError();
if (! *phNewToken) {
showError( L"Failed to create child process token", dwLastError, iStep );
return 5;
}
return 0;
}