124 lines
2.9 KiB
C++
Raw Normal View History

2018-12-24 19:54:44 +01:00
#pragma once
2018-12-27 17:00:46 +01:00
#include "nt.hpp"
2018-12-24 19:54:44 +01:00
#define HOOK_JUMP true
#define HOOK_CALL false
namespace utils
{
class hook final
{
public:
class signature final
{
public:
struct container final
{
const char* signature;
const char* mask;
std::function<void(char*)> callback;
};
2018-12-26 16:28:16 +01:00
signature(void* start, const size_t length) : start_(start), length_(length)
{
}
signature(const DWORD start, const size_t length) : signature(reinterpret_cast<void*>(start), length)
{
}
signature() : signature(0x400000, 0x800000)
{
}
2018-12-24 19:54:44 +01:00
void process();
void add(const container& container);
private:
void* start_;
size_t length_;
std::vector<container> signatures_;
};
2018-12-26 16:28:16 +01:00
hook() : initialized_(false), installed_(false), place_(nullptr), stub_(nullptr), original_(nullptr),
use_jump_(false), protection_(0)
{
ZeroMemory(this->buffer_, sizeof(this->buffer_));
}
2018-12-24 19:54:44 +01:00
hook(void* place, void* stub, const bool use_jump = true) : hook() { this->initialize(place, stub, use_jump); }
2018-12-26 16:28:16 +01:00
hook(void* place, void (*stub)(), const bool use_jump = true) : hook(
place, reinterpret_cast<void*>(stub), use_jump)
{
}
hook(const DWORD place, void* stub, const bool use_jump = true) : hook(
reinterpret_cast<void*>(place), stub, use_jump)
{
}
hook(const DWORD place, const DWORD stub, const bool use_jump = true) : hook(
reinterpret_cast<void*>(place), reinterpret_cast<void*>(stub), use_jump)
{
}
hook(const DWORD place, void (*stub)(), const bool use_jump = true) : hook(
reinterpret_cast<void*>(place), reinterpret_cast<void*>(stub), use_jump)
{
}
2018-12-24 19:54:44 +01:00
2018-12-25 00:32:21 +01:00
hook(const hook&) = delete;
hook(const hook&&) = delete;
2018-12-24 19:54:44 +01:00
~hook();
hook* initialize(void* place, void* stub, bool use_jump = true);
hook* initialize(DWORD place, void* stub, bool use_jump = true);
2018-12-26 16:28:16 +01:00
hook* initialize(DWORD place, void (*stub)(), bool use_jump = true); // For lambdas
2018-12-24 19:54:44 +01:00
hook* install(bool unprotect = true, bool keep_unprotected = false);
hook* uninstall(bool unprotect = true);
void* get_address() const;
2019-01-13 19:03:46 +01:00
void* get_original() const;
2018-12-24 19:54:44 +01:00
void quick();
2018-12-27 17:00:46 +01:00
static bool iat(nt::module module, const std::string& target_module, const std::string& process, void* stub);
2018-12-24 19:54:44 +01:00
static void nop(void* place, size_t length);
static void nop(DWORD place, size_t length);
2018-12-26 16:28:16 +01:00
template <typename T>
static void set(void* place, T value)
2018-12-24 19:54:44 +01:00
{
DWORD old_protect;
VirtualProtect(place, sizeof(T), PAGE_EXECUTE_READWRITE, &old_protect);
*static_cast<T*>(place) = value;
VirtualProtect(place, sizeof(T), old_protect, &old_protect);
FlushInstructionCache(GetCurrentProcess(), place, sizeof(T));
}
2018-12-26 16:28:16 +01:00
template <typename T>
static void set(const DWORD place, T value)
2018-12-24 19:54:44 +01:00
{
return set<T>(reinterpret_cast<void*>(place), value);
}
private:
bool initialized_;
bool installed_;
void* place_;
void* stub_;
void* original_;
char buffer_[5]{};
bool use_jump_;
DWORD protection_;
std::mutex state_mutex_;
};
}