From 54c7526a66a6bb3397e62c32794ffc42e3da061f Mon Sep 17 00:00:00 2001 From: Gravit Date: Sun, 7 Apr 2019 15:33:57 +0700 Subject: [PATCH] =?UTF-8?q?[FIX]=20=D0=A3=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D0=B0=20sm=20=D1=87=D0=B0=D1=81=D1=82=D1=8C=20LauncherAgent?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ru/gravit/launcher/LauncherAgent.java | 108 ------------------ 1 file changed, 108 deletions(-) diff --git a/Launcher/src/main/java/ru/gravit/launcher/LauncherAgent.java b/Launcher/src/main/java/ru/gravit/launcher/LauncherAgent.java index 14117c8e..877f018f 100644 --- a/Launcher/src/main/java/ru/gravit/launcher/LauncherAgent.java +++ b/Launcher/src/main/java/ru/gravit/launcher/LauncherAgent.java @@ -43,117 +43,9 @@ public static void premain(String agentArgument, Instrumentation instrumentation if (trimmedArg.contains("r")) rt = false; } } - if (rt || pb) replaceClasses(pb, rt); } public static boolean isStarted() { return isAgentStarted; } - - /** - * @author https://github.com/Konloch/JVM-Sandbox - * Replaces the Runtime class via instrumentation, transforms the class via ASM - */ - private static void replaceClasses(boolean pb, boolean rt) { - for(Class c : inst.getAllLoadedClasses()) { - if(rt && c.getName().equals("java.lang.Runtime")) { - try { - inst.redefineClasses(new java.lang.instrument.ClassDefinition(java.lang.Runtime.class, transformClass(c.getName(), getClassFile(c)))); - } catch(Exception e) { - e.printStackTrace(); - } - } - if(pb && c.getName().equals("java.lang.ProcessBuilder")) { - try { - inst.redefineClasses(new java.lang.instrument.ClassDefinition(java.lang.ProcessBuilder.class, transformClass(c.getName(), getClassFile(c)))); - } catch(Exception e) { - e.printStackTrace(); - } - } - if(c.getName().equals("java.awt.Robot")) { - try { - inst.redefineClasses(new java.lang.instrument.ClassDefinition(java.lang.ProcessBuilder.class, transformClass(c.getName(), getClassFile(c)))); - } catch(Exception e) { - e.printStackTrace(); - } - } - } - } - - /** - * @author https://github.com/Konloch/JVM-Sandbox - * Use ASM to modify the byte array - */ - private static byte[] transformClass(String className, byte[] classBytes) { - if (className.equals("java.lang.Runtime")) { - ClassReader cr=new ClassReader(classBytes); - ClassNode cn=new ClassNode(); - cr.accept(cn,ClassReader.EXPAND_FRAMES); - - for (Object o : cn.methods.toArray()) { - MethodNode m = (MethodNode) o; - if(m.name.equals("exec")) { - m.instructions.insert(new InsnNode(ARETURN)); - m.instructions.insert(new InsnNode(ACONST_NULL)); - } - } - ClassWriter cw=new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); - cn.accept(cw); - return cw.toByteArray(); - } else if (className.equals("java.lang.ProcessBuilder")) { - ClassReader cr=new ClassReader(classBytes); - ClassNode cn=new ClassNode(); - cr.accept(cn,ClassReader.EXPAND_FRAMES); - - for (Object o : cn.methods.toArray()) { - MethodNode m = (MethodNode) o; - if(m.name.equals("start")) { - m.instructions.insert(new InsnNode(ARETURN)); - m.instructions.insert(new InsnNode(ACONST_NULL)); - } - } - ClassWriter cw=new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); - cn.accept(cw); - return cw.toByteArray(); - } else if (className.equals("java.awt.Robot")) { - ClassReader cr=new ClassReader(classBytes); - ClassNode cn=new ClassNode(); - cr.accept(cn,ClassReader.EXPAND_FRAMES); - - for (Object o : cn.methods.toArray()) { - MethodNode m = (MethodNode) o; - if( m.name.equals("createScreenCapture") || m.name.equals("getPixelColor") || - m.name.equals("keyPress") || m.name.equals("keyRelease") || - m.name.equals("mouseMove") || m.name.equals("mousePress") || - m.name.equals("mouseWheel")) - { - m.instructions.insert(new InsnNode(ARETURN)); - m.instructions.insert(new InsnNode(ACONST_NULL)); - } - } - ClassWriter cw=new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); - cn.accept(cw); - return cw.toByteArray(); - } - return classBytes; - } - - /** - * @author https://github.com/Konloch/JVM-Sandbox - * Do not remove this method. Do not to cause classloading! - * Grab the byte array from the loaded Class object - * @param clazz - * @return array, respending this class in bytecode. - * @throws IOException - */ - private static byte[] getClassFile(Class clazz) throws IOException { - InputStream is = clazz.getResourceAsStream( "/" + clazz.getName().replace('.', '/') + ".class"); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - int r = 0; - byte[] buffer = new byte[8192]; - while((r=is.read(buffer))>=0) { - baos.write(buffer, 0, r); - } - return baos.toByteArray(); - } }