1
0
mirror of https://github.com/momo5502/hypervisor.git synced 2025-07-03 09:41:56 +00:00

Extract into library

This commit is contained in:
Maurice Heumann
2022-12-27 16:27:33 +01:00
parent f8f636a829
commit 28dd94f2ef
31 changed files with 279 additions and 237 deletions

84
src/library/main.cpp Normal file
View File

@ -0,0 +1,84 @@
#include "std_include.hpp"
#include <iostream>
#include <conio.h>
#include <set>
#include "driver.hpp"
#include "driver_device.hpp"
#include <driver_file.h>
#include <irp_data.hpp>
#include "resource.hpp"
#include "utils/io.hpp"
void patch_data(const driver_device& driver_device, const uint32_t pid, const uint64_t address, const uint8_t* buffer,
const size_t length)
{
hook_request hook_request{};
hook_request.process_id = pid;
hook_request.target_address = reinterpret_cast<void*>(address);
hook_request.source_data = buffer;
hook_request.source_data_size = length;
driver_device::data input{};
input.assign(reinterpret_cast<uint8_t*>(&hook_request),
reinterpret_cast<uint8_t*>(&hook_request) + sizeof(hook_request));
(void)driver_device.send(HOOK_DRV_IOCTL, input);
}
driver_device create_driver_device()
{
return driver_device{R"(\\.\HyperHook)"};
}
driver create_driver()
{
return driver{std::filesystem::absolute(DRIVER_NAME), "HyperHook"};
}
driver_device& get_driver_device()
{
static driver hypervisor{};
static driver_device device{};
if (device)
{
return device;
}
try
{
device = create_driver_device();
}
catch (...)
{
hypervisor = create_driver();
device = create_driver_device();
}
return device;
}
extern "C" __declspec(dllexport)
int hyperhook_write(const unsigned int process_id, const unsigned long long address, const void* data,
const unsigned long long size)
{
try
{
const auto& device = get_driver_device();
if (device)
{
patch_data(device, process_id, address, static_cast<const uint8_t*>(data), size);
return 1;
}
}
catch (const std::exception& e)
{
printf("%s\n", e.what());
}
return 0;
}