HOOK的简单实用

通俗的讲,是用自己编写的同名函数去替换存在的系统函数:

这里只是简单的介绍了hook的用法:

更多的使用和细节参看:

Linux Hook方法_vspiders的博客-CSDN博客

linux 系统调用 hook 总结_whatday的博客-CSDN博客_hook调用

举例子:pthread_mutex_lock 和 pthread_mutex_unlock

步骤1:

查找对应系统函数的定义:

使用 man + 函数 进行查找

int pthread_mutex_lock(pthread_mutex_t *mutex);

int pthread_mutex_trylock(pthread_mutex_t *mutex);

int pthread_mutex_unlock(pthread_mutex_t *mutex);

三步处理:

// 定义
typedef int (*pthread_mutex_lock_t) (pthread_mutex_t *mutex);
typedef int (*pthread_mutex_unlock_t) (pthread_mutex_t *mutex);

// 声明变量

pthread_mutex_lock_t pthread_mutex_lock_f;
pthread_mutex_unlock_t pthread_mutex_unlock_f;

// 进行劫持系统的pthread函数

void init_hook(void){

    pthread_mutex_lock_f = dlsym(RTLD_NEXT, "pthread_mutex_lock");
    pthread_mutex_unlock_f = dlsym(RTLD_NEXT, "pthread_mutex_unlock");
}

定义自己的函数(加入一些自己需要的信息,然后调用劫持的系统函数吧):

int pthread_mutex_lock(pthread_mutex_t *mutex){
    pthread_t selfid = pthread_self();
    
    // 使用劫持的系统函数
    pthread_mutex_lock_f(mutex);

    printf("pthread_mutex_lock: %ld,%p\n", selfid, mutex);
}

int pthread_mutex_unlock(pthread_mutex_t *mutex ){
    
    pthread_t selfid = pthread_self();
	
	pthread_mutex_unlock_f(mutex);
	printf("pthread_mutex_unlock: %ld, %p\n", selfid, mutex);
}

完整代码: 

#define _GNU_SOURCE
#include <dlfcn.h>


#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

#include <unistd.h>




typedef int (*pthread_mutex_lock_t)(pthread_mutex_t *mutex);
typedef int (*pthread_mutex_unlock_t)(pthread_mutex_t *mutex);

// 两个函数指针
pthread_mutex_lock_t pthread_mutex_lock_f;
pthread_mutex_unlock_t pthread_mutex_unlock_f;


int pthread_mutex_lock(pthread_mutex_t *mutex) {

	pthread_t selfid = pthread_self();

	
	pthread_mutex_lock_f(mutex);
	printf("pthread_mutex_lock: %ld, %p\n", selfid, mutex);

} 



int pthread_mutex_unlock(pthread_mutex_t *mutex) {

	pthread_t selfid = pthread_self();
	
	pthread_mutex_unlock_f(mutex);
	printf("pthread_mutex_unlock: %ld, %p\n", selfid, mutex);

} 


void init_hook(void) {

	pthread_mutex_lock_f = dlsym(RTLD_NEXT, "pthread_mutex_lock");
	pthread_mutex_unlock_f = dlsym(RTLD_NEXT, "pthread_mutex_unlock");
	
}



#if 1


pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex3 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex4 = PTHREAD_MUTEX_INITIALIZER;




void *thread_funcA(void *arg) {

	pthread_mutex_lock(&mutex1);

	sleep(1);
	
	pthread_mutex_lock(&mutex2);

	printf("thread_funcA\n");


	pthread_mutex_unlock(&mutex2);
	

	pthread_mutex_unlock(&mutex1);

}

void *thread_funcB(void *arg) {

	pthread_mutex_lock(&mutex2);

	sleep(1);

	pthread_mutex_lock(&mutex3);

	printf("thread_funcB\n");

	pthread_mutex_unlock(&mutex3);

	pthread_mutex_unlock(&mutex2);

}

void *thread_funcC(void *arg) {

	pthread_mutex_lock(&mutex3);

	sleep(1);

	pthread_mutex_lock(&mutex4);

	printf("thread_funcC\n");

	pthread_mutex_unlock(&mutex4);

	pthread_mutex_unlock(&mutex3);

}




void *thread_funcD(void *arg) {

	pthread_mutex_lock(&mutex4);

	sleep(1);
	pthread_mutex_lock(&mutex1);
	
	printf("thread_funcD\n");
	
	pthread_mutex_unlock(&mutex1);

	pthread_mutex_unlock(&mutex4);

}


int main() {

	init_hook();

	pthread_t tida, tidb, tidc, tidd;

	pthread_create(&tida, NULL, thread_funcA, NULL);
	pthread_create(&tidb, NULL, thread_funcB, NULL);
	pthread_create(&tidc, NULL, thread_funcC, NULL);
	pthread_create(&tidd, NULL, thread_funcD, NULL);


	pthread_join(tida, NULL);
	pthread_join(tidb, NULL);
	pthread_join(tidc, NULL);
	pthread_join(tidd, NULL);
	
	return 0;

}


#endif


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值