Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[ASM] Bytecode and Runtime contradict each other [Fixed]
#1
Alien 
Here is the code snippet I am using to rename classes
PHP Code:
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.commons.Remapper;
import org.objectweb.asm.commons.RemappingClassAdapter;
import org.objectweb.asm.commons.SimpleRemapper;
import org.objectweb.asm.tree.ClassNode;
//Imports showing which ASM (5.0.4) classes I'm using

private static void doStuff() throws IOException {
    
File jar = new File("Input.jar");
    
//Get's the ClassNodes for each class in a jar
    
ArrayList<ClassNodenodes BCU.loadClasses(jar);
    
//Appends '_Test' to each class except for the main class.
    
Remapper mapper = new SimpleRemapper(getRename(nodes));
    
//Loop through the ClassNodes updating references to renamed classes
    
for (ClassNode cn nodes) {
         
ClassReader cr = new ClassReader(BCU.getNodeBytes(cn));
         
ClassWriter cw = new ClassWriter(crClassWriter.COMPUTE_MAXS); 
         
RemappingClassAdapter rca = new RemappingClassAdapter(cwmapper); 
         
cr.accept(rcaClassReader.EXPAND_FRAMES); 
         
cr = new ClassReader(cw.toByteArray()); 
         
cr.accept(cn,ClassReader.EXPAND_FRAMES);
    }
    
//Export the nodes as a new jar file and copy over the META-INF folder
    
BCU.saveAsJar(nodes"Output.jar");


When run on any jar file, fields in the exported jar are duplicated. The ones at the bottom have updated references to the renamed classes.

Example Output:
PHP Code:
public static String VERSION;
public static final 
ConfigFile config;
public static 
String VERSION;
public static final 
ConfigFile_Test config

Edit: I have a temporary fix by extending ClassNode and removing the duplicates post-rename. But I'd like something more efficient. This will do for now though.

Edit 2: I've run into a problem when exporting classes [Edit in regards to Edit 2: Found the problem. Edit 1 still applies. I would like to improve this]

Edit 3: My currently working approach:
Code:
@Override
    public void handle(ActionEvent event) {
        try {
            Timer t = new Timer();
            File jar = Main.getTargetJar();
            MappingGen.setLast(jar);
            Map<String, ClassNode> nodes = JarUtil.loadClasses(jar);
            System.out.println("Generating new names for " + nodes.size() + " classes... ");
            Map<String, MappedClass> renamed = MappingGen.getRename(tab.getObfuscation(), nodes);
            Map<String, byte[]> out = new HashMap<String, byte[]>();
            System.out.println("Renaming " + renamed.size() + " classes... ");
            int workIndex = 1;
            SkidMapper mapper = new SkidMapper(renamed);
            for (ClassNode cn : nodes.values()) {
                ClassReader cr = new ClassReader(ASMUtil.getNodeBytes(cn));
                ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS);
                ClassVisitor remapper = new RemappingClassAdapter(cw, mapper);
                cr.accept(remapper, ClassReader.EXPAND_FRAMES);
                cr = new ClassReader(cw.toByteArray());
                cw = new ClassWriter(0);
                cr.accept(cw, ClassReader.SKIP_DEBUG);            
                out.put(renamed.get(cn.name).getRenamed(), cw.toByteArray());
                //
                String percentStr = "" + ((workIndex + 0.000000001f) / (renamed.size() - 0.00001f)) * 100;
                percentStr = percentStr.substring(0, percentStr.length() > 5 ? 5 : percentStr.length());
                System.out.println("    " + workIndex + "/" + renamed.size() + " [" + percentStr + "%]");
                workIndex++;
            }
            System.out.println("Saving classes...");
            JarUtil.saveAsJar(out, tab.getExportedName(), tab.yes());
            System.out.println("Done! Completion time: " + (t.getTime()) + " Milliseconds");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
Reply
 


Messages In This Thread
[ASM] Bytecode and Runtime contradict each other [Fixed] - by Dank Vader - 11-30-2015, 06:14 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)

About The Bytecode Club

We're a community forum focused on Reverse Engineering, we try to target Java/Android but we also include other langauges/platforms. We pride ourselves in supporting and free and open sourced applications.

Website