Skip to content

Commit

Permalink
better & fixup hook
Browse files Browse the repository at this point in the history
  • Loading branch information
hax0r31337 committed Oct 12, 2022
1 parent 84e7d2a commit 30315d9
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<IHookInsnPoint> {
val nodes = mutableListOf<IHookInsnPoint>()
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<IHookInsnPoint> {
val nodes = mutableListOf<IHookInsnPoint>()
Expand All @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/me/yuugiri/hutil/util/ByteCodeUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ fun castToType(type: Type): List<AbstractInsnNode> {
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))
Expand Down

0 comments on commit 30315d9

Please sign in to comment.