1
0
mirror of https://github.com/momo5502/hypervisor.git synced 2025-09-04 16:07:25 +00:00

Prepare code watching

This commit is contained in:
momo5502
2022-05-15 21:47:08 +02:00
parent 8510755ea4
commit 11effb4efa
10 changed files with 495 additions and 14 deletions

View File

@@ -1,7 +1,7 @@
file(GLOB_RECURSE runner_sources ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
file(GLOB_RECURSE runner_headers ${CMAKE_CURRENT_SOURCE_DIR}/*.hpp)
add_executable(runner WIN32
add_executable(runner #WIN32
${runner_sources}
${runner_headers}
)

View File

@@ -1,15 +1,20 @@
#include "std_include.hpp"
#include <iostream>
#include <filesystem>
#include <conio.h>
#include <fstream>
#include "std_include.hpp"
#include "driver.hpp"
#include "driver_device.hpp"
#include "process.hpp"
#include <irp_data.hpp>
#include "resource.hpp"
#include "utils/io.hpp"
#include "utils/nt.hpp"
#pragma comment(lib, "Shlwapi.lib")
@@ -74,21 +79,96 @@ std::filesystem::path extract_driver()
return driver_file;
}
std::vector<std::pair<size_t, size_t>> find_executable_regions(const std::string& pe_file)
{
std::string data{};
if (!utils::io::read_file(pe_file, &data))
{
return {};
}
const utils::nt::library library(reinterpret_cast<HMODULE>(data.data()));
if (!library.is_valid())
{
return {};
}
const auto section_headers = library.get_section_headers();
std::vector<std::pair<size_t, size_t>> regions{};
for (const auto& section_header : section_headers)
{
if (section_header->Characteristics & IMAGE_SCN_MEM_EXECUTE)
{
regions.emplace_back(section_header->VirtualAddress, section_header->Misc.VirtualSize);
}
}
return regions;
}
void unsafe_main(const int /*argc*/, char* /*argv*/[])
{
const auto driver_file = extract_driver();
driver driver{driver_file, "MomoLul"};
const driver_device driver_device{R"(\\.\HelloDev)"};
//launcher().run();
std::string pid_str{};
printf("Please enter the pid: ");
std::getline(std::cin, pid_str);
const auto pid = atoi(pid_str.data());
printf("Opening process...\n");
const auto proc = process::open(pid, PROCESS_QUERY_INFORMATION | PROCESS_VM_READ);
if (!proc)
{
printf("Failed to open process...\n");
return;
}
printf("Reading modules...\n");
const auto modules = process::get_modules(proc);
printf("Found %zu modules\n", modules.size());
std::vector<std::string> module_files{};
module_files.reserve(modules.size());
int i = 0;
for (const auto& module : modules)
{
auto name = process::get_module_filename(proc, module);
printf("(%i)\t%p: %s\n", i++, static_cast<void*>(module), name.data());
module_files.emplace_back(std::move(name));
}
std::string module_str{};
printf("\nPlease enter the module number: ");
std::getline(std::cin, module_str);
const auto module_num = atoi(module_str.data());
if (module_num < 0 || static_cast<size_t>(module_num) >= modules.size())
{
printf("Invalid module num\n");
_getch();
return;
}
const auto module_base = reinterpret_cast<uint8_t*>(modules[module_num]);
const auto& file = module_files[module_num];
printf("Analyzing %s...\n", file.data());
const auto regions = find_executable_regions(file);
for (const auto& region : regions)
{
printf("%p - %zu\n", module_base + region.first, region.second);
}
_getch();
return;
const auto driver_file = extract_driver();
driver driver{driver_file, "MomoLul"};
const driver_device driver_device{R"(\\.\HelloDev)"};
/*
// IW5
insert_nop(driver_device, pid, 0x4488A8, 2); // Force calling CG_DrawFriendOrFoeTargetBoxes

81
src/runner/process.cpp Normal file
View File

@@ -0,0 +1,81 @@
#include "std_include.hpp"
#include "process.hpp"
namespace process
{
native_handle open(const uint32_t process_id, const DWORD access)
{
const auto handle = ::OpenProcess(access, FALSE, process_id);
if (handle)
{
return {handle};
}
return {};
}
std::vector<HMODULE> get_modules(const native_handle& process)
{
if (!process)
{
return {};
}
DWORD needed = 1024;
std::vector<HMODULE> result{};
do
{
result.resize(needed);
if (!EnumProcessModulesEx(process, result.data(), static_cast<DWORD>(result.size()), &needed,
LIST_MODULES_ALL))
{
return {};
}
}
while (needed > result.size());
result.resize(needed);
// Remove duplicates
std::ranges::sort(result);
const auto last = std::ranges::unique(result).begin();
result.erase(last, result.end());
// Remove nullptr
for (auto i = result.begin(); i != result.end();)
{
if (*i == nullptr)
{
i = result.erase(i);
break;
}
else
{
++i;
}
}
return result;
}
std::string get_module_filename(const native_handle& process, const HMODULE module)
{
if (!process)
{
return {};
}
std::string buffer{};
buffer.resize(1024);
const auto length = GetModuleFileNameExA(process, module, &buffer[0], static_cast<DWORD>(buffer.size()));
if (length > 0)
{
buffer.resize(length);
return buffer;
}
return {};
}
}

9
src/runner/process.hpp Normal file
View File

@@ -0,0 +1,9 @@
#pragma once
#include "native_handle.hpp"
namespace process
{
native_handle open(uint32_t process_id, DWORD access);
std::vector<HMODULE> get_modules(const native_handle& process);
std::string get_module_filename(const native_handle& process, HMODULE module);
}

View File

@@ -9,5 +9,6 @@
#include <Windows.h>
#include <Shlwapi.h>
#include <ShlObj.h>
#include <Psapi.h>
#pragma comment(lib, "Shlwapi.lib")

125
src/runner/utils/io.cpp Normal file
View File

@@ -0,0 +1,125 @@
#include "io.hpp"
#include "nt.hpp"
#include <fstream>
namespace utils::io
{
bool remove_file(const std::string& file)
{
return DeleteFileA(file.data()) == TRUE;
}
bool move_file(const std::string& src, const std::string& target)
{
return MoveFileA(src.data(), target.data()) == TRUE;
}
bool file_exists(const std::string& file)
{
return std::ifstream(file).good();
}
bool write_file(const std::string& file, const std::string& data, const bool append)
{
const auto pos = file.find_last_of("/\\");
if (pos != std::string::npos)
{
create_directory(file.substr(0, pos));
}
std::ofstream stream(
file, std::ios::binary | std::ofstream::out | (append ? std::ofstream::app : 0));
if (stream.is_open())
{
stream.write(data.data(), data.size());
stream.close();
return true;
}
return false;
}
std::string read_file(const std::string& file)
{
std::string data;
read_file(file, &data);
return data;
}
bool read_file(const std::string& file, std::string* data)
{
if (!data) return false;
data->clear();
if (file_exists(file))
{
std::ifstream stream(file, std::ios::binary);
if (!stream.is_open()) return false;
stream.seekg(0, std::ios::end);
const std::streamsize size = stream.tellg();
stream.seekg(0, std::ios::beg);
if (size > -1)
{
data->resize(static_cast<uint32_t>(size));
stream.read(const_cast<char*>(data->data()), size);
stream.close();
return true;
}
}
return false;
}
size_t file_size(const std::string& file)
{
if (file_exists(file))
{
std::ifstream stream(file, std::ios::binary);
if (stream.good())
{
stream.seekg(0, std::ios::end);
return static_cast<size_t>(stream.tellg());
}
}
return 0;
}
bool create_directory(const std::string& directory)
{
return std::filesystem::create_directories(directory);
}
bool directory_exists(const std::string& directory)
{
return std::filesystem::is_directory(directory);
}
bool directory_is_empty(const std::string& directory)
{
return std::filesystem::is_empty(directory);
}
std::vector<std::string> list_files(const std::string& directory)
{
std::vector<std::string> files;
for (auto& file : std::filesystem::directory_iterator(directory))
{
files.push_back(file.path().generic_string());
}
return files;
}
void copy_folder(const std::filesystem::path& src, const std::filesystem::path& target)
{
std::filesystem::copy(src, target,
std::filesystem::copy_options::overwrite_existing |
std::filesystem::copy_options::recursive);
}
}

21
src/runner/utils/io.hpp Normal file
View File

@@ -0,0 +1,21 @@
#pragma once
#include <string>
#include <vector>
#include <filesystem>
namespace utils::io
{
bool remove_file(const std::string& file);
bool move_file(const std::string& src, const std::string& target);
bool file_exists(const std::string& file);
bool write_file(const std::string& file, const std::string& data, bool append = false);
bool read_file(const std::string& file, std::string* data);
std::string read_file(const std::string& file);
size_t file_size(const std::string& file);
bool create_directory(const std::string& directory);
bool directory_exists(const std::string& directory);
bool directory_is_empty(const std::string& directory);
std::vector<std::string> list_files(const std::string& directory);
void copy_folder(const std::filesystem::path& src, const std::filesystem::path& target);
}