mirror of
https://github.com/HighCapable/YukiHookAPI.git
synced 2025-09-04 01:35:17 +08:00
...
This commit is contained in:
@@ -29,11 +29,34 @@ package com.highcapable.yukihookapi.demo
|
|||||||
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
|
import com.highcapable.yukihookapi.hook.YukiHook
|
||||||
|
import com.highcapable.yukihookapi.hook.factory.yukiHook
|
||||||
|
|
||||||
class MainActivity : AppCompatActivity() {
|
class MainActivity : AppCompatActivity() {
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
setContentView(R.layout.activity_main)
|
setContentView(R.layout.activity_main)
|
||||||
|
YukiHook.encase {
|
||||||
|
optApp(name = "android") {
|
||||||
|
classOf(name = "a.b.c").hook {
|
||||||
|
|
||||||
|
}
|
||||||
|
classOf(name = "a.b.c").hook {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
optApp(name = "bndroid") {
|
||||||
|
classOf(name = "a.b.c").hook {
|
||||||
|
|
||||||
|
}
|
||||||
|
classOf(name = "a.b.c").hook {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
yukiHook {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -1,6 +1,6 @@
|
|||||||
package com.highcapable.yukihookapi.demo
|
package com.highcapable.yukihookapi.demo
|
||||||
|
|
||||||
import com.highcapable.yukihookapi.hook.init.YukiHookLoadPackage
|
import com.highcapable.yukihookapi.hook.xposed.YukiHookLoadPackage
|
||||||
|
|
||||||
class Yuu : YukiHookLoadPackage() {
|
class Yuu : YukiHookLoadPackage() {
|
||||||
|
|
||||||
|
@@ -1 +1 @@
|
|||||||
com.highcapable.yukihookapi.hook.init.YukiHookLoadPackage
|
com.highcapable.yukihookapi.hook.xposed.YukiHookLoadPackage
|
@@ -0,0 +1,75 @@
|
|||||||
|
/**
|
||||||
|
* MIT License
|
||||||
|
*
|
||||||
|
* Copyright (C) 2022 HighCapable
|
||||||
|
*
|
||||||
|
* This file is part of YukiHookAPI.
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
* This file is Created by fankes on 2022/2/2.
|
||||||
|
*/
|
||||||
|
@file:Suppress("MemberVisibilityCanBePrivate", "unused")
|
||||||
|
|
||||||
|
package com.highcapable.yukihookapi.hook
|
||||||
|
|
||||||
|
import android.content.pm.ApplicationInfo
|
||||||
|
import com.highcapable.yukihookapi.hook.YukiHook.encase
|
||||||
|
import com.highcapable.yukihookapi.param.CustomParam
|
||||||
|
import com.highcapable.yukihookapi.param.PackageParam
|
||||||
|
|
||||||
|
/**
|
||||||
|
* YukiHook 的装载 API 调用类
|
||||||
|
* 可以实现作为模块装载和自定义 Hook 装载两种方式
|
||||||
|
* 模块装载方式已经自动对接 Xposed API - 可直接调用 [encase] 完成操作
|
||||||
|
*/
|
||||||
|
object YukiHook {
|
||||||
|
|
||||||
|
/** Xposed Hook API 方法体回调 */
|
||||||
|
internal var packageParamCallback: (PackageParam.() -> Unit)? = null
|
||||||
|
|
||||||
|
/** YukiHook 的 API 只能装载一次 */
|
||||||
|
private var isLoaded = false
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自身作为模块装载调用入口方法 - Xposed API
|
||||||
|
* ⚠️ 注意:只能装载一次
|
||||||
|
* @param initiate Hook 方法体
|
||||||
|
* @throws IllegalStateException 重复调用会抛出异常
|
||||||
|
*/
|
||||||
|
fun encase(initiate: PackageParam.() -> Unit) {
|
||||||
|
if (isLoaded) error("YukiHook API already loaded")
|
||||||
|
isLoaded = true
|
||||||
|
packageParamCallback = initiate
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自定义 Hook 方法装载入口
|
||||||
|
* @param classLoader [ClassLoader]
|
||||||
|
* @param packageName 包名
|
||||||
|
* @param appInfo [ApplicationInfo]
|
||||||
|
* @param initiate Hook 方法体
|
||||||
|
*/
|
||||||
|
fun encase(
|
||||||
|
classLoader: ClassLoader,
|
||||||
|
packageName: String,
|
||||||
|
appInfo: ApplicationInfo,
|
||||||
|
initiate: PackageParam.() -> Unit
|
||||||
|
) = initiate.invoke(PackageParam(customInstance = CustomParam(classLoader, appInfo, packageName)))
|
||||||
|
}
|
@@ -25,7 +25,7 @@
|
|||||||
*
|
*
|
||||||
* This file is Created by fankes on 2022/2/2.
|
* This file is Created by fankes on 2022/2/2.
|
||||||
*/
|
*/
|
||||||
package com.highcapable.yukihookapi.hook
|
package com.highcapable.yukihookapi.hook.core
|
||||||
|
|
||||||
import com.highcapable.yukihookapi.param.HookParam
|
import com.highcapable.yukihookapi.param.HookParam
|
||||||
import com.highcapable.yukihookapi.param.PackageParam
|
import com.highcapable.yukihookapi.param.PackageParam
|
@@ -25,28 +25,31 @@
|
|||||||
*
|
*
|
||||||
* This file is Created by fankes on 2022/2/2.
|
* This file is Created by fankes on 2022/2/2.
|
||||||
*/
|
*/
|
||||||
package com.highcapable.yukihookapi.hook.helper
|
@file:Suppress("unused")
|
||||||
|
|
||||||
|
package com.highcapable.yukihookapi.hook.factory
|
||||||
|
|
||||||
|
import android.content.pm.ApplicationInfo
|
||||||
|
import com.highcapable.yukihookapi.hook.YukiHook
|
||||||
import com.highcapable.yukihookapi.param.PackageParam
|
import com.highcapable.yukihookapi.param.PackageParam
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* YukiHook 的装载 API 调用类
|
* Lambda 方式装载 [YukiHook]
|
||||||
* 可以实现作为模块装载和 Hook 自身 APP 两种方式
|
* @param initiate Hook 方法体
|
||||||
*/
|
*/
|
||||||
object YukiHookHelper {
|
fun yukiHook(initiate: PackageParam.() -> Unit) = YukiHook.encase(initiate)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 自身作为模块装载调用入口方法
|
* Lambda 方式装载 [YukiHook]
|
||||||
* @param initiate Hook 方法体
|
* 自定义 Hook 方法装载入口
|
||||||
*/
|
* @param classLoader [ClassLoader]
|
||||||
fun onHookApp(initiate: PackageParam.() -> Unit) {
|
* @param packageName 包名
|
||||||
|
* @param appInfo [ApplicationInfo]
|
||||||
}
|
* @param initiate Hook 方法体
|
||||||
|
*/
|
||||||
/**
|
fun yukiHook(
|
||||||
* 作为侵入式 Hook 自身装载调用入口方法
|
classLoader: ClassLoader,
|
||||||
* 正在开发敬请期待。
|
packageName: String,
|
||||||
* @param initiate Hook 方法体
|
appInfo: ApplicationInfo,
|
||||||
*/
|
initiate: PackageParam.() -> Unit
|
||||||
fun onHookSelf(initiate: PackageParam.() -> Unit) {}
|
) = YukiHook.encase(classLoader, packageName, appInfo, initiate)
|
||||||
}
|
|
@@ -25,22 +25,24 @@
|
|||||||
*
|
*
|
||||||
* This file is Created by fankes on 2022/2/2.
|
* This file is Created by fankes on 2022/2/2.
|
||||||
*/
|
*/
|
||||||
package com.highcapable.yukihookapi.hook.init
|
package com.highcapable.yukihookapi.hook.xposed
|
||||||
|
|
||||||
import androidx.annotation.Keep
|
import androidx.annotation.Keep
|
||||||
import com.highcapable.yukihookapi.hook.helper.YukiHookHelper
|
import com.highcapable.yukihookapi.hook.YukiHook
|
||||||
|
import com.highcapable.yukihookapi.param.PackageParam
|
||||||
import de.robv.android.xposed.IXposedHookLoadPackage
|
import de.robv.android.xposed.IXposedHookLoadPackage
|
||||||
import de.robv.android.xposed.callbacks.XC_LoadPackage
|
import de.robv.android.xposed.callbacks.XC_LoadPackage
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 接管 Xposed 的 [IXposedHookLoadPackage] 入口
|
* 接管 Xposed 的 [IXposedHookLoadPackage] 入口
|
||||||
* 你可以使用 [YukiHookHelper] 来监听模块开始装载
|
* 你可以使用 [YukiHook] 来监听模块开始装载
|
||||||
*/
|
*/
|
||||||
@Keep
|
@Keep
|
||||||
class YukiHookLoadPackage : IXposedHookLoadPackage {
|
class YukiHookLoadPackage : IXposedHookLoadPackage {
|
||||||
|
|
||||||
override fun handleLoadPackage(lpparam: XC_LoadPackage.LoadPackageParam?) {
|
override fun handleLoadPackage(lpparam: XC_LoadPackage.LoadPackageParam?) {
|
||||||
if (lpparam == null) return
|
if (lpparam == null) return
|
||||||
|
/** 设置装载回调 */
|
||||||
|
YukiHook.packageParamCallback?.invoke(PackageParam(lpparam))
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -25,8 +25,14 @@
|
|||||||
*
|
*
|
||||||
* This file is Created by fankes on 2022/2/2.
|
* This file is Created by fankes on 2022/2/2.
|
||||||
*/
|
*/
|
||||||
package com.highcapable.yukihookapi.hook.factory
|
package com.highcapable.yukihookapi.param
|
||||||
|
|
||||||
import com.highcapable.yukihookapi.hook.YukiHookCreater
|
import android.content.pm.ApplicationInfo
|
||||||
|
|
||||||
fun Class<*>.hook(it: YukiHookCreater.() -> Unit) = YukiHookCreater().apply(it).hook()
|
/**
|
||||||
|
* 自定义 [PackageParam] 的装载入口置换类
|
||||||
|
* @param appClassLoader APP [ClassLoader]
|
||||||
|
* @param appInfo APP [ApplicationInfo]
|
||||||
|
* @param packageName 包名
|
||||||
|
*/
|
||||||
|
class CustomParam(var appClassLoader: ClassLoader, var appInfo: ApplicationInfo, var packageName: String)
|
@@ -1,35 +1,63 @@
|
|||||||
|
/**
|
||||||
|
* MIT License
|
||||||
|
*
|
||||||
|
* Copyright (C) 2022 HighCapable
|
||||||
|
*
|
||||||
|
* This file is part of YukiHookAPI.
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
* This file is Created by fankes on 2022/2/2.
|
||||||
|
*/
|
||||||
@file:Suppress("unused", "MemberVisibilityCanBePrivate")
|
@file:Suppress("unused", "MemberVisibilityCanBePrivate")
|
||||||
|
|
||||||
package com.highcapable.yukihookapi.param
|
package com.highcapable.yukihookapi.param
|
||||||
|
|
||||||
import android.content.pm.ApplicationInfo
|
import android.content.pm.ApplicationInfo
|
||||||
|
import com.highcapable.yukihookapi.hook.core.YukiHookCreater
|
||||||
import de.robv.android.xposed.callbacks.XC_LoadPackage
|
import de.robv.android.xposed.callbacks.XC_LoadPackage
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 装载 Hook 的目标 APP 入口对象实现类
|
* 装载 Hook 的目标 APP 入口对象实现类
|
||||||
* 如果是侵入式 Hook 自身 APP 可将参数 [instance] 置空获得当前类的 [ClassLoader]
|
* 如果是侵入式 Hook 自身 APP 可将参数 [instance] 置空获得当前类的 [ClassLoader]
|
||||||
* @param instance 对接 Xposed API 的 [XC_LoadPackage.LoadPackageParam]
|
* @param instance 对接 Xposed API 的 [XC_LoadPackage.LoadPackageParam] - 默认空
|
||||||
|
* @param customInstance 自定义装载类 - 默认空
|
||||||
*/
|
*/
|
||||||
class PackageParam(private val instance: XC_LoadPackage.LoadPackageParam? = null) {
|
class PackageParam(
|
||||||
|
private val instance: XC_LoadPackage.LoadPackageParam? = null,
|
||||||
/** 当前 classLoader */
|
private val customInstance: CustomParam? = null
|
||||||
private var privateClassLoader = instance?.classLoader ?: javaClass.classLoader
|
) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取、设置当前 APP 的 [ClassLoader]
|
* 获取当前 APP 的 [ClassLoader]
|
||||||
* @return [ClassLoader]
|
* @return [ClassLoader]
|
||||||
|
* @throws IllegalStateException 如果 ClassLoader 是空的
|
||||||
*/
|
*/
|
||||||
var appClassLoader: ClassLoader
|
val appClassLoader
|
||||||
get() = privateClassLoader
|
get() = instance?.classLoader ?: customInstance?.appClassLoader ?: javaClass.classLoader
|
||||||
set(value) {
|
?: error("PackageParam ClassLoader is null")
|
||||||
privateClassLoader = value
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取当前 APP 的 [ApplicationInfo]
|
* 获取当前 APP 的 [ApplicationInfo]
|
||||||
* @return [ApplicationInfo]
|
* @return [ApplicationInfo]
|
||||||
*/
|
*/
|
||||||
val appInfo get() = instance?.appInfo ?: ApplicationInfo()
|
val appInfo get() = instance?.appInfo ?: customInstance?.appInfo ?: ApplicationInfo()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取当前 APP 的进程名称
|
* 获取当前 APP 的进程名称
|
||||||
@@ -42,11 +70,35 @@ class PackageParam(private val instance: XC_LoadPackage.LoadPackageParam? = null
|
|||||||
* 获取当前 APP 的包名
|
* 获取当前 APP 的包名
|
||||||
* @return [String]
|
* @return [String]
|
||||||
*/
|
*/
|
||||||
val packageName get() = instance?.packageName ?: ""
|
val packageName get() = instance?.packageName ?: customInstance?.packageName ?: ""
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取当前 APP 是否为第一个 Application
|
* 获取当前 APP 是否为第一个 Application
|
||||||
* @return [Boolean]
|
* @return [Boolean]
|
||||||
*/
|
*/
|
||||||
val isFirstApplication get() = instance?.isFirstApplication ?: true
|
val isFirstApplication get() = instance?.isFirstApplication ?: true
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hook 指定包名的 APP
|
||||||
|
* @param name 包名
|
||||||
|
* @param initiate 方法体
|
||||||
|
*/
|
||||||
|
fun optApp(name: String, initiate: PackageParam.() -> Unit) {
|
||||||
|
if (packageName == name) initiate(this)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过字符串装载 [Class]
|
||||||
|
* @param name 类名
|
||||||
|
* @return [Class]
|
||||||
|
* @throws NoClassDefFoundError 如果找不到类会报错
|
||||||
|
*/
|
||||||
|
fun classOf(name: String): Class<*> = appClassLoader.loadClass(name)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hook 方法、构造类
|
||||||
|
* @param initiate 方法体
|
||||||
|
*/
|
||||||
|
fun Class<*>.hook(initiate: YukiHookCreater.() -> Unit) =
|
||||||
|
YukiHookCreater(param = this@PackageParam).apply(initiate).hook()
|
||||||
}
|
}
|
Reference in New Issue
Block a user