通俗的讲,是用自己编写的同名函数去替换存在的系统函数:
这里只是简单的介绍了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