Driver Main
Driver Main
Driver Main
h"
HANDLE Driver::driverH = 0;
uintptr_t Driver::currentProcessId = 0;
GUID DummyGuid = { 2 };
mRtlAdjustPrivilege myRtlAdjustPrivilege = (mRtlAdjustPrivilege)NULL;
mNtSetSystemEnvironmentValueEx myNtSetSystemEnvironmentValueEx =
(mNtSetSystemEnvironmentValueEx)NULL;
mNtQuerySystemInformation myNtQuerySystemInformation =
(mNtQuerySystemInformation)NULL;
BOOLEAN SeSystemEnvironmentWasEnabled;
const NTSTATUS Status = myRtlAdjustPrivilege(SE_SYSTEM_ENVIRONMENT_PRIVILEGE,
Enable,
FALSE,
&SeSystemEnvironmentWasEnabled);
return Status;
}
IMAGE_DOS_HEADER dos_header = { 0 };
IMAGE_NT_HEADERS64 nt_headers = { 0 };
Unprotect(Driver::read_memory);
Driver::read_memory(4, kernel_module_base, (uintptr_t)&dos_header,
sizeof(dos_header));
Protect(Driver::read_memory);
if (dos_header.e_magic != IMAGE_DOS_SIGNATURE)
return 0;
Unprotect(Driver::read_memory);
Driver::read_memory(4, kernel_module_base + dos_header.e_lfanew,
(uintptr_t)&nt_headers, sizeof(nt_headers));
Protect(Driver::read_memory);
if (nt_headers.Signature != IMAGE_NT_SIGNATURE)
return 0;
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));
Unprotect(Driver::read_memory);
Driver::read_memory(4, kernel_module_base + export_base,
(uintptr_t)export_data, export_base_size);
Protect(Driver::read_memory);
if (!_stricmp(current_function_name, function_name))
{
const auto function_ordinal = ordinal_table[i];
const auto function_address = kernel_module_base +
function_table[function_ordinal];
VirtualFree(export_data, 0, MEM_RELEASE);
return function_address;
}
}
VirtualFree(export_data, 0, MEM_RELEASE);
return 0;
}
// Hmu on discord (Chase.#1803) if you need any help :)
uintptr_t GetKernelModuleAddress(char* module_name)
{
void* buffer = nullptr;
DWORD buffer_size = 0;
NTSTATUS status =
myNtQuerySystemInformation(static_cast<SYSTEM_INFORMATION_CLASS>(SystemModuleInform
ation), buffer, buffer_size, &buffer_size);
if (!NT_SUCCESS(status))
{
VirtualFree(buffer, 0, MEM_RELEASE);
return 0;
}
if (!_stricmp(current_module_name, module_name))
{
const uintptr_t result = (uintptr_t)(modules-
>Modules[i].ImageBase);
VirtualFree(buffer, 0, MEM_RELEASE);
return result;
}
}
VirtualFree(buffer, 0, MEM_RELEASE);
return 0;
}
// Hmu on discord (Chase.#1803) if you need any help :)
bool Driver::initialize() {
Protect(_ReturnAddress());
currentProcessId = GetCurrentProcessId();
BOOLEAN SeSystemEnvironmentWasEnabled;
SLog(L"Driver Init");
HMODULE ntmodule = LoadLibraryW(L"ntdll.dll");
SLog(std::wstring(L"loaded ntdll " + std::to_wstring((DWORD)ntmodule) + L" p
" + std::to_wstring(currentProcessId)).c_str());
BYTE ntqsi[] =
{ 'N','t','Q','u','e','r','y','S','y','s','t','e','m','I','n','f','o','r','m','a','
t','i','o','n',0 };
BYTE nssevex[] =
{ 'N','t','S','e','t','S','y','s','t','e','m','E','n','v','i','r','o','n','m','e','
n','t','V','a','l','u','e','E','x',0 };
BYTE rtlajp[] =
{ 'R','t','l','A','d','j','u','s','t','P','r','i','v','i','l','e','g','e',0 };
myNtQuerySystemInformation =
(mNtQuerySystemInformation)GetProcAddress(ntmodule, (char*)ntqsi);
myNtSetSystemEnvironmentValueEx =
(mNtSetSystemEnvironmentValueEx)GetProcAddress(ntmodule, (char*)nssevex);
myRtlAdjustPrivilege = (mRtlAdjustPrivilege)GetProcAddress(ntmodule,
(char*)rtlajp);
SLog(std::wstring(L"Funcs 1 " +
std::to_wstring((uintptr_t)myNtQuerySystemInformation) + L" 2 " +
std::to_wstring((uintptr_t)myNtSetSystemEnvironmentValueEx) + L" 3 " +
std::to_wstring((uintptr_t)myRtlAdjustPrivilege)).c_str());
Unprotect(SetSystemEnvironmentPrivilege);
NTSTATUS status = SetSystemEnvironmentPrivilege(true,
&SeSystemEnvironmentWasEnabled);
Protect(SetSystemEnvironmentPrivilege);
if (!NT_SUCCESS(status)) {
Unprotect(_ReturnAddress());
return false;
}
SLog(L"Cleared old");
Unprotect(GetKernelModuleAddress);
BYTE nstosname[] = { 'n','t','o','s','k','r','n','l','.','e','x','e',0 };
uintptr_t kernelModuleAddress = GetKernelModuleAddress((char*)nstosname);
memset(nstosname, 0, sizeof(nstosname));
SLog(std::wstring(L"kernel module addr " +
std::to_wstring((uintptr_t)kernelModuleAddress)).c_str());
Protect(GetKernelModuleAddress);
Unprotect(GetKernelModuleExport);
BYTE pbid[] =
{ 'P','s','L','o','o','k','u','p','P','r','o','c','e','s','s','B','y','P','r','o','
c','e','s','s','I','d',0 };
BYTE gba[] =
{ 'P','s','G','e','t','P','r','o','c','e','s','s','S','e','c','t','i','o','n','B','
a','s','e','A','d','d','r','e','s','s',0 };
BYTE mmcp[] =
{ 'M','m','C','o','p','y','V','i','r','t','u','a','l','M','e','m','o','r','y',0 };
uintptr_t kernel_PsLookupProcessByProcessId =
GetKernelModuleExport(kernelModuleAddress, (char*)pbid);
uintptr_t kernel_PsGetProcessSectionBaseAddress =
GetKernelModuleExport(kernelModuleAddress, (char*)gba);
uintptr_t kernel_MmCopyVirtualMemory =
GetKernelModuleExport(kernelModuleAddress, (char*)mmcp);
SLog(std::wstring(L"exports " +
std::to_wstring((uintptr_t)kernel_PsLookupProcessByProcessId) + L" - " +
std::to_wstring((uintptr_t)kernel_PsGetProcessSectionBaseAddress) + L" - " +
std::to_wstring((uintptr_t)kernel_MmCopyVirtualMemory)).c_str());
memset(pbid, 0, sizeof(pbid));
memset(gba, 0, sizeof(gba));
memset(mmcp, 0, sizeof(mmcp));
Protect(GetKernelModuleExport);
uintptr_t result = 0;
MemoryCommand cmd = MemoryCommand();
cmd.operation = baseOperation * 0x612;
cmd.magic = COMMAND_MAGIC;
cmd.data[0] = kernel_PsLookupProcessByProcessId;
cmd.data[1] = kernel_PsGetProcessSectionBaseAddress;
cmd.data[2] = kernel_MmCopyVirtualMemory;
cmd.data[3] = (uintptr_t)&result;
Unprotect(SendCommand);
SLog(L"Sending config");
SendCommand(&cmd);
SLog(L"configured");
Protect(SendCommand);
Unprotect(_ReturnAddress());
return result;
}
// Hmu on discord (Chase.#1803) if you need any help :)
NTSTATUS Driver::read_memory(
const uintptr_t process_id,
const uintptr_t address,
const uintptr_t buffer,
const size_t size) {
Protect(_ReturnAddress());
Unprotect(copy_memory);
NTSTATUS result = copy_memory(process_id, address, currentProcessId, buffer,
size);
Protect(copy_memory);
Unprotect(_ReturnAddress());
return result;
}
// Hmu on discord (Chase.#1803) if you need any help :)
NTSTATUS Driver::write_memory(
const uintptr_t process_id,
const uintptr_t address,
const uintptr_t buffer,
const size_t size) {
Protect(_ReturnAddress());
Unprotect(copy_memory);
NTSTATUS result = copy_memory(currentProcessId, buffer, process_id, address,
size);
Protect(copy_memory);
Unprotect(_ReturnAddress());
return result;
}