Search in sources :

Example 1 with SingleJarDownloader

use of org.topdank.byteio.in.SingleJarDownloader in project maple-ir by LLVM-but-worse.

the class Boot method dumpJar.

private static void dumpJar(ApplicationClassSource app, SingleJarDownloader<ClassNode> dl, PassGroup masterGroup, String outputFile) throws IOException {
    (new CompleteResolvingJarDumper(dl.getJarContents(), app) {

        @Override
        public int dumpResource(JarOutputStream out, String name, byte[] file) throws IOException {
            // }
            if (name.equals("META-INF/MANIFEST.MF")) {
                ClassRenamerPass renamer = (ClassRenamerPass) masterGroup.getPass(e -> e.is(ClassRenamerPass.class));
                if (renamer != null) {
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(baos));
                    BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(file)));
                    String line;
                    while ((line = br.readLine()) != null) {
                        String[] parts = line.split(": ", 2);
                        if (parts.length != 2) {
                            bw.write(line);
                            continue;
                        }
                        if (parts[0].equals("Main-Class")) {
                            String newMain = renamer.getRemappedName(parts[1].replace(".", "/")).replace("/", ".");
                            LOGGER.info(String.format("%s -> %s%n", parts[1], newMain));
                            parts[1] = newMain;
                        }
                        bw.write(parts[0]);
                        bw.write(": ");
                        bw.write(parts[1]);
                        bw.write(System.lineSeparator());
                    }
                    br.close();
                    bw.close();
                    file = baos.toByteArray();
                }
            }
            return super.dumpResource(out, name, file);
        }
    }).dump(new File(outputFile));
}
Also used : DeadCodeEliminationPass(org.mapleir.deob.passes.DeadCodeEliminationPass) CompleteResolvingJarDumper(org.mapleir.app.service.CompleteResolvingJarDumper) IRCache(org.mapleir.context.IRCache) BoissinotDestructor(org.mapleir.ir.algorithms.BoissinotDestructor) Deque(java.util.Deque) ControlFlowGraph(org.mapleir.ir.cfg.ControlFlowGraph) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ClassPrinter(org.mapleir.ir.printer.ClassPrinter) Logger(org.apache.log4j.Logger) MethodNode(org.objectweb.asm.tree.MethodNode) JarInfo(org.topdank.byteengineer.commons.data.JarInfo) AnalysisContext(org.mapleir.context.AnalysisContext) ConstantExpressionReorderPass(org.mapleir.deob.passes.ConstantExpressionReorderPass) SingleJarDownloader(org.topdank.byteio.in.SingleJarDownloader) IPass(org.mapleir.deob.IPass) FieldNodePrinter(org.mapleir.ir.printer.FieldNodePrinter) LinkedList(java.util.LinkedList) JarOutputStream(java.util.jar.JarOutputStream) PropertyHelper(org.mapleir.propertyframework.util.PropertyHelper) PassGroup(org.mapleir.deob.PassGroup) SimpleApplicationContext(org.mapleir.app.client.SimpleApplicationContext) ControlFlowGraphBuilder(org.mapleir.ir.cfg.builder.ControlFlowGraphBuilder) ClassHelper(org.mapleir.stdlib.collections.ClassHelper) ApplicationClassSource(org.mapleir.app.service.ApplicationClassSource) Set(java.util.Set) BasicAnalysisContext(org.mapleir.context.BasicAnalysisContext) RenamingHeuristic(org.mapleir.deob.util.RenamingHeuristic) IRCallTracer(org.mapleir.deob.interproc.IRCallTracer) MethodNodePrinter(org.mapleir.ir.printer.MethodNodePrinter) LibraryClassSource(org.mapleir.app.service.LibraryClassSource) ControlFlowGraphDumper(org.mapleir.ir.algorithms.ControlFlowGraphDumper) TabbedStringWriter(org.mapleir.stdlib.util.TabbedStringWriter) ClassRenamerPass(org.mapleir.deob.passes.rename.ClassRenamerPass) java.io(java.io) IPropertyDictionary(org.mapleir.propertyframework.api.IPropertyDictionary) Entry(java.util.Map.Entry) ClassNode(org.objectweb.asm.tree.ClassNode) InstalledRuntimeClassSource(org.mapleir.app.service.InstalledRuntimeClassSource) JarOutputStream(java.util.jar.JarOutputStream) CompleteResolvingJarDumper(org.mapleir.app.service.CompleteResolvingJarDumper) ClassRenamerPass(org.mapleir.deob.passes.rename.ClassRenamerPass)

