-
I learn native hook with this wiki: https://github.com/LSPosed/LSPosed/wiki/Native-Hook native_api.h //
// Created by runtime on 2021/12/4.
//
#include <stdint.h>
#ifndef LSPNATIVE_NATVIE_HOOK_H
#define LSPNATIVE_NATVIE_HOOK_H
typedef int (*HookFunType)(void *func, void *replace, void **backup);
typedef int (*UnhookFunType)(void *func);
typedef void (*NativeOnModuleLoaded)(const char *name, void *handle);
typedef struct {
uint32_t version;
HookFunType hook_func;
UnhookFunType unhook_func;
} NativeAPIEntries;
typedef NativeOnModuleLoaded (*NativeInit)(const NativeAPIEntries *entries);
#endif //LSPNATIVE_NATVIE_HOOK_H native-lib.cpp #include <cstdio>
#include <cstring>
#include <string>
#include <dlfcn.h>
#include <jni.h>
#include "native_api.h"
#include "logging.h"
static HookFunType hook_func = nullptr;
int (*backup)();
int fake() {
return backup() + 1;
}
FILE *(*backup_fopen)(const char *filename, const char *mode);
FILE *fake_fopen(const char *filename, const char *mode) {
if (strstr(filename, "banned")) return nullptr;
return backup_fopen(filename, mode);
}
jclass (*backup_FindClass)(JNIEnv *env, const char *name);
jclass fake_FindClass(JNIEnv *env, const char *name)
{
if(!strcmp(name, "dalvik/system/BaseDexClassLoader"))
return nullptr;
return backup_FindClass(env, name);
}
void on_library_loaded(const char *name, void *handle) {
// hooks on `libtarget.so`
// if (std::string(name).ends_with("libtarget.so")) {
void *target = dlsym(handle, "target_fun");
hook_func(target, (void *) fake, (void **) &backup);
// }
}
extern "C" [[gnu::visibility("default")]] [[gnu::used]]
jint JNI_OnLoad(JavaVM *jvm, jobject x) {
JNIEnv *env = nullptr;
jvm->GetEnv((void **)&env, JNI_VERSION_1_6);
hook_func((void *)env->functions->FindClass, (void *)fake_FindClass, (void **)&backup_FindClass);
return JNI_VERSION_1_6;
}
extern "C" [[gnu::visibility("default")]] [[gnu::used]]
NativeOnModuleLoaded native_init(const NativeAPIEntries *entries) {
hook_func = entries->hook_func;
// system hooks
hook_func((void*) fopen, (void*) fake_fopen, (void**) &backup_fopen);
return on_library_loaded;
} when I try to build it,I got this error: What should I do next.. |
Beta Was this translation helpful? Give feedback.
Answered by
ghost
Dec 5, 2021
Replies: 1 comment
-
change the definition of JNI_Onload from When this wiki doesn't exist I tried to develope an example for native hook and here there is my test. I hooked also JNI_OnLoad so my implementation is different. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
yujincheng08
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
change the definition of JNI_Onload from
jint JNI_OnLoad(JavaVM *jvm, jobject x)
to
jint JNI_OnLoad(JavaVM *jvm, void* x)
When this wiki doesn't exist I tried to develope an example for native hook and here there is my test. I hooked also JNI_OnLoad so my implementation is different.