Search in sources :

Example 1 with IR2JConverter

use of com.googlecode.d2j.converter.IR2JConverter in project dex2jar by pxb1988.

the class DecryptStringCmd method decryptByIr.

private boolean decryptByIr(ClassNode cn, Map<MethodConfig, MethodConfig> map) {
    MethodConfig key = this.key;
    boolean changed = false;
    for (Iterator<MethodNode> it = cn.methods.iterator(); it.hasNext(); ) {
        MethodNode m = it.next();
        if (m.instructions == null) {
            continue;
        }
        key.owner = cn.name;
        key.name = m.name;
        key.desc = m.desc;
        if (map.containsKey(key)) {
            if (deleteMethod) {
                it.remove();
            }
            continue;
        }
        if (false && verbose) {
            System.out.println();
            System.out.println("===============");
            System.out.println("on method " + cn.name + ";->" + m.name + m.desc);
        }
        boolean find = false;
        // search for the decrypt method
        for (AbstractInsnNode p = m.instructions.getFirst(); p != null; p = p.getNext()) {
            if (p.getOpcode() == Opcodes.INVOKESTATIC) {
                MethodInsnNode mn = (MethodInsnNode) p;
                key.owner = mn.owner;
                key.name = mn.name;
                key.desc = mn.desc;
                MethodConfig config = map.get(key);
                if (config != null) {
                    find = true;
                }
            }
        }
        if (find) {
            try {
                // copy m to m2 for cleanup debug info
                MethodNode m2 = new MethodNode();
                m2.tryCatchBlocks = new ArrayList<>();
                m2.name = m.name;
                m2.access = m.access;
                m2.desc = m.desc;
                m.accept(m2);
                cleanDebug(m2);
                // convert m2 to ir
                IrMethod irMethod = J2IRConverter.convert(cn.name, m2);
                // opt and decrypt
                optAndDecrypt(irMethod, map);
                // convert ir to m3
                MethodNode m3 = new MethodNode();
                m3.tryCatchBlocks = new ArrayList<>();
                new IR2JConverter(true).convert(irMethod, m3);
                // copy back m3 to m
                m.maxLocals = -1;
                m.maxLocals = -1;
                m.instructions = m3.instructions;
                m.tryCatchBlocks = m3.tryCatchBlocks;
                m.localVariables = null;
                changed = true;
            } catch (Exception ex) {
                if (verbose) {
                    ex.printStackTrace();
                }
            }
        }
    }
    return changed;
}
Also used : IrMethod(com.googlecode.dex2jar.ir.IrMethod) IR2JConverter(com.googlecode.d2j.converter.IR2JConverter) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with IR2JConverter

use of com.googlecode.d2j.converter.IR2JConverter in project dex2jar by pxb1988.

the class Dex2Asm method ir2j.

public void ir2j(IrMethod irMethod, MethodVisitor mv) {
    new IR2JConverter(false).convert(irMethod, mv);
    mv.visitMaxs(-1, -1);
}
Also used : IR2JConverter(com.googlecode.d2j.converter.IR2JConverter)

Example 3 with IR2JConverter

use of com.googlecode.d2j.converter.IR2JConverter in project dex2jar by pxb1988.

the class Dex2jar method doTranslate.

