Add debug info to fail load, use same defineMethod instance

This commit is contained in:
libraryaddict 2020-02-25 22:42:23 +13:00
parent 061161dad7
commit b8de529b25
No known key found for this signature in database
GPG Key ID: 052E4FBCD257AEA4
3 changed files with 39 additions and 22 deletions

View File

@ -1,6 +1,7 @@
package me.libraryaddict.disguise.utilities.reflection.asm; package me.libraryaddict.disguise.utilities.reflection.asm;
import com.google.gson.Gson; import com.google.gson.Gson;
import lombok.Getter;
import org.objectweb.asm.*; import org.objectweb.asm.*;
import java.io.IOException; import java.io.IOException;
@ -14,9 +15,16 @@ import java.util.Map;
* Created by libraryaddict on 17/02/2020. * Created by libraryaddict on 17/02/2020.
*/ */
public class Asm13 implements IAsm { public class Asm13 implements IAsm {
@Getter
private Method defineMethod;
public Asm13() throws NoSuchMethodException {
defineMethod = getDefineClassMethod();
}
public Class<?> createClassWithoutMethods(String className, public Class<?> createClassWithoutMethods(String className,
ArrayList<Map.Entry<String, String>> illegalMethods) throws IOException, InvocationTargetException, ArrayList<Map.Entry<String, String>> illegalMethods) throws IOException, InvocationTargetException,
IllegalAccessException, NoSuchMethodException, NoSuchFieldException { IllegalAccessException, NoSuchFieldException {
ClassReader cr = new ClassReader( ClassReader cr = new ClassReader(
getClass().getClassLoader().getResourceAsStream(className.replace(".", "/") + ".class")); getClass().getClassLoader().getResourceAsStream(className.replace(".", "/") + ".class"));
ClassWriter writer = new ClassWriter(cr, 0); ClassWriter writer = new ClassWriter(cr, 0);
@ -42,14 +50,13 @@ public class Asm13 implements IAsm {
Field field = loader.getClass().getDeclaredField("classes"); Field field = loader.getClass().getDeclaredField("classes");
field.setAccessible(true); field.setAccessible(true);
Map<String, Class<?>> map = (Map<String, Class<?>>) field.get(loader); Map<String, Class<?>> map = (Map<String, Class<?>>) field.get(loader);
Class newClass = (Class<?>) getDefineClassMethod() Class newClass = (Class<?>) defineMethod.invoke(getClass().getClassLoader(), className, bytes, 0, bytes.length);
.invoke(getClass().getClassLoader(), className, bytes, 0, bytes.length);
map.put(className, newClass); map.put(className, newClass);
return newClass; return newClass;
} }
private static Method getDefineClassMethod() throws NoSuchMethodException { private Method getDefineClassMethod() throws NoSuchMethodException {
Method defineClass = ClassLoader.class Method defineClass = ClassLoader.class
.getDeclaredMethod("defineClass", String.class, byte[].class, int.class, int.class); .getDeclaredMethod("defineClass", String.class, byte[].class, int.class, int.class);
defineClass.setAccessible(true); defineClass.setAccessible(true);

View File

@ -1,5 +1,6 @@
package me.libraryaddict.disguise.utilities.reflection.asm; package me.libraryaddict.disguise.utilities.reflection.asm;
import lombok.Getter;
import org.bukkit.craftbukkit.libs.org.objectweb.asm.*; import org.bukkit.craftbukkit.libs.org.objectweb.asm.*;
import java.io.IOException; import java.io.IOException;
@ -13,9 +14,16 @@ import java.util.Map;
* Created by libraryaddict on 17/02/2020. * Created by libraryaddict on 17/02/2020.
*/ */
public class Asm14 implements IAsm { public class Asm14 implements IAsm {
@Getter
private Method defineMethod;
public Asm14() throws NoSuchMethodException {
defineMethod = getDefineClassMethod();
}
public Class<?> createClassWithoutMethods(String className, public Class<?> createClassWithoutMethods(String className,
ArrayList<Map.Entry<String, String>> illegalMethods) throws IOException, InvocationTargetException, ArrayList<Map.Entry<String, String>> illegalMethods) throws IOException, InvocationTargetException,
IllegalAccessException, NoSuchMethodException, NoSuchFieldException { IllegalAccessException, NoSuchFieldException {
ClassReader cr = new ClassReader( ClassReader cr = new ClassReader(
getClass().getClassLoader().getResourceAsStream(className.replace(".", "/") + ".class")); getClass().getClassLoader().getResourceAsStream(className.replace(".", "/") + ".class"));
ClassWriter writer = new ClassWriter(cr, 0); ClassWriter writer = new ClassWriter(cr, 0);
@ -41,16 +49,13 @@ public class Asm14 implements IAsm {
Field field = loader.getClass().getDeclaredField("classes"); Field field = loader.getClass().getDeclaredField("classes");
field.setAccessible(true); field.setAccessible(true);
Map<String, Class<?>> map = (Map<String, Class<?>>) field.get(loader); Map<String, Class<?>> map = (Map<String, Class<?>>) field.get(loader);
Class newClass = Class newClass = (Class<?>) defineMethod.invoke(getClass().getClassLoader(), className, bytes, 0, bytes.length);
(Class<?>) getDefineClassMethod()
.invoke(getClass().getClassLoader(), className, bytes, 0, bytes.length);
map.put(className, newClass); map.put(className, newClass);
return newClass; return newClass;
} }
private static Method getDefineClassMethod() throws NoSuchMethodException { private Method getDefineClassMethod() throws NoSuchMethodException {
Method defineClass = ClassLoader.class Method defineClass = ClassLoader.class
.getDeclaredMethod("defineClass", String.class, byte[].class, int.class, int.class); .getDeclaredMethod("defineClass", String.class, byte[].class, int.class, int.class);
defineClass.setAccessible(true); defineClass.setAccessible(true);

View File

@ -1,5 +1,6 @@
package me.libraryaddict.disguise.utilities.reflection.asm; package me.libraryaddict.disguise.utilities.reflection.asm;
import com.google.gson.Gson;
import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.reflection.NmsVersion; import me.libraryaddict.disguise.utilities.reflection.NmsVersion;
@ -78,6 +79,9 @@ public class WatcherSanitizer {
e.printStackTrace(); e.printStackTrace();
} }
ArrayList<String> mapped = new ArrayList<>();
try (InputStream stream = LibsDisguises.getInstance().getResource("ANTI_PIRACY_ENCRYPTION")) {
IAsm asm; IAsm asm;
if (NmsVersion.v1_14.isSupported()) { if (NmsVersion.v1_14.isSupported()) {
@ -90,7 +94,6 @@ public class WatcherSanitizer {
asm = new Asm13(); asm = new Asm13();
} }
try (InputStream stream = LibsDisguises.getInstance().getResource("ANTI_PIRACY_ENCRYPTION")) {
List<String> lines = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8)).lines() List<String> lines = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8)).lines()
.collect(Collectors.toList()); .collect(Collectors.toList());
@ -115,10 +118,12 @@ public class WatcherSanitizer {
for (Map.Entry<String, ArrayList<Map.Entry<String, String>>> entry : toRemove.entrySet()) { for (Map.Entry<String, ArrayList<Map.Entry<String, String>>> entry : toRemove.entrySet()) {
Class result = asm.createClassWithoutMethods(entry.getKey(), entry.getValue()); Class result = asm.createClassWithoutMethods(entry.getKey(), entry.getValue());
mapped.add(entry.getKey());
} }
} }
catch (IOException | NoClassDefFoundError | IllegalAccessException | InvocationTargetException | NoSuchMethodException | NoSuchFieldException e) { catch (IOException | IllegalAccessException | InvocationTargetException | NoSuchMethodException | NoSuchFieldException | LinkageError e) {
e.printStackTrace(); e.printStackTrace();
LibsDisguises.getInstance().getLogger().severe("Registered: " + new Gson().toJson(mapped));
} }
} }
} }