#include using namespace hook; namespace u { bool patch_stealth(uint32_t pid, void* target, uint8_t* bytes, size_t length) { if (target == nullptr || bytes == nullptr || pid == 0 || length <= 0) return false; if (!hvi::write_virt_memory(pid, mem::addr(target).Base(), bytes, length, true)) return false; return true; } bool patch(void* target, void* bytes, size_t length, bool cow_page) { auto process = win_api::get_current_process_id(); auto prot = ulong{}; auto backup = uint8_t{}; if (cow_page) { if (!hvi::protect_virt_memory( process, (uintptr_t)target, page_size, PAGE_EXECUTE_READWRITE, &prot )) return false; *(uint8_t*)((uintptr_t)target + 0x8) = 0x1; win_api::rebuilt::sleep(1000); *(uint8_t*)((uintptr_t)target + 0x8) = 0x0; } if (!patch_stealth( process, target, (uint8_t*)bytes, length )) return false; return true; } } bool detour::protect_virtual_memory(void* pAddress, size_t uLength, ulong uProt, ulong* uOldProt) { ulong uDummy{}; ulong* puProt = (uOldProt != nullptr) ? uOldProt : &uDummy; return hvi::protect_virt_memory( win_api::get_current_process_id(), (uintptr_t)pAddress, uLength, uProt, puProt ); } bool detour::copy_virtual_memory(void* pDst, void* pSrc, size_t uLength, bool bStealth) { return u::patch(pDst, (uint8_t*)pSrc, uLength, true); } bool detour::place(void* pTarget, void* pHandler, void** pOriginal, size_t* uBackupLength, void* pRealTarget, u32* pdwErrorCode, bool bUseSafeRegister, bool bSmallFunction ) { const bool is_hooking_shadow_page = !(pRealTarget == nullptr); const auto handler = reinterpret_cast(pHandler); auto redirect_shell = instr::jump_t(handler); auto backup_length = size_t{}; // determine if the target is eligible for hooking with this particular method and // also how many bytes we are required to back-up in order to allow smooth transition. constexpr size_t snippet_length = 128; auto* const snippet_buffer = stl::mem_alloc(snippet_length); if (snippet_buffer == nullptr) { if (pdwErrorCode) *pdwErrorCode = 0xE01; return false; } RtlSecureZeroMemory(snippet_buffer, snippet_length); // copy a "snippet/piece" of the target function inside a local buffer and start processing it. // usually, we'll come to our required instruction size by the first 128 bytes, if not, then // we'll refuse to work. TODO: maybe change this logic to allow reading for more? if (!copy_virtual_memory(snippet_buffer, pTarget, snippet_length)) { if (pdwErrorCode) *pdwErrorCode = 0xE02; return false; } uint32_t disasm_sc = 0; if (!hde::is_function_hookable(snippet_buffer, snippet_length, &backup_length, sizeof(redirect_shell), disasm_sc)) { if (pdwErrorCode) *pdwErrorCode = 0xE03; return false; } if (uBackupLength) *uBackupLength = backup_length; // that is only required for determining if it's hookable and some other things. // but after the function has returned, we generally don't need it anymore. stl::mem_free(snippet_buffer); { const size_t original_length = ((sizeof(redirect_shell) * 2) + (2 * sizeof(void*))); auto* const original_ptr = stl::mem_alloc(original_length); if (original_ptr == nullptr) { if (pdwErrorCode) *pdwErrorCode = 0xE04; return false; } if (!protect_virtual_memory(original_ptr, original_length, PAGE_EXECUTE_READWRITE)) return false; void* trampoline_escape_target = (is_hooking_shadow_page ? pRealTarget : pTarget); const auto trampoline_escape_dst = (reinterpret_cast(trampoline_escape_target) + backup_length); auto trampoline_shell = instr::jump_safe_t(trampoline_escape_dst); // zero the original buffer. RtlZeroMemory(original_ptr, original_length); if (!bSmallFunction) { UINT8 backup_data[256] = { 0 }; RtlCopyMemory(&backup_data[0], pTarget, backup_length); // copy backup bytes into the original buffer. RtlCopyMemory(original_ptr, backup_data, backup_length); } // append the jump away from our trampoline at the end of here. auto* const trampoline_dst_ptr = reinterpret_cast( reinterpret_cast(original_ptr) + backup_length); RtlCopyMemory(trampoline_dst_ptr, &trampoline_shell, sizeof(trampoline_shell)); // pass the original to the output parameter. // NOTE: make sure to free this memory this when un-hooking. if (pOriginal && !(*pOriginal)) *pOriginal = original_ptr; } if (!copy_virtual_memory(pTarget, &redirect_shell, sizeof(redirect_shell), true)) { if (pdwErrorCode) *pdwErrorCode = 0xE05; return false; } return true; } bool detour::restore(void* pTarget, void* pOriginal, size_t uBackupLength) { const auto uOriginalLength = uBackupLength; if (!copy_virtual_memory(pTarget, pOriginal, static_cast(uOriginalLength))) return false; stl::mem_free(pOriginal); return true; }