From c45b8c4b5a8e202a813a2113f80d9d7fe40cf5cc Mon Sep 17 00:00:00 2001 From: Konloch Date: Sun, 27 Jun 2021 21:13:55 -0700 Subject: [PATCH] API Changes --- plugins/example/ExampleStringDecrypter.js | 2 +- .../club/bytecodeviewer/api/ASMUtil.java | 41 +++++++++++++++++++ .../bytecodeviewer/api/BytecodeViewer.java | 14 +++++++ .../club/bytecodeviewer/util/JarUtils.java | 10 +---- 4 files changed, 58 insertions(+), 9 deletions(-) create mode 100644 src/main/java/the/bytecode/club/bytecodeviewer/api/ASMUtil.java diff --git a/plugins/example/ExampleStringDecrypter.js b/plugins/example/ExampleStringDecrypter.js index f34ba041..f9cb847d 100644 --- a/plugins/example/ExampleStringDecrypter.js +++ b/plugins/example/ExampleStringDecrypter.js @@ -26,7 +26,7 @@ function execute(classNodeList) var fields = cn.fields.toArray(); //load the class node into the classloader - BytecodeViewer.getClassNodeLoader().addClass(cn); + BytecodeViewer.loadClassIntoClassLoader(cn); for (fieldIndex = 0; fieldIndex < fields.length; fieldIndex++) { diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/api/ASMUtil.java b/src/main/java/the/bytecode/club/bytecodeviewer/api/ASMUtil.java new file mode 100644 index 00000000..53e23ac2 --- /dev/null +++ b/src/main/java/the/bytecode/club/bytecodeviewer/api/ASMUtil.java @@ -0,0 +1,41 @@ +package the.bytecode.club.bytecodeviewer.api; + +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.tree.ClassNode; +import org.objectweb.asm.tree.MethodNode; + +/** + * @author Konloch + * @since 6/27/2021 + */ +public class ASMUtil +{ + /** + * Creates a new ClassNode instances from the provided byte[] + * + * @param b the class file's byte[] + * @return the ClassNode instance + */ + public static ClassNode getClassNode(final byte[] b) + { + ClassReader cr = new ClassReader(b); + ClassNode cn = new ClassNode(); + try { + cr.accept(cn, ClassReader.EXPAND_FRAMES); + } catch (Exception e) { + cr.accept(cn, ClassReader.SKIP_FRAMES); + } + return cn; + } + + public static MethodNode getMethodByName(ClassNode cn, String name) + { + for(MethodNode m : cn.methods) + { + if(m.name.equals(name)) + return m; + } + + return null; + } +} diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/api/BytecodeViewer.java b/src/main/java/the/bytecode/club/bytecodeviewer/api/BytecodeViewer.java index 969eb7cf..653da45b 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/api/BytecodeViewer.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/api/BytecodeViewer.java @@ -71,6 +71,19 @@ public class BytecodeViewer { * @return The loaded classes into the new URLClassLoader instance * @author Cafebabe */ + public static Class loadClassIntoClassLoader(ClassNode cn) + { + getClassNodeLoader().addClass(cn); + + try { + return cl.loadClass(cn.name); + } catch (Exception classLoadException) { + new the.bytecode.club.bytecodeviewer.api.ExceptionUI(classLoadException); + } + + return null; + } + public static List> loadClassesIntoClassLoader() { try @@ -102,6 +115,7 @@ public class BytecodeViewer { new the.bytecode.club.bytecodeviewer.api.ExceptionUI(classLoadException); } } + jarFile.close(); return ret; diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/util/JarUtils.java b/src/main/java/the/bytecode/club/bytecodeviewer/util/JarUtils.java index 91e10580..01a5bb69 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/util/JarUtils.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/util/JarUtils.java @@ -21,6 +21,7 @@ import org.objectweb.asm.ClassReader; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.tree.ClassNode; import the.bytecode.club.bytecodeviewer.BytecodeViewer; +import the.bytecode.club.bytecodeviewer.api.ASMUtil; import static the.bytecode.club.bytecodeviewer.Constants.*; @@ -248,14 +249,7 @@ public class JarUtils { * @return the ClassNode instance */ public static ClassNode getNode(final byte[] bytez) { - ClassReader cr = new ClassReader(bytez); - ClassNode cn = new ClassNode(); - try { - cr.accept(cn, ClassReader.EXPAND_FRAMES); - } catch (Exception e) { - cr.accept(cn, ClassReader.SKIP_FRAMES); - } - return cn; + return ASMUtil.getClassNode(bytez); } /**