Skip to content

Commit

Permalink
增加:记录使用系统api读取签名的调用堆栈;增加:阻止读取联系人
Browse files Browse the repository at this point in the history
  • Loading branch information
littleWhiteDuck committed Jan 29, 2023
1 parent ef32973 commit 8a655ca
Show file tree
Hide file tree
Showing 25 changed files with 510 additions and 275 deletions.
66 changes: 32 additions & 34 deletions hook/FieldHook.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package me.simpleHook.hook

import android.content.Context
import com.github.kyuubiran.ezxhelper.init.InitFields.appContext
import com.github.kyuubiran.ezxhelper.init.InitFields.ezXClassLoader
import com.github.kyuubiran.ezxhelper.utils.*
import com.google.gson.Gson
import de.robv.android.xposed.XC_MethodHook
Expand All @@ -13,128 +10,129 @@ import me.simpleHook.bean.LogBean
import me.simpleHook.constant.Constant
import me.simpleHook.hook.Tip.getTip
import me.simpleHook.hook.utils.*
import me.simpleHook.hook.utils.HookHelper.appClassLoader
import me.simpleHook.hook.utils.HookHelper.hostPackageName
import me.simpleHook.util.LanguageUtils
import me.simpleHook.util.log

object FieldHook {
/**
* @author littleWhiteDuck
* @param configBean 配置类
* @param packageName 目标应用包名
*/
@JvmStatic
fun hookStaticField(configBean: ConfigBean, packageName: String) {
fun hookStaticField(configBean: ConfigBean) {
configBean.apply {
if (className.isEmpty() && methodName.isEmpty() && params.isEmpty()) {
// 直接hook
hookStaticField(fieldClassName, ezXClassLoader, resultValues, fieldName)
hookStaticField(fieldClassName, resultValues, fieldName)
return
}
val hooker: Hooker = if (mode == Constant.HOOK_RECORD_STATIC_FIELD) {
{ recordStaticField(appContext, fieldClassName, packageName, fieldName) }
{ recordStaticField(fieldClassName, fieldName) }
} else {
{ hookStaticField(fieldClassName, ezXClassLoader, resultValues, fieldName) }
{ hookStaticField(fieldClassName, resultValues, fieldName) }
}
hookField(hooker, packageName)
hookField(hooker, hostPackageName)
}
}

private fun ConfigBean.hookField(
hooker: Hooker, packageName: String
) {
val isBeforeHook = hookPoint == "before"
try {
if (methodName == "*") {
findAllMethods(className) {
true
}.hook(hookPoint == "before", hooker)
}.hook(isBeforeHook, hooker)
} else if (params == "*") {
if (methodName == "<init>") {
hookAllConstructorAfter(className, hooker = hooker)
findAllConstructors(className) {
true
}.hook(isBeforeHook, hooker)
} else {
findAllMethods(className) {
name == methodName
}.hook(hookPoint == "before", hooker)
}.hook(isBeforeHook, hooker)
}
} else {
if (methodName == "<init>") {
findConstructor(className) {
isSearchConstructor(params)
}.hookAfter(hooker)
}.hook(isBeforeHook, hooker)
} else {
findMethod(className) {
name == methodName && isSearchMethod(params)
}.hook(hookPoint == "before", hooker)
}.hook(isBeforeHook, hooker)
}
}
} catch (e: NoSuchMethodError) {
LogUtil.noSuchMethod(
packageName, className, "$methodName($params)", e.stackTraceToString()
className, "$methodName($params)", e.stackTraceToString()
)
getTip("noSuchMethod").log(packageName)
XposedBridge.log(e.stackTraceToString())

} catch (e: NoSuchMethodException) {
LogUtil.noSuchMethod(
packageName, className, "$methodName($params)", e.stackTraceToString()
className, "$methodName($params)", e.stackTraceToString()
)
getTip("noSuchMethod").log(packageName)
XposedBridge.log(e.stackTraceToString())
} catch (e: XposedHelpers.ClassNotFoundError) {
LogUtil.notFoundClass(
packageName, className, "$methodName($params)", e.stackTraceToString()
className, "$methodName($params)", e.stackTraceToString()
)
getTip("notFoundClass").log(packageName)
XposedBridge.log(e.stackTraceToString())
} catch (e: ClassNotFoundException) {
LogUtil.notFoundClass(
packageName, className, "$methodName($params)", e.stackTraceToString()
className, "$methodName($params)", e.stackTraceToString()
)
getTip("notFoundClass").log(packageName)
XposedBridge.log(e.stackTraceToString())
}
}

