rovo89 / XposedBridge

The Java part of the Xposed framework.
3.91k stars 1.1k forks source link

Hook java.lang.reflection.Method->invoke() method. #106

Closed melonaerial closed 8 years ago

melonaerial commented 8 years ago

Hello. I'm trying to hook java.lang.reflection.Method->invoke() method with Xposed Framework v54 and using XposedBridge v53(as recommended in wiki) in Android 4.1.2 in Android Emulator. So hook is working, but what I've found and changing of behaviour for just simple application that need to load native library for it working. Here is the difference in logcat log

Without java.lang.reflection.Method->invoke() hook:

06-09 15:25:33.121 13655-13655/com.example.hellojni W/dalvikvm: Exception Ljava/lang/UnsatisfiedLinkError; thrown while initializing Lcom/example/hellojni/HelloJni; 06-09 15:25:33.121 13655-13655/com.example.hellojni W/dalvikvm: Class init failed in newInstance call (Lcom/example/hellojni/HelloJni;) 06-09 15:25:33.121 13655-13655/com.example.hellojni D/AndroidRuntime: Shutting down VM 06-09 15:25:33.121 13655-13655/com.example.hellojni W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x40a122a0) 06-09 15:25:33.131 13655-13655/com.example.hellojni E/AndroidRuntime: FATAL EXCEPTION: main java.lang.ExceptionInInitializerError at java.lang.Class.newInstanceImpl(Native Method) at java.lang.Class.newInstance(Class.java:1319) at android.app.Instrumentation.newActivity(Instrumentation.java:1053) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) at android.app.ActivityThread.access$600(ActivityThread.java:130) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4745) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.UnsatisfiedLinkError: Couldn't load hello-jni11: findLibrary returned null at java.lang.Runtime.loadLibrary(Runtime.java:365) at java.lang.System.loadLibrary(System.java:535) at com.example.hellojni.HelloJni.(HelloJni.java:71) at java.lang.Class.newInstanceImpl(Native Method)  at java.lang.Class.newInstance(Class.java:1319)  at android.app.Instrumentation.newActivity(Instrumentation.java:1053)  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)  at android.app.ActivityThread.access$600(ActivityThread.java:130)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)  at android.os.Handler.dispatchMessage(Handler.java:99)  at android.os.Looper.loop(Looper.java:137)  at android.app.ActivityThread.main(ActivityThread.java:4745)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:511)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)  at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)  at dalvik.system.NativeStart.main(Native Method) 

And this is with hook :

06-09 15:27:23.831 14236-14236/com.example.hellojni W/dalvikvm: Exception Ljava/lang/UnsatisfiedLinkError; thrown while initializing Lcom/example/hellojni/HelloJni; 06-09 15:27:23.851 14236-14236/com.example.hellojni W/dalvikvm: Class init failed in newInstance call (Lcom/example/hellojni/HelloJni;) 06-09 15:27:23.851 14236-14236/com.example.hellojni A/libc: Fatal signal 11 (SIGSEGV) at 0x00000006 (code=1), thread 14236 (xample.hellojni)

So, in first case Android show window with phrase "Unfortunately, HelloJni has stopped.". In second as you may suggested not. Program just couldn't find native library.

So, my question is: Is it possible to hook this reflection method and see the same behavior in both cases? And if yes, how to do that?

rovo89 commented 8 years ago

I'm trying to hook java.lang.reflection.Method->invoke() method

You're basically asking for trouble. I really wouldn't do that and can't support you here.

melonaerial commented 8 years ago

@rovo89 so it is really so hard technical issue for Xposed Framework ?

rovo89 commented 8 years ago

I don't know, but Xposed uses Method.invoke() (or at least same the low-level methods) itself. The crash may or may not be related to that, but as hooking such low-level methods is very risky anyway, I don't want to spend the time to analyse it. If you're willing to dig into it and find out why it crashes, I can see if it can be fixed.

melonaerial commented 8 years ago

@rovo89 ok, thanks. I'll try.

melonaerial commented 8 years ago

@rovo89 and one more question. You wiki said: "Note that the sources jar provided for this version doesn't match the actual implementation, it only makes the documentation available." If I will take this version of code https://github.com/rovo89/XposedBridge/commit/0b66b1498f8f767dc476a5d499adeb998e57e757 is it exactly the code for jar v53 ?

rovo89 commented 8 years ago

Yes - however you mentioned that you're using Xposed v54, so you should use the code of 8491c03bcd82fa4dd1b137fce38bbb28287ef2a2. The sources jar just provides the declarations, what matters for you is the implementation.

ghost commented 6 years ago

@melonaerial Can you share source code hook java.lang.reflection.Method->invoke() method? Thanks