LINUX 函数级热补丁技术、inline hook(待完善)

本文介绍如何使用 C 语言动态加载共享库,并通过修改内存中函数指针的方式实现对原有函数的替换。主要内容包括:利用 dlopen 和 dlsym 加载和获取共享库中的函数地址;通过 /proc/self/mem 打开当前进程内存并定位到目标函数;修改内存保护属性以允许写操作;覆盖目标函数的起始指令以指向新函数。
#include <stdio.h>
#include <dlfcn.h>
#include<string.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
static char *memory;
static int alloc_size;

void helloworld()
{
    printf("helloworld\n");
}
int main(void)
{
    void (*helloworld1)();
    void *handle;
    char* error;
    //open so
    handle = dlopen("./lib/libhelloso.so",RTLD_LAZY);
    if((error=dlerror())!=NULL){
        printf("%s\n",error);
        exit(1);
    }
    //get function
    helloworld1=(void(*)())dlsym(handle,"hellworld1");
    if((error=dlerror())!=NULL){
        printf("%s\n",error);
        exit(1);
    }
    printf("helloworld1:%x\n",helloworld1);
    //get pid
    pid_t self;
    self = getpid();
    //open mem
    int fd;
    char file_name[100];
    sprintf(file_name,"/proc/%d/mem\0",(int)self);
    fd=open(file_name,O_RDWR);
    if(fd<0)
    {
        printf("open file error!!!\n");
        exit(1);
    }
    off_t r=lseek(fd,helloworld,SEEK_SET);
    if(r==-1)printf("lseek error:%m\n");
    printf("process pid:%d\n",(int)self);
    printf("helloworld function start address:%p\n",helloworld);
    /*check seeker start*/
    int var1;
    ssize_t size=read(fd, &var1, sizeof(int));
    printf("function first opcode:%x\n",var1);
    /*check seeker end*/

    /*generate the new opcode start*/
    char new_opcode[6];
    new_opcode[0]=0xff;
    new_opcode[1]=0x25;
    void *addr[2];
    addr[0]=helloworld1;
    addr[1]=addr;
    memcpy(new_opcode+2,addr+1,4);
    /*generate the new opcode end*/

    /*change mem to write*/
    int res;

    alloc_size = sysconf(_SC_PAGE_SIZE);
    void *page_addr=helloworld;
    page_addr= page_addr-(int)page_addr%alloc_size;
    res=mprotect((void*)page_addr,alloc_size,PROT_WRITE|PROT_EXEC|PROT_READ);
    if(res==-1)
    {
        perror("mprotect error\n");
        return 1;
    }
    /*change mem to write end*/

    /*overwrite the mem start*/
    memcpy(helloworld,new_opcode,6);
    /*overwrite the mem end*/
    /*check mem start*/
    printf("\nafter change\n");
    r=lseek(fd,helloworld,SEEK_SET);
    if(r==-1)printf("lseek error:%m\n");
    size=read(fd, &var1, sizeof(int));
    printf("function first:%x\n",var1);
    /*check mem end*/
    /*recall helloworld start*/
    helloworld();
    printf("\n");
    /*recall helloworld end*/
    close (fd);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值