Modify move ClassLoader.fetching detail function to YukiHookBridge

This commit is contained in:
2022-08-02 01:53:33 +08:00
parent c981999dbe
commit 509635b145
2 changed files with 23 additions and 19 deletions

View File

@@ -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 方法、构造方法

View File

@@ -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 支持状态
*