private void doTranslate(final Path dist) throws IOException {
    DexFileNode fileNode = new DexFileNode();
    try {
        reader.accept(fileNode, readerConfig | DexFileReader.IGNORE_READ_EXCEPTION);
    } catch (Exception ex) {
        exceptionHandler.handleFileException(ex);
    }
    ClassVisitorFactory cvf = new ClassVisitorFactory() {

        @Override
        public ClassVisitor create(final String name) {
            return new ClassVisitor(Opcodes.ASM4, new ClassWriter(ClassWriter.COMPUTE_MAXS)) {

                @Override
                public void visitEnd() {
                    super.visitEnd();
                    ClassWriter cw = (ClassWriter) super.cv;
                    byte[] data;
                    try {
                        // FIXME handle 'java.lang.RuntimeException: Method code too large!'
                        data = cw.toByteArray();
                    } catch (Exception ex) {
                        System.err.println(String.format("ASM fail to generate .class file: %s", name));
                        exceptionHandler.handleFileException(ex);
                        return;
                    }
                    try {
                        Path dist1 = dist.resolve(name + ".class");
                        Path parent = dist1.getParent();
                        if (parent != null && !Files.exists(parent)) {
                            Files.createDirectories(parent);
                        }
                        Files.write(dist1, data);
                    } catch (IOException e) {
                        e.printStackTrace(System.err);
                    }
                }
            };
        }
    };
    new ExDex2Asm(exceptionHandler) {

        public void convertCode(DexMethodNode methodNode, MethodVisitor mv) {
            if ((readerConfig & DexFileReader.SKIP_CODE) != 0 && methodNode.method.getName().equals("<clinit>")) {
                // also skip clinit
                return;
            }
            super.convertCode(methodNode, mv);
        }

        @Override
        public void optimize(IrMethod irMethod) {
            T_cleanLabel.transform(irMethod);
            if (0 != (v3Config & V3.TOPOLOGICAL_SORT)) {
            // T_topologicalSort.transform(irMethod);
            }
            T_deadCode.transform(irMethod);
            T_removeLocal.transform(irMethod);
            T_removeConst.transform(irMethod);
            T_zero.transform(irMethod);
            if (T_npe.transformReportChanged(irMethod)) {
                T_deadCode.transform(irMethod);
                T_removeLocal.transform(irMethod);
                T_removeConst.transform(irMethod);
            }
            T_new.transform(irMethod);
            T_fillArray.transform(irMethod);
            T_agg.transform(irMethod);
            T_multiArray.transform(irMethod);
            T_voidInvoke.transform(irMethod);
            if (0 != (v3Config & V3.PRINT_IR)) {
                int i = 0;
                for (Stmt p : irMethod.stmts) {
                    if (p.st == Stmt.ST.LABEL) {
                        LabelStmt labelStmt = (LabelStmt) p;
                        labelStmt.displayName = "L" + i++;
                    }
                }
                System.out.println(irMethod);
            }
            T_type.transform(irMethod);
            T_unssa.transform(irMethod);
            T_ir2jRegAssign.transform(irMethod);
            T_trimEx.transform(irMethod);
        }

        @Override
        public void ir2j(IrMethod irMethod, MethodVisitor mv) {
            new IR2JConverter(0 != (V3.OPTIMIZE_SYNCHRONIZED & v3Config)).convert(irMethod, mv);
        }
    }.convertDex(fileNode, cvf);
}
Also used : Path(java.nio.file.Path) LabelStmt(com.googlecode.dex2jar.ir.stmt.LabelStmt) ClassVisitor(org.objectweb.asm.ClassVisitor) IOException(java.io.IOException) IrMethod(com.googlecode.dex2jar.ir.IrMethod) IOException(java.io.IOException) ClassWriter(org.objectweb.asm.ClassWriter) MethodVisitor(org.objectweb.asm.MethodVisitor) LabelStmt(com.googlecode.dex2jar.ir.stmt.LabelStmt) Stmt(com.googlecode.dex2jar.ir.stmt.Stmt) DexMethodNode(com.googlecode.d2j.node.DexMethodNode) DexFileNode(com.googlecode.d2j.node.DexFileNode) IR2JConverter(com.googlecode.d2j.converter.IR2JConverter)

Aggregations

IR2JConverter (com.googlecode.d2j.converter.IR2JConverter)3 IrMethod (com.googlecode.dex2jar.ir.IrMethod)2 IOException (java.io.IOException)2 DexFileNode (com.googlecode.d2j.node.DexFileNode)1 DexMethodNode (com.googlecode.d2j.node.DexMethodNode)1 LabelStmt (com.googlecode.dex2jar.ir.stmt.LabelStmt)1 Stmt (com.googlecode.dex2jar.ir.stmt.Stmt)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Path (java.nio.file.Path)1 ClassVisitor (org.objectweb.asm.ClassVisitor)1 ClassWriter (org.objectweb.asm.ClassWriter)1 MethodVisitor (org.objectweb.asm.MethodVisitor)1