c++写代码编写hook系统的inotify文件监管返回0
时间: 2024-03-18 14:39:14 浏览: 109
以下是使用C++编写hook系统的inotify文件监管的代码示例:
```C++
#include <stdio.h>
#include <stdlib.h>
#include <android/log.h>
#include <sys/inotify.h>
#include <dlfcn.h>
#define TAG "INOTIFY_HOOK"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__)
static int (*original_inotify_init)(void);
static int (*original_inotify_add_watch)(int fd, const char *pathname, uint32_t mask);
int my_inotify_init(void) {
int fd = original_inotify_init();
LOGD("inotify_init hooked, fd = %d", fd);
return fd;
}
int my_inotify_add_watch(int fd, const char *pathname, uint32_t mask) {
int wd = original_inotify_add_watch(fd, pathname, mask);
LOGD("inotify_add_watch hooked, wd = %d, pathname = %s, mask = %u", wd, pathname, mask);
return wd;
}
void hook_inotify() {
void *handle = dlopen("libc.so", RTLD_NOW);
if (handle != NULL) {
original_inotify_init = (int (*)(void)) dlsym(handle, "inotify_init");
original_inotify_add_watch = (int (*)(int, const char *, uint32_t)) dlsym(handle, "inotify_add_watch");
if (original_inotify_init != NULL && original_inotify_add_watch != NULL) {
MSHookFunction((void *) original_inotify_init, (void *) my_inotify_init, (void **) &original_inotify_init);
MSHookFunction((void *) original_inotify_add_watch, (void *) my_inotify_add_watch, (void **) &original_inotify_add_watch);
LOGD("inotify hook success!");
} else {
LOGE("dlsym failed: %s", dlerror());
}
dlclose(handle);
} else {
LOGE("dlopen failed: %s", dlerror());
}
}
```
这段代码使用了MSHookFunction函数来hook系统的inotify_init和inotify_add_watch函数,然后在hook函数中打印日志信息。如果您需要使用其他的操作,请根据您的需求进行修改。请注意,这段代码需要在root的设备上运行,并且需要使用cydia substrate或其他的hook框架来注入代码。
阅读全文