android - JNI混淆器混淆

标签 android java-native-interface native obfuscation

我有混淆问题。为了更好的想象力:

Java代码

class JniTest...

public void test()
{
    //some code
}

public void runJniCode()
{
    //here I call native code
}

本地代码

JNIEXPORT void JNICALL
Java_path_to_class_test(JNIEnv* env, jobject  obj)
{
    //here I call test method from Java

}

一切正常,直到我想发布混淆版本。 Java 类的名称(例如 JniTest)和该类中的方法 test 被混淆器重命名为“a”和“a()”(这可能不是始终相同),但在 native 代码中,方法和类的原始名称仍然存在,因为它被硬编码为字符串,例如:

jmethodID mid = env->GetMethodID(cls, "test", "someSignature");

...有什么方法可以动态设置方法名吗?

最佳答案

在研究这个完全相同的问题时,我遇到了一个我认为合理的解决方案。不幸的是,该解决方案不会按要求自动混淆 native Java 代码和 JNI 方法,但我仍然认为它值得分享。

引自来源:

I present here a simple trick which allows obfuscation of the JNI layer, renaming the method names to meaningless names on both the Java and native side, while keeping the source code relatively readable and maintainable and without affecting performance.

Let’s consider an example, initial situation:

class Native {
    native static int rotateRGBA(int rgb, int w, int h);
}

extern "C" int Java_pakage_Native_rotateRGBA(JNIEnv *env, jclass, int rgb, int w, int h);

In the example above Proguard can’t obfuscate the method name rotateRGBA, which remains visible on the Java side and on the native side.

The solution is to use directly a meaningless method name in the source, while taking care to minimally disrupt the readability and maintainability of the code.

class Native {
    private native static int a(int rgb, int w, int h); //rotateRGBA

    static int rotateRGBA(int rgb, int w, int h) {
        return a(rgb, w, h);
    }
}

// rotateRGBA
extern "C" int Java_pakage_Native_a(JNIEnv *env, jclass, int rgb, int w, int h);

The JNI method is renamed to a meaningless a. But the call on the Java side is wrapped by the meaningfully named method rotateRGBA. The Java clients continue to invoke Native.rotateRGBA() as before, without being affected at all by the rename.

What is interesting is that the new Native.rotateRGBA method is not native anymore, and thus can be renamed by Proguard at will. The result is that the name rotateRGBA completely disappears from the obfuscated code, on both Dalvik and native side. What’s more, Proguard optimizes away the wrapper method, thus removing the (negligible) performance impact of wrapping the native call.

Conclusion: eliminated the JNI method name from the obfuscated code (both Dalvik bytecode and native library), with minimal impact to readability and no performance impact.

来源:Obfuscating the JNI surface layer

我仍在寻找可以自动混淆 native Java 代码和关联的 JNI 的工具。

关于android - JNI混淆器混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19004114/

相关文章:

java - 在 Eclipse 中将 Android 项目转换为 'regular' Java 项目?

reactjs - 我想在初始屏幕 React Native 中播放 lottie 动画

android - 如何在 C++ OpenCV 中显示带有 putText 的计时器结果?

javascript - 使用react-native-print直接打印文档html

java - Android,第二个 Activity 未启动

java - C# 服务器、Android Java 客户端 - 连接问题

android - 在 Apportable 的 StoreKit 和 StoreKitAmazon 中测试本地化价格

c++ - JNI_CreateJavaVM() 以退出代码 1 终止

java - android studio 使用JNI 构建c/c++,但c/c++ 需要另一个lib.a,如何解决

android - 如何从 Android 的 C++ 项目创建库(.so 或 .a 文件)?