-
Notifications
You must be signed in to change notification settings - Fork 74
/
r0ak.c
247 lines (215 loc) · 5.47 KB
/
r0ak.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
/*++
Copyright (c) Alex Ionescu. All rights reserved.
Module Name:
r0ak.c
Abstract:
This module implements the main command line interface for r0ak
Author:
Alex Ionescu (@aionescu) 21-Jul-2018 - First public version
Environment:
User mode only.
--*/
#include "r0ak.h"
_Success_(return != 0)
BOOL
CmdParseInputParameters (
_In_ PCHAR Arguments[],
_Out_ PVOID* Function,
_Out_ PULONG_PTR FunctionArgument
)
{
PVOID functionPointer;
PCHAR moduleName, functionName, pBang, functionNameAndModule;
//
// Check if the user passed in a module!function instead
//
functionPointer = (PVOID)strtoull(Arguments[2], NULL, 0);
if (functionPointer == NULL)
{
//
// Separate out the module name from the symbol name
//
functionNameAndModule = Arguments[2];
pBang = strchr(functionNameAndModule, '!');
if (pBang == NULL)
{
printf("[-] Malformed symbol string: %s\n",
Arguments[2]);
return FALSE;
}
//
// Now get the remaining function name
//
functionName = pBang + 1;
*pBang = ANSI_NULL;
moduleName = functionNameAndModule;
//
// Get the symbol requested
//
functionPointer = SymLookup(moduleName, functionName);
if (functionPointer == NULL)
{
printf("[-] Could not find symbol!\n");
return FALSE;
}
}
//
// Return the data back
//
*Function = functionPointer;
*FunctionArgument = strtoull(Arguments[3], NULL, 0);
return TRUE;
}
INT
main (
_In_ INT ArgumentCount,
_In_ PCHAR Arguments[]
)
{
PKERNEL_EXECUTE kernelExecute;
BOOL b;
ULONG_PTR kernelValue;
PVOID kernelPointer;
INT errValue;
//
// Print header
//
printf("r0ak v1.0.0 -- Ring 0 Army Knife\n");
printf("http://www.github.com/ionescu007/r0ak\n");
printf("Copyright (c) 2018 Alex Ionescu [@aionescu]\n");
printf("http://www.windows-internals.com\n\n");
kernelExecute = NULL;
errValue = -1;
//
// We need four arguments
//
if (ArgumentCount != 4)
{
printf("USAGE: r0ak.exe\n"
" [--execute <Address | module!function> <Argument>]\n"
" [--write <Address | module!function> <Value>]\n"
" [--read <Address | module!function> <Size>]\n");
goto Cleanup;
}
//
// Initialize symbol engine
//
b = SymSetup();
if (b == FALSE)
{
printf("[-] Failed to initialize Symbol Engine\n");
goto Cleanup;
}
//
// Initialize our execution engine
//
b = KernelExecuteSetup(&kernelExecute, g_TrampolineFunction);
if (b == FALSE)
{
printf("[-] Failed to setup Ring 0 execution engine\n");
goto Cleanup;
}
//
// Caller wants to execute their own routine
//
if (strstr(Arguments[1], "--execute"))
{
//
// Get the initial inputs
//
b = CmdParseInputParameters(Arguments, &kernelPointer, &kernelValue);
if (b == FALSE)
{
goto Cleanup;
}
//
// Execute it
//
b = CmdExecuteKernel(kernelExecute, kernelPointer, kernelValue);
if (b == FALSE)
{
printf("[-] Failed to execute function\n");
goto Cleanup;
}
//
// It's now safe to exit/cleanup state
//
printf("[+] Function executed successfuly!\n");
errValue = 0;
}
else if (strstr(Arguments[1], "--write"))
{
//
// Get the initial inputs
//
b = CmdParseInputParameters(Arguments, &kernelPointer, &kernelValue);
if (b == FALSE)
{
goto Cleanup;
}
//
// Only 32-bit values can be written
//
if (kernelValue > ULONG_MAX)
{
printf("[-] Invalid 64-bit value, r0ak only supports 32-bit\n");
goto Cleanup;
}
//
// Write it!
//
b = CmdWriteKernel(kernelExecute, kernelPointer, (ULONG)kernelValue);
if (b == FALSE)
{
printf("[-] Failed to write variable\n");
goto Cleanup;
}
//
// It's now safe to exit/cleanup state
//
printf("[+] Write executed successfuly!\n");
errValue = 0;
}
else if (strstr(Arguments[1], "--read"))
{
//
// Get the initial inputs
//
b = CmdParseInputParameters(Arguments, &kernelPointer, &kernelValue);
if (b == FALSE)
{
goto Cleanup;
}
//
// Only 4GB of data can be read
//
if (kernelValue > ULONG_MAX)
{
printf("[-] Invalid size, r0ak can only read up to 4GB of data\n");
goto Cleanup;
}
//
// Write it!
//
b = CmdReadKernel(kernelExecute, kernelPointer, (ULONG)kernelValue);
if (b == FALSE)
{
printf("[-] Failed to read variable\n");
goto Cleanup;
}
//
// It's now safe to exit/cleanup state
//
printf("[+] Read executed successfuly!\n");
errValue = 0;
}
Cleanup:
//
// Teardown the execution engine if we initialized it
//
if (kernelExecute != NULL)
{
KernelExecuteTeardown(kernelExecute);
}
return errValue;
}