#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);
}
LINUX 函数级热补丁技术、inline hook(待完善)
最新推荐文章于 2024-01-03 10:21:14 发布
