From 30315d9bf5024f2878438ec120d42b17292683e8 Mon Sep 17 00:00:00 2001 From: hax0r31337 Date: Wed, 12 Oct 2022 22:21:24 +0800 Subject: [PATCH] better & fixup hook --- .../yuugiri/hutil/obfuscation/AbstractObfuscationMap.kt | 5 +++++ .../me/yuugiri/hutil/obfuscation/SimpleObfuscationMap.kt | 2 ++ .../me/yuugiri/hutil/processor/hook/HookInsnPoint.kt | 9 ++++++--- .../yuugiri/hutil/processor/hook/MethodHookProcessor.kt | 2 +- .../yuugiri/hutil/processor/hook/point/HookPointField.kt | 9 ++++++--- .../hutil/processor/hook/point/HookPointInvoke.kt | 9 ++++++--- src/main/kotlin/me/yuugiri/hutil/util/ByteCodeUtil.kt | 1 + 7 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/me/yuugiri/hutil/obfuscation/AbstractObfuscationMap.kt b/src/main/kotlin/me/yuugiri/hutil/obfuscation/AbstractObfuscationMap.kt index c97186f..b90567d 100644 --- a/src/main/kotlin/me/yuugiri/hutil/obfuscation/AbstractObfuscationMap.kt +++ b/src/main/kotlin/me/yuugiri/hutil/obfuscation/AbstractObfuscationMap.kt @@ -11,6 +11,8 @@ abstract class AbstractObfuscationMap { */ abstract fun mapClass(name: String): ClassObfuscationRecord? + abstract fun reverseMapClass(name: String): ClassObfuscationRecord? + /** * @return null when no record found */ @@ -50,6 +52,9 @@ abstract class AbstractObfuscationMap { fun classObfuscationRecord(obfuscationMap: AbstractObfuscationMap?, name: String) = obfuscationMap?.mapClass(name) ?: ClassObfuscationRecord(name, name) + fun classObfuscationRecordReverse(obfuscationMap: AbstractObfuscationMap?, name: String) = + obfuscationMap?.reverseMapClass(name) ?: ClassObfuscationRecord(name, name) + fun fieldObfuscationRecord(obfuscationMap: AbstractObfuscationMap?, owner: String, field: FieldNode) = fieldObfuscationRecord(obfuscationMap, owner, field.name) diff --git a/src/main/kotlin/me/yuugiri/hutil/obfuscation/SimpleObfuscationMap.kt b/src/main/kotlin/me/yuugiri/hutil/obfuscation/SimpleObfuscationMap.kt index 8e22570..44c5a42 100644 --- a/src/main/kotlin/me/yuugiri/hutil/obfuscation/SimpleObfuscationMap.kt +++ b/src/main/kotlin/me/yuugiri/hutil/obfuscation/SimpleObfuscationMap.kt @@ -23,6 +23,8 @@ open class SimpleObfuscationMap : AbstractObfuscationMap() { override fun mapClass(name: String) = classRecords[name] + override fun reverseMapClass(name: String) = classRecords.values.find { it.name == name } + override fun mapField(owner: String, name: String) = fieldRecords["$owner/$name"] override fun mapMethod(owner: String, name: String, desc: String) = methodRecords["$owner/$name$desc"] diff --git a/src/main/kotlin/me/yuugiri/hutil/processor/hook/HookInsnPoint.kt b/src/main/kotlin/me/yuugiri/hutil/processor/hook/HookInsnPoint.kt index 1542c72..d5dec8e 100644 --- a/src/main/kotlin/me/yuugiri/hutil/processor/hook/HookInsnPoint.kt +++ b/src/main/kotlin/me/yuugiri/hutil/processor/hook/HookInsnPoint.kt @@ -37,7 +37,13 @@ class HookInsnPoint( } } + // parse method description to get arguments and return type + val methodType = Type.getMethodType(method.desc) + // throwable/return already on stack + if (type == EnumPointType.RETURN && methodType.returnType.sort.let{ it != Type.OBJECT && it != Type.ARRAY && it != Type.VOID }) { + push(getPrimitiveValueOf(methodType.returnType)) + } // push thisObject to stack var loadIndex = 0 @@ -48,9 +54,6 @@ class HookInsnPoint( loadIndex++ } - // parse method description to get arguments and return type - val methodType = Type.getMethodType(method.desc) - // create array for arguments push(getInsnInt(methodType.argumentTypes.size)) push(TypeInsnNode(Opcodes.ANEWARRAY, "java/lang/Object")) diff --git a/src/main/kotlin/me/yuugiri/hutil/processor/hook/MethodHookProcessor.kt b/src/main/kotlin/me/yuugiri/hutil/processor/hook/MethodHookProcessor.kt index 8c80ab6..f20f54d 100644 --- a/src/main/kotlin/me/yuugiri/hutil/processor/hook/MethodHookProcessor.kt +++ b/src/main/kotlin/me/yuugiri/hutil/processor/hook/MethodHookProcessor.kt @@ -64,9 +64,9 @@ object MethodHookProcessor : IClassProcessor { if (info.ordinal >= points.size) throw IllegalArgumentException("Attempt hook point-${info.ordinal} but only ${points.size} exists") listOf(points[info.ordinal]) } else points).forEach { point -> + entry.second.set(true) point.injectHook(method, entry.first.first, info.hookShift) } - entry.second.set(true) } } if (selectedRecords.any { !it.second.get() }) { diff --git a/src/main/kotlin/me/yuugiri/hutil/processor/hook/point/HookPointField.kt b/src/main/kotlin/me/yuugiri/hutil/processor/hook/point/HookPointField.kt index be60923..039035c 100644 --- a/src/main/kotlin/me/yuugiri/hutil/processor/hook/point/HookPointField.kt +++ b/src/main/kotlin/me/yuugiri/hutil/processor/hook/point/HookPointField.kt @@ -10,7 +10,7 @@ import org.objectweb.asm.tree.MethodNode /** * inject hook at every matched [FieldInsnNode] */ -class HookPointField(val matcher: IHookPointMatcher) : IHookPoint { +class HookPointField(val matcher: IHookPointMatcher, val superclass: String = "") : IHookPoint { override fun hookPoints(obfuscationMap: AbstractObfuscationMap?, klass: ClassNode, method: MethodNode): List { val nodes = mutableListOf() @@ -19,8 +19,11 @@ class HookPointField(val matcher: IHookPointMatcher) : IHookPoint { if (it is FieldInsnNode) { var id = "${it.owner};${it.name}" if (!matcher.matches(id)) { - val obf = AbstractObfuscationMap.fieldObfuscationRecord(obfuscationMap, it.owner, it.name) - id = "${obf.owner};${obf.name}" + val supercl = if(superclass.isNotEmpty()) { + AbstractObfuscationMap.classObfuscationRecordReverse(obfuscationMap, superclass).obfuscatedName + } else it.owner + val obf = AbstractObfuscationMap.fieldObfuscationRecord(obfuscationMap, supercl, it.name) + id = "${AbstractObfuscationMap.classObfuscationRecord(obfuscationMap, it.owner).name};${obf.name}" if (!matcher.matches(id)) { return@forEach } diff --git a/src/main/kotlin/me/yuugiri/hutil/processor/hook/point/HookPointInvoke.kt b/src/main/kotlin/me/yuugiri/hutil/processor/hook/point/HookPointInvoke.kt index 6c606d4..d233b1a 100644 --- a/src/main/kotlin/me/yuugiri/hutil/processor/hook/point/HookPointInvoke.kt +++ b/src/main/kotlin/me/yuugiri/hutil/processor/hook/point/HookPointInvoke.kt @@ -10,7 +10,7 @@ import org.objectweb.asm.tree.MethodNode /** * inject hook at every matched [MethodInsnNode] */ -class HookPointInvoke(val matcher: IHookPointMatcher) : IHookPoint { +class HookPointInvoke(val matcher: IHookPointMatcher, val superclass: String = "") : IHookPoint { override fun hookPoints(obfuscationMap: AbstractObfuscationMap?, klass: ClassNode, method: MethodNode): List { val nodes = mutableListOf() @@ -19,8 +19,11 @@ class HookPointInvoke(val matcher: IHookPointMatcher) : IHookPoint { if (it is MethodInsnNode) { var id = "${it.owner};${it.name}${it.desc}" if (!matcher.matches(id)) { - val obf = AbstractObfuscationMap.methodObfuscationRecord(obfuscationMap, it.owner, it.name, it.desc) - id = "${obf.owner};${obf.name}${obf.description}" + val supercl = if(superclass.isNotEmpty()) { + AbstractObfuscationMap.classObfuscationRecordReverse(obfuscationMap, superclass).obfuscatedName + } else it.owner + val obf = AbstractObfuscationMap.methodObfuscationRecord(obfuscationMap, supercl, it.name, it.desc) + id = "${AbstractObfuscationMap.classObfuscationRecord(obfuscationMap, it.owner).name};${obf.name}${obf.description}" if (!matcher.matches(id)) { return@forEach } diff --git a/src/main/kotlin/me/yuugiri/hutil/util/ByteCodeUtil.kt b/src/main/kotlin/me/yuugiri/hutil/util/ByteCodeUtil.kt index a76e1c6..4608080 100644 --- a/src/main/kotlin/me/yuugiri/hutil/util/ByteCodeUtil.kt +++ b/src/main/kotlin/me/yuugiri/hutil/util/ByteCodeUtil.kt @@ -111,6 +111,7 @@ fun castToType(type: Type): List { Type.OBJECT, Type.ARRAY -> { list.add(TypeInsnNode(Opcodes.CHECKCAST, type.internalName)) } + Type.VOID -> {} else -> { // primitive type val className = getPrimitiveObjectClassName(type.sort) list.add(TypeInsnNode(Opcodes.CHECKCAST, className))