diff --git a/linux/hook.cpp b/linux/hook.cpp index d25e447..bd36a75 100644 --- a/linux/hook.cpp +++ b/linux/hook.cpp @@ -28,6 +28,21 @@ bool get_dottext_info(uintptr_t& start, uintptr_t& end) return false; } +void write_jmp(const uintptr_t from, const uintptr_t to) +{ + uint8_t shellcode[] = + { + 0xFF, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp [rip+0x06] + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // ? + }; + + *reinterpret_cast(&shellcode[6]) = to; + + mprotect(reinterpret_cast(from), sizeof(shellcode), PROT_READ|PROT_WRITE|PROT_EXEC); + memcpy(reinterpret_cast(from), shellcode, sizeof(shellcode)); + mprotect(reinterpret_cast(from), sizeof(shellcode), PROT_READ|PROT_EXEC); +} + uintptr_t sig_scan(const uintptr_t start, const uintptr_t end, std::string_view pattern) { constexpr const uint16_t WILDCARD = 0xFFFF; @@ -116,7 +131,5 @@ void hook() *reinterpret_cast(&shellcode[6]) = &hook_is_feature_available; - mprotect(reinterpret_cast(_is_feature_available), sizeof(shellcode), PROT_READ|PROT_WRITE|PROT_EXEC); - memcpy(reinterpret_cast(_is_feature_available), shellcode, sizeof(shellcode)); - mprotect(reinterpret_cast(_is_feature_available), sizeof(shellcode), PROT_READ|PROT_EXEC); + write_jmp(_is_feature_available, reinterpret_cast(&hook_is_feature_available)); } diff --git a/linux/hook.hpp b/linux/hook.hpp index edc7da0..8ac6c81 100644 --- a/linux/hook.hpp +++ b/linux/hook.hpp @@ -4,6 +4,7 @@ #include bool get_dottext_info(uintptr_t& start, uintptr_t& end); +void write_jmp(const uintptr_t from, const uintptr_t to); uintptr_t sig_scan(const uintptr_t start, const uintptr_t end, std::string_view pattern); uint64_t hook_is_feature_available(uintptr_t user, const char* feature); void hook(); diff --git a/windows/hook.cpp b/windows/hook.cpp index 7d983da..6c5c42c 100644 --- a/windows/hook.cpp +++ b/windows/hook.cpp @@ -4,7 +4,6 @@ #define NOMINMAX #include -#include #include #include #include @@ -38,6 +37,22 @@ bool get_section_info(std::string_view name, uintptr_t& start, uintptr_t& end) return false; } +void write_jmp(const uintptr_t from, const uintptr_t to) +{ + uint8_t shellcode[] = + { + 0xFF, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp [rip+0x06] + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // ? + }; + + *reinterpret_cast(&shellcode[6]) = to; + + DWORD old_prot; + VirtualProtect(reinterpret_cast(from), sizeof(shellcode), PAGE_EXECUTE_READWRITE, &old_prot); + memcpy(reinterpret_cast(from), shellcode, sizeof(shellcode)); + VirtualProtect(reinterpret_cast(from), sizeof(shellcode), old_prot, &old_prot); +} + uintptr_t sig_scan(const uintptr_t start, const uintptr_t end, std::string_view pattern) { constexpr const uint16_t WILDCARD = 0xFFFF; @@ -77,7 +92,7 @@ uintptr_t sig_scan(const uintptr_t start, const uintptr_t end, std::string_view break; } - end_address = std::min(end, reinterpret_cast(mbi.BaseAddress) + mbi.RegionSize); + end_address = std::min(end, static_cast(reinterpret_cast(mbi.BaseAddress) + mbi.RegionSize)); } const auto vec_length = pattern_vec.size(); @@ -134,18 +149,6 @@ void hook() return; } - - // Jumps to specified address - uint8_t shellcode[] = - { - 0xFF, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp [rip+0x06] - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // ? - }; - *reinterpret_cast(&shellcode[6]) = &hook_is_feature_available; - - DWORD old_prot; - VirtualProtect(reinterpret_cast(_is_feature_available), sizeof(shellcode), PAGE_EXECUTE_READWRITE, &old_prot); - memcpy(reinterpret_cast(_is_feature_available), shellcode, sizeof(shellcode)); - VirtualProtect(reinterpret_cast(_is_feature_available), sizeof(shellcode), old_prot, &old_prot); + write_jmp(_is_feature_available, reinterpret_cast(&hook_is_feature_available)); } diff --git a/windows/hook.hpp b/windows/hook.hpp index 8ebecbc..b6c4498 100644 --- a/windows/hook.hpp +++ b/windows/hook.hpp @@ -4,6 +4,7 @@ #include bool get_section_info(std::string_view name, uintptr_t& start, uintptr_t& end); +void write_jmp(const uintptr_t from, const uintptr_t to); uintptr_t sig_scan(const uintptr_t start, const uintptr_t end, std::string_view pattern); uint64_t hook_is_feature_available(uintptr_t user, const char* feature); void hook();