-
Notifications
You must be signed in to change notification settings - Fork 74
/
r0akutil.c
268 lines (235 loc) · 5.41 KB
/
r0akutil.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
/*++
Copyright (c) Alex Ionescu. All rights reserved.
Module Name:
r0akutil.c
Abstract:
This module implements utility functions for r0ak
Author:
Alex Ionescu (@aionescu) 21-Jul-2018 - First public version
Environment:
User mode only.
--*/
#include "r0ak.h"
VOID
DumpHex (
_In_ LPCVOID Data,
_In_ SIZE_T Size
)
{
CHAR ascii[17];
SIZE_T i, j;
//
// Parse each byte in the stream
//
ascii[16] = ANSI_NULL;
for (i = 0; i < Size; ++i)
{
//
// Every new line, print a TAB
//
if ((i % 16) == 0)
{
printf("\t");
}
//
// Print the hex representation of the data
//
printf("%02X", ((PUCHAR)Data)[i]);
//
// And as long as this isn't the middle dash, print a space
//
if ((i + 1) % 16 != 8)
{
printf(" ");
}
else
{
printf("-");
}
//
// Is this a printable character? If not, use a '.' to represent it
//
if (isprint(((PUCHAR)Data)[i]))
{
ascii[i % 16] = ((PUCHAR)Data)[i];
}
else
{
ascii[i % 16] = '.';
}
//
// Is this end of the line? If so, print it out
//
if (((i + 1) % 16) == 0)
{
printf(" %s\n", ascii);
}
if ((i + 1) == Size)
{
//
// We've reached the end of the buffer, keep printing spaces
// until we get to the end of the line
//
ascii[(i + 1) % 16] = ANSI_NULL;
for (j = ((i + 1) % 16); j < 16; j++)
{
printf(" ");
}
printf(" %s\n", ascii);
}
}
}
_Success_(return != 0)
ULONG_PTR
GetDriverBaseAddr (
_In_ PCCH BaseName
)
{
LPVOID BaseAddresses[1024];
DWORD cbNeeded;
CHAR FileName[MAX_PATH];
//
// Enumerate all the device drivers
//
if (!EnumDeviceDrivers(BaseAddresses, sizeof(BaseAddresses), &cbNeeded))
{
printf("[-] Failed to enumerate driver base addresses: %lx\n",
GetLastError());
return 0;
}
//
// Go through each one
//
for (int i = 0; i < (cbNeeded / sizeof(LPVOID)); i++)
{
//
// Get its name
//
if (!GetDeviceDriverBaseNameA(BaseAddresses[i],
FileName,
sizeof(FileName)))
{
printf("[-] Failed to get driver name: %lx\n",
GetLastError());
return 0;
}
//
// Compare it
//
if (!_stricmp(FileName, BaseName))
{
return (ULONG_PTR)BaseAddresses[i];
}
}
return 0;
}
_Success_(return != 0)
BOOL
ElevateToSystem (
VOID
)
{
HANDLE hProcess;
HANDLE hToken, hNewtoken;
HANDLE hSnapshot;
DWORD logonPid;
BOOL b;
PROCESSENTRY32W processEntry;
BOOLEAN old;
NTSTATUS status;
//
// Create toolhelp snaapshot
//
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot == NULL)
{
printf("[-] Failed to initialize toolhelp snapshot: %lx\n",
GetLastError());
return FALSE;
}
//
// Scan process list
//
logonPid = 0;
processEntry.dwSize = sizeof(processEntry);
Process32First(hSnapshot, &processEntry);
do
{
//
// Look for winlogon
//
if (wcsstr(processEntry.szExeFile, L"winlogon.exe") != NULL)
{
//
// Found it
//
logonPid = processEntry.th32ProcessID;
break;
}
} while (Process32Next(hSnapshot, &processEntry) != 0);
//
// Fail it not found
//
if (logonPid == 0)
{
printf("[-] Couldn't find Winlogon.exe\n");
return FALSE;
}
//
// Enable debug privileges, so that we may open the processes we need
//
status = RtlAdjustPrivilege(SE_DEBUG_PRIVILEGE, TRUE, FALSE, &old);
if (!NT_SUCCESS(status))
{
printf("[-] Failed to get SE_DEBUG_PRIVILEGE: %lx\n",
status);
return FALSE;
}
//
// Open handle to it
//
hProcess = OpenProcess(MAXIMUM_ALLOWED, FALSE, logonPid);
if (hProcess == NULL)
{
printf("[-] Failed to open handle to Winlogon: %lx\n",
GetLastError());
return FALSE;
}
//
// Open winlogon's token
//
b = OpenProcessToken(hProcess, MAXIMUM_ALLOWED, &hToken);
if (b == 0)
{
printf("[-] Failed to open Winlogon Token: %lx\n",
GetLastError());
return b;
}
//
// Make an impersonation token copy out of it
//
b = DuplicateToken(hToken, SecurityImpersonation, &hNewtoken);
if (b == 0)
{
printf("[-] Failed to duplicate Winlogon Token: %lx\n",
GetLastError());
return b;
}
//
// And assign it as our thread token
//
b = SetThreadToken(NULL, hNewtoken);
if (b == 0)
{
printf("[-] Failed to impersonate Winlogon Token: %lx\n",
GetLastError());
return b;
}
//
// Close the handle to wininit, its token, and our copy
//
CloseHandle(hProcess);
CloseHandle(hToken);
CloseHandle(hNewtoken);
return TRUE;
}