2023-02-27 12:32:38 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
2025-06-19 11:58:07 +02:00
|
|
|
#include <cstddef>
|
2025-11-16 12:01:48 +00:00
|
|
|
#include <unordered_set>
|
2025-01-23 21:55:41 +01:00
|
|
|
#include "../helpers/memory/Memory.hpp"
|
2023-02-27 12:32:38 +00:00
|
|
|
|
2024-04-04 18:50:37 +01:00
|
|
|
#define HANDLE void*
|
2025-10-07 12:37:21 +01:00
|
|
|
#define HOOK_TRAMPOLINE_MAX_SIZE 32
|
2023-02-27 12:32:38 +00:00
|
|
|
|
|
|
|
|
class CFunctionHook {
|
|
|
|
|
public:
|
|
|
|
|
CFunctionHook(HANDLE owner, void* source, void* destination);
|
|
|
|
|
~CFunctionHook();
|
|
|
|
|
|
|
|
|
|
bool hook();
|
|
|
|
|
bool unhook();
|
|
|
|
|
|
2023-12-06 23:54:56 +01:00
|
|
|
CFunctionHook(const CFunctionHook&) = delete;
|
|
|
|
|
CFunctionHook(CFunctionHook&&) = delete;
|
2023-02-27 12:32:38 +00:00
|
|
|
CFunctionHook& operator=(const CFunctionHook&) = delete;
|
2023-12-06 23:54:56 +01:00
|
|
|
CFunctionHook& operator=(CFunctionHook&&) = delete;
|
2023-02-27 12:32:38 +00:00
|
|
|
|
2025-05-03 16:06:24 +02:00
|
|
|
void* m_original = nullptr;
|
2023-02-27 12:32:38 +00:00
|
|
|
|
|
|
|
|
private:
|
2025-10-07 12:37:21 +01:00
|
|
|
void* m_source = nullptr;
|
|
|
|
|
void* m_launchTrampolineAddr = nullptr;
|
|
|
|
|
void* m_landTrampolineAddr = nullptr;
|
|
|
|
|
void* m_destination = nullptr;
|
|
|
|
|
size_t m_hookLen = 0;
|
|
|
|
|
HANDLE m_owner = nullptr;
|
|
|
|
|
bool m_active = false;
|
2025-06-19 11:58:07 +02:00
|
|
|
|
|
|
|
|
std::vector<unsigned char> m_originalBytes;
|
2023-02-27 18:34:44 +00:00
|
|
|
|
2023-12-27 11:43:04 +01:00
|
|
|
struct SInstructionProbe {
|
|
|
|
|
size_t len = 0;
|
|
|
|
|
std::string assembly = "";
|
|
|
|
|
std::vector<size_t> insSizes;
|
|
|
|
|
};
|
2023-02-27 19:17:58 +00:00
|
|
|
|
2023-12-27 11:43:04 +01:00
|
|
|
struct SAssembly {
|
|
|
|
|
std::vector<char> bytes;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
SInstructionProbe probeMinimumJumpSize(void* start, size_t min);
|
|
|
|
|
SInstructionProbe getInstructionLenAt(void* start);
|
|
|
|
|
|
|
|
|
|
SAssembly fixInstructionProbeRIPCalls(const SInstructionProbe& probe);
|
2023-02-27 12:32:38 +00:00
|
|
|
|
|
|
|
|
friend class CHookSystem;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class CHookSystem {
|
|
|
|
|
public:
|
|
|
|
|
CFunctionHook* initHook(HANDLE handle, void* source, void* destination);
|
|
|
|
|
bool removeHook(CFunctionHook* hook);
|
|
|
|
|
|
|
|
|
|
void removeAllHooksFrom(HANDLE handle);
|
|
|
|
|
|
|
|
|
|
private:
|
2025-05-03 16:06:24 +02:00
|
|
|
std::vector<UP<CFunctionHook>> m_hooks;
|
2024-04-04 18:50:37 +01:00
|
|
|
|
2025-01-23 21:55:41 +01:00
|
|
|
uint64_t getAddressForTrampo();
|
2024-04-04 18:50:37 +01:00
|
|
|
|
|
|
|
|
struct SAllocatedPage {
|
|
|
|
|
uint64_t addr = 0;
|
|
|
|
|
uint64_t len = 0;
|
|
|
|
|
uint64_t used = 0;
|
|
|
|
|
};
|
|
|
|
|
|
2025-11-16 12:01:48 +00:00
|
|
|
std::vector<SAllocatedPage> m_pages;
|
|
|
|
|
std::unordered_set<uint64_t> m_activeHooks;
|
2024-04-04 18:50:37 +01:00
|
|
|
|
|
|
|
|
friend class CFunctionHook;
|
2023-02-27 12:32:38 +00:00
|
|
|
};
|
|
|
|
|
|
2025-01-23 21:55:41 +01:00
|
|
|
inline UP<CHookSystem> g_pFunctionHookSystem;
|