Closed eirv closed 1 year ago
你是怎么验证“没被hook”的?
12-20 17:35:44.973 13042 13042 E LSPTestNative: Symbol '_ZN3art11ClassLinker22FixupStaticTrampolinesEPNS_6ThreadENS_6ObjPtrINS_6mirror5ClassEEE' not found in elf libart.so 12-20 17:35:44.976 13042 13042 E LSPTestNative: Symbol '_ZN3art11ClassLinker14RegisterNativeEPNS_6ThreadEPNS_9ArtMethodEPKv' not found in elf libart.so 12-20 17:35:44.978 13042 13042 E LSPTestNative: Symbol '_ZN3art9ArtMethod14RegisterNativeEPKv' not found in elf libart.so 12-20 17:35:44.981 13042 13042 E LSPTestNative: Symbol '_ZN3art11ClassLinker16UnregisterNativeEPNS_6ThreadEPNS_9ArtMethodE' not found in elf libart.so 12-20 17:35:44.985 13042 13042 E LSPTestNative: Symbol '_ZN3art6mirror5Class9SetStatusENS_6HandleIS1_EENS_11ClassStatusEPNS_6ThreadE' not found in elf libart.so
?
你是怎么验证“没被hook”的?
target方法返回值永远是-1,callback方法返回值永远是Integer.MIN_VALUE,hook之后仅在这台设备返回值为-1,其他设备返回值为Integer.MIN_VALUE,hook前后调用Thread#dumpStack仅在这台设备没有出现callback方法
从上面看到几个符号没找到,所以导致 static 方法 hook 不上。日志上也没看到 static 方法第一次调用时候会有的 fixup trampoline 的日志。你可以试试改成非 static 方法,如果可以那就证明是那几个符号的问题。
非static方法也还是无法hook logcat如下。:
--------- beginning of main
12-21 11:19:39.962 5158 5158 I zygote64: Late-enabling -Xcheck:jni
12-21 11:19:40.124 5158 5158 I LoadedApk: No resource references to update in package androidzte
12-21 11:19:40.153 5158 5158 I Settings: Requested generation tracker for type: /global in package:com.example.lsptest and user:0
12-21 11:19:40.158 5158 5158 I Settings: Received generation tracker for type:/global in package:com.example.lsptest and user:0 with index:0
12-21 11:19:40.243 5158 5158 W asset : addOverlayPath: packagePath: /data/resource-cache/theme/default_theme_01/androidzte/, idmapPath Path: /data/resource-cache/theme/default_theme_01/androidzte/idmap, resApkPath /data/resource-cache/theme/default_theme_01/androidzte/resources.apk
12-21 11:19:40.316 5158 5158 E BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /data/resource-cache/cache/icon-cache/icon/icon/com_example_lsptest.png (No such file or directory)
12-21 11:19:40.332 5158 5158 I IconPackHelper: translateBitMap bmp = android.graphics.Bitmap@9e32bd3
12-21 11:19:40.366 5158 5158 I IconPackHelper: light = 54 start = 66 end = 100
12-21 11:19:40.367 5158 5158 I IconPackHelper: light = 54 start = 0 end = 65
12-21 11:19:40.377 5158 5158 I IconPackHelper: translateBitMap bgBmp = android.graphics.Bitmap@5462310 maskBmp = android.graphics.Bitmap@bebfe09
12-21 11:19:40.378 5158 5158 I IconPackHelper: translateBitMap overlap = 0
12-21 11:19:40.418 5158 5158 W System.err: java.lang.Exception: Stack trace
12-21 11:19:40.419 5158 5158 W System.err: at java.lang.Thread.dumpStack(Thread.java:1348)
12-21 11:19:40.420 5158 5158 W System.err: at com.example.lsptest.MainActivity.test(MainActivity.java:36)
12-21 11:19:40.420 5158 5158 W System.err: at com.example.lsptest.MainActivity.onCreate(MainActivity.java:17)
12-21 11:19:40.420 5158 5158 W System.err: at android.app.Activity.performCreate(Activity.java:7023)
12-21 11:19:40.421 5158 5158 W System.err: at android.app.Activity.performCreate(Activity.java:7014)
12-21 11:19:40.422 5158 5158 W System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
12-21 11:19:40.422 5158 5158 W System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2762)
12-21 11:19:40.423 5158 5158 W System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2889)
12-21 11:19:40.423 5158 5158 W System.err: at android.app.ActivityThread.-wrap11(Unknown Source:0)
12-21 11:19:40.424 5158 5158 W System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1617)
12-21 11:19:40.424 5158 5158 W System.err: at android.os.Handler.dispatchMessage(Handler.java:106)
12-21 11:19:40.425 5158 5158 W System.err: at android.os.Looper.loop(Looper.java:164)
12-21 11:19:40.425 5158 5158 W System.err: at android.app.ActivityThread.main(ActivityThread.java:6542)
12-21 11:19:40.425 5158 5158 W System.err: at java.lang.reflect.Method.invoke(Native Method)
12-21 11:19:40.426 5158 5158 W System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
12-21 11:19:40.426 5158 5158 W System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:857)
12-21 11:19:40.426 5158 5158 I MainActivity: test = -1
12-21 11:19:40.427 5158 5158 I MainActivity: before hook
12-21 11:19:40.427 5158 5158 I MainActivity: target = public int com.example.lsptest.MainActivity.test()
12-21 11:19:40.431 5158 5158 W linker : "/data/app/com.example.lsptest-QVbJTma9570TF1yn-jzIRw==/lib/arm64/libc++_shared.so" unused DT entry: type 0x70000001 arg 0x0
12-21 11:19:40.449 5158 5158 D LSPlant : art_method.hpp:186#static bool lsplant::art::ArtMethod::Init(JNIEnv *, const lsplant::HookHandler): ArtMethod size: 48
12-21 11:19:40.449 5158 5158 D LSPlant : art_method.hpp:240#static bool lsplant::art::ArtMethod::Init(JNIEnv *, const lsplant::HookHandler): ArtMethod::declaring_class offset: 0
12-21 11:19:40.449 5158 5158 D LSPlant : art_method.hpp:241#static bool lsplant::art::ArtMethod::Init(JNIEnv *, const lsplant::HookHandler): ArtMethod::entrypoint offset: 40
12-21 11:19:40.449 5158 5158 D LSPlant : art_method.hpp:242#static bool lsplant::art::ArtMethod::Init(JNIEnv *, const lsplant::HookHandler): ArtMethod::data offset: 32
12-21 11:19:40.449 5158 5158 D LSPlant : art_method.hpp:243#static bool lsplant::art::ArtMethod::Init(JNIEnv *, const lsplant::HookHandler): ArtMethod::access_flags offset: 4
12-21 11:19:40.456 5158 5158 E LSPTestNative: Symbol '_ZN3art11ClassLinker22FixupStaticTrampolinesEPNS_6ThreadENS_6ObjPtrINS_6mirror5ClassEEE' not found in elf libart.so
12-21 11:19:40.460 5158 5158 E LSPTestNative: Symbol '_ZN3art11ClassLinker14RegisterNativeEPNS_6ThreadEPNS_9ArtMethodEPKv' not found in elf libart.so
12-21 11:19:40.462 5158 5158 E LSPTestNative: Symbol '_ZN3art9ArtMethod14RegisterNativeEPKv' not found in elf libart.so
12-21 11:19:40.465 5158 5158 E LSPTestNative: Symbol '_ZN3art11ClassLinker16UnregisterNativeEPNS_6ThreadEPNS_9ArtMethodE' not found in elf libart.so
12-21 11:19:40.470 5158 5158 E LSPTestNative: Symbol '_ZN3art6mirror5Class9SetStatusENS_6HandleIS1_EENS_11ClassStatusEPNS_6ThreadE' not found in elf libart.so
12-21 11:19:40.476 5158 5158 D LSPlant : runtime.hpp:75#static bool lsplant::art::Runtime::Init(const lsplant::HookHandler &): runtime instance = 0x7c31ebd600
12-21 11:19:40.477 5158 5158 D LSPTestNative: LSPlant-Init return true
12-21 11:19:40.497 5158 5158 V LSPlant : lsplant.cc:505#bool lsplant::(anonymous namespace)::DoHook(lsplant::art::ArtMethod *, lsplant::art::ArtMethod *, lsplant::art::ArtMethod *): Hooking: target = int com.example.lsptest.MainActivity.test()(0x7cb62b5080), hook = int LSPHooker_.test(java.lang.Object)(0x7cb3a8a180), backup = int LSPHooker_.backup(java.lang.Object)(0x7cb3a8a150)
12-21 11:19:40.497 5158 5158 V LSPlant : lsplant.cc:486#void *lsplant::(anonymous namespace)::GenerateTrampolineFor(art::ArtMethod *): trampoline: count = 0, address = 7cb43fe000, target = 7cb43fe000
12-21 11:19:40.497 5158 5158 V LSPlant : lsplant.cc:512#bool lsplant::(anonymous namespace)::DoHook(lsplant::art::ArtMethod *, lsplant::art::ArtMethod *, lsplant::art::ArtMethod *): Generated trampoline 0x7cb43fe000
12-21 11:19:40.497 5158 5158 V LSPlant : lsplant.cc:528#bool lsplant::(anonymous namespace)::DoHook(lsplant::art::ArtMethod *, lsplant::art::ArtMethod *, lsplant::art::ArtMethod *): Done hook: target(0x7cb62b5080:0xa080001) -> 0x7cb43fe000; backup(0x7cb3a8a150:0xa080002) -> 0x7c31c042b0; hook(0x7cb3a8a180:0x2080009) -> 0x7c31c042b0
12-21 11:19:40.498 5158 5158 I MainActivity: backup = public static int LSPHooker_.test()
12-21 11:19:40.499 5158 5158 I MainActivity: after hook
12-21 11:19:40.499 5158 5158 W System.err: java.lang.Exception: Stack trace
12-21 11:19:40.500 5158 5158 W System.err: at java.lang.Thread.dumpStack(Thread.java:1348)
12-21 11:19:40.501 5158 5158 W System.err: at com.example.lsptest.MainActivity.test(MainActivity.java:36)
12-21 11:19:40.501 5158 5158 W System.err: at com.example.lsptest.MainActivity.onCreate(MainActivity.java:31)
12-21 11:19:40.502 5158 5158 W System.err: at android.app.Activity.performCreate(Activity.java:7023)
12-21 11:19:40.502 5158 5158 W System.err: at android.app.Activity.performCreate(Activity.java:7014)
12-21 11:19:40.503 5158 5158 W System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
12-21 11:19:40.504 5158 5158 W System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2762)
12-21 11:19:40.505 5158 5158 W System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2889)
12-21 11:19:40.506 5158 5158 W System.err: at android.app.ActivityThread.-wrap11(Unknown Source:0)
12-21 11:19:40.506 5158 5158 W System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1617)
12-21 11:19:40.507 5158 5158 W System.err: at android.os.Handler.dispatchMessage(Handler.java:106)
12-21 11:19:40.508 5158 5158 W System.err: at android.os.Looper.loop(Looper.java:164)
12-21 11:19:40.508 5158 5158 W System.err: at android.app.ActivityThread.main(ActivityThread.java:6542)
12-21 11:19:40.509 5158 5158 W System.err: at java.lang.reflect.Method.invoke(Native Method)
12-21 11:19:40.510 5158 5158 W System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
12-21 11:19:40.510 5158 5158 W System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:857)
12-21 11:19:40.511 5158 5158 I MainActivity: test = -1
你编译的是 debug build?试试 release build。
你编译的是 debug build?试试 release build。
这次hook成功了
我们对 debug 编译有一定兼容,但是没保证一定能 hook 上。主要原因是 debugger 会把所有方法都给重设一遍导致我们的 hook 失效。我们会尝试在 debug 版本上阻止这个过程重设已经 hook 的方法,但是部分设备上可能会阻止失败,这属于预期行为,不会花太多时间修复。
具体阻止逻辑如下:
可能你的设备还需要加上 UpdateMethodsCodeForJavaDebuggable
的 hook。
我们对 debug 编译有一定兼容,但是没保证一定能 hook 上。主要原因是 debugger 会把所有方法都给重设一遍导致我们的 hook 失效。我们会尝试在 debug 版本上阻止这个过程重设已经 hook 的方法,但是部分设备上可能会阻止失败,这属于预期行为,不会花太多时间修复。
具体阻止逻辑如下:
可能你的设备还需要加上
UpdateMethodsCodeForJavaDebuggable
的 hook。
感谢
InlineHookFunType,InlineUnhookFunType,ArtSymbolResolver,ArtSymbolPrefixResolver均已实现且可正常使用,只在这台设备无法hook,其他设备都能正常hook LSPlant为最新一次提交debug版 logcat如下: