List of usage examples for org.objectweb.asm.tree InsnList toArray
public AbstractInsnNode[] toArray()
From source file:com.android.builder.testing.MockableJarGenerator.java
License:Apache License
/** * Rewrites the method bytecode to remove the "Stub!" exception. *//*from w ww. j ava2 s .c o m*/ private void fixMethodBody(MethodNode methodNode, ClassNode classNode) { if ((methodNode.access & Opcodes.ACC_NATIVE) != 0 || (methodNode.access & Opcodes.ACC_ABSTRACT) != 0) { // Abstract and native method don't have bodies to rewrite. return; } if ((classNode.access & Opcodes.ACC_ENUM) != 0 && ENUM_METHODS.contains(methodNode.name)) { // Don't break enum classes. return; } Type returnType = Type.getReturnType(methodNode.desc); InsnList instructions = methodNode.instructions; if (methodNode.name.equals(CONSTRUCTOR)) { // Keep the call to parent constructor, delete the exception after that. boolean deadCode = false; for (AbstractInsnNode instruction : instructions.toArray()) { if (!deadCode) { if (instruction.getOpcode() == Opcodes.INVOKESPECIAL) { instructions.insert(instruction, new InsnNode(Opcodes.RETURN)); // Start removing all following instructions. deadCode = true; } } else { instructions.remove(instruction); } } } else { instructions.clear(); if (returnDefaultValues || methodNode.name.equals(CLASS_CONSTRUCTOR)) { if (INTEGER_LIKE_TYPES.contains(returnType)) { instructions.add(new InsnNode(Opcodes.ICONST_0)); } else if (returnType.equals(Type.LONG_TYPE)) { instructions.add(new InsnNode(Opcodes.LCONST_0)); } else if (returnType.equals(Type.FLOAT_TYPE)) { instructions.add(new InsnNode(Opcodes.FCONST_0)); } else if (returnType.equals(Type.DOUBLE_TYPE)) { instructions.add(new InsnNode(Opcodes.DCONST_0)); } else { instructions.add(new InsnNode(Opcodes.ACONST_NULL)); } instructions.add(new InsnNode(returnType.getOpcode(Opcodes.IRETURN))); } else { instructions.insert(throwExceptionsList(methodNode, classNode)); } } }
From source file:com.github.jasmo.util.BytecodeHelper.java
License:Open Source License
public static <T extends AbstractInsnNode> void forEach(InsnList instructions, Class<T> type, Consumer<T> consumer) { AbstractInsnNode[] array = instructions.toArray(); for (AbstractInsnNode node : array) { if (node.getClass() == type) { //noinspection unchecked consumer.accept((T) node);/*from ww w . j a va2 s .co m*/ } } }
From source file:com.heliosdecompiler.helios.handler.addons.builtin.ExtractStrings.java
License:Apache License
public void start(Display display, MenuItem menuItem) { menuItem.addSelectionListener(new SelectionAdapter() { @Override// w w w .ja v a 2 s. c o m public void widgetSelected(SelectionEvent e) { Helios.submitBackgroundTask(() -> { StringBuilder stringBuilder = new StringBuilder(); for (ClassNode classNode : FileManager.loadAllClasses()) { for (FieldNode fieldNode : classNode.fields) { Object v = fieldNode.value; if (v instanceof String) { String s = (String) v; if (!s.isEmpty()) { stringBuilder.append(String.format("%s.%s%s -> \"%s\"\n", classNode.name, fieldNode.name, fieldNode.desc, StringEscapeUtils.escapeJava(s))); } } if (v instanceof String[]) { for (int i = 0; i < ((String[]) v).length; i++) { String s = ((String[]) v)[i]; if (!s.isEmpty()) { stringBuilder.append(String.format("%s.%s%s[%s] -> \"%s\"\n", classNode.name, fieldNode.name, fieldNode.desc, i, StringEscapeUtils.escapeJava(s))); } } } } for (MethodNode m : classNode.methods) { InsnList insnList = m.instructions; for (AbstractInsnNode abstractInsnNode : insnList.toArray()) { if (abstractInsnNode instanceof LdcInsnNode) { if (((LdcInsnNode) abstractInsnNode).cst instanceof String) { final String s = (String) ((LdcInsnNode) abstractInsnNode).cst; if (!s.isEmpty()) { stringBuilder .append(String.format("%s.%s%s -> \"%s\"\n", classNode.name, m.name, m.desc, StringEscapeUtils.escapeJava(s))); } } } } } } display.syncExec(() -> { Shell shell = new Shell(display); shell.setImage(Resources.ICON.getImage()); shell.setText("Addon | Extract Strings"); shell.setLayout(new FillLayout()); Text text = new Text(shell, SWT.V_SCROLL | SWT.H_SCROLL); text.setText(stringBuilder.toString()); text.addListener(SWT.KeyDown, event -> { if (event.keyCode == 'a' && (event.stateMask & SWT.CTRL) != 0) { text.selectAll(); event.doit = false; } }); SWTUtil.center(shell); shell.open(); }); }); } }); }
From source file:com.samczsun.helios.handler.addons.builtin.ExtractStrings.java
License:Apache License
public void start(Display display, MenuItem menuItem) { menuItem.addSelectionListener(new SelectionAdapter() { @Override//from ww w. j a va 2s . c o m public void widgetSelected(SelectionEvent e) { Helios.submitBackgroundTask(() -> { StringBuilder stringBuilder = new StringBuilder(); for (ClassNode classNode : Helios.loadAllClasses()) { for (FieldNode fieldNode : classNode.fields) { Object v = fieldNode.value; if (v instanceof String) { String s = (String) v; if (!s.isEmpty()) { stringBuilder.append(String.format("%s.%s%s -> \"%s\"\n", classNode.name, fieldNode.name, fieldNode.desc, s.replaceAll("\\n", "\\\\n").replaceAll("\\r", "\\\\r"))); } } if (v instanceof String[]) { for (int i = 0; i < ((String[]) v).length; i++) { String s = ((String[]) v)[i]; if (!s.isEmpty()) { stringBuilder.append(String.format("%s.%s%s[%s] -> \"%s\"\n", classNode.name, fieldNode.name, fieldNode.desc, i, s.replaceAll("\\n", "\\\\n").replaceAll("\\r", "\\\\r"))); } } } } for (MethodNode m : classNode.methods) { InsnList insnList = m.instructions; for (AbstractInsnNode abstractInsnNode : insnList.toArray()) { if (abstractInsnNode instanceof LdcInsnNode) { if (((LdcInsnNode) abstractInsnNode).cst instanceof String) { final String s = (String) ((LdcInsnNode) abstractInsnNode).cst; if (!s.isEmpty()) { stringBuilder.append(String.format("%s.%s%s -> \"%s\"\n", classNode.name, m.name, m.desc, s.replaceAll("\\n", "\\\\n").replaceAll("\\r", "\\\\r"))); } } } } } } display.syncExec(() -> { Shell shell = new Shell(display); Text text = new Text(shell, SWT.V_SCROLL | SWT.H_SCROLL); text.setText(stringBuilder.toString().replace("\0", "{NUL}")); GC gc = new GC(shell); FontMetrics fm = gc.getFontMetrics(); int width = 128 * fm.getAverageCharWidth(); int height = fm.getHeight() * 32; text.setSize(width, height); text.addListener(SWT.KeyDown, event -> { if (event.keyCode == 'a' && (event.stateMask & SWT.CTRL) != 0) { text.selectAll(); event.doit = false; } }); shell.pack(); SWTUtil.center(shell); shell.open(); }); }); } }); }
From source file:me.themallard.bitmmo.api.analysis.util.LdcContains.java
License:Open Source License
public static boolean ListContains(InsnList il, String s) { for (AbstractInsnNode ain : il.toArray()) if (InstructionContains(ain, s)) return true; return false; }
From source file:org.springsource.loaded.TypeDiffComputer.java
License:Apache License
/** * Determine if there any differences between the methods supplied. A MethodDelta object is built to record any differences and * stored against the type delta.//from ww w .ja v a 2 s . co m * * @param oMethod 'old' method * @param nMethod 'new' method * @param td the type delta where changes are currently being accumulated */ private static void computeAnyMethodDifferences(MethodNode oMethod, MethodNode nMethod, TypeDelta td) { MethodDelta md = new MethodDelta(oMethod.name, oMethod.desc); if (oMethod.access != nMethod.access) { md.setAccessChanged(oMethod.access, nMethod.access); } // TODO annotations InsnList oInstructions = oMethod.instructions; InsnList nInstructions = nMethod.instructions; if (oInstructions.size() != nInstructions.size()) { md.setInstructionsChanged(oInstructions.toArray(), nInstructions.toArray()); } else { // TODO Just interested in constructors right now - should add others if (oMethod.name.charAt(0) == '<') { String oInvokeSpecialDescriptor = null; String nInvokeSpecialDescriptor = null; int oUninitCount = 0; int nUninitCount = 0; boolean codeChange = false; for (int i = 0, max = oInstructions.size(); i < max; i++) { AbstractInsnNode oInstruction = oInstructions.get(i); AbstractInsnNode nInstruction = nInstructions.get(i); if (!codeChange) { if (!sameInstruction(oInstruction, nInstruction)) { codeChange = true; } } if (oInstruction.getType() == AbstractInsnNode.TYPE_INSN) { if (oInstruction.getOpcode() == Opcodes.NEW) { oUninitCount++; } } if (nInstruction.getType() == AbstractInsnNode.TYPE_INSN) { if (nInstruction.getOpcode() == Opcodes.NEW) { nUninitCount++; } } if (oInstruction.getType() == AbstractInsnNode.METHOD_INSN) { MethodInsnNode mi = (MethodInsnNode) oInstruction; if (mi.getOpcode() == INVOKESPECIAL && mi.name.equals("<init>")) { if (oUninitCount == 0) { // this is the one! oInvokeSpecialDescriptor = mi.desc; } else { oUninitCount--; } } } if (nInstruction.getType() == AbstractInsnNode.METHOD_INSN) { MethodInsnNode mi = (MethodInsnNode) nInstruction; if (mi.getOpcode() == INVOKESPECIAL && mi.name.equals("<init>")) { if (nUninitCount == 0) { // this is the one! nInvokeSpecialDescriptor = mi.desc; } else { nUninitCount--; } } } } // Has the invokespecial changed? if (oInvokeSpecialDescriptor == null) { if (nInvokeSpecialDescriptor != null) { md.setInvokespecialChanged(oInvokeSpecialDescriptor, nInvokeSpecialDescriptor); } } else { if (!oInvokeSpecialDescriptor.equals(nInvokeSpecialDescriptor)) { md.setInvokespecialChanged(oInvokeSpecialDescriptor, nInvokeSpecialDescriptor); } } if (codeChange) { md.setCodeChanged(oInstructions.toArray(), nInstructions.toArray()); } } } if (md.hasAnyChanges()) { // it needs recording td.addChangedMethod(md); } }
From source file:the.bytecode.club.bytecodeviewer.plugin.preinstalled.AllatoriStringDecrypter.java
License:Open Source License
public void scanMethodNode(ClassNode classNode, MethodNode methodNode) throws Exception { InsnList iList = methodNode.instructions; log("Scanning method " + methodNode.name + " of " + classNode.name); LdcInsnNode laststringldconstack = null; for (AbstractInsnNode i : iList.toArray()) { if (i instanceof LdcInsnNode) { LdcInsnNode ldci = (LdcInsnNode) i; if (ldci.cst instanceof String) { laststringldconstack = ldci; }//from w w w . jav a2 s.c om continue; } else if (i instanceof MethodInsnNode) { MethodInsnNode methodi = (MethodInsnNode) i; if (laststringldconstack != null && methodi.opcode() == 0xb8) { // Decryption is always a static call - 0xb8 - invokestatic String decrypterclassname = methodi.owner; String decryptermethodname = methodi.name; if (decrypterclassname.contains("$")) { // Decrypter is always a static method of other class's inner class byte[] decrypterFileContents = BytecodeViewer .getFileContents(decrypterclassname + ".class"); // We have to create new node for editing // Also, one decrypter method could be used for multiple methods in code, what gives us only part of string decrypted ClassNode decrypterclassnode = JarUtils.getNode(decrypterFileContents); if (decrypterclassnode != null) { MethodNode decryptermethodnode = null; for (Object uncasted : decrypterclassnode.methods) { if (((MethodNode) uncasted).name.equals(decryptermethodname)) { decryptermethodnode = (MethodNode) uncasted; } } if (decryptermethodnode != null) { String keyString = (getConstantPoolSize(classNode.name) + classNode.name + methodNode.name + getConstantPoolSize(classNode.name)); int newHashCode = keyString.hashCode(); scanDecrypter(decryptermethodnode, newHashCode); try { System.out.println("loading " + decrypterclassname); List<Class<?>> decrypterclasslist = the.bytecode.club.bytecodeviewer.api.BytecodeViewer .loadClassesIntoClassLoader(new ArrayList<ClassNode>( Arrays.asList(new ClassNode[] { decrypterclassnode }))); String decrypted = invokeDecrypter(decrypterclasslist.get(0), decryptermethodname, (String) laststringldconstack.cst); if (decrypted != null) { log("Succesfully invoked decrypter method: " + decrypted); laststringldconstack.cst = decrypted; iList.remove(methodi); } } catch (IndexOutOfBoundsException | ClassNotFoundException | IOException e) { e.printStackTrace(); log("Could not load decrypter class: " + decrypterclassname); } } else { log("Could not find decrypter method (" + decryptermethodname + ") of class " + decrypterclassname); } } else { log("Could not find decrypter ClassNode of class " + decrypterclassname); } } } } else if (i instanceof InvokeDynamicInsnNode) { InvokeDynamicInsnNode methodi = (InvokeDynamicInsnNode) i; if (methodi.opcode() == 0xba) { // TODO: Safe-reflection deobfuscator here // Allatori replaces invokeinterface and invokestatic with invokedynamic //log(methodi.bsm.getOwner()+" dot "+methodi.bsm.getName()); //iList.set(methodi, new MethodInsnNode(0xb8, methodi.bsm.getOwner(), methodi.bsm.getName(), methodi.bsm.getDesc(), false)); } } laststringldconstack = null; } }
From source file:the.bytecode.club.bytecodeviewer.plugin.preinstalled.AllatoriStringDecrypter.java
License:Open Source License
private boolean scanDecrypter(MethodNode decryptermethodnode, int newHashCode) { InsnList iList = decryptermethodnode.instructions; AbstractInsnNode insn = null, removeInsn = null; for (AbstractInsnNode i : iList.toArray()) { if (i instanceof MethodInsnNode) { MethodInsnNode methodi = ((MethodInsnNode) i); if ("currentThread".equals(methodi.name)) { // find code form this instruction insn = i;/*from ww w . j a v a 2 s . co m*/ break; } } } if (insn == null) { return false; } while (insn != null) { if (insn instanceof MethodInsnNode) { MethodInsnNode methodi = ((MethodInsnNode) insn); if ("hashCode".equals(methodi.name)) { // to this instruction break; } } removeInsn = insn; insn = insn.getNext(); iList.remove(removeInsn); // and remove it } if (insn == null) return false; iList.set(insn, new LdcInsnNode(newHashCode)); // then replace it with pre-computed key LDC return true; }
From source file:the.bytecode.club.bytecodeviewer.plugin.preinstalled.MaliciousCodeScanner.java
License:Open Source License
@Override public void execute(ArrayList<ClassNode> classNodeList) { PluginConsole frame = new PluginConsole("Malicious Code Scanner"); StringBuilder sb = new StringBuilder(); for (ClassNode classNode : classNodeList) { for (Object o : classNode.fields.toArray()) { FieldNode f = (FieldNode) o; Object v = f.value;/*from w ww. j av a 2s .c om*/ if (v instanceof String) { String s = (String) v; if ((LWW && s.contains("www.")) || (LHT && s.contains("http://")) || (LHS && s.contains("https://")) || (ORE && s.contains("java/lang/Runtime")) || (ORE && s.contains("java.lang.Runtime")) || (ROB && s.contains("java.awt.Robot")) || (ROB && s.contains("java/awt/Robot")) || (LIP && s.matches("\\b\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\b"))) sb.append("Found LDC \"" + s + "\" at field " + classNode.name + "." + f.name + "(" + f.desc + ")" + BytecodeViewer.nl); } if (v instanceof String[]) { for (int i = 0; i < ((String[]) v).length; i++) { String s = ((String[]) v)[i]; if ((LWW && s.contains("www.")) || (LHT && s.contains("http://")) || (LHS && s.contains("https://")) || (ORE && s.contains("java/lang/Runtime")) || (ORE && s.contains("java.lang.Runtime")) || (ROB && s.contains("java.awt.Robot")) || (ROB && s.contains("java/awt/Robot")) || (LIP && s.matches("\\b\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\b"))) sb.append("Found LDC \"" + s + "\" at field " + classNode.name + "." + f.name + "(" + f.desc + ")" + BytecodeViewer.nl); } } } boolean prevInsn_aconst_null = false; for (Object o : classNode.methods.toArray()) { MethodNode m = (MethodNode) o; InsnList iList = m.instructions; for (AbstractInsnNode a : iList.toArray()) { if (a instanceof MethodInsnNode) { final MethodInsnNode min = (MethodInsnNode) a; if ((ORE && min.owner.startsWith("java/lang/reflect")) || (ONE && min.owner.startsWith("java/net")) || (ORU && min.owner.equals("java/lang/Runtime")) || (ROB && min.owner.equals("java/awt/Robot")) || (OIO && min.owner.startsWith("java/io"))) { sb.append("Found Method call to " + min.owner + "." + min.name + "(" + min.desc + ") at " + classNode.name + "." + m.name + "(" + m.desc + ")" + BytecodeViewer.nl); } } if (a instanceof LdcInsnNode) { if (((LdcInsnNode) a).cst instanceof String) { final String s = (String) ((LdcInsnNode) a).cst; if ((LWW && s.contains("www.")) || (LHT && s.contains("http://")) || (LHS && s.contains("https://")) || (ORE && s.contains("java/lang/Runtime")) || (ORE && s.contains("java.lang.Runtime")) || (ROB && s.contains("java.awt.Robot")) || (ROB && s.contains("java/awt/Robot")) || (LIP && s.matches("\\b\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\b"))) { sb.append("Found LDC \"" + s + "\" at method " + classNode.name + "." + m.name + "(" + m.desc + ")" + BytecodeViewer.nl); } } } // Check if the security manager is getting set to null if ((a instanceof InsnNode) && (a.opcode() == Opcodes.ACONST_NULL)) { prevInsn_aconst_null = true; } else if ((a instanceof MethodInsnNode) && (a.opcode() == Opcodes.INVOKESTATIC)) { final String owner = ((MethodInsnNode) a).owner; final String name = ((MethodInsnNode) a).name; if ((NSM && prevInsn_aconst_null && owner.equals("java/lang/System") && name.equals("setSecurityManager"))) { sb.append("Found Security Manager set to null at method " + classNode.name + "." + m.name + "(" + m.desc + ")" + BytecodeViewer.nl); prevInsn_aconst_null = false; } } else { prevInsn_aconst_null = false; } } } } frame.appendText(sb.toString()); frame.setVisible(true); }
From source file:the.bytecode.club.bytecodeviewer.plugin.preinstalled.ReplaceStrings.java
License:Open Source License
public void scanClassNode(ClassNode classNode) { for (Object o : classNode.fields.toArray()) { FieldNode f = (FieldNode) o;// ww w.j a va 2s. c o m Object v = f.value; if (v instanceof String) { String s = (String) v; if (contains) { if (s.contains(originalLDC)) f.value = ((String) f.value).replaceAll(originalLDC, newLDC); } else { if (s.equals(originalLDC)) f.value = newLDC; } } if (v instanceof String[]) { for (int i = 0; i < ((String[]) v).length; i++) { String s = ((String[]) v)[i]; if (contains) { if (s.contains(originalLDC)) { f.value = ((String[]) f.value)[i].replaceAll(originalLDC, newLDC); String ugh = s.replaceAll("\\n", "\\\\n").replaceAll("\\r", "\\\\r"); frame.appendText(classNode.name + "." + f.name + "" + f.desc + " -> \"" + ugh + "\" replaced with \"" + s.replaceAll(originalLDC, newLDC) + "\""); } } else { if (s.equals(originalLDC)) { ((String[]) f.value)[i] = newLDC; String ugh = s.replaceAll("\\n", "\\\\n").replaceAll("\\r", "\\\\r"); frame.appendText(classNode.name + "." + f.name + "" + f.desc + " -> \"" + ugh + "\" replaced with \"" + newLDC + "\""); } } } } } for (Object o : classNode.methods.toArray()) { MethodNode m = (MethodNode) o; InsnList iList = m.instructions; for (AbstractInsnNode a : iList.toArray()) { if (a instanceof LdcInsnNode) { if (((LdcInsnNode) a).cst instanceof String) { final String s = (String) ((LdcInsnNode) a).cst; if (contains) { if (s.contains(originalLDC)) { ((LdcInsnNode) a).cst = ((String) ((LdcInsnNode) a).cst).replaceAll(originalLDC, newLDC); String ugh = s.replaceAll("\\n", "\\\\n").replaceAll("\\r", "\\\\r"); frame.appendText( classNode.name + "." + m.name + "" + m.desc + " -> \"" + ugh + "\" replaced with \"" + s.replaceAll(originalLDC, newLDC) .replaceAll("\\n", "\\\\n").replaceAll("\\r", "\\\\r") + "\""); } } else { if (s.equals(originalLDC)) { ((LdcInsnNode) a).cst = newLDC; String ugh = s.replaceAll("\\n", "\\\\n").replaceAll("\\r", "\\\\r"); frame.appendText(classNode.name + "." + m.name + "" + m.desc + " -> \"" + ugh + "\" replaced with \"" + newLDC.replaceAll("\\n", "\\\\n").replaceAll("\\r", "\\\\r") + "\""); } } } } } } }