当前位置:网站首页>Android应用安全之JNI混淆
Android应用安全之JNI混淆
2022-06-28 11:38:00 【一杯苦芥】
简介
JNI全称为Java Native Interface,是使Java方法与C\C++函数互通的一座桥梁。通俗的讲,它的作用就是使Java可以调用C\C++写的函数、使C\C++可以调用Java写的方法。
特点
1、性能
众所周知,Android程序一般是基于Java语言开发的,虽然Google随后推出了Kotlin语言,但是Kotlin语言依然运行在JVM虚拟机上,所以其性能与采用Java开发的程序是没有任何区别的。
由于Java是虚拟机语言(指需要被编译成虚拟机代码,由虚拟机执行的语言),所以无论是JVM虚拟机,还是Dalvik、Art虚拟机,在对性能要求较高的情况下,就显得有些不足了。此时,便需要编译型语言将源代码编译为机器码,直接由CPU执行代码,从而提升性能。
2、安全性
相对于java代码容易被反编译,使用NDK开发出来的原生C++代码编译后,生成的so库是一个二进制文件,这无疑增加了破解的难度。利用这个特性,可以将客户端敏感信息写在C++代码中,增强应用的安全性。
逆向分析
1、新建一个Native C++项目,将自动生成一个默认的JNI方法:
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_myapplication_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
2、上面是一个很简单的JNI方法,我们将生成的so库使用IDA工具进行反汇编,可以看到以下的内容:

默认情况下,JNI函数名以Java为前缀,并且用“_”下划线将包名、类名以及native方法名连接起来。这种格式的so库被反汇编后,很容易就找到对应的方法。
动态注册
原理
利用 RegisterNatives 方法来注册 java 方法与 JNI 函数的一一对应关系;
实现流程
1.利用结构体 JNINativeMethod 数组记录 java 方法与 JNI 函数的对应关系;
// 第一个参数:Java层的方法名
// 第二个参数:方法的签名,括号内为参数类型,后面为返回类型
// 第三个参数:需要重新注册的方法名
static JNINativeMethod getMethods[] = {
{"stringFromJNI", "()Ljava/lang/String;", (void *) getStr1}
};
// 指定代码所在的段。在编译时,把该函数编译到自定义的section里。
// 由于在java层没有定义该函数,因此需要写到一个自定义的section里。
extern "C"
__attribute__((section(".mysection"))) JNICALL jstring getStr1(JNIEnv *env, jobject obj) {
return env->NewStringUTF("Hello from C++");
}
2.实现 JNI_OnLoad 方法,在加载动态库后,执行动态注册;
extern "C"
jint JNI_OnLoad(JavaVM *vm, void *reserved) {
JNIEnv *env;
if (vm->GetEnv((void **) (&env), JNI_VERSION_1_6) != JNI_OK) {
return -1;
}
if (!registerNatives(env)) {
return -1;
}
return JNI_VERSION_1_6;
}
3.调用 FindClass 方法,获取 java 对象;
extern "C"
int registerNativeMethods(JNIEnv *env, const char *className, JNINativeMethod *getMethods, int numMethods) {
jclass clazz;
clazz = env->FindClass(className);
if (clazz == NULL) {
return JNI_FALSE;
}
if (env->RegisterNatives(clazz, getMethods, numMethods) < 0) {
return JNI_FALSE;
}
return JNI_TRUE;
}
4.调用 RegisterNatives 方法,传入 java 对象,以及 JNINativeMethod 数组,以及注册数目完成注册;
// 指定要注册的类
#define JNIREG_CLASS "com/example/jnilibrary/JniUtils"
extern "C"
int registerNatives(JNIEnv *env) {
if (!registerNativeMethods(env, JNIREG_CLASS, getMethods,
sizeof(getMethods) / sizeof(getMethods[0]))) {
return JNI_FALSE;
}
return JNI_TRUE;
}
将生成的so库使用IDA工具进行反汇编,可以看到函数被编译到自定义的section里面去了:

隐藏符号表
ndk-build
在Android.mk文件中,添加 LOCAL_CFLAGS := -fvisibility=hidden
cmake
在CmakeLists.txt文件中,添加 add_definitions("-fvisibility=hidden")
我们将生成的so库使用IDA工具进行反汇编,发现找不到编写的JNI函数,其次也没有找到函数的符号表,而且整个逻辑是完全混淆的:

边栏推荐
- Random forest and poetry maker trained by AMR
- recent developments
- Day29 JS notes 2021.09.23
- 【C语言】如何产生正态分布或高斯分布随机数
- [no title] the virtual machine vmnet0 cannot be found and an error is reported: there is no un bridged host network adapter
- AGCO AI frontier promotion (2.16)
- FTP protocol for Wireshark packet capture analysis
- 2018 joint examination of nine provinces & Merging of line segment trees
- Chendanqi, Fang Fei, guquanquan and Li Bo won the prize, and the list of Sloan research award in 2022 was released
- How to deploy the software testing environment?
猜你喜欢

Multi dimensional monitoring: the data base of intelligent monitoring

携手Cigent:群联为SSD主控固件引入高级网络安全防护特性

The default point of this in JS and how to modify it to 2021.11.09

【C语言】NextDay问题

Day30 JS notes BOM and DOM 2021.09.24

What method is required for word, PDF and txt files to realize full-text content retrieval?

Necessary for beginners PR 2021 quick start tutorial, PR green screen matting operation method

If you want to change to software testing, how can you package your resume as a test engineer with 1 year of work experience

js中的class类模式及语法 2021.11.10

Chendanqi, Fang Fei, guquanquan and Li Bo won the prize, and the list of Sloan research award in 2022 was released
随机推荐
Convert black mask picture to color annotation file
【C语言】NextDay问题
AcWing 605. Simple product (implemented in C language)
[sciter]:sciter如何使用i18实现桌面应用多语言切换及其利弊
【C语言】结构体嵌套二级指针的使用
day33 js笔记 事件(下)2021.09.28
Data analysis learning notes
MapReduce项目案例1
[sciter]: how sciter uses i18 to realize multi language switching of desktop applications and its advantages and disadvantages
2022 open source software security status report: over 41% of enterprises do not have enough confidence in open source security
Why do many people want to change careers as programmers, while some programmers want to change careers as others?
What is the main chain system?
Day36 JS notes ecma6 syntax 2021.10.09
Zero basic C language (I)
來吧元宇宙,果然這熱度一時半會兒過不去了
Multi dimensional monitoring: the data base of intelligent monitoring
【无标题】虚拟机vmnet0找不到且报错:没有未桥接的主机网络适配器
ArrayList源码解析
Characteristics of solar wireless LED display
AcWing 604. Area of circle (implemented in C language)