Skip to content

Commit

Permalink
fix(script): add example script for resources rename (#2126)
Browse files Browse the repository at this point in the history
  • Loading branch information
skylot committed Mar 20, 2024
1 parent 463d2b9 commit 2807dc5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
6 changes: 5 additions & 1 deletion jadx-core/src/main/java/jadx/api/ResourceFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public String getDeobfName() {
return deobfName != null ? deobfName : name;
}

public void setDeobfName(String resFullName) {
this.deobfName = resFullName;
}

public ResourceType getType() {
return type;
}
Expand All @@ -84,7 +88,7 @@ public boolean setAlias(ResourceEntry ri) {
}
String alias = sb.toString();
if (!alias.equals(name)) {
deobfName = alias;
setDeobfName(alias);
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import jadx.api.plugins.options.OptionFlag.PER_PROJECT

/**
* Custom resources regexp deobfuscator
*/

val jadx = getJadxInstance()

val regexOpt = jadx.options.registerString(
name = "regex",
desc = "Apply resources rename for file names matches regex",
defaultValue = """[Oo0]+\.xml""",
).flags(PER_PROJECT)

val regex = regexOpt.value.toRegex()
var n = 0

jadx.stages.prepare {
for (resFile in jadx.internalDecompiler.resources) {
val fullName = resFile.originalName
val name = fullName.substringAfterLast('/')
if (name matches regex) {
val path = fullName.substringBeforeLast('/') // TODO: path also may be obfuscated
val ext = name.substringAfterLast('.')
val newName = "$path/res-${n++}.$ext"
log.info { "renaming resource: '$fullName' to '$newName'" }
resFile.deobfName = newName
}
}
}

jadx.afterLoad {
log.info { "Renames count: $n" }
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,22 @@ package jadx.plugins.script.runtime.data
import jadx.core.dex.nodes.BlockNode
import jadx.core.dex.nodes.InsnNode
import jadx.core.dex.nodes.MethodNode
import jadx.core.dex.nodes.RootNode
import jadx.core.dex.regions.Region
import jadx.plugins.script.runtime.JadxScriptInstance

class Stages(private val jadx: JadxScriptInstance) {

fun prepare(block: (RootNode) -> Unit) {
jadx.addPass(object : ScriptPreparePass(jadx, "StagePrepare") {
override fun init(root: RootNode) {
jadx.debug.catchExceptions("Prepare init block") {
block.invoke(root)
}
}
})
}

fun rawInsns(block: (MethodNode, Array<InsnNode?>) -> Unit) {
jadx.addPass(object : ScriptOrderedDecompilePass(
jadx,
Expand Down

1 comment on commit 2807dc5

@iiasceri
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well done, do a nightly channel of releases and add github actions (pipelines) for auto build of those releases please if possible as of plugins that might be nice to have is Lauries JADXecute plugin showcased here: https://www.youtube.com/watch?v=g0r3C1iEeBg

Please sign in to comment.