From 509635b1456a5a1e384d1c4402df525aa2c4add7 Mon Sep 17 00:00:00 2001 From: fankesyooni Date: Tue, 2 Aug 2022 01:53:33 +0800 Subject: [PATCH] Modify move ClassLoader.fetching detail function to YukiHookBridge --- .../yukihookapi/hook/param/PackageParam.kt | 20 +---------------- .../hook/xposed/bridge/YukiHookBridge.kt | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/param/PackageParam.kt b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/param/PackageParam.kt index 95f48733..52396578 100644 --- a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/param/PackageParam.kt +++ b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/param/PackageParam.kt @@ -45,19 +45,12 @@ import com.highcapable.yukihookapi.hook.core.YukiResourcesHookCreater import com.highcapable.yukihookapi.hook.entity.YukiBaseHooker import com.highcapable.yukihookapi.hook.factory.classOf import com.highcapable.yukihookapi.hook.factory.hasClass -import com.highcapable.yukihookapi.hook.log.yLoggerW import com.highcapable.yukihookapi.hook.param.type.HookEntryType -import com.highcapable.yukihookapi.hook.param.wrapper.HookParamWrapper import com.highcapable.yukihookapi.hook.param.wrapper.PackageParamWrapper -import com.highcapable.yukihookapi.hook.type.java.BooleanType -import com.highcapable.yukihookapi.hook.type.java.JavaClassLoader -import com.highcapable.yukihookapi.hook.type.java.StringType import com.highcapable.yukihookapi.hook.utils.value import com.highcapable.yukihookapi.hook.xposed.bridge.YukiHookBridge import com.highcapable.yukihookapi.hook.xposed.bridge.dummy.YukiModuleResources import com.highcapable.yukihookapi.hook.xposed.bridge.dummy.YukiResources -import com.highcapable.yukihookapi.hook.xposed.bridge.factory.YukiHookHelper -import com.highcapable.yukihookapi.hook.xposed.bridge.factory.YukiMemberHook import com.highcapable.yukihookapi.hook.xposed.channel.YukiHookDataChannel import com.highcapable.yukihookapi.hook.xposed.helper.YukiHookAppHelper import com.highcapable.yukihookapi.hook.xposed.prefs.YukiHookModulePrefs @@ -371,18 +364,7 @@ open class PackageParam internal constructor(@PublishedApi internal var wrapper: * - ❗这是一个实验性功能 - 一般情况下不会用到此方法 - 不保证不会发生错误 * @param result 回调 - ([Class] 实例对象,[Boolean] 是否 resolve) */ - fun ClassLoader.fetching(result: (clazz: Class<*>, resolve: Boolean) -> Unit) { - runCatching { - YukiHookHelper.hookMethod( - YukiHookHelper.findMethod(JavaClassLoader, name = "loadClass", StringType, BooleanType), - object : YukiMemberHook() { - override fun afterHookedMember(wrapper: HookParamWrapper) { - if (wrapper.instance?.javaClass?.name == this@fetching.javaClass.name) - (wrapper.result as? Class<*>?)?.also { result(it, wrapper.args?.get(1) as? Boolean ?: false) } - } - }) - }.onFailure { yLoggerW(msg = "Try to hook ClassLoader failed: $it") } - } + fun ClassLoader.fetching(result: (clazz: Class<*>, resolve: Boolean) -> Unit) = YukiHookBridge.hookClassLoader(loader = this, result) /** * Hook 方法、构造方法 diff --git a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/xposed/bridge/YukiHookBridge.kt b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/xposed/bridge/YukiHookBridge.kt index ab859421..39e0a3d4 100644 --- a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/xposed/bridge/YukiHookBridge.kt +++ b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/xposed/bridge/YukiHookBridge.kt @@ -41,12 +41,16 @@ import android.content.res.Resources import com.highcapable.yukihookapi.YukiHookAPI import com.highcapable.yukihookapi.annotation.YukiGenerateApi import com.highcapable.yukihookapi.hook.factory.hasClass +import com.highcapable.yukihookapi.hook.log.yLoggerW import com.highcapable.yukihookapi.hook.param.PackageParam import com.highcapable.yukihookapi.hook.param.type.HookEntryType import com.highcapable.yukihookapi.hook.param.wrapper.HookParamWrapper import com.highcapable.yukihookapi.hook.param.wrapper.PackageParamWrapper import com.highcapable.yukihookapi.hook.type.android.* +import com.highcapable.yukihookapi.hook.type.java.BooleanType import com.highcapable.yukihookapi.hook.type.java.IntType +import com.highcapable.yukihookapi.hook.type.java.JavaClassLoader +import com.highcapable.yukihookapi.hook.type.java.StringType import com.highcapable.yukihookapi.hook.xposed.bridge.dummy.YukiModuleResources import com.highcapable.yukihookapi.hook.xposed.bridge.dummy.YukiResources import com.highcapable.yukihookapi.hook.xposed.bridge.factory.YukiHookHelper @@ -323,6 +327,24 @@ object YukiHookBridge { dynamicModuleAppResources?.let { moduleAppResources = it } } + /** + * 监听并 Hook 当前 [ClassLoader] 的 [ClassLoader.loadClass] 方法 + * @param loader 当前 [ClassLoader] + * @param result 回调 - ([Class] 实例对象,[Boolean] 是否 resolve) + */ + internal fun hookClassLoader(loader: ClassLoader?, result: (clazz: Class<*>, resolve: Boolean) -> Unit) { + runCatching { + YukiHookHelper.hookMethod( + YukiHookHelper.findMethod(JavaClassLoader, name = "loadClass", StringType, BooleanType), + object : YukiMemberHook() { + override fun afterHookedMember(wrapper: HookParamWrapper) { + if (wrapper.instance?.javaClass?.name == loader?.javaClass?.name) + (wrapper.result as? Class<*>?)?.also { result(it, wrapper.args?.get(1) as? Boolean ?: false) } + } + }) + }.onFailure { yLoggerW(msg = "Try to hook ClassLoader failed: $it") } + } + /** * Hook 模块自身激活状态和 Resources Hook 支持状态 *