From 8680c08533d2e14098dc8846b7930eab9388f599 Mon Sep 17 00:00:00 2001 From: zeruth Date: Thu, 18 Apr 2019 17:30:51 -0400 Subject: [PATCH 1/4] ByteCodePatcher Adds PlayerTransform -Adds transformProtectedGetSkullIcon -Adds transformGetSkullIcon --- .../net/runelite/client/rs/ClientLoader.java | 3 +- .../client/rs/bytecode/ByteCodePatcher.java | 38 +++++++++-- .../runelite/client/rs/bytecode/Hooks.java | 1 + .../transformers/PlayerTransform.java | 63 +++++++++++++++++++ 4 files changed, 98 insertions(+), 7 deletions(-) create mode 100644 runelite-client/src/main/java/net/runelite/client/rs/bytecode/transformers/PlayerTransform.java diff --git a/runelite-client/src/main/java/net/runelite/client/rs/ClientLoader.java b/runelite-client/src/main/java/net/runelite/client/rs/ClientLoader.java index d77c9da4ee..9c70435b0b 100644 --- a/runelite-client/src/main/java/net/runelite/client/rs/ClientLoader.java +++ b/runelite-client/src/main/java/net/runelite/client/rs/ClientLoader.java @@ -233,7 +233,8 @@ public class ClientLoader if (hooks.clientInstance.equals("")|| hooks.projectileClass.equals("") || - hooks.actorClass.equals("")) { + hooks.actorClass.equals("") || + hooks.playerClass.equals("")) { System.out.println("[RuneLit] Bad hooks, re-scraping."); ByteCodePatcher.clientInstance = getClientInstance(ByteCodeUtils.injectedClientFile.getPath()); ByteCodePatcher.findHooks(injectedClientFile.getPath()); diff --git a/runelite-client/src/main/java/net/runelite/client/rs/bytecode/ByteCodePatcher.java b/runelite-client/src/main/java/net/runelite/client/rs/bytecode/ByteCodePatcher.java index 2e7b740ee9..b75a8c9631 100644 --- a/runelite-client/src/main/java/net/runelite/client/rs/bytecode/ByteCodePatcher.java +++ b/runelite-client/src/main/java/net/runelite/client/rs/bytecode/ByteCodePatcher.java @@ -8,6 +8,7 @@ import javassist.NotFoundException; import net.runelite.client.RuneLite; import net.runelite.client.rs.ClientLoader; import net.runelite.client.rs.bytecode.transformers.ActorTransform; +import net.runelite.client.rs.bytecode.transformers.PlayerTransform; import net.runelite.client.rs.bytecode.transformers.ProjectileTransform; import net.runelite.http.api.RuneLiteAPI; import org.xeustechnologies.jcl.JarClassLoader; @@ -50,6 +51,8 @@ public class ByteCodePatcher { transformActor(actorClass); Class projectileClass = Class.forName(hooks.projectileClass, false, child); transformProjectile(projectileClass); + Class playerClass = Class.forName(hooks.playerClass, false, child); + transformPlayer(playerClass); ByteCodeUtils.updateHijackedJar(); } catch (Exception e) { e.printStackTrace(); @@ -103,6 +106,7 @@ public class ByteCodePatcher { Class classToLoad = Class.forName(entry.getName().replace(".class", ""), false, child); checkActor(classToLoad); checkProjectile(classToLoad); + checkPlayer(classToLoad); } catch (Exception e) { e.printStackTrace(); } @@ -129,10 +133,10 @@ public class ByteCodePatcher { } } - public static void transformActor(Class current) { - System.out.println("[RuneLit] Transforming Actor at class: "+current.getName()); + public static void transformActor(Class actor) { + System.out.println("[RuneLit] Transforming Actor at class: "+actor.getName()); ActorTransform at = new ActorTransform(); - at.modify(current); + at.modify(actor); } public static void checkProjectile(Class current) { @@ -151,10 +155,32 @@ public class ByteCodePatcher { } } - public static void transformProjectile(Class current) { - System.out.println("[RuneLit] Transforming Projectile at class: "+current.getName()); + public static void transformProjectile(Class projectile) { + System.out.println("[RuneLit] Transforming Projectile at class: "+projectile.getName()); ProjectileTransform pt = new ProjectileTransform(); - pt.modify(current); + pt.modify(projectile); + } + + public static void checkPlayer(Class current) { + try { + Method method = current.getDeclaredMethod("getSkullIcon"); + if (method!=null) { + hooks.playerClass = current.getName(); + System.out.println("[RuneLit] Transforming Player at class: "+current.getName()); + PlayerTransform pt = new PlayerTransform(); + pt.modify(current); + } + } catch (NoSuchMethodException e) { + //e.printStackTrace(); + } catch (NoClassDefFoundError e) { + //e.printStackTrace(); + } + } + + public static void transformPlayer(Class player) { + System.out.println("[RuneLit] Transforming Player at class: "+player.getName()); + PlayerTransform pt = new PlayerTransform(); + pt.modify(player); } } diff --git a/runelite-client/src/main/java/net/runelite/client/rs/bytecode/Hooks.java b/runelite-client/src/main/java/net/runelite/client/rs/bytecode/Hooks.java index a67e1bdf08..6d75fc81da 100644 --- a/runelite-client/src/main/java/net/runelite/client/rs/bytecode/Hooks.java +++ b/runelite-client/src/main/java/net/runelite/client/rs/bytecode/Hooks.java @@ -5,6 +5,7 @@ public class Hooks { public String clientInstance = ""; public String actorClass = ""; public String projectileClass = ""; + public String playerClass = ""; public Hooks() { } diff --git a/runelite-client/src/main/java/net/runelite/client/rs/bytecode/transformers/PlayerTransform.java b/runelite-client/src/main/java/net/runelite/client/rs/bytecode/transformers/PlayerTransform.java new file mode 100644 index 0000000000..1fbec240c6 --- /dev/null +++ b/runelite-client/src/main/java/net/runelite/client/rs/bytecode/transformers/PlayerTransform.java @@ -0,0 +1,63 @@ +package net.runelite.client.rs.bytecode.transformers; + +import javassist.CtClass; +import javassist.CtMethod; +import javassist.CtNewMethod; +import net.runelite.client.rs.bytecode.ByteCodePatcher; + +public class PlayerTransform { + public CtClass ct = null; + + public void modify(Class player) { + try { + ct = ByteCodePatcher.classPool.get(player.getName()); + transformProtectedGetSkullIcon(); + transformGetSkullIcon(); + ByteCodePatcher.modifiedClasses.add(ct); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public void transformProtectedGetSkullIcon() { + CtMethod protectedGetSkullIcon; + try { + protectedGetSkullIcon = ct.getDeclaredMethod("1protect$getRsSkullIcon"); + ct.removeMethod(protectedGetSkullIcon); + protectedGetSkullIcon.setName("getRsSkullIcon"); + ct.addMethod(protectedGetSkullIcon); + } catch (Exception e) { + e.printStackTrace(); + } + + } + + public void transformGetSkullIcon() { + CtMethod getSkullIcon; + try { + String SkullIcon = "net.runelite.api.SkullIcon"; + getSkullIcon = ct.getDeclaredMethod("getSkullIcon"); + ct.removeMethod(getSkullIcon); + getSkullIcon = CtNewMethod.make("public "+SkullIcon+" getSkullIcon() {" + + " switch (this.getRsSkullIcon()) {" + + " case 0: {" + + " return "+SkullIcon+".SKULL; }" + + " case 1: {" + + " return "+SkullIcon+".SKULL_FIGHT_PIT; }" + + " case 8: {" + + " return "+SkullIcon+".DEAD_MAN_FIVE; }" + + " case 9: {" + + " return "+SkullIcon+".DEAD_MAN_FOUR; }" + + " case 10: {" + + " return "+SkullIcon+".DEAD_MAN_THREE; }" + + " case 11: {" + + " return "+SkullIcon+".DEAD_MAN_TWO; }" + + " case 12: {" + + " return "+SkullIcon+".DEAD_MAN_ONE; } }" + + " return null; }",ct); + ct.addMethod(getSkullIcon); + } catch (Exception e) { + e.printStackTrace(); + } + } +} From f84621bfbffd1adf5f9b7701f2d85059332002d0 Mon Sep 17 00:00:00 2001 From: Lordzuku Date: Thu, 18 Apr 2019 18:20:03 -0400 Subject: [PATCH 2/4] Added getProjectile tranform & zeruths player transform cleaned up a bit of the imports / code --- .../client/rs/ClientConfigLoader.java | 17 +- .../net/runelite/client/rs/ClientLoader.java | 540 ++++++++---------- .../net/runelite/client/rs/RSAppletStub.java | 3 +- .../java/net/runelite/client/rs/RSConfig.java | 3 +- .../client/rs/bytecode/ByteCodePatcher.java | 98 +++- .../client/rs/bytecode/ByteCodeUtils.java | 34 +- .../runelite/client/rs/bytecode/Hooks.java | 2 + .../bytecode/transformers/ActorTransform.java | 11 +- .../transformers/PlayerTransform.java | 64 +++ .../transformers/ProjectileTransform.java | 1 - .../transformers/getProjectileTransform.java | 56 ++ 11 files changed, 470 insertions(+), 359 deletions(-) create mode 100644 runelite-client/src/main/java/net/runelite/client/rs/bytecode/transformers/PlayerTransform.java create mode 100644 runelite-client/src/main/java/net/runelite/client/rs/bytecode/transformers/getProjectileTransform.java diff --git a/runelite-client/src/main/java/net/runelite/client/rs/ClientConfigLoader.java b/runelite-client/src/main/java/net/runelite/client/rs/ClientConfigLoader.java index 9d3804ebab..17041f6129 100644 --- a/runelite-client/src/main/java/net/runelite/client/rs/ClientConfigLoader.java +++ b/runelite-client/src/main/java/net/runelite/client/rs/ClientConfigLoader.java @@ -26,15 +26,16 @@ package net.runelite.client.rs; import com.google.common.annotations.VisibleForTesting; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import javax.inject.Inject; -import javax.inject.Singleton; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; +import javax.inject.Inject; +import javax.inject.Singleton; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + @Singleton class ClientConfigLoader { @@ -51,13 +52,13 @@ class ClientConfigLoader RSConfig fetch() throws IOException { final Request request = new Request.Builder() - .url(CONFIG_URL) - .build(); + .url(CONFIG_URL) + .build(); final RSConfig config = new RSConfig(); try (final Response response = httpClient.newCall(request).execute(); final BufferedReader in = new BufferedReader( - new InputStreamReader(response.body().byteStream()))) + new InputStreamReader(response.body().byteStream()))) { String str; diff --git a/runelite-client/src/main/java/net/runelite/client/rs/ClientLoader.java b/runelite-client/src/main/java/net/runelite/client/rs/ClientLoader.java index d77c9da4ee..ed1fa46452 100644 --- a/runelite-client/src/main/java/net/runelite/client/rs/ClientLoader.java +++ b/runelite-client/src/main/java/net/runelite/client/rs/ClientLoader.java @@ -32,18 +32,24 @@ import com.google.common.reflect.TypeToken; import com.google.gson.Gson; import io.sigpipe.jbsdiff.InvalidHeaderException; import io.sigpipe.jbsdiff.Patch; +import javassist.ClassPool; +import javassist.NotFoundException; +import lombok.extern.slf4j.Slf4j; +import net.runelite.client.RuneLite; +import net.runelite.client.rs.bytecode.ByteCodePatcher; +import net.runelite.client.rs.bytecode.ByteCodeUtils; +import net.runelite.client.rs.bytecode.Hooks; +import net.runelite.http.api.RuneLiteAPI; +import okhttp3.Request; +import okhttp3.Response; +import org.apache.commons.compress.compressors.CompressorException; +import org.xeustechnologies.jcl.JarClassLoader; + +import javax.inject.Inject; +import javax.inject.Named; +import javax.inject.Singleton; import java.applet.Applet; -import java.io.BufferedInputStream; -import java.io.BufferedReader; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.FileReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; +import java.io.*; import java.lang.reflect.Field; import java.net.URL; import java.net.URLClassLoader; @@ -53,292 +59,38 @@ import java.security.cert.CertificateFactory; import java.util.Collection; import java.util.HashMap; import java.util.Map; -import java.util.jar.Attributes; -import java.util.jar.JarEntry; -import java.util.jar.JarInputStream; -import java.util.jar.JarOutputStream; -import java.util.jar.Manifest; +import java.util.jar.*; import java.util.logging.Logger; -import javax.inject.Inject; -import javax.inject.Named; -import javax.inject.Singleton; -import javassist.ClassPool; -import javassist.NotFoundException; -import lombok.extern.slf4j.Slf4j; -import static net.runelite.client.rs.ClientUpdateCheckMode.AUTO; -import static net.runelite.client.rs.ClientUpdateCheckMode.NONE; -import static net.runelite.client.rs.ClientUpdateCheckMode.VANILLA; - -import net.runelite.client.RuneLite; -import net.runelite.client.rs.bytecode.ByteCodeUtils; -import net.runelite.client.rs.bytecode.ByteCodePatcher; -import net.runelite.client.rs.bytecode.Hooks; -import net.runelite.http.api.RuneLiteAPI; -import okhttp3.Request; -import okhttp3.Response; -import org.apache.commons.compress.compressors.CompressorException; -import org.xeustechnologies.jcl.JarClassLoader; +import static net.runelite.client.rs.ClientUpdateCheckMode.*; @Slf4j @Singleton -public class ClientLoader -{ - public static File hooksFile = new File(RuneLite.RUNELITE_DIR+"/hooks-"+ RuneLiteAPI.getVersion() +"-.json"); - private final ClientConfigLoader clientConfigLoader; - private ClientUpdateCheckMode updateCheckMode; - private JarOutputStream target; +public class ClientLoader { + public static File hooksFile = new File(RuneLite.RUNELITE_DIR + "/hooks-" + RuneLiteAPI.getVersion() + "-.json"); + private final ClientConfigLoader clientConfigLoader; + private ClientUpdateCheckMode updateCheckMode; + private JarOutputStream target; - @Inject - private ClientLoader( - @Named("updateCheckMode") final ClientUpdateCheckMode updateCheckMode, - final ClientConfigLoader clientConfigLoader) - { - this.updateCheckMode = updateCheckMode; - this.clientConfigLoader = clientConfigLoader; - } + @Inject + private ClientLoader( + @Named("updateCheckMode") final ClientUpdateCheckMode updateCheckMode, + final ClientConfigLoader clientConfigLoader) { + this.updateCheckMode = updateCheckMode; + this.clientConfigLoader = clientConfigLoader; + } - public Applet load() - { - if (updateCheckMode == NONE) - { - return null; - } - - try - { - File injectedClientFile = ByteCodeUtils.injectedClientFile; - File hijackedClientFile = ByteCodeUtils.hijackedClientFile; - Manifest manifest = new Manifest(); - manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); - target = new JarOutputStream(new FileOutputStream(injectedClientFile), manifest); - RSConfig config = clientConfigLoader.fetch(); - - Map zipFile = new HashMap<>(); - { - Certificate[] jagexCertificateChain = getJagexCertificateChain(); - String codebase = config.getCodeBase(); - String initialJar = config.getInitialJar(); - URL url = new URL(codebase + initialJar); - Request request = new Request.Builder() - .url(url) - .build(); - - try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute()) - { - JarInputStream jis; - - jis = new JarInputStream(response.body().byteStream()); - - byte[] tmp = new byte[4096]; - ByteArrayOutputStream buffer = new ByteArrayOutputStream(756 * 1024); - for (; ; ) - { - JarEntry metadata = jis.getNextJarEntry(); - if (metadata == null) - { - break; - } - - buffer.reset(); - for (; ; ) - { - int n = jis.read(tmp); - if (n <= -1) - { - break; - } - buffer.write(tmp, 0, n); - } - - zipFile.put(metadata.getName(), buffer.toByteArray()); - } - } - } - - if (updateCheckMode == AUTO) - { - Map hashes; - try (InputStream is = ClientLoader.class.getResourceAsStream("/patch/hashes.json")) - { - hashes = new Gson().fromJson(new InputStreamReader(is), new TypeToken>() - { - }.getType()); - } - - for (Map.Entry file : hashes.entrySet()) - { - byte[] bytes = zipFile.get(file.getKey()); - - String ourHash = null; - if (bytes != null) - { - ourHash = Hashing.sha512().hashBytes(bytes).toString(); - } - - if (!file.getValue().equals(ourHash)) - { - if (hijackedClientFile.exists()) { - Logger.getAnonymousLogger().warning("[RuneLit] Hash checking / Client patching skipped due to hijacked client."); - updateCheckMode = VANILLA; - break; - } else { - log.info("{} had a hash mismatch; falling back to vanilla. {} != {}", file.getKey(), file.getValue(), ourHash); - log.info("Client is outdated!"); - updateCheckMode = VANILLA; - break; - } - } - } - } - - if (updateCheckMode == AUTO) - { - ByteArrayOutputStream patchOs = new ByteArrayOutputStream(756 * 1024); - int patchCount = 0; - - for (Map.Entry file : zipFile.entrySet()) - { - byte[] bytes; - try (InputStream is = ClientLoader.class.getResourceAsStream("/patch/" + file.getKey() + ".bs")) - { - if (is == null) - { - continue; - } - - bytes = ByteStreams.toByteArray(is); - } - - patchOs.reset(); - Patch.patch(file.getValue(), bytes, patchOs); - file.setValue(patchOs.toByteArray()); - - ++patchCount; - - if (!file.getKey().startsWith("META")) { - add(file.getValue(), file.getKey(), target); - } - } - if (target!=null) - target.close(); - - log.info("Patched {} classes", patchCount); - } - if (hooksFile.exists()) { - ByteCodePatcher.classPool = new ClassPool(true); - ByteCodePatcher.classPool.appendClassPath(RuneLite.RUNELITE_DIR+"/injectedClient-"+ RuneLiteAPI.getVersion() +"-.jar"); - Gson gson = new Gson(); - Hooks hooks = gson.fromJson(new BufferedReader(new FileReader(hooksFile)), Hooks.class); - - if (hooks.clientInstance.equals("")|| - hooks.projectileClass.equals("") || - hooks.actorClass.equals("")) { - System.out.println("[RuneLit] Bad hooks, re-scraping."); - ByteCodePatcher.clientInstance = getClientInstance(ByteCodeUtils.injectedClientFile.getPath()); - ByteCodePatcher.findHooks(injectedClientFile.getPath()); - } else { - ByteCodePatcher.clientInstance = hooks.clientInstance; - ByteCodePatcher.applyHooks(ByteCodeUtils.injectedClientFile, hooks); - System.out.println("[RuneLit] Loaded hooks"); - } - - } else { - System.out.println("[RuneLit] Hooks file not found, scraping hooks."); - ByteCodePatcher.clientInstance = getClientInstance(ByteCodeUtils.injectedClientFile.getPath()); - ByteCodePatcher.findHooks(injectedClientFile.getPath()); - } - - Map zipFile2 = new HashMap<>(); - JarInputStream jis = new JarInputStream(new FileInputStream(hijackedClientFile)); - - byte[] tmp = new byte[4096]; - ByteArrayOutputStream buffer = new ByteArrayOutputStream(756 * 1024); - for (; ; ) { - JarEntry metadata = jis.getNextJarEntry(); - if (metadata == null) { - break; - } - - buffer.reset(); - for (; ; ) { - int n = jis.read(tmp); - if (n <= -1) { - break; - } - buffer.write(tmp, 0, n); - } - - zipFile2.put(metadata.getName(), buffer.toByteArray()); - } - - String initialClass = config.getInitialClass(); - - ClassLoader rsClassLoader = new ClassLoader(ClientLoader.class.getClassLoader()) - { - @Override - protected Class findClass(String name) throws ClassNotFoundException - { - String path = name.replace('.', '/').concat(".class"); - byte[] data = zipFile2.get(path); - if (data == null) - { - throw new ClassNotFoundException(name); - } - - return defineClass(name, data, 0, data.length); - } - }; - - Class clientClass = rsClassLoader.loadClass(initialClass); - - Applet rs = (Applet) clientClass.newInstance(); - rs.setStub(new RSAppletStub(config)); - return rs; - } - catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException - | CompressorException | InvalidHeaderException | CertificateException | SecurityException e) - { - if (e instanceof ClassNotFoundException) - { - log.error("Unable to load client - class not found. This means you" - + " are not running RuneLite with Maven as the client patch" - + " is not in your classpath."); - } - - log.error("Error loading RS!", e); - return null; - } catch (NotFoundException e) { - e.printStackTrace(); - } - return null; - } - - private void add(byte[] bytes, String entryName ,JarOutputStream target) throws IOException { - BufferedInputStream in = null; - try { - JarEntry entry = new JarEntry(entryName); - target.putNextEntry(entry); - target.write(bytes); - target.closeEntry(); - } finally { - if (in != null) - in.close(); - } - } - - private static Certificate[] getJagexCertificateChain() throws CertificateException - { - CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); - Collection certificates = certificateFactory.generateCertificates(ClientLoader.class.getResourceAsStream("jagex.crt")); - return certificates.toArray(new Certificate[certificates.size()]); - } + private static Certificate[] getJagexCertificateChain() throws CertificateException { + CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); + Collection certificates = certificateFactory.generateCertificates(ClientLoader.class.getResourceAsStream("jagex.crt")); + return certificates.toArray(new Certificate[certificates.size()]); + } public static String getClientInstance(String jarFile) { JarClassLoader jcl = new JarClassLoader(); try { ClassPool classPool = new ClassPool(true); - classPool.appendClassPath(RuneLite.RUNELITE_DIR+"/injectedClient-"+ RuneLiteAPI.getVersion() +"-.jar"); + classPool.appendClassPath(RuneLite.RUNELITE_DIR + "/injectedClient-" + RuneLiteAPI.getVersion() + "-.jar"); } catch (NotFoundException e) { e.printStackTrace(); } @@ -353,7 +105,7 @@ public class ClientLoader ClassLoader cl = ClassLoader.getSystemClassLoader(); try { URLClassLoader child = new URLClassLoader( - new URL[] {temp.toURI().toURL()}, + new URL[]{temp.toURI().toURL()}, cl ); try { @@ -364,10 +116,10 @@ public class ClientLoader Field[] fields = classToLoad.getDeclaredFields(); for (Field f : fields) { try { - if (f.getType().getName()=="client") { - ByteCodePatcher.hooks.clientInstance = classToLoad.getName()+"."+f.getName(); - System.out.println("[RuneLit] Found client instance at "+classToLoad.getName()+"."+f.getName()); - return classToLoad.getName()+"."+f.getName(); + if (f.getType().getName() == "client") { + ByteCodePatcher.hooks.clientInstance = classToLoad.getName() + "." + f.getName(); + System.out.println("[RuneLit] Found client instance at " + classToLoad.getName() + "." + f.getName()); + return classToLoad.getName() + "." + f.getName(); } } catch (Exception e) { e.printStackTrace(); @@ -382,7 +134,7 @@ public class ClientLoader } catch (Exception e) { e.printStackTrace(); - System.out.println("Class not found: "+entry.getName()); + System.out.println("Class not found: " + entry.getName()); } } } @@ -392,4 +144,210 @@ public class ClientLoader } return ""; } + + public Applet load() { + if (updateCheckMode == NONE) { + return null; + } + + try { + File injectedClientFile = ByteCodeUtils.injectedClientFile; + File hijackedClientFile = ByteCodeUtils.hijackedClientFile; + Manifest manifest = new Manifest(); + manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); + target = new JarOutputStream(new FileOutputStream(injectedClientFile), manifest); + RSConfig config = clientConfigLoader.fetch(); + + Map zipFile = new HashMap<>(); + { + Certificate[] jagexCertificateChain = getJagexCertificateChain(); + String codebase = config.getCodeBase(); + String initialJar = config.getInitialJar(); + URL url = new URL(codebase + initialJar); + Request request = new Request.Builder() + .url(url) + .build(); + + try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute()) { + JarInputStream jis; + + jis = new JarInputStream(response.body().byteStream()); + + byte[] tmp = new byte[4096]; + ByteArrayOutputStream buffer = new ByteArrayOutputStream(756 * 1024); + for (; ; ) { + JarEntry metadata = jis.getNextJarEntry(); + if (metadata == null) { + break; + } + + buffer.reset(); + for (; ; ) { + int n = jis.read(tmp); + if (n <= -1) { + break; + } + buffer.write(tmp, 0, n); + } + + zipFile.put(metadata.getName(), buffer.toByteArray()); + } + } + } + + if (updateCheckMode == AUTO) { + Map hashes; + try (InputStream is = ClientLoader.class.getResourceAsStream("/patch/hashes.json")) { + hashes = new Gson().fromJson(new InputStreamReader(is), new TypeToken>() { + }.getType()); + } + + for (Map.Entry file : hashes.entrySet()) { + byte[] bytes = zipFile.get(file.getKey()); + + String ourHash = null; + if (bytes != null) { + ourHash = Hashing.sha512().hashBytes(bytes).toString(); + } + + if (!file.getValue().equals(ourHash)) { + if (hijackedClientFile.exists()) { + Logger.getAnonymousLogger().warning("[RuneLit] Hash checking / Client patching skipped due to hijacked client."); + updateCheckMode = VANILLA; + break; + } else { + log.info("{} had a hash mismatch; falling back to vanilla. {} != {}", file.getKey(), file.getValue(), ourHash); + log.info("Client is outdated!"); + updateCheckMode = VANILLA; + break; + } + } + } + } + + if (updateCheckMode == AUTO) { + ByteArrayOutputStream patchOs = new ByteArrayOutputStream(756 * 1024); + int patchCount = 0; + + for (Map.Entry file : zipFile.entrySet()) { + byte[] bytes; + try (InputStream is = ClientLoader.class.getResourceAsStream("/patch/" + file.getKey() + ".bs")) { + if (is == null) { + continue; + } + + bytes = ByteStreams.toByteArray(is); + } + + patchOs.reset(); + Patch.patch(file.getValue(), bytes, patchOs); + file.setValue(patchOs.toByteArray()); + + ++patchCount; + + if (!file.getKey().startsWith("META")) { + add(file.getValue(), file.getKey(), target); + } + } + if (target != null) + target.close(); + + log.info("Patched {} classes", patchCount); + } + if (hooksFile.exists()) { + ByteCodePatcher.classPool = new ClassPool(true); + ByteCodePatcher.classPool.appendClassPath(RuneLite.RUNELITE_DIR + "/injectedClient-" + RuneLiteAPI.getVersion() + "-.jar"); + Gson gson = new Gson(); + Hooks hooks = gson.fromJson(new BufferedReader(new FileReader(hooksFile)), Hooks.class); + if (hooks.clientInstance.equals("") || + hooks.projectileClass.equals("") || + hooks.actorClass.equals("") || + hooks.mainClientInstance.equals("") || + hooks.playerClass.equals("")) { + System.out.println("[RuneLit] Bad hooks, re-scraping."); + ByteCodePatcher.clientInstance = getClientInstance(ByteCodeUtils.injectedClientFile.getPath()); + ByteCodePatcher.findHooks(injectedClientFile.getPath()); + } else { + ByteCodePatcher.clientInstance = hooks.clientInstance; + ByteCodePatcher.applyHooks(ByteCodeUtils.injectedClientFile, hooks); + System.out.println("[RuneLit] Loaded hooks"); + } + + } else { + System.out.println("[RuneLit] Hooks file not found, scraping hooks."); + ByteCodePatcher.clientInstance = getClientInstance(ByteCodeUtils.injectedClientFile.getPath()); + ByteCodePatcher.findHooks(injectedClientFile.getPath()); + } + + Map zipFile2 = new HashMap<>(); + JarInputStream jis = new JarInputStream(new FileInputStream(hijackedClientFile)); + + byte[] tmp = new byte[4096]; + ByteArrayOutputStream buffer = new ByteArrayOutputStream(756 * 1024); + for (; ; ) { + JarEntry metadata = jis.getNextJarEntry(); + if (metadata == null) { + break; + } + + buffer.reset(); + for (; ; ) { + int n = jis.read(tmp); + if (n <= -1) { + break; + } + buffer.write(tmp, 0, n); + } + + zipFile2.put(metadata.getName(), buffer.toByteArray()); + } + + String initialClass = config.getInitialClass(); + + ClassLoader rsClassLoader = new ClassLoader(ClientLoader.class.getClassLoader()) { + @Override + protected Class findClass(String name) throws ClassNotFoundException { + String path = name.replace('.', '/').concat(".class"); + byte[] data = zipFile2.get(path); + if (data == null) { + throw new ClassNotFoundException(name); + } + + return defineClass(name, data, 0, data.length); + } + }; + + Class clientClass = rsClassLoader.loadClass(initialClass); + + Applet rs = (Applet) clientClass.newInstance(); + rs.setStub(new RSAppletStub(config)); + return rs; + } catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException + | CompressorException | InvalidHeaderException | CertificateException | SecurityException e) { + if (e instanceof ClassNotFoundException) { + log.error("Unable to load client - class not found. This means you" + + " are not running RuneLite with Maven as the client patch" + + " is not in your classpath."); + } + + log.error("Error loading RS!", e); + return null; + } catch (NotFoundException e) { + e.printStackTrace(); + } + return null; + } + + private void add(byte[] bytes, String entryName, JarOutputStream target) throws IOException { + BufferedInputStream in = null; + try { + JarEntry entry = new JarEntry(entryName); + target.putNextEntry(entry); + target.write(bytes); + target.closeEntry(); + } finally { + if (in != null) + in.close(); + } + } } diff --git a/runelite-client/src/main/java/net/runelite/client/rs/RSAppletStub.java b/runelite-client/src/main/java/net/runelite/client/rs/RSAppletStub.java index a3a2a0f14a..04a046b94e 100644 --- a/runelite-client/src/main/java/net/runelite/client/rs/RSAppletStub.java +++ b/runelite-client/src/main/java/net/runelite/client/rs/RSAppletStub.java @@ -25,11 +25,12 @@ */ package net.runelite.client.rs; +import lombok.RequiredArgsConstructor; + import java.applet.AppletContext; import java.applet.AppletStub; import java.net.MalformedURLException; import java.net.URL; -import lombok.RequiredArgsConstructor; @RequiredArgsConstructor class RSAppletStub implements AppletStub diff --git a/runelite-client/src/main/java/net/runelite/client/rs/RSConfig.java b/runelite-client/src/main/java/net/runelite/client/rs/RSConfig.java index e0c5539323..3dc50d18e5 100644 --- a/runelite-client/src/main/java/net/runelite/client/rs/RSConfig.java +++ b/runelite-client/src/main/java/net/runelite/client/rs/RSConfig.java @@ -25,9 +25,10 @@ */ package net.runelite.client.rs; +import lombok.Getter; + import java.util.HashMap; import java.util.Map; -import lombok.Getter; @Getter class RSConfig diff --git a/runelite-client/src/main/java/net/runelite/client/rs/bytecode/ByteCodePatcher.java b/runelite-client/src/main/java/net/runelite/client/rs/bytecode/ByteCodePatcher.java index 2e7b740ee9..555d47a970 100644 --- a/runelite-client/src/main/java/net/runelite/client/rs/bytecode/ByteCodePatcher.java +++ b/runelite-client/src/main/java/net/runelite/client/rs/bytecode/ByteCodePatcher.java @@ -8,21 +8,14 @@ import javassist.NotFoundException; import net.runelite.client.RuneLite; import net.runelite.client.rs.ClientLoader; import net.runelite.client.rs.bytecode.transformers.ActorTransform; +import net.runelite.client.rs.bytecode.transformers.PlayerTransform; import net.runelite.client.rs.bytecode.transformers.ProjectileTransform; +import net.runelite.client.rs.bytecode.transformers.getProjectileTransform; import net.runelite.http.api.RuneLiteAPI; import org.xeustechnologies.jcl.JarClassLoader; -import java.io.BufferedInputStream; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileWriter; -import java.io.IOException; -import java.io.Writer; -import java.lang.reflect.Field; +import java.io.*; import java.lang.reflect.Method; -import java.net.URISyntaxException; import java.net.URL; import java.net.URLClassLoader; import java.util.ArrayList; @@ -42,7 +35,7 @@ public class ByteCodePatcher { public static void applyHooks(File jf, Hooks hooks) { try { URLClassLoader child = new URLClassLoader( - new URL[] {jf.toURI().toURL()}, + new URL[]{jf.toURI().toURL()}, cl ); try { @@ -50,6 +43,11 @@ public class ByteCodePatcher { transformActor(actorClass); Class projectileClass = Class.forName(hooks.projectileClass, false, child); transformProjectile(projectileClass); + Class getProjectileClass = Class.forName(hooks.mainClientInstance, false, child); + transformGetProjectile(getProjectileClass); + Class playerClass = Class.forName(hooks.playerClass, false, child); + transformPlayer(playerClass); + ByteCodeUtils.updateHijackedJar(); } catch (Exception e) { e.printStackTrace(); @@ -63,7 +61,7 @@ public class ByteCodePatcher { public static void findHooks(String jf) { try { classPool = new ClassPool(true); - classPool.appendClassPath(RuneLite.RUNELITE_DIR+"/injectedClient-"+ RuneLiteAPI.getVersion() +"-.jar"); + classPool.appendClassPath(RuneLite.RUNELITE_DIR + "/injectedClient-" + RuneLiteAPI.getVersion() + "-.jar"); } catch (NotFoundException e) { e.printStackTrace(); } @@ -96,29 +94,31 @@ public class ByteCodePatcher { public static void checkClasses(File jf, JarEntry entry) { try { URLClassLoader child = new URLClassLoader( - new URL[] {jf.toURI().toURL()}, + new URL[]{jf.toURI().toURL()}, cl ); try { Class classToLoad = Class.forName(entry.getName().replace(".class", ""), false, child); checkActor(classToLoad); checkProjectile(classToLoad); + checkgetProjectiles(classToLoad); + checkPlayer(classToLoad); } catch (Exception e) { - e.printStackTrace(); + e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); - System.out.println("Class not found: "+entry.getName()); + System.out.println("Class not found: " + entry.getName()); } } public static void checkActor(Class current) { try { - Method method = current.getDeclaredMethod("setCombatInfo", new Class[] { int.class, int.class, int.class, int.class, int.class, int.class }); - if (method!=null) { + Method method = current.getDeclaredMethod("setCombatInfo", int.class, int.class, int.class, int.class, int.class, int.class); + if (method != null) { hooks.actorClass = current.getName(); - System.out.println("[RuneLit] Transforming Actor at class: "+current.getName()); + System.out.println("[Z-lyte] Transforming Actor at class: " + current.getName()); ActorTransform at = new ActorTransform(); at.modify(current); } @@ -129,18 +129,18 @@ public class ByteCodePatcher { } } - public static void transformActor(Class current) { - System.out.println("[RuneLit] Transforming Actor at class: "+current.getName()); + public static void transformActor(Class actor) { + System.out.println("[RuneLit] Transforming Actor at class: " + actor.getName()); ActorTransform at = new ActorTransform(); - at.modify(current); + at.modify(actor); } public static void checkProjectile(Class current) { try { - Method method = current.getDeclaredMethod("projectileMoved", new Class[] { int.class, int.class, int.class, int.class}); - if (method!=null) { + Method method = current.getDeclaredMethod("projectileMoved", int.class, int.class, int.class, int.class); + if (method != null) { hooks.projectileClass = current.getName(); - System.out.println("[RuneLit] Transforming Projectile at class: "+current.getName()); + System.out.println("[RuneLit] Transforming Projectile at class: " + current.getName()); ProjectileTransform pt = new ProjectileTransform(); pt.modify(current); } @@ -151,10 +151,54 @@ public class ByteCodePatcher { } } - public static void transformProjectile(Class current) { - System.out.println("[RuneLit] Transforming Projectile at class: "+current.getName()); + public static void transformProjectile(Class projectile) { + System.out.println("[RuneLit] Transforming Projectile at class: " + projectile.getName()); ProjectileTransform pt = new ProjectileTransform(); - pt.modify(current); + pt.modify(projectile); } + public static void checkgetProjectiles(Class getprojectile) { + try { + Method method = getprojectile.getDeclaredMethod("getProjectiles"); + if (method != null) { + hooks.mainClientInstance = getprojectile.getName(); + System.out.println("[RuneLit] Transforming Projectile at class: " + getprojectile.getName()); + getProjectileTransform gpt = new getProjectileTransform(); + gpt.modify(getprojectile); + } + } catch (NoSuchMethodException e) { + //e.printStackTrace(); + } catch (NoClassDefFoundError e) { + //e.printStackTrace(); + } + } + + public static void transformGetProjectile(Class current) { + System.out.println("[RuneLit] Transforming getProjectile at class: " + current.getName()); + getProjectileTransform gpt = new getProjectileTransform(); + gpt.modify(current); + } + + public static void checkPlayer(Class current) { + try { + Method method = current.getDeclaredMethod("getSkullIcon"); + if (method != null) { + hooks.playerClass = current.getName(); + System.out.println("[RuneLit] Transforming Player at class: " + current.getName()); + PlayerTransform pt = new PlayerTransform(); + pt.modify(current); + } + } catch (NoSuchMethodException e) { + //e.printStackTrace(); + } catch (NoClassDefFoundError e) { + //e.printStackTrace(); + } + } + + public static void transformPlayer(Class player) { + System.out.println("[RuneLit] Transforming Player at class: " + player.getName()); + PlayerTransform pt = new PlayerTransform(); + pt.modify(player); + + } } diff --git a/runelite-client/src/main/java/net/runelite/client/rs/bytecode/ByteCodeUtils.java b/runelite-client/src/main/java/net/runelite/client/rs/bytecode/ByteCodeUtils.java index 646d595982..7e4f1172aa 100644 --- a/runelite-client/src/main/java/net/runelite/client/rs/bytecode/ByteCodeUtils.java +++ b/runelite-client/src/main/java/net/runelite/client/rs/bytecode/ByteCodeUtils.java @@ -4,26 +4,18 @@ import javassist.CtClass; import net.runelite.client.RuneLite; import net.runelite.http.api.RuneLiteAPI; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; +import java.io.*; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; -import java.util.jar.Attributes; -import java.util.jar.JarEntry; -import java.util.jar.JarFile; -import java.util.jar.JarOutputStream; -import java.util.jar.Manifest; +import java.util.jar.*; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; public class ByteCodeUtils { //TODO: Write method to delete old revision injected clients. - public static File injectedClientFile = new File(RuneLite.RUNELITE_DIR+"/injectedClient-"+ RuneLiteAPI.getVersion() +"-.jar"); - public static File hijackedClientFile = new File(RuneLite.RUNELITE_DIR+"/hijackedClient-"+ RuneLiteAPI.getVersion() +"-.jar"); + public static File injectedClientFile = new File(RuneLite.RUNELITE_DIR + "/injectedClient-" + RuneLiteAPI.getVersion() + "-.jar"); + public static File hijackedClientFile = new File(RuneLite.RUNELITE_DIR + "/hijackedClient-" + RuneLiteAPI.getVersion() + "-.jar"); public static JarOutputStream target; @@ -47,12 +39,12 @@ public class ByteCodeUtils { JarEntry entry = entries.nextElement(); boolean skip = false; for (CtClass ct : ByteCodePatcher.modifiedClasses) { - if ((ct.getName()+".class").equals(entry.getName())) { + if ((ct.getName() + ".class").equals(entry.getName())) { skip = true; } } if (!skip) - add(entry); + add(entry); } for (CtClass ct : ByteCodePatcher.modifiedClasses) { @@ -67,10 +59,10 @@ public class ByteCodeUtils { private static void add(CtClass ct) { try { - JarEntry newEntry = new JarEntry(ct.getName()+".class"); - target.putNextEntry(newEntry); - target.write(ct.toBytecode()); - target.closeEntry(); + JarEntry newEntry = new JarEntry(ct.getName() + ".class"); + target.putNextEntry(newEntry); + target.write(ct.toBytecode()); + target.closeEntry(); } catch (Exception e) { e.printStackTrace(); } @@ -78,7 +70,7 @@ public class ByteCodeUtils { private static void add(JarEntry entry) throws IOException { try { - if (!entry.getName().startsWith("META")&&!entry.getName().equals("")) { + if (!entry.getName().startsWith("META") && !entry.getName().equals("")) { target.putNextEntry(entry); target.write(getBytesFromZipFile(entry.getName())); target.closeEntry(); @@ -94,7 +86,7 @@ public class ByteCodeUtils { zipFile = new ZipFile(injectedClientFile); Enumeration entries = zipFile.entries(); - while(entries.hasMoreElements()){ + while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); if (entry.getName().equals(entryName)) { InputStream stream = zipFile.getInputStream(entry); @@ -112,6 +104,6 @@ public class ByteCodeUtils { } catch (IOException e) { e.printStackTrace(); } - return null; + return null; } } diff --git a/runelite-client/src/main/java/net/runelite/client/rs/bytecode/Hooks.java b/runelite-client/src/main/java/net/runelite/client/rs/bytecode/Hooks.java index a67e1bdf08..e4d197b2bf 100644 --- a/runelite-client/src/main/java/net/runelite/client/rs/bytecode/Hooks.java +++ b/runelite-client/src/main/java/net/runelite/client/rs/bytecode/Hooks.java @@ -5,6 +5,8 @@ public class Hooks { public String clientInstance = ""; public String actorClass = ""; public String projectileClass = ""; + public String mainClientInstance = ""; + public String playerClass = ""; public Hooks() { } diff --git a/runelite-client/src/main/java/net/runelite/client/rs/bytecode/transformers/ActorTransform.java b/runelite-client/src/main/java/net/runelite/client/rs/bytecode/transformers/ActorTransform.java index 37b4777c56..b97cd0dcfb 100644 --- a/runelite-client/src/main/java/net/runelite/client/rs/bytecode/transformers/ActorTransform.java +++ b/runelite-client/src/main/java/net/runelite/client/rs/bytecode/transformers/ActorTransform.java @@ -1,17 +1,10 @@ package net.runelite.client.rs.bytecode.transformers; -import javassist.CannotCompileException; import javassist.CtClass; import javassist.CtMethod; import javassist.CtNewMethod; import javassist.NotFoundException; import net.runelite.client.rs.bytecode.ByteCodePatcher; -import net.runelite.client.rs.bytecode.ByteCodeUtils; - -import java.io.File; -import java.io.IOException; -import java.net.URL; -import java.net.URLClassLoader; public class ActorTransform { public CtClass ct = null; @@ -36,7 +29,7 @@ public class ActorTransform { ct.addMethod(protectedAnimation); CtMethod getAnimation = ct.getDeclaredMethod("getAnimation"); ct.removeMethod(getAnimation); - getAnimation = CtNewMethod.make("public int getAnimation() { return this.getRsAnimation(); }",ct); + getAnimation = CtNewMethod.make("public int getAnimation() { return this.getRsAnimation(); }", ct); ct.addMethod(getAnimation); } catch (Exception e) { e.printStackTrace(); @@ -51,7 +44,7 @@ public class ActorTransform { getAnimationChanged = CtNewMethod.make("public void animationChanged(int n) { " + " net.runelite.api.events.AnimationChanged animationChanged = new net.runelite.api.events.AnimationChanged();" + " animationChanged.setActor((net.runelite.api.Actor)this);" + - " "+ByteCodePatcher.clientInstance+".getCallbacks().post((java.lang.Object)animationChanged); }",ct); + " " + ByteCodePatcher.clientInstance + ".getCallbacks().post((java.lang.Object)animationChanged); }", ct); ct.addMethod(getAnimationChanged); } catch (Exception e) { e.printStackTrace(); diff --git a/runelite-client/src/main/java/net/runelite/client/rs/bytecode/transformers/PlayerTransform.java b/runelite-client/src/main/java/net/runelite/client/rs/bytecode/transformers/PlayerTransform.java new file mode 100644 index 0000000000..86222601d4 --- /dev/null +++ b/runelite-client/src/main/java/net/runelite/client/rs/bytecode/transformers/PlayerTransform.java @@ -0,0 +1,64 @@ + +package net.runelite.client.rs.bytecode.transformers; + +import javassist.CtClass; +import javassist.CtMethod; +import javassist.CtNewMethod; +import net.runelite.client.rs.bytecode.ByteCodePatcher; + +public class PlayerTransform { + public CtClass ct = null; + + public void modify(Class player) { + try { + ct = ByteCodePatcher.classPool.get(player.getName()); + transformProtectedGetSkullIcon(); + transformGetSkullIcon(); + ByteCodePatcher.modifiedClasses.add(ct); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public void transformProtectedGetSkullIcon() { + CtMethod protectedGetSkullIcon; + try { + protectedGetSkullIcon = ct.getDeclaredMethod("1protect$getRsSkullIcon"); + ct.removeMethod(protectedGetSkullIcon); + protectedGetSkullIcon.setName("getRsSkullIcon"); + ct.addMethod(protectedGetSkullIcon); + } catch (Exception e) { + e.printStackTrace(); + } + + } + + public void transformGetSkullIcon() { + CtMethod getSkullIcon; + try { + String SkullIcon = "net.runelite.api.SkullIcon"; + getSkullIcon = ct.getDeclaredMethod("getSkullIcon"); + ct.removeMethod(getSkullIcon); + getSkullIcon = CtNewMethod.make("public " + SkullIcon + " getSkullIcon() {" + + " switch (this.getRsSkullIcon()) {" + + " case 0: {" + + " return " + SkullIcon + ".SKULL; }" + + " case 1: {" + + " return " + SkullIcon + ".SKULL_FIGHT_PIT; }" + + " case 8: {" + + " return " + SkullIcon + ".DEAD_MAN_FIVE; }" + + " case 9: {" + + " return " + SkullIcon + ".DEAD_MAN_FOUR; }" + + " case 10: {" + + " return " + SkullIcon + ".DEAD_MAN_THREE; }" + + " case 11: {" + + " return " + SkullIcon + ".DEAD_MAN_TWO; }" + + " case 12: {" + + " return " + SkullIcon + ".DEAD_MAN_ONE; } }" + + " return null; }", ct); + ct.addMethod(getSkullIcon); + } catch (Exception e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/runelite-client/src/main/java/net/runelite/client/rs/bytecode/transformers/ProjectileTransform.java b/runelite-client/src/main/java/net/runelite/client/rs/bytecode/transformers/ProjectileTransform.java index 9cdae79a77..1ad1e94e7f 100644 --- a/runelite-client/src/main/java/net/runelite/client/rs/bytecode/transformers/ProjectileTransform.java +++ b/runelite-client/src/main/java/net/runelite/client/rs/bytecode/transformers/ProjectileTransform.java @@ -4,7 +4,6 @@ import javassist.CtClass; import javassist.CtMethod; import javassist.CtNewMethod; import net.runelite.client.rs.bytecode.ByteCodePatcher; -import net.runelite.client.rs.bytecode.ByteCodeUtils; public class ProjectileTransform { public CtClass ct = null; diff --git a/runelite-client/src/main/java/net/runelite/client/rs/bytecode/transformers/getProjectileTransform.java b/runelite-client/src/main/java/net/runelite/client/rs/bytecode/transformers/getProjectileTransform.java new file mode 100644 index 0000000000..4242c88f59 --- /dev/null +++ b/runelite-client/src/main/java/net/runelite/client/rs/bytecode/transformers/getProjectileTransform.java @@ -0,0 +1,56 @@ +package net.runelite.client.rs.bytecode.transformers; + +import javassist.CtClass; +import javassist.CtMethod; +import javassist.CtNewMethod; +import javassist.bytecode.AnnotationsAttribute; +import javassist.bytecode.ClassFile; +import javassist.bytecode.ConstPool; +import net.runelite.client.rs.bytecode.ByteCodePatcher; + + +public class getProjectileTransform { + public CtClass ct = null; + + public void modify(Class getprojectile) { + try { + ct = ByteCodePatcher.classPool.get(getprojectile.getName()); + + transformGetProjectile(); + ByteCodePatcher.modifiedClasses.add(ct); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public void transformGetProjectile() { + + CtMethod getProjectiles; + try { + CtMethod protectedAnimation = ct.getDeclaredMethod("1protect$getProjectilesDeque"); + ct.removeMethod(protectedAnimation); + protectedAnimation.setName("getProjectilesDeque"); + ct.addMethod(protectedAnimation); + getProjectiles = ct.getDeclaredMethod("getProjectiles"); + ct.removeMethod(getProjectiles); + getProjectiles = CtNewMethod.make("public java.util.List getProjectiles() { " + + " java.util.ArrayList localArrayList = new java.util.ArrayList();" + + " net.runelite.rs.api.RSDeque localRSDeque = getProjectilesDeque();" + + " net.runelite.rs.api.RSNode localRSNode = localRSDeque.getHead();" + + " for (net.runelite.api.Node localNode = localRSNode.getNext(); localNode != localRSNode; localNode = localNode.getNext()) {" + + " net.runelite.api.Projectile localProjectile = (net.runelite.api.Projectile)localNode;" + + " localArrayList.add(localProjectile); }" + + " return localArrayList; }", ct); + + ct.addMethod(getProjectiles); + ClassFile classFile = ct.getClassFile(); + ConstPool constPool = classFile.getConstPool(); + AnnotationsAttribute attr = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag); + javassist.bytecode.annotation.Annotation annotation = new javassist.bytecode.annotation.Annotation("Override", constPool); + attr.setAnnotation(annotation); + getProjectiles.getMethodInfo().addAttribute(attr); + } catch (Exception e) { + e.printStackTrace(); + } + } +} From 07a8b2daf1142b6761f0d5d263f8cfc9ad295eef Mon Sep 17 00:00:00 2001 From: zeruth Date: Thu, 18 Apr 2019 18:39:14 -0400 Subject: [PATCH 3/4] ClientLoader Makes scraping/loading hooks a bit more robust/safer Dumps protected RS methods to Hooks on update (not really needed but will keep for now) --- .../net/runelite/client/rs/ClientLoader.java | 44 +++++++++++++++---- .../runelite/client/rs/bytecode/Hooks.java | 4 ++ 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/runelite-client/src/main/java/net/runelite/client/rs/ClientLoader.java b/runelite-client/src/main/java/net/runelite/client/rs/ClientLoader.java index 9c70435b0b..d49b97c9a2 100644 --- a/runelite-client/src/main/java/net/runelite/client/rs/ClientLoader.java +++ b/runelite-client/src/main/java/net/runelite/client/rs/ClientLoader.java @@ -45,13 +45,16 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.lang.reflect.Field; +import java.lang.reflect.Method; import java.net.URL; import java.net.URLClassLoader; import java.security.cert.Certificate; import java.security.cert.CertificateException; import java.security.cert.CertificateFactory; +import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.jar.Attributes; import java.util.jar.JarEntry; @@ -88,6 +91,7 @@ public class ClientLoader private final ClientConfigLoader clientConfigLoader; private ClientUpdateCheckMode updateCheckMode; private JarOutputStream target; + private boolean scrapedHooks = false; @Inject private ClientLoader( @@ -231,22 +235,31 @@ public class ClientLoader Gson gson = new Gson(); Hooks hooks = gson.fromJson(new BufferedReader(new FileReader(hooksFile)), Hooks.class); - if (hooks.clientInstance.equals("")|| + if (hooks == null && !scrapedHooks) { + System.out.println("[RuneLit] Bad hooks, re-scraping."); + ByteCodePatcher.clientInstance = initVanillaInjected(ByteCodeUtils.injectedClientFile.getPath()); + ByteCodePatcher.findHooks(injectedClientFile.getPath()); + scrapedHooks=true; + } + if ((hooks.clientInstance.equals("")|| hooks.projectileClass.equals("") || hooks.actorClass.equals("") || - hooks.playerClass.equals("")) { + hooks.playerClass.equals("")) && !scrapedHooks) { System.out.println("[RuneLit] Bad hooks, re-scraping."); - ByteCodePatcher.clientInstance = getClientInstance(ByteCodeUtils.injectedClientFile.getPath()); + ByteCodePatcher.clientInstance = initVanillaInjected(ByteCodeUtils.injectedClientFile.getPath()); ByteCodePatcher.findHooks(injectedClientFile.getPath()); - } else { + scrapedHooks=true; + + } else if (!scrapedHooks) { ByteCodePatcher.clientInstance = hooks.clientInstance; ByteCodePatcher.applyHooks(ByteCodeUtils.injectedClientFile, hooks); System.out.println("[RuneLit] Loaded hooks"); + scrapedHooks=true; } } else { System.out.println("[RuneLit] Hooks file not found, scraping hooks."); - ByteCodePatcher.clientInstance = getClientInstance(ByteCodeUtils.injectedClientFile.getPath()); + ByteCodePatcher.clientInstance = initVanillaInjected(ByteCodeUtils.injectedClientFile.getPath()); ByteCodePatcher.findHooks(injectedClientFile.getPath()); } @@ -335,7 +348,9 @@ public class ClientLoader return certificates.toArray(new Certificate[certificates.size()]); } - public static String getClientInstance(String jarFile) { + public static String initVanillaInjected(String jarFile) { + List protectedMethods = new ArrayList<>(); + String clientInstance = ""; JarClassLoader jcl = new JarClassLoader(); try { ClassPool classPool = new ClassPool(true); @@ -363,17 +378,23 @@ public class ClientLoader try { jcl2.add(new FileInputStream(ByteCodeUtils.injectedClientFile)); Field[] fields = classToLoad.getDeclaredFields(); + Method[] methods = classToLoad.getMethods(); for (Field f : fields) { try { if (f.getType().getName()=="client") { ByteCodePatcher.hooks.clientInstance = classToLoad.getName()+"."+f.getName(); System.out.println("[RuneLit] Found client instance at "+classToLoad.getName()+"."+f.getName()); - return classToLoad.getName()+"."+f.getName(); + clientInstance = classToLoad.getName()+"."+f.getName(); } } catch (Exception e) { e.printStackTrace(); } } + for (Method m : methods) { + if (m.getName().contains("protect")) { + protectedMethods.add(classToLoad.getName()+":"+m.getName()); + } + } } catch (FileNotFoundException e) { e.printStackTrace(); } @@ -391,6 +412,13 @@ public class ClientLoader } catch (Exception e) { e.printStackTrace(); } - return ""; + String[] hooksProtectedMethods = new String[protectedMethods.size()]; + int i = 0; + for (String s : protectedMethods) { + hooksProtectedMethods[i] = s; + i++; + } + ByteCodePatcher.hooks.protectedMethods = hooksProtectedMethods; + return clientInstance; } } diff --git a/runelite-client/src/main/java/net/runelite/client/rs/bytecode/Hooks.java b/runelite-client/src/main/java/net/runelite/client/rs/bytecode/Hooks.java index 6d75fc81da..e07199187d 100644 --- a/runelite-client/src/main/java/net/runelite/client/rs/bytecode/Hooks.java +++ b/runelite-client/src/main/java/net/runelite/client/rs/bytecode/Hooks.java @@ -1,11 +1,15 @@ package net.runelite.client.rs.bytecode; +import java.lang.reflect.Method; +import java.util.List; + public class Hooks { public String clientInstance = ""; public String actorClass = ""; public String projectileClass = ""; public String playerClass = ""; + public String[] protectedMethods; public Hooks() { } From 227ae81082197d238b8d0e3161981da6c4c980dd Mon Sep 17 00:00:00 2001 From: zeruth Date: Thu, 18 Apr 2019 19:02:21 -0400 Subject: [PATCH 4/4] ByteCodePatcher Fix the bungle that was my last merge. Refactored class/method names to adhere to ByteCodePatcher layout Import all transformers --- .../net/runelite/client/rs/ClientLoader.java | 245 +----------------- .../client/rs/bytecode/ByteCodePatcher.java | 64 ++--- .../runelite/client/rs/bytecode/Hooks.java | 1 + ...ileTransform.java => ClientTransform.java} | 16 +- 4 files changed, 36 insertions(+), 290 deletions(-) rename runelite-client/src/main/java/net/runelite/client/rs/bytecode/transformers/{getProjectileTransform.java => ClientTransform.java} (89%) diff --git a/runelite-client/src/main/java/net/runelite/client/rs/ClientLoader.java b/runelite-client/src/main/java/net/runelite/client/rs/ClientLoader.java index 883882b790..47a8bba151 100644 --- a/runelite-client/src/main/java/net/runelite/client/rs/ClientLoader.java +++ b/runelite-client/src/main/java/net/runelite/client/rs/ClientLoader.java @@ -86,245 +86,6 @@ public class ClientLoader this.clientConfigLoader = clientConfigLoader; } - public Applet load() - { - if (updateCheckMode == NONE) - { - return null; - } - - try - { - File injectedClientFile = ByteCodeUtils.injectedClientFile; - File hijackedClientFile = ByteCodeUtils.hijackedClientFile; - Manifest manifest = new Manifest(); - manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); - target = new JarOutputStream(new FileOutputStream(injectedClientFile), manifest); - RSConfig config = clientConfigLoader.fetch(); - - Map zipFile = new HashMap<>(); - { - Certificate[] jagexCertificateChain = getJagexCertificateChain(); - String codebase = config.getCodeBase(); - String initialJar = config.getInitialJar(); - URL url = new URL(codebase + initialJar); - Request request = new Request.Builder() - .url(url) - .build(); - - try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute()) - { - JarInputStream jis; - - jis = new JarInputStream(response.body().byteStream()); - - byte[] tmp = new byte[4096]; - ByteArrayOutputStream buffer = new ByteArrayOutputStream(756 * 1024); - for (; ; ) - { - JarEntry metadata = jis.getNextJarEntry(); - if (metadata == null) - { - break; - } - - buffer.reset(); - for (; ; ) - { - int n = jis.read(tmp); - if (n <= -1) - { - break; - } - buffer.write(tmp, 0, n); - } - - zipFile.put(metadata.getName(), buffer.toByteArray()); - } - } - } - - if (updateCheckMode == AUTO) - { - Map hashes; - try (InputStream is = ClientLoader.class.getResourceAsStream("/patch/hashes.json")) - { - hashes = new Gson().fromJson(new InputStreamReader(is), new TypeToken>() - { - }.getType()); - } - - for (Map.Entry file : hashes.entrySet()) - { - byte[] bytes = zipFile.get(file.getKey()); - - String ourHash = null; - if (bytes != null) - { - ourHash = Hashing.sha512().hashBytes(bytes).toString(); - } - - if (!file.getValue().equals(ourHash)) - { - if (hijackedClientFile.exists()) { - Logger.getAnonymousLogger().warning("[RuneLit] Hash checking / Client patching skipped due to hijacked client."); - updateCheckMode = VANILLA; - break; - } else { - log.info("{} had a hash mismatch; falling back to vanilla. {} != {}", file.getKey(), file.getValue(), ourHash); - log.info("Client is outdated!"); - updateCheckMode = VANILLA; - break; - } - } - } - } - - if (updateCheckMode == AUTO) - { - ByteArrayOutputStream patchOs = new ByteArrayOutputStream(756 * 1024); - int patchCount = 0; - - for (Map.Entry file : zipFile.entrySet()) - { - byte[] bytes; - try (InputStream is = ClientLoader.class.getResourceAsStream("/patch/" + file.getKey() + ".bs")) - { - if (is == null) - { - continue; - } - - bytes = ByteStreams.toByteArray(is); - } - - patchOs.reset(); - Patch.patch(file.getValue(), bytes, patchOs); - file.setValue(patchOs.toByteArray()); - - ++patchCount; - - if (!file.getKey().startsWith("META")) { - add(file.getValue(), file.getKey(), target); - } - } - if (target!=null) - target.close(); - - log.info("Patched {} classes", patchCount); - } - if (hooksFile.exists()) { - ByteCodePatcher.classPool = new ClassPool(true); - ByteCodePatcher.classPool.appendClassPath(RuneLite.RUNELITE_DIR+"/injectedClient-"+ RuneLiteAPI.getVersion() +"-.jar"); - Gson gson = new Gson(); - Hooks hooks = gson.fromJson(new BufferedReader(new FileReader(hooksFile)), Hooks.class); - - if (hooks == null && !scrapedHooks) { - System.out.println("[RuneLit] Bad hooks, re-scraping."); - ByteCodePatcher.clientInstance = initVanillaInjected(ByteCodeUtils.injectedClientFile.getPath()); - ByteCodePatcher.findHooks(injectedClientFile.getPath()); - scrapedHooks=true; - } - if ((hooks.clientInstance.equals("")|| - hooks.projectileClass.equals("") || - hooks.actorClass.equals("") || - hooks.playerClass.equals("")) && !scrapedHooks) { - System.out.println("[RuneLit] Bad hooks, re-scraping."); - ByteCodePatcher.clientInstance = initVanillaInjected(ByteCodeUtils.injectedClientFile.getPath()); - ByteCodePatcher.findHooks(injectedClientFile.getPath()); - scrapedHooks=true; - - } else if (!scrapedHooks) { - ByteCodePatcher.clientInstance = hooks.clientInstance; - ByteCodePatcher.applyHooks(ByteCodeUtils.injectedClientFile, hooks); - System.out.println("[RuneLit] Loaded hooks"); - scrapedHooks=true; - } - - } else { - System.out.println("[RuneLit] Hooks file not found, scraping hooks."); - ByteCodePatcher.clientInstance = initVanillaInjected(ByteCodeUtils.injectedClientFile.getPath()); - ByteCodePatcher.findHooks(injectedClientFile.getPath()); - } - - Map zipFile2 = new HashMap<>(); - JarInputStream jis = new JarInputStream(new FileInputStream(hijackedClientFile)); - - byte[] tmp = new byte[4096]; - ByteArrayOutputStream buffer = new ByteArrayOutputStream(756 * 1024); - for (; ; ) { - JarEntry metadata = jis.getNextJarEntry(); - if (metadata == null) { - break; - } - - buffer.reset(); - for (; ; ) { - int n = jis.read(tmp); - if (n <= -1) { - break; - } - buffer.write(tmp, 0, n); - } - - zipFile2.put(metadata.getName(), buffer.toByteArray()); - } - - String initialClass = config.getInitialClass(); - - ClassLoader rsClassLoader = new ClassLoader(ClientLoader.class.getClassLoader()) - { - @Override - protected Class findClass(String name) throws ClassNotFoundException - { - String path = name.replace('.', '/').concat(".class"); - byte[] data = zipFile2.get(path); - if (data == null) - { - throw new ClassNotFoundException(name); - } - - return defineClass(name, data, 0, data.length); - } - }; - - Class clientClass = rsClassLoader.loadClass(initialClass); - - Applet rs = (Applet) clientClass.newInstance(); - rs.setStub(new RSAppletStub(config)); - return rs; - } - catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException - | CompressorException | InvalidHeaderException | CertificateException | SecurityException e) - { - if (e instanceof ClassNotFoundException) - { - log.error("Unable to load client - class not found. This means you" - + " are not running RuneLite with Maven as the client patch" - + " is not in your classpath."); - } - - log.error("Error loading RS!", e); - return null; - } catch (NotFoundException e) { - e.printStackTrace(); - } - return null; - } - - private void add(byte[] bytes, String entryName ,JarOutputStream target) throws IOException { - BufferedInputStream in = null; - try { - JarEntry entry = new JarEntry(entryName); - target.putNextEntry(entry); - target.write(bytes); - target.closeEntry(); - } finally { - if (in != null) - in.close(); - } - } - private static Certificate[] getJagexCertificateChain() throws CertificateException { CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); @@ -524,10 +285,10 @@ public class ClientLoader if (hooks.clientInstance.equals("") || hooks.projectileClass.equals("") || hooks.actorClass.equals("") || - hooks.mainClientInstance.equals("") || + hooks.clientInstance.equals("") || hooks.playerClass.equals("")) { System.out.println("[RuneLit] Bad hooks, re-scraping."); - ByteCodePatcher.clientInstance = getClientInstance(ByteCodeUtils.injectedClientFile.getPath()); + ByteCodePatcher.clientInstance = ByteCodeUtils.injectedClientFile.getPath(); ByteCodePatcher.findHooks(injectedClientFile.getPath()); } else { ByteCodePatcher.clientInstance = hooks.clientInstance; @@ -537,7 +298,7 @@ public class ClientLoader } else { System.out.println("[RuneLit] Hooks file not found, scraping hooks."); - ByteCodePatcher.clientInstance = getClientInstance(ByteCodeUtils.injectedClientFile.getPath()); + ByteCodePatcher.clientInstance = initVanillaInjected(ByteCodeUtils.injectedClientFile.getPath()); ByteCodePatcher.findHooks(injectedClientFile.getPath()); } diff --git a/runelite-client/src/main/java/net/runelite/client/rs/bytecode/ByteCodePatcher.java b/runelite-client/src/main/java/net/runelite/client/rs/bytecode/ByteCodePatcher.java index 573569d570..9b1b614cc4 100644 --- a/runelite-client/src/main/java/net/runelite/client/rs/bytecode/ByteCodePatcher.java +++ b/runelite-client/src/main/java/net/runelite/client/rs/bytecode/ByteCodePatcher.java @@ -7,10 +7,7 @@ import javassist.CtClass; import javassist.NotFoundException; import net.runelite.client.RuneLite; import net.runelite.client.rs.ClientLoader; -import net.runelite.client.rs.bytecode.transformers.ActorTransform; -import net.runelite.client.rs.bytecode.transformers.PlayerTransform; -import net.runelite.client.rs.bytecode.transformers.ProjectileTransform; -import net.runelite.client.rs.bytecode.transformers.getProjectileTransform; +import net.runelite.client.rs.bytecode.transformers.*; import net.runelite.http.api.RuneLiteAPI; import org.xeustechnologies.jcl.JarClassLoader; @@ -45,6 +42,8 @@ public class ByteCodePatcher { transformProjectile(projectileClass); Class playerClass = Class.forName(hooks.playerClass, false, child); transformPlayer(playerClass); + Class clientClass = Class.forName(hooks.clientClass, false, child); + transformClient(clientClass); ByteCodeUtils.updateHijackedJar(); } catch (Exception e) { e.printStackTrace(); @@ -99,6 +98,7 @@ public class ByteCodePatcher { checkActor(classToLoad); checkProjectile(classToLoad); checkPlayer(classToLoad); + checkClient(classToLoad); } catch (Exception e) { e.printStackTrace(); } @@ -153,42 +153,6 @@ public class ByteCodePatcher { pt.modify(projectile); } - public static void checkPlayer(Class current) { - try { - Method method = current.getDeclaredMethod("getSkullIcon"); - if (method!=null) { - hooks.playerClass = current.getName(); - System.out.println("[RuneLit] Transforming Player at class: "+current.getName()); - PlayerTransform pt = new PlayerTransform(); - pt.modify(current); - - public static void checkgetProjectiles(Class getprojectile) { - try { - Method method = getprojectile.getDeclaredMethod("getProjectiles"); - if (method != null) { - hooks.mainClientInstance = getprojectile.getName(); - System.out.println("[RuneLit] Transforming Projectile at class: " + getprojectile.getName()); - getProjectileTransform gpt = new getProjectileTransform(); - gpt.modify(getprojectile); - } - } catch (NoSuchMethodException e) { - //e.printStackTrace(); - } catch (NoClassDefFoundError e) { - //e.printStackTrace(); - } - } - - public static void transformPlayer(Class player) { - System.out.println("[RuneLit] Transforming Player at class: "+player.getName()); - PlayerTransform pt = new PlayerTransform(); - pt.modify(player); - - public static void transformGetProjectile(Class current) { - System.out.println("[RuneLit] Transforming getProjectile at class: " + current.getName()); - getProjectileTransform gpt = new getProjectileTransform(); - gpt.modify(current); - } - public static void checkPlayer(Class current) { try { Method method = current.getDeclaredMethod("getSkullIcon"); @@ -198,6 +162,20 @@ public class ByteCodePatcher { PlayerTransform pt = new PlayerTransform(); pt.modify(current); } + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static void checkClient(Class current) { + try { + Method method = current.getDeclaredMethod("getProjectiles"); + if (method != null) { + hooks.clientInstance = current.getName(); + System.out.println("[RuneLit] Transforming Projectile at class: " + current.getName()); + ClientTransform ct = new ClientTransform(); + ct.modify(current); + } } catch (NoSuchMethodException e) { //e.printStackTrace(); } catch (NoClassDefFoundError e) { @@ -205,6 +183,12 @@ public class ByteCodePatcher { } } + public static void transformClient(Class client) { + System.out.println("[RuneLit] Transforming Client at class: " + client.getName()); + ClientTransform ct = new ClientTransform(); + ct.modify(client); + } + public static void transformPlayer(Class player) { System.out.println("[RuneLit] Transforming Player at class: " + player.getName()); PlayerTransform pt = new PlayerTransform(); diff --git a/runelite-client/src/main/java/net/runelite/client/rs/bytecode/Hooks.java b/runelite-client/src/main/java/net/runelite/client/rs/bytecode/Hooks.java index e07199187d..a879bb6aae 100644 --- a/runelite-client/src/main/java/net/runelite/client/rs/bytecode/Hooks.java +++ b/runelite-client/src/main/java/net/runelite/client/rs/bytecode/Hooks.java @@ -9,6 +9,7 @@ public class Hooks { public String actorClass = ""; public String projectileClass = ""; public String playerClass = ""; + public String clientClass = "client"; //Always named client public String[] protectedMethods; public Hooks() { diff --git a/runelite-client/src/main/java/net/runelite/client/rs/bytecode/transformers/getProjectileTransform.java b/runelite-client/src/main/java/net/runelite/client/rs/bytecode/transformers/ClientTransform.java similarity index 89% rename from runelite-client/src/main/java/net/runelite/client/rs/bytecode/transformers/getProjectileTransform.java rename to runelite-client/src/main/java/net/runelite/client/rs/bytecode/transformers/ClientTransform.java index 4242c88f59..75e4f6ff93 100644 --- a/runelite-client/src/main/java/net/runelite/client/rs/bytecode/transformers/getProjectileTransform.java +++ b/runelite-client/src/main/java/net/runelite/client/rs/bytecode/transformers/ClientTransform.java @@ -3,27 +3,26 @@ package net.runelite.client.rs.bytecode.transformers; import javassist.CtClass; import javassist.CtMethod; import javassist.CtNewMethod; +import javassist.NotFoundException; import javassist.bytecode.AnnotationsAttribute; import javassist.bytecode.ClassFile; import javassist.bytecode.ConstPool; import net.runelite.client.rs.bytecode.ByteCodePatcher; - -public class getProjectileTransform { +public class ClientTransform { public CtClass ct = null; - public void modify(Class getprojectile) { + public void modify(Class client) { try { - ct = ByteCodePatcher.classPool.get(getprojectile.getName()); - - transformGetProjectile(); + ct = ByteCodePatcher.classPool.get(client.getName()); + transformGetProjectiles(); ByteCodePatcher.modifiedClasses.add(ct); - } catch (Exception e) { + } catch (NotFoundException e) { e.printStackTrace(); } } - public void transformGetProjectile() { + public void transformGetProjectiles() { CtMethod getProjectiles; try { @@ -53,4 +52,5 @@ public class getProjectileTransform { e.printStackTrace(); } } + }