-
Notifications
You must be signed in to change notification settings - Fork 5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feature] Help with Script to rename Variables based on Constructor #2422
Comments
@marcohald as I understand, you want to rename fields to variable name used in constructor on field init. import jadx.core.deobf.NameMapper
import jadx.core.dex.attributes.nodes.RenameReasonAttr
import jadx.core.dex.info.FieldInfo
import jadx.core.dex.instructions.IndexInsnNode
import jadx.core.dex.instructions.InsnType
import jadx.core.dex.instructions.args.RegisterArg
import jadx.core.dex.nodes.ClassNode
import jadx.core.dex.nodes.MethodNode
import jadx.plugins.script.runtime.data.ScriptOrderedDecompilePass
val jadx = getJadxInstance()
jadx.addPass(object : ScriptOrderedDecompilePass(
jadx,
"DeobfFieldFromVariableName",
runAfter = listOf("SimplifyVisitor"),
) {
override fun visit(mth: MethodNode) {
val parentClass = mth.parentClass
if (parentClass.name == "CLASS_NAME_CHANGE_ME!" && mth.isConstructor) {
for (block in mth.basicBlocks) {
for (insn in block.instructions) {
if (insn.type == InsnType.IPUT) {
renameByVarName(insn as IndexInsnNode, parentClass)
}
}
}
}
}
private fun renameByVarName(fldPut: IndexInsnNode, parentClass: ClassNode) {
val fld = fldPut.index as FieldInfo
if (fld.declClass == parentClass.classInfo) {
val arg = fldPut.getArg(0)
if (arg is RegisterArg) {
val varName = arg.name
if (NameMapper.isValidIdentifier(varName)) {
val fldNode = parentClass.searchField(fld)
fldNode.rename(varName)
RenameReasonAttr.forNode(fldNode).append("by deobf script")
log.info { "Rename field $fldNode to $varName" }
}
}
}
}
}) Also, such rename looks very useful and it will be nice to implement such rename in jadx, I will do this 👍 |
Please make this feature optional. This string metadata should not be trusted: code protectors can change those strings to anything they'd like, including bad or irrelevant names that could bring even more confusion. |
@skylot I would definitely vote for an optional feature. import jadx.core.deobf.NameMapper
import jadx.core.dex.attributes.nodes.RenameReasonAttr
import jadx.core.dex.info.FieldInfo
import jadx.core.dex.instructions.IndexInsnNode
import jadx.core.dex.instructions.InsnType
import jadx.core.dex.instructions.args.RegisterArg
import jadx.core.dex.nodes.ClassNode
import jadx.core.dex.nodes.MethodNode
import jadx.plugins.script.runtime.data.ScriptOrderedDecompilePass
val jadx = getJadxInstance()
jadx.addPass(object : ScriptOrderedDecompilePass(
jadx,
"DeobfFieldFromVariableName",
runAfter = listOf("SimplifyVisitor"),
) {
override fun visit(mth: MethodNode) {
val parentClass = mth.parentClass
if (parentClass.fullName.contains("C5365c0") && mth.isConstructor) {
log.warn { "$parentClass" }
log.warn("Processing constructor: $mth")
for (block in mth.basicBlocks) {
for (insn in block.instructions) {
if (insn.type == InsnType.IPUT) {
renameByVarName(insn as IndexInsnNode, parentClass)
}
}
}
}
}
private fun renameByVarName(fldPut: IndexInsnNode, parentClass: ClassNode) {
val fld = fldPut.index as FieldInfo
log.warn("Processing fld: $fld")
if (fld.declClass == parentClass.classInfo) {
log.warn("Processing fldPut: $fldPut")
val arg = fldPut.getArg(0)
log.warn("Processing arg: $arg")
if (arg is RegisterArg) {
val varName = arg.name
log.warn("Processing varName: $varName")
if (NameMapper.isValidIdentifier(varName)) {
val fldNode = parentClass.searchField(fld)
fldNode.rename(varName)
RenameReasonAttr.forNode(fldNode).append("by deobf script")
log.warn { "Rename field $fldNode to $varName" }
}
}
}
}
}) With this Output
I'm running this nightly: |
Well, now variable names is available only if set in debug info or in |
Describe your idea
I try to rename Variables based on the type of the Constructor.
For example this:
should become
What I tried so far is:
Output:
But here i'm stuck with unable to rename the field variable, as there seems no rename function.
I tried another approach with this code:
But at this Point the Constructor Types seems not to be replaced yet:
The text was updated successfully, but these errors were encountered: