From 2be4d4a2e80d226cd6923057e031e327323dc635 Mon Sep 17 00:00:00 2001 From: Fankesyooni Date: Fri, 4 Feb 2022 00:00:52 +0800 Subject: [PATCH] ... --- .../yukihookapi/demo/hook/HookMain.kt | 38 +---------- .../yukihookapi/demo/hook/HookSecond.kt | 43 +++++++++++++ .../yukihookapi/demo/hook/HookThird.kt | 64 +++++++++++++++++++ .../yukihookapi/hook/entity/YukiBaseHooker.kt | 46 +++++++++++++ .../yukihookapi/param/PackageParam.kt | 18 +++++- 5 files changed, 172 insertions(+), 37 deletions(-) create mode 100644 app/src/main/java/com/highcapable/yukihookapi/demo/hook/HookSecond.kt create mode 100644 app/src/main/java/com/highcapable/yukihookapi/demo/hook/HookThird.kt create mode 100644 yukihookapi/src/main/java/com/highcapable/yukihookapi/hook/entity/YukiBaseHooker.kt diff --git a/app/src/main/java/com/highcapable/yukihookapi/demo/hook/HookMain.kt b/app/src/main/java/com/highcapable/yukihookapi/demo/hook/HookMain.kt index 058dd296..832d0d67 100644 --- a/app/src/main/java/com/highcapable/yukihookapi/demo/hook/HookMain.kt +++ b/app/src/main/java/com/highcapable/yukihookapi/demo/hook/HookMain.kt @@ -29,17 +29,12 @@ package com.highcapable.yukihookapi.demo.hook -import android.app.AlertDialog -import android.widget.Toast import com.highcapable.yukihookapi.annotation.xposed.InjectYukiHookWithXposed import com.highcapable.yukihookapi.demo.BuildConfig import com.highcapable.yukihookapi.demo.InjectTest import com.highcapable.yukihookapi.demo.MainActivity import com.highcapable.yukihookapi.hook.factory.encase -import com.highcapable.yukihookapi.hook.factory.findMethod import com.highcapable.yukihookapi.hook.proxy.YukiHookXposedInitProxy -import com.highcapable.yukihookapi.hook.type.ActivityClass -import com.highcapable.yukihookapi.hook.type.BundleClass import com.highcapable.yukihookapi.hook.type.StringType @InjectYukiHookWithXposed @@ -72,37 +67,8 @@ class HookMain : YukiHookXposedInitProxy { beforeHook { args().set("构造方法已被 Hook 成功") } } } - findClass(name = "$packageName.InjectTestName").hook { - injectMember { - constructor { param(StringType) } - beforeHook { args().set("构造方法已被 Hook 成功 [2]") } - } - } - } - loadApp(name = "com.android.browser") { - ActivityClass.hook { - injectMember { - method { - name = "onCreate" - param(BundleClass) - } - afterHook { - AlertDialog.Builder(instance()) - .setCancelable(false) - .setTitle("测试 Hook") - .setMessage("Hook 已成功") - .setPositiveButton("OK") { _, _ -> - Toast.makeText(instance(), "Hook Success", Toast.LENGTH_SHORT).show() - }.show() - } - } - injectMember { - member = hookClass.findMethod(name = "onStart") - afterHook { - Toast.makeText(instance(), "手动 Hook", Toast.LENGTH_SHORT).show() - } - } - } + loadHooker(hooker = HookSecond()) } + loadApp(name = "com.android.browser", hooker = HookThird()) } } \ No newline at end of file diff --git a/app/src/main/java/com/highcapable/yukihookapi/demo/hook/HookSecond.kt b/app/src/main/java/com/highcapable/yukihookapi/demo/hook/HookSecond.kt new file mode 100644 index 00000000..bfb1f686 --- /dev/null +++ b/app/src/main/java/com/highcapable/yukihookapi/demo/hook/HookSecond.kt @@ -0,0 +1,43 @@ +/** + * 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/3. + */ +package com.highcapable.yukihookapi.demo.hook + +import com.highcapable.yukihookapi.hook.entity.YukiBaseHooker +import com.highcapable.yukihookapi.hook.type.StringType + +// for test +class HookSecond : YukiBaseHooker() { + + override fun onHook() = + findClass(name = "$packageName.InjectTestName").hook { + injectMember { + constructor { param(StringType) } + beforeHook { args().set("构造方法已被 Hook 成功 [2]") } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/highcapable/yukihookapi/demo/hook/HookThird.kt b/app/src/main/java/com/highcapable/yukihookapi/demo/hook/HookThird.kt new file mode 100644 index 00000000..774170d4 --- /dev/null +++ b/app/src/main/java/com/highcapable/yukihookapi/demo/hook/HookThird.kt @@ -0,0 +1,64 @@ +/** + * 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/3. + */ +package com.highcapable.yukihookapi.demo.hook + +import android.app.AlertDialog +import android.widget.Toast +import com.highcapable.yukihookapi.hook.entity.YukiBaseHooker +import com.highcapable.yukihookapi.hook.factory.findMethod +import com.highcapable.yukihookapi.hook.type.ActivityClass +import com.highcapable.yukihookapi.hook.type.BundleClass + +// for test +class HookThird : YukiBaseHooker() { + + override fun onHook() = + ActivityClass.hook { + injectMember { + method { + name = "onCreate" + param(BundleClass) + } + afterHook { + AlertDialog.Builder(instance()) + .setCancelable(false) + .setTitle("测试 Hook") + .setMessage("Hook 已成功") + .setPositiveButton("OK") { _, _ -> + Toast.makeText(instance(), "Hook Success", Toast.LENGTH_SHORT).show() + }.show() + } + } + injectMember { + member = hookClass.findMethod(name = "onStart") + afterHook { + Toast.makeText(instance(), "手动 Hook", Toast.LENGTH_SHORT).show() + } + } + } +} \ No newline at end of file diff --git a/yukihookapi/src/main/java/com/highcapable/yukihookapi/hook/entity/YukiBaseHooker.kt b/yukihookapi/src/main/java/com/highcapable/yukihookapi/hook/entity/YukiBaseHooker.kt new file mode 100644 index 00000000..76677696 --- /dev/null +++ b/yukihookapi/src/main/java/com/highcapable/yukihookapi/hook/entity/YukiBaseHooker.kt @@ -0,0 +1,46 @@ +/** + * 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/3. + */ +package com.highcapable.yukihookapi.hook.entity + +import com.highcapable.yukihookapi.hook.proxy.YukiHookXposedInitProxy +import com.highcapable.yukihookapi.param.PackageParam + +/** + * YukiHook 的子类实现 + * + * 你可以在 [YukiHookXposedInitProxy] 的 [YukiHookXposedInitProxy.onHook] 中实现如下用法 + * + * 调用 [PackageParam.loadApp] loadApp(name = "包名", hooker = 继承于此类的自定义 Hooker) + * + * 详情请参考 https://github.com/fankes/YukiHookAPI/wiki + */ +abstract class YukiBaseHooker : PackageParam() { + + /** 子类 Hook 开始 */ + abstract fun onHook() +} \ No newline at end of file diff --git a/yukihookapi/src/main/java/com/highcapable/yukihookapi/param/PackageParam.kt b/yukihookapi/src/main/java/com/highcapable/yukihookapi/param/PackageParam.kt index 9599e6f6..bdababdd 100644 --- a/yukihookapi/src/main/java/com/highcapable/yukihookapi/param/PackageParam.kt +++ b/yukihookapi/src/main/java/com/highcapable/yukihookapi/param/PackageParam.kt @@ -31,6 +31,7 @@ package com.highcapable.yukihookapi.param import android.content.pm.ApplicationInfo import com.highcapable.yukihookapi.hook.core.YukiHookCreater +import com.highcapable.yukihookapi.hook.entity.YukiBaseHooker import de.robv.android.xposed.callbacks.XC_LoadPackage /** @@ -42,7 +43,7 @@ import de.robv.android.xposed.callbacks.XC_LoadPackage * @param baseParam 对接 Xposed API 的 [XC_LoadPackage.LoadPackageParam] - 默认空 * @param customParam 自定义装载类 - 默认空 */ -class PackageParam( +open class PackageParam( private val baseParam: XC_LoadPackage.LoadPackageParam? = null, private val customParam: CustomParam? = null ) { @@ -93,6 +94,21 @@ class PackageParam( if (packageName == name) initiate(this) } + /** + * 装载并 Hook 指定包名的 APP + * @param name 包名 + * @param hooker Hook 子类 + */ + fun loadApp(name: String, hooker: YukiBaseHooker) { + if (packageName == name) hooker.onHook() + } + + /** + * 装载 Hook 子类 + * @param hooker Hook 子类 + */ + fun loadHooker(hooker: YukiBaseHooker) = hooker.onHook() + /** * 将目标 [Class] 绑定到 [appClassLoader] *