Skip to content

Commit

Permalink
add minhook
Browse files Browse the repository at this point in the history
  • Loading branch information
ThirteenAG committed Jul 1, 2024
1 parent 9da9f94 commit 1391267
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
[submodule "kananlib"]
path = kananlib
url = https://github.com/cursey/kananlib
[submodule "minhook"]
path = minhook
url = https://github.com/TsudaKageyu/minhook
1 change: 1 addition & 0 deletions minhook
Submodule minhook added at 91cc94
64 changes: 64 additions & 0 deletions utility/FunctionHookMinHook.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <MinHook.h>

#include "FunctionHookMinHook.hpp"

using namespace std;

bool g_isMinHookInitialized{ false };

FunctionHookMinHook::FunctionHookMinHook(uintptr_t target, uintptr_t destination)
: m_target{ 0 },
m_destination{ 0 },
m_original{ 0 }
{
// Initialize MinHook if it hasn't been already.
if (!g_isMinHookInitialized && MH_Initialize() == MH_OK) {
g_isMinHookInitialized = true;
}

// Create the hook. Call create afterwards to prevent race conditions accessing FunctionHookMinHook before it leaves its constructor.
if (auto status = MH_CreateHook(reinterpret_cast<void*>(target), reinterpret_cast<void*>(destination), (LPVOID*)&m_original); status == MH_OK) {
m_target = target;
m_destination = destination;
}
}

FunctionHookMinHook::~FunctionHookMinHook() {
remove();
}

bool FunctionHookMinHook::create() {
if (m_target == 0 || m_destination == 0 || m_original == 0) {
return false;
}

if (auto status = MH_EnableHook((LPVOID)m_target); status != MH_OK) {
m_original = 0;
m_destination = 0;
m_target = 0;

return false;
}

return true;
}

bool FunctionHookMinHook::remove() {
// Don't try to remove invalid hooks.
if (m_original == 0) {
return true;
}

// Disable then remove the hook.
if (MH_DisableHook((LPVOID)m_target) != MH_OK ||
MH_RemoveHook((LPVOID)m_target) != MH_OK) {
return false;
}

// Invalidate the members.
m_target = 0;
m_destination = 0;
m_original = 0;

return true;
}
40 changes: 40 additions & 0 deletions utility/FunctionHookMinHook.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once

#include <windows.h>
#include <cstdint>

class FunctionHookMinHook {
public:
FunctionHookMinHook() = delete;
FunctionHookMinHook(const FunctionHookMinHook& other) = delete;
FunctionHookMinHook(FunctionHookMinHook&& other) = delete;
FunctionHookMinHook(uintptr_t target, uintptr_t destination);
virtual ~FunctionHookMinHook();

bool create();

// Called automatically by the destructor, but you can call it explicitly
// if you need to remove the hook.
bool remove();

auto get_original() const {
return m_original;
}

template <typename T>
T* get_original() const {
return (T*)m_original;
}

auto is_valid() const {
return m_original != 0;
}

FunctionHookMinHook& operator=(const FunctionHookMinHook& other) = delete;
FunctionHookMinHook& operator=(FunctionHookMinHook&& other) = delete;

private:
uintptr_t m_target{ 0 };
uintptr_t m_destination{ 0 };
uintptr_t m_original{ 0 };
};
21 changes: 21 additions & 0 deletions utility/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 praydog

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

0 comments on commit 1391267

Please sign in to comment.