ASM Rename Util Cleanup

This commit is contained in:
Konloch 2021-07-06 15:27:20 -07:00
parent d52a5b450b
commit 14b3b04661
4 changed files with 69 additions and 49 deletions

View file

@ -30,25 +30,33 @@ import the.bytecode.club.bytecodeviewer.BytecodeViewer;
***************************************************************************/ ***************************************************************************/
/** /**
* Used to rename/replace methods/classes/fields * Used to rename/replace methods/classes/fields loaded as a BCV resource
* *
* @author Konloch * @author Konloch
*/ */
public final class ASMUtil_OLD {
public final class ASMRenameUtil
{
public static void renameFieldNode(String originalParentName, public static void renameFieldNode(String originalParentName,
String originalFieldName, String originalFieldDesc, String originalFieldName, String originalFieldDesc,
String newFieldParent, String newFieldName, String newFieldDesc) { String newFieldParent, String newFieldName, String newFieldDesc)
for (ClassNode c : BytecodeViewer.getLoadedClasses()) { {
for (Object o : c.methods.toArray()) { for (ClassNode c : BytecodeViewer.getLoadedClasses())
{
for (Object o : c.methods.toArray())
{
MethodNode m = (MethodNode) o; MethodNode m = (MethodNode) o;
for (AbstractInsnNode i : m.instructions.toArray()) { for (AbstractInsnNode i : m.instructions.toArray())
if (i instanceof FieldInsnNode) { {
if (i instanceof FieldInsnNode)
{
FieldInsnNode field = (FieldInsnNode) i; FieldInsnNode field = (FieldInsnNode) i;
if (field.owner.equals(originalParentName) if (field.owner.equals(originalParentName)
&& field.name.equals(originalFieldName) && field.name.equals(originalFieldName)
&& field.desc.equals(originalFieldDesc)) { && field.desc.equals(originalFieldDesc))
{
if (newFieldParent != null) if (newFieldParent != null)
field.owner = newFieldParent; field.owner = newFieldParent;
if (newFieldName != null) if (newFieldName != null)
@ -64,16 +72,22 @@ public final class ASMUtil_OLD {
public static void renameMethodNode(String originalParentName, public static void renameMethodNode(String originalParentName,
String originalMethodName, String originalMethodDesc, String originalMethodName, String originalMethodDesc,
String newParent, String newName, String newDesc) { String newParent, String newName, String newDesc)
for (ClassNode c : BytecodeViewer.getLoadedClasses()) { {
for (Object o : c.methods.toArray()) { for (ClassNode c : BytecodeViewer.getLoadedClasses())
{
for (Object o : c.methods.toArray())
{
MethodNode m = (MethodNode) o; MethodNode m = (MethodNode) o;
for (AbstractInsnNode i : m.instructions.toArray()) { for (AbstractInsnNode i : m.instructions.toArray())
if (i instanceof MethodInsnNode) { {
if (i instanceof MethodInsnNode)
{
MethodInsnNode mi = (MethodInsnNode) i; MethodInsnNode mi = (MethodInsnNode) i;
if (mi.owner.equals(originalParentName) if (mi.owner.equals(originalParentName)
&& mi.name.equals(originalMethodName) && mi.name.equals(originalMethodName)
&& mi.desc.equals(originalMethodDesc)) { && mi.desc.equals(originalMethodDesc))
{
if (newParent != null) if (newParent != null)
mi.owner = newParent; mi.owner = newParent;
if (newName != null) if (newName != null)
@ -86,7 +100,8 @@ public final class ASMUtil_OLD {
}*/ }*/
} }
if (m.signature != null) { if (m.signature != null)
{
if (newName != null) if (newName != null)
m.signature = m.signature.replace(originalMethodName, m.signature = m.signature.replace(originalMethodName,
newName); newName);
@ -97,7 +112,8 @@ public final class ASMUtil_OLD {
if (m.name.equals(originalMethodName) if (m.name.equals(originalMethodName)
&& m.desc.equals(originalMethodDesc) && m.desc.equals(originalMethodDesc)
&& c.name.equals(originalParentName)) { && c.name.equals(originalParentName))
{
if (newName != null) if (newName != null)
m.name = newName; m.name = newName;
if (newDesc != null) if (newDesc != null)
@ -108,63 +124,67 @@ public final class ASMUtil_OLD {
} }
public static void renameClassNode(final String oldName, public static void renameClassNode(final String oldName,
final String newName) { final String newName)
for (ClassNode c : BytecodeViewer.getLoadedClasses()) { {
for (InnerClassNode oo : c.innerClasses) { for (ClassNode c : BytecodeViewer.getLoadedClasses())
if (oo.innerName != null {
&& oo.innerName.equals(oldName)) { for (InnerClassNode oo : c.innerClasses)
{
if (oo.innerName != null && oo.innerName.equals(oldName))
oo.innerName = newName; oo.innerName = newName;
}
if (oo.name.equals(oldName)) { if (oo.name.equals(oldName))
oo.name = newName; oo.name = newName;
}
if (oo.outerName != null if (oo.outerName != null && oo.outerName.equals(oldName))
&& oo.outerName.equals(oldName)) {
oo.outerName = newName; oo.outerName = newName;
} }
}
if (c.signature != null) if (c.signature != null)
c.signature = c.signature.replace(oldName, newName); c.signature = c.signature.replace(oldName, newName);
if (c.superName.equals(oldName)) { if (c.superName.equals(oldName))
c.superName = newName; c.superName = newName;
}
for (Object o : c.fields.toArray()) { for (Object o : c.fields.toArray())
{
FieldNode f = (FieldNode) o; FieldNode f = (FieldNode) o;
f.desc = f.desc.replace(oldName, newName); f.desc = f.desc.replace(oldName, newName);
} }
for (Object o : c.methods.toArray()) {
for (Object o : c.methods.toArray())
{
MethodNode m = (MethodNode) o; MethodNode m = (MethodNode) o;
if (m.localVariables != null) { if (m.localVariables != null)
for (LocalVariableNode node : m.localVariables) { for (LocalVariableNode node : m.localVariables)
node.desc = node.desc.replace(oldName, newName); node.desc = node.desc.replace(oldName, newName);
}
}
if (m.signature != null) if (m.signature != null)
m.signature = m.signature.replace(oldName, newName); m.signature = m.signature.replace(oldName, newName);
for (int i = 0; i < m.exceptions.size(); i++) { for (int i = 0; i < m.exceptions.size(); i++)
if (m.exceptions.get(i).equals(oldName)) if (m.exceptions.get(i).equals(oldName))
m.exceptions.set(i, newName); m.exceptions.set(i, newName);
}
for (AbstractInsnNode i : m.instructions.toArray()) { for (AbstractInsnNode i : m.instructions.toArray())
if (i instanceof TypeInsnNode) { {
if (i instanceof TypeInsnNode)
{
TypeInsnNode t = (TypeInsnNode) i; TypeInsnNode t = (TypeInsnNode) i;
if (t.desc.equals(oldName)) { if (t.desc.equals(oldName))
t.desc = newName; t.desc = newName;
} }
}
if (i instanceof MethodInsnNode) { if (i instanceof MethodInsnNode)
{
MethodInsnNode mi = (MethodInsnNode) i; MethodInsnNode mi = (MethodInsnNode) i;
if (mi.owner.equals(oldName)) if (mi.owner.equals(oldName))
mi.owner = newName; mi.owner = newName;
mi.desc = mi.desc.replace(oldName, newName); mi.desc = mi.desc.replace(oldName, newName);
} }
if (i instanceof FieldInsnNode) { if (i instanceof FieldInsnNode)
{
FieldInsnNode fi = (FieldInsnNode) i; FieldInsnNode fi = (FieldInsnNode) i;
if (fi.owner.equals(oldName)) if (fi.owner.equals(oldName))
fi.owner = newName; fi.owner = newName;

View file

@ -2,7 +2,7 @@ package the.bytecode.club.bytecodeviewer.obfuscators;
import org.objectweb.asm.tree.ClassNode; import org.objectweb.asm.tree.ClassNode;
import the.bytecode.club.bytecodeviewer.BytecodeViewer; import the.bytecode.club.bytecodeviewer.BytecodeViewer;
import the.bytecode.club.bytecodeviewer.api.ASMUtil_OLD; import the.bytecode.club.bytecodeviewer.api.ASMRenameUtil;
/*************************************************************************** /***************************************************************************
* Bytecode Viewer (BCV) - Java & Android Reverse Engineering Suite * * Bytecode Viewer (BCV) - Java & Android Reverse Engineering Suite *
@ -37,7 +37,7 @@ public class RenameClasses extends JavaObfuscator {
System.out.println("Obfuscating class names..."); System.out.println("Obfuscating class names...");
for (ClassNode c : BytecodeViewer.getLoadedClasses()) { for (ClassNode c : BytecodeViewer.getLoadedClasses()) {
String newName = generateUniqueName(stringLength); String newName = generateUniqueName(stringLength);
ASMUtil_OLD.renameClassNode(c.name, newName); ASMRenameUtil.renameClassNode(c.name, newName);
c.name = newName; c.name = newName;
} }

View file

@ -3,7 +3,7 @@ package the.bytecode.club.bytecodeviewer.obfuscators;
import org.objectweb.asm.tree.ClassNode; import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.FieldNode; import org.objectweb.asm.tree.FieldNode;
import the.bytecode.club.bytecodeviewer.BytecodeViewer; import the.bytecode.club.bytecodeviewer.BytecodeViewer;
import the.bytecode.club.bytecodeviewer.api.ASMUtil_OLD; import the.bytecode.club.bytecodeviewer.api.ASMRenameUtil;
/*************************************************************************** /***************************************************************************
* Bytecode Viewer (BCV) - Java & Android Reverse Engineering Suite * * Bytecode Viewer (BCV) - Java & Android Reverse Engineering Suite *
@ -40,7 +40,7 @@ public class RenameFields extends JavaObfuscator {
for (Object o : c.fields.toArray()) { for (Object o : c.fields.toArray()) {
FieldNode f = (FieldNode) o; FieldNode f = (FieldNode) o;
String newName = generateUniqueName(stringLength); String newName = generateUniqueName(stringLength);
ASMUtil_OLD.renameFieldNode(c.name, f.name, f.desc, null, ASMRenameUtil.renameFieldNode(c.name, f.name, f.desc, null,
newName, null); newName, null);
f.name = newName; f.name = newName;
} }

View file

@ -4,7 +4,7 @@ import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.ClassNode; import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.MethodNode; import org.objectweb.asm.tree.MethodNode;
import the.bytecode.club.bytecodeviewer.BytecodeViewer; import the.bytecode.club.bytecodeviewer.BytecodeViewer;
import the.bytecode.club.bytecodeviewer.api.ASMUtil_OLD; import the.bytecode.club.bytecodeviewer.api.ASMRenameUtil;
/*************************************************************************** /***************************************************************************
* Bytecode Viewer (BCV) - Java & Android Reverse Engineering Suite * * Bytecode Viewer (BCV) - Java & Android Reverse Engineering Suite *
@ -58,7 +58,7 @@ public class RenameMethods extends JavaObfuscator {
if (!m.name.equals("main") && !m.name.equals("<init>") if (!m.name.equals("main") && !m.name.equals("<init>")
&& !m.name.equals("<clinit>")) { && !m.name.equals("<clinit>")) {
String newName = generateUniqueName(stringLength); String newName = generateUniqueName(stringLength);
ASMUtil_OLD.renameMethodNode(c.name, m.name, m.desc, ASMRenameUtil.renameMethodNode(c.name, m.name, m.desc,
null, newName, null); null, newName, null);
} }
} }