Example 2 with SingleJarDownloader

use of org.topdank.byteio.in.SingleJarDownloader in project maple-ir by LLVM-but-worse.

the class Boot method main.

public static void main(String[] args) throws Exception {
    sections = new LinkedList<>();
    logging = true;
    File rtjar = new File("res/rt.jar");
    // Load input jar
    File f = locateRevFile(135);
    // File f = new File("res/allatori6.1san.jar");
    section("Preparing to run on " + f.getAbsolutePath());
    SingleJarDownloader<ClassNode> dl = new SingleJarDownloader<>(new JarInfo(f));
    dl.download();
    String name = f.getName().substring(0, f.getName().length() - 4);
    // ApplicationClassSource app = new ApplicationClassSource(name, dl.getJarContents().getClassContents());
    // 
    ApplicationClassSource app = new ApplicationClassSource("test", ClassHelper.parseClasses(CGExample.class));
    // app.addLibraries(new InstalledRuntimeClassSource(app));
    app.addLibraries(rt(app, rtjar), new InstalledRuntimeClassSource(app));
    section("Initialising context.");
    AnalysisContext cxt = new BasicAnalysisContext.BasicContextBuilder().setApplication(app).setInvocationResolver(new DefaultInvocationResolver(app)).setCache(new IRCache(ControlFlowGraphBuilder::build)).setApplicationContext(new SimpleApplicationContext(app)).build();
    section("Expanding callgraph and generating cfgs.");
    IRCallTracer tracer = new IRCallTracer(cxt);
    for (MethodNode m : cxt.getApplicationContext().getEntryPoints()) {
        // System.out.println(m);
        tracer.trace(m);
        if (m.instructions.size() > 500 && m.instructions.size() < 100) {
            System.out.println(m);
            System.out.println(cxt.getIRCache().get(m));
        }
    }
    for (ClassNode cn : app.iterate()) {
        TabbedStringWriter sw = new TabbedStringWriter();
        sw.setTabString("  ");
        IPropertyDictionary settings = PropertyHelper.createDictionary();
        // settings.put(new BooleanProperty(ASMPrinter.PROP_ACCESS_FLAG_SAFE, true));
        ClassPrinter cp = new ClassPrinter(sw, settings, new FieldNodePrinter(sw, settings), new MethodNodePrinter(sw, settings) {

            @Override
            protected ControlFlowGraph getCfg(MethodNode mn) {
                return cxt.getIRCache().getFor(mn);
            }
        });
        cp.print(cn);
        System.out.println(sw.toString());
    }
    section0("...generated " + cxt.getIRCache().size() + " cfgs in %fs.%n", "Preparing to transform.");
    // do passes
    PassGroup masterGroup = new PassGroup("MasterController");
    for (IPass p : getTransformationPasses()) {
        masterGroup.add(p);
    }
    run(cxt, masterGroup);
    // for(MethodNode m : cxt.getIRCache().getActiveMethods()) {
    // if(m.instructions.size() > 100 && m.instructions.size() < 500) {
    // System.out.println(cxt.getIRCache().get(m));
    // }
    // }
    section("Retranslating SSA IR to standard flavour.");
    for (Entry<MethodNode, ControlFlowGraph> e : cxt.getIRCache().entrySet()) {
        MethodNode mn = e.getKey();
        ControlFlowGraph cfg = e.getValue();
        BoissinotDestructor.leaveSSA(cfg);
        cfg.getLocals().realloc(cfg);
        (new ControlFlowGraphDumper(cfg, mn)).dump();
    }
    section("Rewriting jar.");
    // dumpJar(app, dl, masterGroup, "out/osb5.jar");
    section("Finished.");
}
Also used : IPropertyDictionary(org.mapleir.propertyframework.api.IPropertyDictionary) ClassPrinter(org.mapleir.ir.printer.ClassPrinter) AnalysisContext(org.mapleir.context.AnalysisContext) BasicAnalysisContext(org.mapleir.context.BasicAnalysisContext) IPass(org.mapleir.deob.IPass) InstalledRuntimeClassSource(org.mapleir.app.service.InstalledRuntimeClassSource) ApplicationClassSource(org.mapleir.app.service.ApplicationClassSource) MethodNode(org.objectweb.asm.tree.MethodNode) MethodNodePrinter(org.mapleir.ir.printer.MethodNodePrinter) SingleJarDownloader(org.topdank.byteio.in.SingleJarDownloader) TabbedStringWriter(org.mapleir.stdlib.util.TabbedStringWriter) ClassNode(org.objectweb.asm.tree.ClassNode) FieldNodePrinter(org.mapleir.ir.printer.FieldNodePrinter) IRCache(org.mapleir.context.IRCache) IRCallTracer(org.mapleir.deob.interproc.IRCallTracer) SimpleApplicationContext(org.mapleir.app.client.SimpleApplicationContext) ControlFlowGraphDumper(org.mapleir.ir.algorithms.ControlFlowGraphDumper) PassGroup(org.mapleir.deob.PassGroup) JarInfo(org.topdank.byteengineer.commons.data.JarInfo) ControlFlowGraph(org.mapleir.ir.cfg.ControlFlowGraph) BasicAnalysisContext(org.mapleir.context.BasicAnalysisContext)

