From 555c6a6c0bd90e9144800e2085f3b8a675f99e83 Mon Sep 17 00:00:00 2001 From: Zaxar163 Date: Tue, 24 Sep 2019 17:50:24 +0200 Subject: [PATCH] =?UTF-8?q?[FIX]=20=D0=9F=D0=BE=D0=B4=D0=B0=D1=80=D0=BE?= =?UTF-8?q?=D0=BA=20=D1=87=D0=B8=D1=82=D0=B5=D1=80=D0=B0=D0=BC.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../launcher/ClientLauncherWrapper.java | 2 +- .../pro/gravit/launcher/LauncherAgent.java | 151 +----------------- .../launcher/client/ClientLauncher.java | 6 +- 3 files changed, 14 insertions(+), 145 deletions(-) diff --git a/Launcher/src/main/java/pro/gravit/launcher/ClientLauncherWrapper.java b/Launcher/src/main/java/pro/gravit/launcher/ClientLauncherWrapper.java index 02cb11dc..aaeba1ec 100644 --- a/Launcher/src/main/java/pro/gravit/launcher/ClientLauncherWrapper.java +++ b/Launcher/src/main/java/pro/gravit/launcher/ClientLauncherWrapper.java @@ -83,7 +83,7 @@ public static void main(String[] arguments) throws IOException, InterruptedExcep } Collections.addAll(args, MAGIC_ARG); Collections.addAll(args, "-XX:+DisableAttachMechanism"); - Collections.addAll(args, "-javaagent:".concat(pathLauncher).concat("=pr")); + Collections.addAll(args, "-javaagent:".concat(pathLauncher)); Collections.addAll(args, "-cp"); Collections.addAll(args, pathLauncher); Collections.addAll(args, LauncherEngine.class.getName()); diff --git a/Launcher/src/main/java/pro/gravit/launcher/LauncherAgent.java b/Launcher/src/main/java/pro/gravit/launcher/LauncherAgent.java index 01122aff..1fd1c15c 100644 --- a/Launcher/src/main/java/pro/gravit/launcher/LauncherAgent.java +++ b/Launcher/src/main/java/pro/gravit/launcher/LauncherAgent.java @@ -1,25 +1,11 @@ package pro.gravit.launcher; -import static org.objectweb.asm.Opcodes.ACONST_NULL; -import static org.objectweb.asm.Opcodes.ARETURN; - -import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; -import java.io.InputStream; import java.lang.instrument.Instrumentation; import java.nio.file.Path; -import java.util.ArrayList; -import java.util.List; -import java.util.Locale; import java.util.jar.JarFile; -import org.objectweb.asm.ClassReader; -import org.objectweb.asm.ClassWriter; -import org.objectweb.asm.tree.ClassNode; -import org.objectweb.asm.tree.InsnNode; -import org.objectweb.asm.tree.MethodNode; - import cpw.mods.fml.SafeExitJVMLegacy; import net.minecraftforge.fml.SafeExitJVM; import pro.gravit.launcher.utils.NativeJVMHalt; @@ -52,138 +38,17 @@ public static void premain(String agentArgument, Instrumentation instrumentation SafeExitJVM.class.getName(); NativeJVMHalt.class.getName(); NativeJVMHalt.initFunc(); - isAgentStarted = true; - if (System.getProperty("java.vm.name").toUpperCase(Locale.US).contains("HOTSPOT")) - try { - if (JVMHelper.OS_TYPE == JVMHelper.OS.MUSTDIE) { - boolean pb = true; - boolean rt = true; - if (agentArgument != null) { - String trimmedArg = agentArgument.trim(); - if (!trimmedArg.isEmpty()) { - if (trimmedArg.contains("p")) pb = false; - if (trimmedArg.contains("r")) rt = false; - } - } - replaceClasses(pb, rt); - } else replaceClasses(false, false); - } catch (Error e) { - NativeJVMHalt.haltA(294); - throw e; - } + JVMHelper.checkStackTrace(LauncherAgent.class); + boolean bad = false; + try { + for (StackTraceElement e : new Throwable().getStackTrace()) + if (Class.forName(e.getClassName()).getClassLoader() != Runtime.class.getClassLoader() && Class.forName(e.getClassName()) != LauncherAgent.class) bad = true; + } catch(Throwable e) { bad = true; } + if (bad) NativeJVMHalt.haltA(-17); + else isAgentStarted = true; } public static boolean isStarted() { return isAgentStarted; } - - private static void replaceClasses(boolean pb, boolean rt) { - java.awt.Robot.class.getName(); - List defs = new ArrayList<>(); - if (rt) { - try { - defs.add(new java.lang.instrument.ClassDefinition(java.lang.Runtime.class, transformClass(java.lang.Runtime.class.getName(), getClassFile(java.lang.Runtime.class)))); - } catch (Exception e) { - throw new Error(e); - } - } - if (pb) { - try { - defs.add(new java.lang.instrument.ClassDefinition(java.lang.ProcessBuilder.class, transformClass(java.lang.ProcessBuilder.class.getName(), getClassFile(java.lang.ProcessBuilder.class)))); - } catch (Exception e) { - throw new Error(e); - } - } - try { - defs.add(new java.lang.instrument.ClassDefinition(java.awt.Robot.class, transformClass(java.awt.Robot.class.getName(), getClassFile(java.awt.Robot.class)))); - } catch (Exception e) { - throw new Error(e); - } - try { - inst.redefineClasses(defs.toArray(new java.lang.instrument.ClassDefinition[0])); - } catch (Exception e) { - throw new Error(e); - } - } - - /** - * @author https://github.com/Konloch/JVM-Sandbox - * Use ASM to modify the byte array - */ - private static byte[] transformClass(String className, byte[] classBytes) { - switch (className) { - case "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(); - } - case "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(); - } - case "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; - } - - /** - * @param clazz - * @return array, respending this class in bytecode. - * @throws IOException - * @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 - */ - private static byte[] getClassFile(Class clazz) throws IOException { - try (InputStream is = clazz.getResourceAsStream("/" + clazz.getName().replace('.', '/') + ".class"); - ByteArrayOutputStream baos = new ByteArrayOutputStream()) { - int r; - byte[] buffer = new byte[8192]; - while ((r = is.read(buffer)) >= 0) { - baos.write(buffer, 0, r); - } - return baos.toByteArray(); - } - } } diff --git a/Launcher/src/main/java/pro/gravit/launcher/client/ClientLauncher.java b/Launcher/src/main/java/pro/gravit/launcher/client/ClientLauncher.java index e0438ab8..7cd52285 100644 --- a/Launcher/src/main/java/pro/gravit/launcher/client/ClientLauncher.java +++ b/Launcher/src/main/java/pro/gravit/launcher/client/ClientLauncher.java @@ -44,7 +44,6 @@ import pro.gravit.launcher.hwid.HWIDProvider; import pro.gravit.launcher.managers.ClientGsonManager; import pro.gravit.launcher.managers.ClientHookManager; -import pro.gravit.launcher.modules.events.PostInitPhase; import pro.gravit.launcher.modules.events.PreConfigPhase; import pro.gravit.launcher.profiles.ClientProfile; import pro.gravit.launcher.profiles.PlayerProfile; @@ -55,6 +54,7 @@ import pro.gravit.launcher.serialize.HOutput; import pro.gravit.launcher.serialize.stream.StreamObject; import pro.gravit.launcher.utils.DirWatcher; +import pro.gravit.launcher.utils.NativeJVMHalt; import pro.gravit.utils.PublicURLClassLoader; import pro.gravit.utils.Version; import pro.gravit.utils.helper.CommonHelper; @@ -463,6 +463,10 @@ public static void main(String... args) throws Throwable { LauncherEngine.modulesManager.initModules(null); initGson(LauncherEngine.modulesManager); //Launcher.modulesManager.preInitModules(); + if (!LauncherAgent.isStarted()) { + NativeJVMHalt.haltA(100); + return; + } LauncherEngine.modulesManager.invokeEvent(new PreConfigPhase()); JVMHelper.verifySystemProperties(ClientLauncher.class, true); EnvHelper.checkDangerousParams();