|
| 1 | +import club.bytecode.the.jda.FileContainer; |
| 2 | +import club.bytecode.the.jda.api.JDAPlugin; |
| 3 | +import club.bytecode.the.jda.decompilers.Decompilers; |
| 4 | +import club.bytecode.the.jda.decompilers.JDADecompiler; |
| 5 | +import org.mapleir.DefaultInvocationResolver; |
| 6 | +import org.mapleir.app.client.SimpleApplicationContext; |
| 7 | +import org.mapleir.app.service.ApplicationClassSource; |
| 8 | +import org.mapleir.app.service.InstalledRuntimeClassSource; |
| 9 | +import org.mapleir.app.service.LibraryClassSource; |
| 10 | +import org.mapleir.context.AnalysisContext; |
| 11 | +import org.mapleir.context.BasicAnalysisContext; |
| 12 | +import org.mapleir.context.IRCache; |
| 13 | +import org.mapleir.deob.IPass; |
| 14 | +import org.mapleir.deob.PassGroup; |
| 15 | +import org.mapleir.deob.interproc.IRCallTracer; |
| 16 | +import org.mapleir.deob.passes.ConstantExpressionReorderPass; |
| 17 | +import org.mapleir.deob.util.RenamingHeuristic; |
| 18 | +import org.mapleir.ir.algorithms.BoissinotDestructor; |
| 19 | +import org.mapleir.ir.algorithms.ControlFlowGraphDumper; |
| 20 | +import org.mapleir.ir.cfg.ControlFlowGraph; |
| 21 | +import org.mapleir.ir.cfg.builder.ControlFlowGraphBuilder; |
| 22 | +import org.objectweb.asm.tree.ClassNode; |
| 23 | +import org.objectweb.asm.tree.MethodNode; |
| 24 | +import org.topdank.byteengineer.commons.data.JarInfo; |
| 25 | +import org.topdank.byteio.in.SingleJarDownloader; |
| 26 | + |
| 27 | +import java.io.File; |
| 28 | +import java.io.IOException; |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.HashMap; |
| 31 | +import java.util.Map; |
| 32 | + |
| 33 | +public class MaplePlugin implements JDAPlugin { |
| 34 | + public static Map<MethodNode, ControlFlowGraph> cfgs; |
| 35 | + public static AnalysisContext cxt; |
| 36 | + public static JDADecompiler MAPLEIR = new IRDecompiler(); |
| 37 | + public static JDADecompiler MAPLEIL = new ILDecompiler(); |
| 38 | + |
| 39 | + public MaplePlugin() { |
| 40 | + Decompilers.BY_NAME.put("MapleIR", MAPLEIR); |
| 41 | + Decompilers.BY_NAME.put("MapleIL", MAPLEIL); |
| 42 | + System.out.println("MapleIR plugin loaded"); |
| 43 | + } |
| 44 | + |
| 45 | + public static void main(String[] args) { |
| 46 | + new MaplePlugin(); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public int onGUILoad() { |
| 51 | + cfgs = new HashMap<>(); |
| 52 | + return 0; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public int onExit() { |
| 57 | + return 0; |
| 58 | + } |
| 59 | + |
| 60 | + private static LibraryClassSource rt(ApplicationClassSource app, File rtjar) throws IOException { |
| 61 | + System.out.println("Loading rt.jar from " + rtjar.getAbsolutePath()); |
| 62 | + SingleJarDownloader<ClassNode> dl = new SingleJarDownloader<>(new JarInfo(rtjar)); |
| 63 | + dl.download(); |
| 64 | + |
| 65 | + return new LibraryClassSource(app, dl.getJarContents().getClassContents()); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public int onAddFile(FileContainer fileContainer) { |
| 70 | + //ManagementFactory.getRuntimeMXBean().getBootClassPath() |
| 71 | + File rtjar = new File("C:\\Program Files\\Java\\jdk1.8.0_131\\jre\\lib\\rt.jar"); |
| 72 | + // Load input jar |
| 73 | + ApplicationClassSource app = new ApplicationClassSource(fileContainer.name, fileContainer.getClasses()); |
| 74 | + // app.addLibraries(new InstalledRuntimeClassSource(app)); |
| 75 | + try { |
| 76 | + app.addLibraries(rt(app, rtjar), new InstalledRuntimeClassSource(app)); |
| 77 | + } catch (IOException e) { |
| 78 | + e.printStackTrace(); |
| 79 | + return 1; |
| 80 | + } |
| 81 | + System.out.println("Initialising context."); |
| 82 | + |
| 83 | + cxt = new BasicAnalysisContext.BasicContextBuilder() |
| 84 | + .setApplication(app) |
| 85 | + .setInvocationResolver(new DefaultInvocationResolver(app)) |
| 86 | + .setCache(new IRCache(ControlFlowGraphBuilder::build)) |
| 87 | + .setApplicationContext(new SimpleApplicationContext(app)) |
| 88 | + .build(); |
| 89 | + |
| 90 | + System.out.println("Expanding callgraph and generating cfgs."); |
| 91 | + |
| 92 | + IRCallTracer tracer = new IRCallTracer(cxt); |
| 93 | + for(ClassNode cn : app.iterate()) { |
| 94 | + System.out.println(cn); |
| 95 | + for(MethodNode m : cn.methods) { |
| 96 | + System.out.println(m); |
| 97 | + try { |
| 98 | + tracer.trace(m); |
| 99 | + }catch(Exception e){e.printStackTrace();} |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // for(ClassNode cn : app.iterate()) { |
| 104 | + // TabbedStringWriter sw = new TabbedStringWriter(); |
| 105 | + // sw.setTabString(" "); |
| 106 | + // IPropertyDictionary settings = PropertyHelper.createDictionary(); |
| 107 | + // // settings.put(new BooleanProperty(ASMPrinter.PROP_ACCESS_FLAG_SAFE, true)); |
| 108 | + // ClassPrinter cp = new ClassPrinter(sw, settings, |
| 109 | + // new FieldNodePrinter(sw, settings), |
| 110 | + // new MethodNodePrinter(sw, settings) { |
| 111 | + // @Override |
| 112 | + // protected ControlFlowGraph getCfg(MethodNode mn) { |
| 113 | + // return cxt.getIRCache().getFor(mn); |
| 114 | + // } |
| 115 | + // |
| 116 | + // }); |
| 117 | + // cp.print(cn); |
| 118 | + // System.out.println(sw.toString()); |
| 119 | + // } |
| 120 | + |
| 121 | + System.out.println("...generated " + cxt.getIRCache().size() + " cfgs.\nPreparing to transform."); |
| 122 | + |
| 123 | + // do passes |
| 124 | + PassGroup masterGroup = new PassGroup("MasterController"); |
| 125 | + for(IPass p : getTransformationPasses()) { |
| 126 | + masterGroup.add(p); |
| 127 | + } |
| 128 | + run(cxt, masterGroup); |
| 129 | + |
| 130 | + // for(MethodNode m : cxt.getIRCache().getActiveMethods()) { |
| 131 | + // if(m.instructions.size() > 100 && m.instructions.size() < 500) { |
| 132 | + // System.out.println(cxt.getIRCache().get(m)); |
| 133 | + // } |
| 134 | + // } |
| 135 | + |
| 136 | + System.out.println("Retranslating SSA IR to standard flavour."); |
| 137 | + for(Map.Entry<MethodNode, ControlFlowGraph> e : cxt.getIRCache().entrySet()) { |
| 138 | + try { |
| 139 | + MethodNode mn = e.getKey(); |
| 140 | + ControlFlowGraph cfg = e.getValue(); |
| 141 | + |
| 142 | + BoissinotDestructor.leaveSSA(cfg); |
| 143 | + cfg.getLocals().realloc(cfg); |
| 144 | + (new ControlFlowGraphDumper(cfg, mn)).dump(); |
| 145 | + cfgs.put(mn, cfg); |
| 146 | + }catch(Exception e3){e3.printStackTrace();} |
| 147 | + } |
| 148 | + |
| 149 | + System.out.println("Rewriting jar."); |
| 150 | + // dumpJar(app, dl, masterGroup, "out/osb5.jar"); |
| 151 | + |
| 152 | + System.out.println("Finished."); |
| 153 | + |
| 154 | + return 0; |
| 155 | + } |
| 156 | + |
| 157 | + private static void run(AnalysisContext cxt, PassGroup group) { |
| 158 | + group.accept(cxt, null, new ArrayList<>()); |
| 159 | + } |
| 160 | + |
| 161 | + private static IPass[] getTransformationPasses() { |
| 162 | + RenamingHeuristic heuristic = RenamingHeuristic.RENAME_ALL; |
| 163 | + return new IPass[] { |
| 164 | + // new ConcreteStaticInvocationPass(), |
| 165 | + // new ClassRenamerPass(heuristic), |
| 166 | + // new MethodRenamerPass(heuristic), |
| 167 | + // new FieldRenamerPass(), |
| 168 | + // new CallgraphPruningPass(), |
| 169 | + |
| 170 | + // new PassGroup("Interprocedural Optimisations") |
| 171 | + // .add(new ConstantParameterPass()) |
| 172 | + // new LiftConstructorCallsPass(), |
| 173 | + // new DemoteRangesPass(), |
| 174 | + |
| 175 | + new ConstantExpressionReorderPass(), |
| 176 | + // new FieldRSADecryptionPass(), |
| 177 | + // new ConstantParameterPass(), |
| 178 | + // new ConstantExpressionEvaluatorPass(), |
| 179 | + // new DeadCodeEliminationPass() |
| 180 | + |
| 181 | + }; |
| 182 | + } |
| 183 | +} |
0 commit comments