2023-02-27 13:32:38 +01:00
|
|
|
#include "HookSystem.hpp"
|
|
|
|
|
|
|
|
#define register
|
|
|
|
#include <udis86.h>
|
|
|
|
#undef register
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
CFunctionHook::CFunctionHook(HANDLE owner, void* source, void* destination) {
|
|
|
|
m_pSource = source;
|
|
|
|
m_pDestination = destination;
|
|
|
|
m_pOwner = owner;
|
|
|
|
}
|
|
|
|
|
|
|
|
CFunctionHook::~CFunctionHook() {
|
2023-06-16 18:45:11 +02:00
|
|
|
if (m_bActive)
|
2023-02-27 13:32:38 +01:00
|
|
|
unhook();
|
|
|
|
}
|
|
|
|
|
2023-02-27 19:34:44 +01:00
|
|
|
size_t CFunctionHook::getInstructionLenAt(void* start) {
|
2023-02-27 13:32:38 +01:00
|
|
|
ud_t udis;
|
|
|
|
|
|
|
|
ud_init(&udis);
|
|
|
|
ud_set_mode(&udis, 64);
|
|
|
|
ud_set_syntax(&udis, UD_SYN_INTEL);
|
|
|
|
|
|
|
|
size_t curOffset = 1;
|
|
|
|
size_t insSize = 0;
|
|
|
|
while (true) {
|
|
|
|
ud_set_input_buffer(&udis, (uint8_t*)start, curOffset);
|
|
|
|
insSize = ud_disassemble(&udis);
|
|
|
|
if (insSize != curOffset)
|
|
|
|
break;
|
|
|
|
curOffset++;
|
|
|
|
}
|
|
|
|
|
2023-02-27 19:34:44 +01:00
|
|
|
// check for RIP refs
|
|
|
|
std::string ins;
|
|
|
|
if (const auto CINS = ud_insn_asm(&udis); CINS)
|
|
|
|
ins = std::string(CINS);
|
|
|
|
|
|
|
|
if (!ins.empty() && ins.find("rip") != std::string::npos) {
|
|
|
|
// todo: support something besides call qword ptr [rip + 0xdeadbeef]
|
|
|
|
// I don't have an assembler. I don't think udis provides one. Besides, variables might be tricky.
|
|
|
|
if (((uint8_t*)start)[0] == 0xFF && ((uint8_t*)start)[1] == 0x15)
|
|
|
|
m_vTrampolineRIPUses.emplace_back(std::make_pair<>((uint64_t)start - (uint64_t)m_pSource, ins));
|
2023-11-26 18:53:51 +01:00
|
|
|
else {
|
|
|
|
Debug::log(ERR, "[CFunctionHook] Cannot hook: unsupported %rip usage: {}", ins);
|
|
|
|
throw std::runtime_error("unsupported %rip usage");
|
|
|
|
}
|
2023-02-27 19:34:44 +01:00
|
|
|
}
|
|
|
|
|
2023-02-27 13:32:38 +01:00
|
|
|
return insSize;
|
|
|
|
}
|
|
|
|
|
2023-02-27 19:34:44 +01:00
|
|
|
size_t CFunctionHook::probeMinimumJumpSize(void* start, size_t min) {
|
2023-02-27 13:32:38 +01:00
|
|
|
|
|
|
|
size_t size = 0;
|
|
|
|
|
|
|
|
while (size <= min) {
|
|
|
|
// find info about this instruction
|
2023-03-04 15:02:40 +01:00
|
|
|
size_t insLen = getInstructionLenAt((uint8_t*)start + size);
|
2023-02-27 13:32:38 +01:00
|
|
|
size += insLen;
|
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CFunctionHook::hook() {
|
|
|
|
|
|
|
|
// check for unsupported platforms
|
|
|
|
#if !defined(__x86_64__)
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// movabs $0,%rax | jmpq *%rax
|
2023-02-27 19:34:44 +01:00
|
|
|
// offset for addr: 2
|
|
|
|
static constexpr uint8_t ABSOLUTE_JMP_ADDRESS[] = {0x48, 0xB8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xE0};
|
|
|
|
static constexpr size_t ABSOLUTE_JMP_ADDRESS_OFFSET = 2;
|
2023-02-27 13:32:38 +01:00
|
|
|
// pushq %rax
|
|
|
|
static constexpr uint8_t PUSH_RAX[] = {0x50};
|
|
|
|
// popq %rax
|
|
|
|
static constexpr uint8_t POP_RAX[] = {0x58};
|
|
|
|
// nop
|
|
|
|
static constexpr uint8_t NOP = 0x90;
|
2023-02-27 19:34:44 +01:00
|
|
|
/*
|
|
|
|
movabs $0,%rax
|
|
|
|
callq *%rax
|
|
|
|
|
|
|
|
offset for addr: 3
|
|
|
|
*/
|
2023-02-27 20:47:42 +01:00
|
|
|
static constexpr uint8_t CALL_WITH_RAX[] = {0x48, 0xB8, 0xEF, 0xBE, 0xAD, 0xDE, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x10};
|
|
|
|
static constexpr size_t CALL_WITH_RAX_ADDRESS_OFFSET = 2;
|
2023-02-27 13:32:38 +01:00
|
|
|
|
|
|
|
// get minimum size to overwrite
|
2023-11-26 18:53:51 +01:00
|
|
|
size_t HOOKSIZE = 0;
|
|
|
|
try {
|
|
|
|
HOOKSIZE = probeMinimumJumpSize(m_pSource, sizeof(ABSOLUTE_JMP_ADDRESS) + sizeof(PUSH_RAX) + sizeof(POP_RAX));
|
|
|
|
} catch (std::exception& e) { return false; }
|
2023-02-27 13:32:38 +01:00
|
|
|
|
|
|
|
// alloc trampoline
|
2023-02-27 19:34:44 +01:00
|
|
|
const auto TRAMPOLINE_SIZE = sizeof(ABSOLUTE_JMP_ADDRESS) + HOOKSIZE + sizeof(PUSH_RAX) + m_vTrampolineRIPUses.size() * (sizeof(CALL_WITH_RAX) - 6);
|
|
|
|
m_pTrampolineAddr = mmap(NULL, TRAMPOLINE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
2023-02-27 13:32:38 +01:00
|
|
|
|
2023-02-27 20:17:58 +01:00
|
|
|
m_pOriginalBytes = malloc(HOOKSIZE);
|
|
|
|
memcpy(m_pOriginalBytes, m_pSource, HOOKSIZE);
|
|
|
|
|
2023-02-27 13:32:38 +01:00
|
|
|
// populate trampoline
|
2023-06-16 18:45:11 +02:00
|
|
|
memcpy(m_pTrampolineAddr, m_pSource, HOOKSIZE); // first, original func bytes
|
2023-03-04 15:02:40 +01:00
|
|
|
memcpy((uint8_t*)m_pTrampolineAddr + HOOKSIZE, PUSH_RAX, sizeof(PUSH_RAX)); // then, pushq %rax
|
|
|
|
memcpy((uint8_t*)m_pTrampolineAddr + HOOKSIZE + sizeof(PUSH_RAX), ABSOLUTE_JMP_ADDRESS, sizeof(ABSOLUTE_JMP_ADDRESS)); // then, jump to source
|
2023-02-27 13:32:38 +01:00
|
|
|
|
2023-02-27 19:34:44 +01:00
|
|
|
// fix trampoline %rip calls
|
|
|
|
for (size_t i = 0; i < m_vTrampolineRIPUses.size(); ++i) {
|
|
|
|
size_t callOffset = i * (sizeof(CALL_WITH_RAX) - 6 /* callq [rip + x] */) + m_vTrampolineRIPUses[i].first;
|
2023-03-04 15:02:40 +01:00
|
|
|
size_t realCallAddress = (uint64_t)m_pSource + callOffset + 6 + *((uint32_t*)((uint8_t*)m_pSource + callOffset + 2));
|
2023-02-27 19:34:44 +01:00
|
|
|
|
2023-03-04 15:02:40 +01:00
|
|
|
memmove((uint8_t*)m_pTrampolineAddr + callOffset + sizeof(CALL_WITH_RAX), (uint8_t*)m_pTrampolineAddr + callOffset + 6, TRAMPOLINE_SIZE - callOffset - 6);
|
|
|
|
memcpy((uint8_t*)m_pTrampolineAddr + callOffset, CALL_WITH_RAX, sizeof(CALL_WITH_RAX));
|
2023-02-27 19:34:44 +01:00
|
|
|
|
2023-03-04 15:02:40 +01:00
|
|
|
*(uint64_t*)((uint8_t*)m_pTrampolineAddr + callOffset + CALL_WITH_RAX_ADDRESS_OFFSET) = (uint64_t)realCallAddress;
|
2023-02-27 19:34:44 +01:00
|
|
|
}
|
|
|
|
|
2023-02-27 13:32:38 +01:00
|
|
|
// fixup trampoline addr
|
2023-06-16 18:45:11 +02:00
|
|
|
*(uint64_t*)((uint8_t*)m_pTrampolineAddr + TRAMPOLINE_SIZE - sizeof(ABSOLUTE_JMP_ADDRESS) + ABSOLUTE_JMP_ADDRESS_OFFSET) =
|
|
|
|
(uint64_t)((uint8_t*)m_pSource + sizeof(ABSOLUTE_JMP_ADDRESS));
|
2023-02-27 13:32:38 +01:00
|
|
|
|
|
|
|
// make jump to hk
|
2023-03-04 15:02:40 +01:00
|
|
|
mprotect((uint8_t*)m_pSource - ((uint64_t)m_pSource) % sysconf(_SC_PAGE_SIZE), sysconf(_SC_PAGE_SIZE), PROT_READ | PROT_WRITE | PROT_EXEC);
|
2023-02-27 13:32:38 +01:00
|
|
|
memcpy(m_pSource, ABSOLUTE_JMP_ADDRESS, sizeof(ABSOLUTE_JMP_ADDRESS));
|
|
|
|
|
|
|
|
// make popq %rax and NOP all remaining
|
2023-03-04 15:02:40 +01:00
|
|
|
memcpy((uint8_t*)m_pSource + sizeof(ABSOLUTE_JMP_ADDRESS), POP_RAX, sizeof(POP_RAX));
|
2023-02-27 13:32:38 +01:00
|
|
|
size_t currentOp = sizeof(ABSOLUTE_JMP_ADDRESS) + sizeof(POP_RAX);
|
2023-03-04 15:02:40 +01:00
|
|
|
memset((uint8_t*)m_pSource + currentOp, NOP, HOOKSIZE - currentOp);
|
2023-02-27 13:32:38 +01:00
|
|
|
|
|
|
|
// fixup jump addr
|
2023-03-04 15:02:40 +01:00
|
|
|
*(uint64_t*)((uint8_t*)m_pSource + ABSOLUTE_JMP_ADDRESS_OFFSET) = (uint64_t)(m_pDestination);
|
2023-02-27 13:32:38 +01:00
|
|
|
|
|
|
|
// revert mprot
|
2023-03-04 15:02:40 +01:00
|
|
|
mprotect((uint8_t*)m_pSource - ((uint64_t)m_pSource) % sysconf(_SC_PAGE_SIZE), sysconf(_SC_PAGE_SIZE), PROT_READ | PROT_EXEC);
|
2023-02-27 13:32:38 +01:00
|
|
|
|
|
|
|
// set original addr to trampo addr
|
|
|
|
m_pOriginal = m_pTrampolineAddr;
|
|
|
|
|
|
|
|
m_bActive = true;
|
|
|
|
m_iHookLen = HOOKSIZE;
|
2023-02-27 19:34:44 +01:00
|
|
|
m_iTrampoLen = TRAMPOLINE_SIZE;
|
2023-02-27 13:32:38 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CFunctionHook::unhook() {
|
|
|
|
// check for unsupported platforms
|
|
|
|
#if !defined(__x86_64__)
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!m_bActive)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// allow write to src
|
2023-03-04 15:02:40 +01:00
|
|
|
mprotect((uint8_t*)m_pSource - ((uint64_t)m_pSource) % sysconf(_SC_PAGE_SIZE), sysconf(_SC_PAGE_SIZE), PROT_READ | PROT_WRITE | PROT_EXEC);
|
2023-02-27 13:32:38 +01:00
|
|
|
|
|
|
|
// write back original bytes
|
2023-02-27 20:17:58 +01:00
|
|
|
memcpy(m_pSource, m_pOriginalBytes, m_iHookLen);
|
2023-02-27 13:32:38 +01:00
|
|
|
|
|
|
|
// revert mprot
|
2023-03-04 15:02:40 +01:00
|
|
|
mprotect((uint8_t*)m_pSource - ((uint64_t)m_pSource) % sysconf(_SC_PAGE_SIZE), sysconf(_SC_PAGE_SIZE), PROT_READ | PROT_EXEC);
|
2023-02-27 13:32:38 +01:00
|
|
|
|
|
|
|
// unmap
|
|
|
|
munmap(m_pTrampolineAddr, m_iTrampoLen);
|
|
|
|
|
|
|
|
// reset vars
|
|
|
|
m_bActive = false;
|
|
|
|
m_iHookLen = 0;
|
|
|
|
m_iTrampoLen = 0;
|
|
|
|
m_pTrampolineAddr = nullptr;
|
2023-02-27 20:17:58 +01:00
|
|
|
m_pOriginalBytes = nullptr;
|
|
|
|
|
|
|
|
free(m_pOriginalBytes);
|
2023-02-27 13:32:38 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
CFunctionHook* CHookSystem::initHook(HANDLE owner, void* source, void* destination) {
|
|
|
|
return m_vHooks.emplace_back(std::make_unique<CFunctionHook>(owner, source, destination)).get();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CHookSystem::removeHook(CFunctionHook* hook) {
|
|
|
|
std::erase_if(m_vHooks, [&](const auto& other) { return other.get() == hook; });
|
|
|
|
return true; // todo: make false if not found
|
|
|
|
}
|
|
|
|
|
|
|
|
void CHookSystem::removeAllHooksFrom(HANDLE handle) {
|
|
|
|
std::erase_if(m_vHooks, [&](const auto& other) { return other->m_pOwner == handle; });
|
2023-03-04 15:02:40 +01:00
|
|
|
}
|