1. 程式人生 > >JNI 獲取系統類載入器處理反制Xposed框架

JNI 獲取系統類載入器處理反制Xposed框架

 在網上找了很久沒有找到 jni 相應的程式碼,基本都是java的

下面是全部程式碼:

/**
 * 檢測xposed
 * checkXposed
 */
bool checkXposed(JNIEnv *env) {
    //找到ClassLoader類
    jclass classloaderClass = env->FindClass("java/lang/ClassLoader");
    //找到ClassLoader類中的靜態方法getSystemClassLoader
    jmethodID getSysLoaderMethod = env->GetStaticMethodID(classloaderClass, "getSystemClassLoader",
                                                          "()Ljava/lang/ClassLoader;");
    //呼叫ClassLoader中的getSystemClassLoader方法,返回ClassLoader物件
    jobject classLoader = env->CallStaticObjectMethod(classloaderClass, getSysLoaderMethod);
    //DexClassLoader:能夠載入自定義的jar/apk/dex
    //PathClassLoader:只能載入系統中已經安裝過的apk
    jclass dexLoaderClass = env->FindClass("dalvik/system/DexClassLoader");
    //找到ClassLoader中的方法loadClass
    jmethodID loadClass = env->GetMethodID(dexLoaderClass, "loadClass",
                                           "(Ljava/lang/String;)Ljava/lang/Class;");
    //呼叫DexClassLoader的loadClass方法,載入需要呼叫的類
    jstring dir = env->NewStringUTF("de.robv.android.xposed.XposedBridge");
    jclass targetClass = (jclass) env->CallObjectMethod(classLoader, loadClass, dir);

    if (env->ExceptionCheck()) {  // 檢查JNI呼叫是否有引發異常
        env->ExceptionDescribe();
        env->ExceptionClear();        // 清除引發的異常,在Java層不會列印異常的堆疊資訊
       // env->ThrowNew(env->FindClass("java/lang/Exception"), "JNI丟擲的異常!");
        LOGD("error! not found");
        return false;
    }

    if (targetClass != NULL) {
        jfieldID disableHooksFiled = env->GetStaticFieldID(targetClass, "disableHooks", "Z");
        env->SetStaticBooleanField(targetClass, disableHooksFiled, true);
        jfieldID runtimeFiled = env->GetStaticFieldID(targetClass, "runtime", "I");
        env->SetStaticIntField(targetClass, runtimeFiled, 2);
        return true;
    } else {
        return false;
    }
}

參考來源

https://bbs.pediy.com/thread-179829.htm