3 #include "HookHandlers.h"
4 #include "../Process/Process.h"
9 template<
typename Fn,
class C = NoClass>
41 CallOrder::e order = CallOrder::HookFirst,
42 ReturnMethod::e retType = ReturnMethod::UseOriginal
50 this->_retType = retType;
51 this->_callOriginal = this->_original = ptr;
52 this->_callback = hkPtr;
54 if (!AllocateBuffer( reinterpret_cast<uint8_t*>(ptr) ))
59 case HookType::Inline:
88 CallOrder::e order = CallOrder::HookFirst,
89 ReturnMethod::e retType = ReturnMethod::UseOriginal
92 this->_callbackClass = pClass;
93 return Hook( Ptr, brutal_cast<hktype>(hkPtr), type, order, retType );
108 case HookType::Inline:
109 case HookType::InternalInline:
111 WriteProcessMemory( GetCurrentProcess(), this->_original, this->_origCode, this->_origSize, NULL );
117 thisProc.
Attach( GetCurrentProcessId() );
119 for (
auto& thd : thisProc.threads().
getAll())
120 thd.RemoveHWBP( reinterpret_cast<ptr_t>(this->_original) );
122 this->_hwbpIdx.clear();
130 this->_hooked =
false;
142 AsmJitHelper jmpToHook, jmpToThunk;
148 jmpToThunk->mov( asmjit::host::rax, (uint64_t)this->_buf );
149 jmpToThunk->jmp( asmjit::host::rax );
151 this->_origSize = jmpToThunk->getCodeSize( );
153 jmpToThunk->jmp( (asmjit::Ptr)this->_buf );
154 this->_origSize = jmpToThunk->getCodeSize();
162 jmpToHook->mov( asmjit::host::rax, (uint64_t)
this );
163 jmpToHook->mov( asmjit::host::qword_ptr_abs( 0x28 ).setSegment( asmjit::host::gs ), asmjit::host::rax );
166 jmpToHook->mov( asmjit::host::dword_ptr_abs( 0x14 ).setSegment( asmjit::host::fs ) , (uint32_t)
this );
170 jmpToHook->relocCode( this->_buf );
172 jmpToThunk->setBaseAddress( (uintptr_t)this->_original );
173 auto codeSize = jmpToThunk->relocCode( this->_newCode );
176 if (!VirtualProtect( this->_original, codeSize, PAGE_EXECUTE_READWRITE, &flOld ))
179 memcpy( this->_original, this->_newCode, codeSize );
181 VirtualProtect( this->_original, codeSize, flOld, &flOld );
183 return (this->_hooked = codeSize != 0);
192 this->_newCode[0] = 0xCC;
193 this->_origSize =
sizeof( this->_newCode[0] );
196 if (this->_vecHandler ==
nullptr)
199 if (!this->_vecHandler)
202 this->_breakpoints.insert( std::make_pair( this->_original, (DetourBase*)
this ) );
205 memcpy( this->_origCode, this->_original, this->_origSize );
209 VirtualProtect( this->_original, this->_origSize, PAGE_EXECUTE_READWRITE, &flOld );
210 memcpy( this->_original, this->_newCode, this->_origSize );
211 VirtualProtect( this->_original, this->_origSize, flOld, &flOld );
213 WriteProcessMemory( GetCurrentProcess(), this->_original, this->_newCode, this->_origSize, NULL );
215 return this->_hooked = TRUE;
225 thisProc.Attach( GetCurrentProcessId() );
228 if (this->_vecHandler ==
nullptr)
231 if (!this->_vecHandler)
234 this->_breakpoints.insert( std::make_pair( this->_original, (DetourBase*)
this ) );
237 for (
auto& thd : thisProc.threads().getAll())
238 this->_hwbpIdx[thd.id()] = thd.AddHWBP( reinterpret_cast<ptr_t>(this->_original), hwbp_execute, hwbp_1 );
240 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:49
bool Restore()
Restore hooked function
Definition: LocalHook.hpp:101
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:83
Definition: LocalHook.hpp:10
Definition: HookHandlers.h:10
static BLACKBONE_API LONG NTAPI VectoredHandler(PEXCEPTION_POINTERS excpt)
Exception handlers
Definition: LocalHookBase.cpp:185
BLACKBONE_API void CopyOldCode(uint8_t *Ptr)
Copy original function bytes
Definition: LocalHookBase.cpp:110
BLACKBONE_API NTSTATUS Attach(DWORD pid, DWORD access=DEFAULT_ACCESS_P)
Attach to existing process
Definition: Process.cpp:39
Definition: AsmHelper32.cpp:6