■ ■ ■ ■ ■ ■
jadx-plugins/jadx-script/examples/scripts/gui_custom_frida.jadx.kts
| 1 | + | @file:DependsOn("org.apache.commons:commons-text:1.10.0") |
| 2 | + | |
| 3 | + | import jadx.api.metadata.ICodeNodeRef |
| 4 | + | import jadx.core.codegen.TypeGen |
| 5 | + | import jadx.core.dex.instructions.args.ArgType |
| 6 | + | import jadx.core.dex.nodes.ClassNode |
| 7 | + | import jadx.core.dex.nodes.FieldNode |
| 8 | + | import jadx.core.dex.nodes.MethodNode |
| 9 | + | import jadx.core.utils.exceptions.JadxRuntimeException |
| 10 | + | import org.apache.commons.text.StringEscapeUtils |
| 11 | + | |
| 12 | + | val jadx = getJadxInstance() |
| 13 | + | |
| 14 | + | jadx.gui.ifAvailable { |
| 15 | + | addPopupMenuAction( |
| 16 | + | "Custom Frida snippet (g)", |
| 17 | + | enabled = ::isActionEnabled, |
| 18 | + | keyBinding = "G", |
| 19 | + | action = ::runAction |
| 20 | + | ) |
| 21 | + | } |
| 22 | + | |
| 23 | + | fun isActionEnabled(node: ICodeNodeRef): Boolean { |
| 24 | + | return node is MethodNode || node is ClassNode || node is FieldNode |
| 25 | + | } |
| 26 | + | |
| 27 | + | fun runAction(node: ICodeNodeRef) { |
| 28 | + | try { |
| 29 | + | val fridaSnippet = generateFridaSnippet(node) |
| 30 | + | log.info { "Custom frida snippet:\n$fridaSnippet" } |
| 31 | + | jadx.gui.copyToClipboard(fridaSnippet) |
| 32 | + | } catch (e: Exception) { |
| 33 | + | log.error(e) { "Failed to generate Frida code snippet" } |
| 34 | + | } |
| 35 | + | } |
| 36 | + | |
| 37 | + | fun generateFridaSnippet(node: ICodeNodeRef): String { |
| 38 | + | return when (node) { |
| 39 | + | is MethodNode -> generateMethodSnippet(node) |
| 40 | + | is ClassNode -> generateClassSnippet(node) |
| 41 | + | is FieldNode -> generateFieldSnippet(node) |
| 42 | + | else -> throw JadxRuntimeException("Unsupported node type: " + node.javaClass) |
| 43 | + | } |
| 44 | + | } |
| 45 | + | |
| 46 | + | fun generateClassSnippet(cls: ClassNode): String { |
| 47 | + | return """let ${cls.name} = Java.use("${StringEscapeUtils.escapeEcmaScript(cls.rawName)}");""" |
| 48 | + | } |
| 49 | + | |
| 50 | + | fun generateMethodSnippet(mthNode: MethodNode): String { |
| 51 | + | val methodInfo = mthNode.methodInfo |
| 52 | + | val methodName = if (methodInfo.isConstructor) { |
| 53 | + | "\$init" |
| 54 | + | } else { |
| 55 | + | StringEscapeUtils.escapeEcmaScript(methodInfo.name) |
| 56 | + | } |
| 57 | + | val overload = if (isOverloaded(mthNode)) { |
| 58 | + | ".overload(${methodInfo.argumentsTypes.joinToString(transform = this::parseArgType)})" |
| 59 | + | } else { |
| 60 | + | "" |
| 61 | + | } |
| 62 | + | val shortClassName = mthNode.parentClass.name |
| 63 | + | val argNames = mthNode.collectArgsWithoutLoading().map { a -> a.name } |
| 64 | + | val args = argNames.joinToString(separator = ", ") |
| 65 | + | val logArgs = if (argNames.isNotEmpty()) { |
| 66 | + | argNames.joinToString(separator = " + ', ' + ", prefix = " + ', ' + ") { p -> "'$p: ' + $p" } |
| 67 | + | } else { |
| 68 | + | "" |
| 69 | + | } |
| 70 | + | val clsSnippet = generateClassSnippet(mthNode.parentClass) |
| 71 | + | return if (methodInfo.isConstructor || methodInfo.returnType == ArgType.VOID) { |
| 72 | + | // no return value |
| 73 | + | """ |
| 74 | + | $clsSnippet |
| 75 | + | $shortClassName["$methodName"]$overload.implementation = function ($args) { |
| 76 | + | console.log('$shortClassName.$methodName is called'$logArgs); |
| 77 | + | this["$methodName"]($args); |
| 78 | + | }; |
| 79 | + | """.trimIndent() |
| 80 | + | } else { |
| 81 | + | """ |
| 82 | + | $clsSnippet |
| 83 | + | $shortClassName["$methodName"]$overload.implementation = function ($args) { |
| 84 | + | console.log('$shortClassName.$methodName is called'$logArgs); |
| 85 | + | let ret = this["$methodName"]($args); |
| 86 | + | console.log('$shortClassName.$methodName return: ' + ret); |
| 87 | + | return ret; |
| 88 | + | }; |
| 89 | + | """.trimIndent() |
| 90 | + | } |
| 91 | + | } |
| 92 | + | |
| 93 | + | fun generateFieldSnippet(fld: FieldNode): String { |
| 94 | + | var rawFieldName = StringEscapeUtils.escapeEcmaScript(fld.name) |
| 95 | + | for (methodNode in fld.parentClass.methods) { |
| 96 | + | if (methodNode.name == rawFieldName) { |
| 97 | + | rawFieldName = "_$rawFieldName" |
| 98 | + | break |
| 99 | + | } |
| 100 | + | } |
| 101 | + | return """ |
| 102 | + | ${generateClassSnippet(fld.parentClass)} |
| 103 | + | ${fld.name} = ${fld.parentClass.name}.$rawFieldName.value; |
| 104 | + | """.trimIndent() |
| 105 | + | } |
| 106 | + | |
| 107 | + | fun isOverloaded(methodNode: MethodNode): Boolean { |
| 108 | + | return methodNode.parentClass.methods.stream().anyMatch { m: MethodNode -> |
| 109 | + | m.name == methodNode.name && methodNode.methodInfo.shortId != m.methodInfo.shortId |
| 110 | + | } |
| 111 | + | } |
| 112 | + | |
| 113 | + | fun parseArgType(x: ArgType): String { |
| 114 | + | val typeStr = if (x.isArray) { |
| 115 | + | TypeGen.signature(x).replace("/", ".") |
| 116 | + | } else { |
| 117 | + | x.toString() |
| 118 | + | } |
| 119 | + | return "'$typeStr'" |
| 120 | + | } |
| 121 | + | |