letNetCrypto = Java.use("com.izuiyou.network.NetCrypto"); let arg1 = "hello world"; let arg2 = "V I 50"; let ret = NetCrypto.sign(arg1, stringToBytes(arg2)); console.log("ret:"+ret); }) }
对于 libandroid.so 这样的系统库出现频率很高,Unidbg 提供了一种叫做虚拟模块的机制,提供对这些 SO 一部分函数的模拟实现。目前 Unidbg 提供了 libandroid.so、libjnigraphics.so、libmediandk.so 三个库的虚拟模块。使用它们相当简单,比如此处缺少 libandroid.so,只需要在早于目标 SO 加载的时机,添下面这行代码即可。
1
newAndroidModule(emulator, vm).register(memory);;
五、补 JNI 环境
运行代码,报错如下
1 2 3 4 5 6
JNIEnv->GetStaticMethodID(com/izuiyou/common/base/BaseApplication.getAppContext()Landroid/content/Context;) => 0x2157b33c was called from RX@0x1204da57[libnet_crypto.so]0x4da57 [20:30:08237] WARN [com.github.unidbg.linux.ARM32SyscallHandler] (ARM32SyscallHandler:538) - handleInterrupt intno=2, NR=-452987556, svcNumber=0x170, PC=unidbg@0xfffe0794, LR=RX@0x1204db2f[libnet_crypto.so]0x4db2f, syscall=null java.lang.UnsupportedOperationException: com/izuiyou/common/base/BaseApplication->getAppContext()Landroid/content/Context; at com.github.unidbg.linux.android.dvm.AbstractJni.callStaticObjectMethodV(AbstractJni.java:504) at com.github.unidbg.linux.android.dvm.AbstractJni.callStaticObjectMethodV(AbstractJni.java:438) at com.github.unidbg.linux.android.dvm.DvmMethod.callStaticObjectMethodV(DvmMethod.java:59)
java.lang.UnsupportedOperationException: com/izuiyou/common/base/BaseApplication->getAppContext()Landroid/content/Context; at com.github.unidbg.linux.android.dvm.AbstractJni.callStaticObjectMethodV(AbstractJni.java:503) at com.github.unidbg.linux.android.dvm.AbstractJni.callStaticObjectMethodV(AbstractJni.java:437)
privatestatic DvmClass getObjectType(VM vm, String serviceName) { switch (serviceName) { case TELEPHONY_SERVICE: return vm.resolveClass("android/telephony/TelephonyManager"); case WIFI_SERVICE: return vm.resolveClass("android/net/wifi/WifiManager"); case CONNECTIVITY_SERVICE: return vm.resolveClass("android/net/ConnectivityManager"); case ACCESSIBILITY_SERVICE: return vm.resolveClass("android/view/accessibility/AccessibilityManager"); case KEYGUARD_SERVICE: return vm.resolveClass("android/app/KeyguardManager"); case ACTIVITY_SERVICE: return vm.resolveClass("android/os/BinderProxy"); // android/app/ActivityManager case SENSOR_SERVICE: return vm.resolveClass("android/hardware/SensorManager"); case INPUT_METHOD_SERVICE: return vm.resolveClass("android/view/inputmethod/InputMethodManager"); case LOCATION_SERVICE: return vm.resolveClass("android/location/LocationManager"); case WINDOW_SERVICE: return vm.resolveClass("android/view/WindowManager"); case UI_MODE_SERVICE: return vm.resolveClass("android/app/UiModeManager"); case DISPLAY_SERVICE: return vm.resolveClass("android/hardware/display/DisplayManager"); case AUDIO_SERVICE: return vm.resolveClass("android/media/AudioManager"); default: thrownewBackendException("service failed: " + serviceName); } }
}
关于 AbstractJNI 可以做两点总结。
确实能让我们免于一些补环境的苦恼
相较于所有可能的 JNI 访问,它只是杯水车薪,补 JNI 环境这个步骤不可避免。
5.3 基本规范
回到最初的报错上
1 2 3 4 5 6 7 8 9 10 11
JNIEnv->FindClass(com/izuiyou/common/base/BaseApplication) was called from RX@0x4004da21[libnet_crypto.so]0x4da21 JNIEnv->GetStaticMethodID(com/izuiyou/common/base/BaseApplication.getAppContext()Landroid/content/Context;) => 0x2157b33c was called from RX@0x4004da57[libnet_crypto.so]0x4da57 [08:21:02 080] WARN [com.github.unidbg.linux.ARM32SyscallHandler] (ARM32SyscallHandler:532) - handleInterrupt intno=2, NR=-1073744548, svcNumber=0x170, PC=unidbg@0xfffe0794, LR=RX@0x4004db2f[libnet_crypto.so]0x4db2f, syscall=null java.lang.UnsupportedOperationException: com/izuiyou/common/base/BaseApplication->getAppContext()Landroid/content/Context; at com.github.unidbg.linux.android.dvm.AbstractJni.callStaticObjectMethodV(AbstractJni.java:503) at com.github.unidbg.linux.android.dvm.AbstractJni.callStaticObjectMethodV(AbstractJni.java:437) at com.github.unidbg.linux.android.dvm.DvmMethod.callStaticObjectMethodV(DvmMethod.java:64) at com.github.unidbg.linux.android.dvm.DalvikVM$113.handle(DalvikVM.java:1810) at com.github.unidbg.linux.ARM32SyscallHandler.hook(ARM32SyscallHandler.java:131) at com.github.unidbg.arm.backend.Unicorn2Backend$11.hook(Unicorn2Backend.java:347) at com.github.unidbg.arm.backend.unicorn.Unicorn$NewHook.onInterrupt(Unicorn.java:109)
JNIEnv->GetMethodID(java/io/File.getAbsolutePath()Ljava/lang/String;) => 0xb4553f34 was called from RX@0x1205af99[libnet_crypto.so]0x5af99 [21:18:39 172] WARN [com.github.unidbg.linux.ARM32SyscallHandler] (ARM32SyscallHandler:538) - handleInterrupt intno=2, NR=-452988524, svcNumber=0x11f, PC=unidbg@0xfffe0284, LR=RX@0x1204d563[libnet_crypto.so]0x4d563, syscall=null java.lang.ClassCastException: class java.lang.String cannot be cast to class java.io.File (java.lang.String and java.io.File are in module java.base of loader 'bootstrap') at com.github.unidbg.linux.android.dvm.AbstractJni.callObjectMethodV(AbstractJni.java:306) at com.izuiyou.network.NetCrypto.callObjectMethodV(NetCrypto.java:96)
JNIEnv->GetStaticMethodID(android/os/Debug.isDebuggerConnected()Z) => 0xd74c7fb4 was called from RX@0x40050c1f[libnet_crypto.so]0x50c1f [11:04:59 936] WARN [com.github.unidbg.linux.ARM32SyscallHandler] (ARM32SyscallHandler:532) - handleInterrupt intno=2, NR=-1073744460, svcNumber=0x173, PC=unidbg@0xfffe07c4, LR=RX@0x40050cf7[libnet_crypto.so]0x50cf7, syscall=null java.lang.UnsupportedOperationException: android/os/Debug->isDebuggerConnected()Z at com.github.unidbg.linux.android.dvm.AbstractJni.callStaticBooleanMethodV(AbstractJni.java:191) at com.github.unidbg.linux.android.dvm.AbstractJni.callStaticBooleanMethodV(AbstractJni.java:186) at com.github.unidbg.linux.android.dvm.DvmMethod.callStaticBooleanMethodV(DvmMethod.java:179) at com.github.unidbg.linux.android.dvm.DalvikVM$116.handle(DalvikVM.java:1884) at com.github.unidbg.linux.ARM32SyscallHandler.hook(ARM32SyscallHandler.java:131)
JNIEnv->GetStaticMethodID(android/os/Process.myPid()I) => 0xfb198f3e was called from RX@0x1205b3b5[libnet_crypto.so]0x5b3b5 [21:25:00006] WARN [com.github.unidbg.linux.ARM32SyscallHandler] (ARM32SyscallHandler:538) - handleInterrupt intno=2, NR=-452987428, svcNumber=0x17e, PC=unidbg@0xfffe0874, LR=RX@0x1205b4a7[libnet_crypto.so]0x5b4a7, syscall=null java.lang.UnsupportedOperationException: android/os/Process->myPid()I at com.github.unidbg.linux.android.dvm.AbstractJni.callStaticIntMethodV(AbstractJni.java:211) at com.github.unidbg.linux.android.dvm.AbstractJni.callStaticIntMethodV(AbstractJni.java:206) at com.github.unidbg.linux.android.dvm.DvmMethod.callStaticIntMethodV(DvmMethod.java:159)