3 #include "HookHandlers.h"
4 #include "../Process/Process.h"
9 template<
typename Fn,
class C = NoClass>
37 bool Hook( type ptr, hktype hkPtr, HookType::e type,
38 CallOrder::e order = CallOrder::HookFirst,
39 ReturnMethod::e retType = ReturnMethod::UseOriginal )
46 this->_retType = retType;
47 this->_callOriginal = this->_original = ptr;
48 this->_callback = hkPtr;
52 case HookType::Inline:
76 bool Hook( type Ptr, hktypeC hkPtr, C* pClass, HookType::e type,
77 CallOrder::e order = CallOrder::HookFirst,
78 ReturnMethod::e retType = ReturnMethod::UseOriginal )
80 this->_callbackClass = pClass;
81 return Hook( Ptr, brutal_cast<hktype>(hkPtr), type, order, retType );
96 case HookType::Inline:
97 case HookType::InternalInline:
99 WriteProcessMemory( GetCurrentProcess(), this->_original, this->_origCode, this->_origSize, NULL );
105 thisProc.
Attach( GetCurrentProcessId() );
107 for (
auto& thd : thisProc.threads().
getAll())
108 thd.RemoveHWBP( reinterpret_cast<ptr_t>(this->_original) );
110 this->_hwbpIdx.clear();
118 this->_hooked =
false;
130 AsmJitHelper jmpToHook, jmpToThunk;
136 jmpToThunk->mov( asmjit::host::rax, (uint64_t)this->_buf );
137 jmpToThunk->jmp( asmjit::host::rax );
139 this->_origSize = jmpToThunk->getCodeSize( );
141 jmpToThunk->jmp( _buf );
142 this->_origSize = jmpToThunk->getCodeSize();
150 jmpToHook->mov( asmjit::host::rax, (uint64_t)
this );
151 jmpToHook->mov( asmjit::host::qword_ptr_abs( 0x28 ).setSegment( asmjit::host::gs ), asmjit::host::rax );
154 jmpToHook->mov( asmjit::host::dword_ptr_abs( 0x14 ).setSegment( asmjit::host::fs ) , (uint32_t)
this );
158 jmpToHook->relocCode( this->_buf );
160 BOOL res = WriteProcessMemory( GetCurrentProcess(), this->_original, this->_newCode,
161 jmpToThunk->relocCode( this->_newCode, (uintptr_t)this->_original ), NULL );
163 return (this->_hooked = (res == TRUE));
172 this->_newCode[0] = 0xCC;
173 this->_origSize =
sizeof( this->_newCode[0] );
176 if (this->_vecHandler ==
nullptr)
179 if (!this->_vecHandler)
182 this->_breakpoints.insert( std::make_pair( this->_original, (DetourBase*)
this ) );
185 memcpy( this->_origCode, this->_original, this->_origSize );
188 BOOL res = WriteProcessMemory( GetCurrentProcess(), this->_original, this->_newCode, this->_origSize, NULL );
190 return (this->_hooked = (res == TRUE));
200 thisProc.Attach( GetCurrentProcessId() );
203 if (this->_vecHandler ==
nullptr)
206 if (!this->_vecHandler)
209 this->_breakpoints.insert( std::make_pair( this->_original, (DetourBase*)
this ) );
212 for (
auto& thd : thisProc.threads().getAll())
213 this->_hwbpIdx[thd.id()] = thd.AddHWBP( reinterpret_cast<ptr_t>(this->_original), hwbp_execute, hwbp_1 );
215 return this->_hooked =
true;
bool Hook(type ptr, hktype hkPtr, HookType::e type, CallOrder::e order=CallOrder::HookFirst, ReturnMethod::e retType=ReturnMethod::UseOriginal)
Hook function
Definition: LocalHook.hpp:37
BLACKBONE_API std::vector< Thread > & getAll(bool dontUpdate=false)
Gets all process threads
Definition: Threads.cpp:39
bool Restore()
Restore hooked function
Definition: LocalHook.hpp:89
bool Hook(type Ptr, hktypeC hkPtr, C *pClass, HookType::e type, CallOrder::e order=CallOrder::HookFirst, ReturnMethod::e retType=ReturnMethod::UseOriginal)
Hook function
Definition: LocalHook.hpp:76
Definition: LocalHook.hpp:10
Definition: HookHandlers.h:12
static BLACKBONE_API LONG NTAPI VectoredHandler(PEXCEPTION_POINTERS excpt)
Exception handlers
Definition: LocalHookBase.cpp:137
BLACKBONE_API void CopyOldCode(uint8_t *Ptr)
Copy original function bytes
Definition: LocalHookBase.cpp:74
BLACKBONE_API NTSTATUS Attach(DWORD pid, DWORD access=DEFAULT_ACCESS_P)
Attach to existing process
Definition: Process.cpp:36
Definition: AsmHelper32.cpp:6