-
Notifications
You must be signed in to change notification settings - Fork 74
/
r0akmem.c
257 lines (223 loc) · 5.64 KB
/
r0akmem.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
/*++
Copyright (c) Alex Ionescu. All rights reserved.
Module Name:
r0akmem.c
Abstract:
This module implements kernel memory allocation and mapping for r0ak
Author:
Alex Ionescu (@aionescu) 21-Jul-2018 - First public version
Environment:
User mode only.
--*/
#include "r0ak.h"
//
// Internal definitions
//
#define POOL_TAG_FIXED_BUFFER (32 * 1024 * 1024)
#define PAGE_SIZE 4096
#define NPFS_DATA_ENTRY_SIZE 0x30
#define NPFS_DATA_ENTRY_POOL_TAG 'rFpN'
//
// Tracks allocation state between calls
//
typedef struct _KERNEL_ALLOC
{
HANDLE Pipes[2];
PVOID UserBase;
PVOID KernelBase;
ULONG MagicSize;
} KERNEL_ALLOC, *PKERNEL_ALLOC;
_Success_(return != 0)
PVOID
GetKernelAddress (
_In_ ULONG Size
)
{
NTSTATUS status;
PSYSTEM_BIGPOOL_INFORMATION bigPoolInfo;
PSYSTEM_BIGPOOL_ENTRY entry;
ULONG resultLength;
ULONG_PTR resultAddress;
ULONG i;
//
// Allocate a large 32MB buffer to store pool tags in
//
bigPoolInfo = VirtualAlloc(NULL,
POOL_TAG_FIXED_BUFFER,
MEM_COMMIT | MEM_RESERVE,
PAGE_READWRITE);
if (!bigPoolInfo)
{
printf("[-] No memory for pool buffer\n");
return NULL;
}
//
// Dump all pool tags
//
status = NtQuerySystemInformation(SystemBigPoolInformation,
bigPoolInfo,
POOL_TAG_FIXED_BUFFER,
&resultLength);
if (!NT_SUCCESS(status))
{
printf("[-] Failed to dump pool allocations: %lx\n", status);
return NULL;
}
//
// Scroll through them all
//
for (resultAddress = 0, i = 0; i < bigPoolInfo->Count; i++)
{
//
// Check for the desired allocation
//
entry = &bigPoolInfo->AllocatedInfo[i];
if (entry->TagUlong == NPFS_DATA_ENTRY_POOL_TAG)
{
//
// With the Heap-Backed Pool in RS5/19H1, sizes are precise, while
// the large pool allocator uses page-aligned pages
//
if ((entry->SizeInBytes == (Size + PAGE_SIZE)) ||
(entry->SizeInBytes == (Size + NPFS_DATA_ENTRY_SIZE)))
{
//
// Mask out the nonpaged pool bit
//
resultAddress = (ULONG_PTR)entry->VirtualAddress & ~1;
break;
}
}
}
//
// Weird..
//
if (resultAddress == 0)
{
printf("[-] Kernel buffer not found!\n");
return NULL;
}
//
// Free the buffer
//
VirtualFree(bigPoolInfo, 0, MEM_RELEASE);
return (PVOID)(resultAddress + NPFS_DATA_ENTRY_SIZE);
}
_Success_(return != 0)
PVOID
KernelAlloc (
_Outptr_ PKERNEL_ALLOC* KernelAlloc,
_In_ ULONG Size
)
{
BOOL b;
//
// Catch bad callers
//
if (KernelAlloc == NULL)
{
return NULL;
}
//
// Only support < 2KB allocations
//
*KernelAlloc = NULL;
if (Size > 2048)
{
return NULL;
}
//
// Allocate our tracker structure
//
*KernelAlloc = HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY,
sizeof(**KernelAlloc));
if (*KernelAlloc == NULL)
{
return NULL;
}
//
// Compute a magic size to get something in big pool that should be unique
// This will use at most ~5MB of non-paged pool
//
(*KernelAlloc)->MagicSize = 0;
while ((*KernelAlloc)->MagicSize == 0)
{
(*KernelAlloc)->MagicSize = (((__rdtsc() & 0xFF000000) >> 24) * 0x5000);
}
//
// Allocate the right child page that will be sent to the trampoline
//
(*KernelAlloc)->UserBase = VirtualAlloc(NULL,
(*KernelAlloc)->MagicSize,
MEM_COMMIT | MEM_RESERVE,
PAGE_READWRITE);
if ((*KernelAlloc)->UserBase == NULL)
{
printf("[-] Failed to allocate user-mode memory for kernel buffer\n");
return NULL;
}
//
// Allocate a pipe to hold on to the buffer
//
b = CreatePipe(&(*KernelAlloc)->Pipes[0],
&(*KernelAlloc)->Pipes[1],
NULL,
(*KernelAlloc)->MagicSize);
if (!b)
{
printf("[-] Failed creating the pipe: %lx\n",
GetLastError());
return NULL;
}
//
// Return the allocated user-mode base
//
return (*KernelAlloc)->UserBase;
}
_Success_(return != 0)
PVOID
KernelWrite (
_In_ PKERNEL_ALLOC KernelAlloc
)
{
BOOL b;
//
// Write into the buffer
//
b = WriteFile(KernelAlloc->Pipes[1],
KernelAlloc->UserBase,
KernelAlloc->MagicSize,
NULL,
NULL);
if (!b)
{
printf("[-] Failed writing kernel buffer: %lx\n",
GetLastError());
return NULL;
}
//
// Compute the kernel address and return it
//
KernelAlloc->KernelBase = GetKernelAddress(KernelAlloc->MagicSize);
return KernelAlloc->KernelBase;
}
VOID
KernelFree (
_In_ PKERNEL_ALLOC KernelAlloc
)
{
//
// Free the UM side of the allocation
//
VirtualFree(KernelAlloc->UserBase, 0, MEM_RELEASE);
//
// Close the pipes, which will free the kernel side
//
CloseHandle(KernelAlloc->Pipes[0]);
CloseHandle(KernelAlloc->Pipes[1]);
//
// Free the structure
//
HeapFree(GetProcessHeap(), 0, KernelAlloc);
}