linux简单hook

2023-04-04  本文已影响0人  烨哥

linux hook原理

一个简单通过LD_PRELOAD 注入的代码的例子

#include <stdio.h>
void my_printf() {
    printf("my_printf in function\n");
}
#include <stdio.h>
void my_printf();

int main()
{
    my_printf();
    return 0;
}
gcc -shared -fPIC -o libfunction.so function.c 
gcc -o app main.c -L. -lfunction
export LD_LIBRARY_PATH=.
./app
#include <stdio.h>
void my_printf() {
    printf("my_printf in hook\n");
}
gcc -shared -fPIC -o libhook.so hook.c 
LD_PRELOAD=./libhook.so ./app

hook原函数

  #include <stdio.h>
#define __USE_GNU
#include<dlfcn.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>
#include <sys/mman.h>
#include <elf.h>
#include <link.h>

 void (*orig_func)();

void my_printf_hook() {
    printf("my_printf in hook on enter\n");
    orig_func();
    printf("my_printf in hook on level\n");
}
const Elf64_Dyn* find_dyn(const Elf64_Dyn* dyn, Elf64_Sxword tag) {
    while (dyn->d_tag != DT_NULL) {
        if (dyn->d_tag == tag) {
            return dyn;
        }
        dyn++;
    }
    return NULL;
}
void do_replace_function() {
    // 打开我们的可执行文件
    void* handle = dlopen(RTLD_DEFAULT, RTLD_LAZY);
    if (handle == NULL) {
        printf("open so error %s", dlerror());
        return;
    }
    struct link_map* map = NULL;
    //获取可执行文件的dlinfo
    if (dlinfo(handle, RTLD_DI_LINKMAP, &map) != 0) {
        printf("dlinfo error");
    }
    const char* plt_addr_base = (const char *)map->l_addr;
    const Elf64_Dyn* dyn = NULL;
    // 获取符号表信息
    //.dynsym
    dyn = find_dyn(map->l_ld, DT_SYMTAB);
    if (dyn == NULL) {
        printf("failed to find DT_SYMTAB\n");
        return;
    }
    const Elf64_Sym* dyn_systab =(const Elf64_Sym*) dyn->d_un.d_ptr;
    //获取字符串表
    //.dynstr
    dyn = find_dyn(map->l_ld, DT_STRTAB);
    if (dyn == NULL) {
        printf("failed to find DT_STRTAB\n");
        return;
    }
    const char* dyn_strtab = (const char*)dyn->d_un.d_ptr;
    //获取plt
    dyn = find_dyn(map->l_ld, DT_JMPREL);
    if (dyn == NULL) {
        printf("failed to find DT_JMPREL\n");
        return;
    }
    const Elf64_Rela* plt = (const Elf64_Rela*)dyn->d_un.d_ptr;
    //获取plt大小
    dyn = find_dyn(map->l_ld, DT_PLTRELSZ);
    if (dyn == NULL) {
        printf("failed to find DT_PLTRELSZ\n");
        return;
    }
    int plt_cont = dyn->d_un.d_val / sizeof(Elf64_Rela);
    int i = 0;
    size_t len = strlen("my_printf");
    //便利plt
    for ( i = 0; i < plt_cont; i++) {
        const Elf64_Rela* current_plt = plt + i;
        if (ELF64_R_TYPE(current_plt->r_info) != R_X86_64_JUMP_SLOT) {
            continue;
        }
        size_t index = ELF64_R_SYM(current_plt->r_info);//找到符号表的偏移
        size_t index2 = dyn_systab[index].st_name;//站到字符串表的偏移
        const char* name = dyn_strtab + index2;
        if (strncmp("my_printf", name, len) == 0) {
            void** addr = (void **)(plt_addr_base + current_plt->r_offset);//找到这个got表的偏移的位置
            orig_func = (void (*)())(*addr);//把记录的值取出来
            *addr = (void *)my_printf_hook;//新的地址复制过去 完成替换
        }
    }
}

void __attribute__((constructor)) my_init(void) {
    printf("Hello from LD_PRELOAD!\n");
    do_replace_function();
}
  gcc -shared   -fPIC -o libhook.so hook.c  -ldl
  LD_PRELOAD=./libhook.so ./app 
Hello from LD_PRELOAD!
my_printf in hook on enter
my_printf in function
my_printf in hook on level
上一篇 下一篇

猜你喜欢

热点阅读