|
|
|
@ -1,3 +1,4 @@
|
|
|
|
|
//内核中实现x86架构下的函数钩子(hook)
|
|
|
|
|
#include "../internal.h"
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
@ -10,7 +11,7 @@ static struct {
|
|
|
|
|
typeof(insn_init) *init;
|
|
|
|
|
typeof(insn_get_length) *get_length;
|
|
|
|
|
} khook_arch_lde;
|
|
|
|
|
|
|
|
|
|
//初始化长度解析引擎
|
|
|
|
|
static inline int khook_arch_lde_init(void) {
|
|
|
|
|
khook_arch_lde.init = khook_lookup_name("insn_init");
|
|
|
|
|
if (!khook_arch_lde.init) return -EINVAL;
|
|
|
|
@ -18,7 +19,7 @@ static inline int khook_arch_lde_init(void) {
|
|
|
|
|
if (!khook_arch_lde.get_length) return -EINVAL;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//获取指令长度
|
|
|
|
|
static inline int khook_arch_lde_get_length(const void *p) {
|
|
|
|
|
struct insn insn;
|
|
|
|
|
int x86_64 = 0;
|
|
|
|
@ -35,7 +36,7 @@ static inline int khook_arch_lde_get_length(const void *p) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
//插入跳转指令
|
|
|
|
|
// place a jump at addr @a from addr @f to addr @t
|
|
|
|
|
static inline void x86_put_jmp(void *a, void *f, void *t)
|
|
|
|
|
{
|
|
|
|
@ -46,12 +47,12 @@ static inline void x86_put_jmp(void *a, void *f, void *t)
|
|
|
|
|
static const char khook_stub_template[] = {
|
|
|
|
|
# include KHOOK_STUB_FILE_NAME
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//修复函数钩子中的占位符,
|
|
|
|
|
static inline void stub_fixup(void *stub, const void *value) {
|
|
|
|
|
while (*(int *)stub != 0xcacacaca) stub++;
|
|
|
|
|
*(long *)stub = (long)value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//初始化单个钩子
|
|
|
|
|
static inline void khook_arch_sm_init_one(khook_t *hook) {
|
|
|
|
|
khook_stub_t *stub = KHOOK_STUB(hook);
|
|
|
|
|
if (hook->target.addr[0] == (char)0xE9 ||
|
|
|
|
@ -73,7 +74,7 @@ static inline void khook_arch_sm_init_one(khook_t *hook) {
|
|
|
|
|
}
|
|
|
|
|
hook->orig = stub->orig; // the only link from hook to stub
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//清理单个钩子
|
|
|
|
|
static inline void khook_arch_sm_cleanup_one(khook_t *hook) {
|
|
|
|
|
khook_stub_t *stub = KHOOK_STUB(hook);
|
|
|
|
|
memcpy(hook->target.addr_map, stub->orig, stub->nbytes);
|
|
|
|
|