diff --git a/cpp/LunaHook/LunaHook/hookfinder.cc b/cpp/LunaHook/LunaHook/hookfinder.cc index caaf1fca..62ee964e 100644 --- a/cpp/LunaHook/LunaHook/hookfinder.cc +++ b/cpp/LunaHook/LunaHook/hookfinder.cc @@ -243,9 +243,8 @@ bool SendJitVeh(PCONTEXT context, uintptr_t address, uint64_t em_addr, JITTYPE j if (tm - addresscalledtime[address] < 100) return false; addresscalledtime[address] = tm; - auto stack = std::make_unique(); - context_get(stack.get(), context); - SafeSendJitVeh(stack.get(), address, em_addr, jittype, padding); + hook_stack stack = hook_stack::fromContext(context); + SafeSendJitVeh(&stack, address, em_addr, jittype, padding); return true; } std::vector GetFunctions(uintptr_t module) diff --git a/cpp/LunaHook/LunaHook/main.cc b/cpp/LunaHook/LunaHook/main.cc index 8f869a4f..5849cb99 100644 --- a/cpp/LunaHook/LunaHook/main.cc +++ b/cpp/LunaHook/LunaHook/main.cc @@ -333,71 +333,4 @@ std::string LoadResData(LPCWSTR pszResID, LPCWSTR _type) GlobalFree(m_hMem); FreeResource(lpRsrc); return data; -} - -void context_get(hook_stack *stack, PCONTEXT context) -{ -#ifndef _WIN64 - stack->eax = context->Eax; - stack->ecx = context->Ecx; - stack->edx = context->Edx; - stack->ebx = context->Ebx; - stack->esp = context->Esp; - stack->ebp = context->Ebp; - stack->esi = context->Esi; - stack->edi = context->Edi; - stack->eflags = context->EFlags; - stack->retaddr = *(DWORD *)context->Esp; -#else - stack->rax = context->Rax; - stack->rbx = context->Rbx; - stack->rcx = context->Rcx; - stack->rdx = context->Rdx; - stack->rsp = context->Rsp; - stack->rbp = context->Rbp; - stack->rsi = context->Rsi; - stack->rdi = context->Rdi; - stack->r8 = context->R8; - stack->r9 = context->R9; - stack->r10 = context->R10; - stack->r11 = context->R11; - stack->r12 = context->R12; - stack->r13 = context->R13; - stack->r14 = context->R14; - stack->r15 = context->R15; - stack->eflags = context->EFlags; - stack->retaddr = *(DWORD64 *)context->Rsp; -#endif -} -void context_set(hook_stack *stack, PCONTEXT context) -{ -#ifndef _WIN64 - context->Eax = stack->eax; - context->Ecx = stack->ecx; - context->Edx = stack->edx; - context->Ebx = stack->ebx; - context->Esp = stack->esp; - context->Ebp = stack->ebp; - context->Esi = stack->esi; - context->Edi = stack->edi; - context->EFlags = stack->eflags; -#else - context->Rax = stack->rax; - context->Rbx = stack->rbx; - context->Rcx = stack->rcx; - context->Rdx = stack->rdx; - context->Rsp = stack->rsp; - context->Rbp = stack->rbp; - context->Rsi = stack->rsi; - context->Rdi = stack->rdi; - context->R8 = stack->r8; - context->R9 = stack->r9; - context->R10 = stack->r10; - context->R11 = stack->r11; - context->R12 = stack->r12; - context->R13 = stack->r13; - context->R14 = stack->r14; - context->R15 = stack->r15; - context->EFlags = stack->eflags; -#endif -} +} \ No newline at end of file diff --git a/cpp/LunaHook/LunaHook/main.h b/cpp/LunaHook/LunaHook/main.h index 5455aceb..5caa2304 100644 --- a/cpp/LunaHook/LunaHook/main.h +++ b/cpp/LunaHook/LunaHook/main.h @@ -24,9 +24,6 @@ extern std::unordered_map> jitaddr2emuad void jitaddraddr(uint64_t em_addr, uintptr_t jitaddr, JITTYPE); void jitaddrclear(); -void context_get(hook_stack *, PCONTEXT); -void context_set(hook_stack *, PCONTEXT); - void delayinsertadd(HookParam, std::string); void delayinsertNewHook(uint64_t); inline bool safeautoleaveveh = false; diff --git a/cpp/LunaHook/LunaHook/stackoffset.hpp b/cpp/LunaHook/LunaHook/stackoffset.hpp index 3c9e7a90..185696b0 100644 --- a/cpp/LunaHook/LunaHook/stackoffset.hpp +++ b/cpp/LunaHook/LunaHook/stackoffset.hpp @@ -159,8 +159,8 @@ inline uintptr_t *argidx(hook_stack *stack, int idx) default: offset = get_stack(idx); } - return (uintptr_t *)(stack->get_base() + offset); + return (uintptr_t *)(stack->base + offset); #else - return (uintptr_t *)(stack->get_base() + get_stack(idx)); + return (uintptr_t *)(stack->base + get_stack(idx)); #endif } \ No newline at end of file diff --git a/cpp/LunaHook/LunaHook/texthook.cc b/cpp/LunaHook/LunaHook/texthook.cc index 4be4c6ed..e72da1c0 100644 --- a/cpp/LunaHook/LunaHook/texthook.cc +++ b/cpp/LunaHook/LunaHook/texthook.cc @@ -245,13 +245,16 @@ void commonfilter(TextBuffer *buffer, HookParam *hp) } } void TextHook::Send(uintptr_t lpDataBase) +{ + Send(hook_stack::fromBase(lpDataBase)); +} +void TextHook::Send(hook_stack *stack) { auto buffer = (TextOutput_T *)local_buffer; TextBuffer buff{buffer->data, 0}; _InterlockedIncrement((long *)&useCount); __try { - auto stack = get_hook_stack(lpDataBase); if (auto current_trigger_fun = trigger_fun.exchange(nullptr)) if (!current_trigger_fun(location, stack)) @@ -280,7 +283,7 @@ void TextHook::Send(uintptr_t lpDataBase) uintptr_t lpSplit = 0, lpRetn = stack->retaddr, - plpdatain = (lpDataBase + hp.offset), + plpdatain = (uintptr_t)(stack->base + hp.offset), lpDataIn = *(uintptr_t *)plpdatain; if (hp.jittype != JITTYPE::PC && hp.jittype != JITTYPE::UNITY) @@ -312,7 +315,7 @@ void TextHook::Send(uintptr_t lpDataBase) if (hp.jittype != JITTYPE::PC && hp.jittype != JITTYPE::UNITY) lpSplit = jitgetaddr(stack, &hp, false); else - lpSplit = *(uintptr_t *)(lpDataBase + hp.split); + lpSplit = *(uintptr_t *)(stack->base + hp.split); if (hp.type & SPLIT_INDIRECT) lpSplit = *(uintptr_t *)(lpSplit + hp.split_index); } @@ -444,11 +447,9 @@ void TextHook::Send(uintptr_t lpDataBase) } bool TextHook::breakpointcontext(PCONTEXT context) { - auto stack = std::make_unique(); - context_get(stack.get(), context); - auto lpDataBase = stack->get_base(); - Send(lpDataBase); - context_set(stack.get(), context); + hook_stack stack = hook_stack::fromContext(context); + Send(&stack); + stack.toContext(context); return true; } bool TextHook::InsertBreakPoint() diff --git a/cpp/LunaHook/LunaHook/texthook.h b/cpp/LunaHook/LunaHook/texthook.h index 353dc9e1..2eef3b00 100644 --- a/cpp/LunaHook/LunaHook/texthook.h +++ b/cpp/LunaHook/LunaHook/texthook.h @@ -18,7 +18,8 @@ private: bool InsertBreakPoint(); bool RemoveBreakPoint(); bool breakpointcontext(PCONTEXT); - void Send(uintptr_t dwDatabase); + void Send(uintptr_t); + void Send(hook_stack*); int GetLength(hook_stack *stack, uintptr_t in); // jichi 12/25/2013: Return 0 if failed int HookStrlen(BYTE *data); void RemoveHookCode(); diff --git a/cpp/LunaHook/include/types.h b/cpp/LunaHook/include/types.h index ecb80295..ef67fc46 100644 --- a/cpp/LunaHook/include/types.h +++ b/cpp/LunaHook/include/types.h @@ -65,16 +65,79 @@ struct hook_stack uintptr_t retaddr; BYTE base[1]; }; - uintptr_t get_base() + void toContext(PCONTEXT context) { - return (uintptr_t)this + sizeof(hook_stack) - sizeof(uintptr_t); +#ifndef _WIN64 + context->Eax = eax; + context->Ecx = ecx; + context->Edx = edx; + context->Ebx = ebx; + context->Esp = esp; + context->Ebp = ebp; + context->Esi = esi; + context->Edi = edi; + context->EFlags = eflags; +#else + context->Rax = rax; + context->Rbx = rbx; + context->Rcx = rcx; + context->Rdx = rdx; + context->Rsp = rsp; + context->Rbp = rbp; + context->Rsi = rsi; + context->Rdi = rdi; + context->R8 = r8; + context->R9 = r9; + context->R10 = r10; + context->R11 = r11; + context->R12 = r12; + context->R13 = r13; + context->R14 = r14; + context->R15 = r15; + context->EFlags = eflags; +#endif + } + static hook_stack fromContext(PCONTEXT context) + { + hook_stack stack; +#ifndef _WIN64 + stack.eax = context->Eax; + stack.ecx = context->Ecx; + stack.edx = context->Edx; + stack.ebx = context->Ebx; + stack.esp = context->Esp; + stack.ebp = context->Ebp; + stack.esi = context->Esi; + stack.edi = context->Edi; + stack.eflags = context->EFlags; + stack.retaddr = *(DWORD *)context->Esp; +#else + stack.rax = context->Rax; + stack.rbx = context->Rbx; + stack.rcx = context->Rcx; + stack.rdx = context->Rdx; + stack.rsp = context->Rsp; + stack.rbp = context->Rbp; + stack.rsi = context->Rsi; + stack.rdi = context->Rdi; + stack.r8 = context->R8; + stack.r9 = context->R9; + stack.r10 = context->R10; + stack.r11 = context->R11; + stack.r12 = context->R12; + stack.r13 = context->R13; + stack.r14 = context->R14; + stack.r15 = context->R15; + stack.eflags = context->EFlags; + stack.retaddr = *(DWORD64 *)context->Rsp; +#endif + return stack; + } + static hook_stack *fromBase(uintptr_t lpDataBase) + { + return (hook_stack *)(lpDataBase - (uintptr_t)((hook_stack *)0)->base); } }; - -inline hook_stack *get_hook_stack(uintptr_t lpDataBase) -{ - return (hook_stack *)(lpDataBase - sizeof(hook_stack) + sizeof(uintptr_t)); -} // jichi 3/7/2014: Add guessed comment #define ALIGNPTR(Y, X) \ @@ -236,14 +299,14 @@ struct TextBuffer if (!c) return; size = strlenEx(c) * sizeof(CharT); - if(size) + if (size) strncpyEx((CharT *)buff, c, TEXT_BUFFER_SIZE); } template >> void from(const StringT &c) { size = min(TEXT_BUFFER_SIZE, strSize(c)); - if(size) + if (size) memcpy(buff, c.data(), size); } template @@ -252,7 +315,7 @@ struct TextBuffer if (!ptr || !t) return; size = min(TEXT_BUFFER_SIZE, t); - if(size) + if (size) memcpy(buff, (void *)ptr, size); } template diff --git a/cpp/version.cmake b/cpp/version.cmake index 4283d0ea..21dc269c 100644 --- a/cpp/version.cmake +++ b/cpp/version.cmake @@ -1,7 +1,7 @@ set(VERSION_MAJOR 6) set(VERSION_MINOR 10) -set(VERSION_PATCH 3) +set(VERSION_PATCH 4) set(VERSION_REVISION 0) set(LUNA_VERSION "{${VERSION_MAJOR},${VERSION_MINOR},${VERSION_PATCH},${VERSION_REVISION}}") add_library(VERSION_DEF ${CMAKE_CURRENT_LIST_DIR}/version_def.cpp)