Example 3 with SingleJarDownloader

use of org.topdank.byteio.in.SingleJarDownloader in project maple-ir by LLVM-but-worse.

the class Boot method rt.

private static LibraryClassSource rt(ApplicationClassSource app, File rtjar) throws IOException {
    section("Loading rt.jar from " + rtjar.getAbsolutePath());
    SingleJarDownloader<ClassNode> dl = new SingleJarDownloader<>(new JarInfo(rtjar));
    dl.download();
    return new LibraryClassSource(app, dl.getJarContents().getClassContents());
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) JarInfo(org.topdank.byteengineer.commons.data.JarInfo) LibraryClassSource(org.mapleir.app.service.LibraryClassSource) SingleJarDownloader(org.topdank.byteio.in.SingleJarDownloader)

Aggregations

ClassNode (org.objectweb.asm.tree.ClassNode)3 JarInfo (org.topdank.byteengineer.commons.data.JarInfo)3 SingleJarDownloader (org.topdank.byteio.in.SingleJarDownloader)3 SimpleApplicationContext (org.mapleir.app.client.SimpleApplicationContext)2 ApplicationClassSource (org.mapleir.app.service.ApplicationClassSource)2 InstalledRuntimeClassSource (org.mapleir.app.service.InstalledRuntimeClassSource)2 LibraryClassSource (org.mapleir.app.service.LibraryClassSource)2 AnalysisContext (org.mapleir.context.AnalysisContext)2 BasicAnalysisContext (org.mapleir.context.BasicAnalysisContext)2 IRCache (org.mapleir.context.IRCache)2 IPass (org.mapleir.deob.IPass)2 PassGroup (org.mapleir.deob.PassGroup)2 IRCallTracer (org.mapleir.deob.interproc.IRCallTracer)2 ControlFlowGraphDumper (org.mapleir.ir.algorithms.ControlFlowGraphDumper)2 ControlFlowGraph (org.mapleir.ir.cfg.ControlFlowGraph)2 ClassPrinter (org.mapleir.ir.printer.ClassPrinter)2 FieldNodePrinter (org.mapleir.ir.printer.FieldNodePrinter)2 MethodNodePrinter (org.mapleir.ir.printer.MethodNodePrinter)2 IPropertyDictionary (org.mapleir.propertyframework.api.IPropertyDictionary)2 TabbedStringWriter (org.mapleir.stdlib.util.TabbedStringWriter)2