private fun recordStaticField(
context: Context, fieldClassName: String, packageName: String, fieldName: String
fieldClassName: String, fieldName: String
) {
val type = if (LanguageUtils.isNotChinese()) "Static field" else "静态变量"
val hookClass = XposedHelpers.findClass(fieldClassName, context.classLoader)
val hookClass = XposedHelpers.findClass(fieldClassName, appClassLoader)
val result = XposedHelpers.getStaticObjectField(hookClass, fieldName)
val list = listOf(
getTip("className") + fieldClassName,
getTip("fieldName") + fieldName,
getTip("fieldValue") + result
)
val logBean = LogBean(type = type, other = list, packageName = packageName)
LogUtil.toLogMsg(Gson().toJson(logBean), packageName, type)
val logBean = LogBean(type = type, other = list, packageName = hostPackageName)
LogUtil.toLogMsg(Gson().toJson(logBean), type)
}

private fun hookStaticField(
fieldClassName: String, classLoader: ClassLoader, values: String, fieldName: String
fieldClassName: String, values: String, fieldName: String
) {
val clazz: Class<*> = XposedHelpers.findClass(fieldClassName, classLoader)
val clazz: Class<*> = XposedHelpers.findClass(fieldClassName, appClassLoader)
XposedHelpers.setStaticObjectField(clazz, fieldName, Type.getDataTypeValue(values))
}

@JvmStatic
fun hookInstanceField(
configBean: ConfigBean, packageName: String
configBean: ConfigBean
) {
configBean.apply {
val hooker: Hooker = if (mode == Constant.HOOK_RECORD_STATIC_FIELD) {
{ recordInstanceField(className, packageName, it, fieldName) }
val hooker: Hooker = if (mode == Constant.HOOK_RECORD_INSTANCE_FIELD) {
{ recordInstanceField(className, it, fieldName) }
} else {
{ hookInstanceField(it, resultValues, fieldName) }
}
hookField(hooker, packageName)
hookField(hooker, hostPackageName)
}
}

private fun recordInstanceField(
className: String,
packageName: String,
param: XC_MethodHook.MethodHookParam,
fieldName: String
className: String, param: XC_MethodHook.MethodHookParam, fieldName: String
) {
val type = if (LanguageUtils.isNotChinese()) "Instance field" else "实例变量"
val thisObj = param.thisObject
Expand All @@ -144,8 +142,8 @@ object FieldHook {
getTip("fieldName") + fieldName,
getTip("fieldValue") + result
)
val logBean = LogBean(type = type, other = list, packageName = packageName)
LogUtil.toLogMsg(Gson().toJson(logBean), packageName, type)
val logBean = LogBean(type = type, other = list, packageName = hostPackageName)
LogUtil.toLogMsg(Gson().toJson(logBean), type)
}

private fun hookInstanceField(
Expand Down
12 changes: 10 additions & 2 deletions hook/HookInit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import com.github.kyuubiran.ezxhelper.utils.findMethod
import com.github.kyuubiran.ezxhelper.utils.hookAfter
import com.github.kyuubiran.ezxhelper.utils.hookReturnConstant
import de.robv.android.xposed.IXposedHookLoadPackage
import de.robv.android.xposed.IXposedHookZygoteInit
import de.robv.android.xposed.callbacks.XC_LoadPackage
import me.simpleHook.BuildConfig
import me.simpleHook.hook.utils.HookHelper

class HookInit : IXposedHookLoadPackage {
class HookInit : IXposedHookLoadPackage, IXposedHookZygoteInit {

override fun handleLoadPackage(lpparam: XC_LoadPackage.LoadPackageParam) {
EzXHelperInit.initHandleLoadPackage(lpparam)
Expand All @@ -20,12 +22,18 @@ class HookInit : IXposedHookLoadPackage {
}.hookReturnConstant(true)

} else {
if (HookHelper.isAppContextInitialized) return
findMethod(Application::class.java) {
name == "attach"
}.hookAfter {
EzXHelperInit.initAppContext(context = it.args[0] as Context)
HookHelper.initFields(context = it.args[0] as Context, lpparam)
MainHook.startHook(lpparam.packageName)
}
}
}

override fun initZygote(startupParam: IXposedHookZygoteInit.StartupParam) {
EzXHelperInit.initZygote(startupParam)
}

}
Loading

0 comments on commit 8a655ca

Please sign in to comment.