jmpews / Dobby

a lightweight, multi-platform, multi-architecture hook framework.
Apache License 2.0
3.89k stars 798 forks source link

JNI Hook FindClass works only one time #141

Closed mywalkb closed 3 years ago

mywalkb commented 3 years ago

I hooked JNI_OnLoad with success, but I have an issue on hook FindClass. It's works only one time. In JNI_OnLoad there is a call to FindClass and I can catch it, but when the app android call native method and inside this method there is another call to FindClass, I can't catch. Could you tell me where my mistake?

This is my code:

static jclass (*orig_FindClass)(JNIEnv *env, const char *name);
static jclass fake_FindClass(JNIEnv *env, const char *name) {
    LOGI("find class call %s", name);
    LOGI("find class call env %p", env);
    return orig_FindClass(env, name);
}

static jint (*orig_JNI_OnLoad)(JavaVM *jvm, jobject x);
static jint fake_JNI_OnLoad(JavaVM *jvm, jobject x) {
    jint i = orig_JNI_OnLoad(jvm, x);
    JNIEnv *env;
    jvm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6);
    LOGI("JNI_OnLoad %d", i);
    LOGI("JNI_OnLoad jvm %p", jvm);
    LOGI("JNI_OnLoad env %p", env);
    return i;
}

void *jnionload = dlsym(handle, "JNI_OnLoad");
void *findClass = dlsym(handle, "_ZN7_JNIEnv9FindClassEPKc");
LOGI("Result ONLoad: %p", jnionload);
LOGI("Result FindClass: %p", findClass);
DobbyHook(jnionload, (void *)fake_JNI_OnLoad, (void **)&orig_JNI_OnLoad);
DobbyHook(findClass, (void *)fake_FindClass, (void **)&orig_FindClass);
mywalkb commented 3 years ago

I solved.

In fake_JNI_Onload I called DobbyHook((void *)env->functions->FindClass, (void *)fake_FindClass, (void **)&orig_FindClass); and I have all call to FindClass.