Advertisement
Clem585

Untitled

Jul 23rd, 2021
1,287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 5.79 KB | None | 0 0
  1. package org.openrs2.deob.bytecode
  2.  
  3. import com.github.michaelbull.logging.InlineLogger
  4. import org.openrs2.asm.classpath.ClassPath
  5. import org.openrs2.asm.classpath.Library
  6. import org.openrs2.asm.io.JarLibraryReader
  7. import org.openrs2.asm.io.JarLibraryWriter
  8. import org.openrs2.asm.io.Pack200LibraryReader
  9. import org.openrs2.asm.transform.Transformer
  10. import org.openrs2.deob.bytecode.remap.ClassNamePrefixRemapper
  11. import org.openrs2.deob.bytecode.remap.StripClassNamePrefixRemapper
  12. import java.nio.file.Files
  13. import java.nio.file.Path
  14. import javax.inject.Inject
  15. import javax.inject.Singleton
  16.  
  17. @Singleton
  18. public class BytecodeDeobfuscator @Inject constructor(
  19.     @DeobfuscatorQualifier private val transformers: Set<Transformer>
  20. ) {
  21.     public fun run(input: Path, output: Path) {
  22.         // read input jars/packs
  23.         logger.info { "Reading input jars" }
  24.         // val client = Library.read("client", input.resolve("runescape_gl.pack200"), Pack200LibraryReader)
  25.         // val gl = Library.read("gl", input.resolve("jaggl.pack200"), Pack200LibraryReader)
  26.         // val loader = Library.read("loader", input.resolve("loader_gl.jar"), JarLibraryReader)
  27.         // val unpackClass = Library.read("unpackclass", input.resolve("unpackclass.pack"), JarLibraryReader)
  28.         val runescape = Library.read("runescape", input.resolve("runescape.jar"), JarLibraryReader)
  29.  
  30.         // overwrite client's classes with signed classes from the loader
  31.         // logger.info { "Moving signed classes from loader to signlink" }
  32.         // val signLink = Library("signlink")
  33.         // SignedClassUtils.move(loader, client, signLink)
  34.  
  35.         // move unpack class out of the loader (so the unpacker and loader can both depend on it)
  36.         // logger.info { "Moving unpack from loader to unpack" }
  37.         // val unpack = Library("unpack")
  38.         // unpack.add(loader.remove("unpack")!!)
  39.  
  40.         /*
  41.          * Prefix class names with the name of the library the class
  42.          * came from (e.g. `a` => `client!a`).
  43.          *
  44.          * Using ! as the separator was chosen because it is not valid in Java
  45.          * source code, so we won't expect to see it in the obfuscator's input.
  46.          * Furthermore, if any prefixes accidentally remain unstripped, the
  47.          * problem will be detected quickly as the deobfuscator's output will
  48.          * not compile. It also mirrors the syntax used in JarURLConnection,
  49.          * which has a similar purpose.
  50.          *
  51.          * In the early parts of the deobfuscation pipeline, this allows us to
  52.          * disambiguate a small number of classes in the signlink which clash
  53.          * with classes in the client.
  54.          *
  55.          * After name mapping has been performed, it allows us to disambiguate
  56.          * classes across separate libraries that have been refactored and
  57.          * given the same name.
  58.          *
  59.          * For example, the client and unpackclass both contain many common
  60.          * classes (e.g. the exception wrapper, linked list/node classes,
  61.          * bzip2/gzip decompression classes, and so on). Giving these the same
  62.          * names across both the client and unpackclass is desirable.
  63.          *
  64.          * (Unfortunately we can't deduplicate the classes, as they both expose
  65.          * different sets of fields/methods, presumably as a result of the
  66.          * obfuscator removing unused code.)
  67.          */
  68.         // val clientRemapper = ClassNamePrefixRemapper(client, gl, signLink)
  69.         // val glRemapper = ClassNamePrefixRemapper(gl)
  70.         // val loaderRemapper = ClassNamePrefixRemapper(loader, signLink, unpack)
  71.         // val signLinkRemapper = ClassNamePrefixRemapper(signLink)
  72.         // val unpackClassRemapper = ClassNamePrefixRemapper(unpackClass, unpack)
  73.         // val unpackRemapper = ClassNamePrefixRemapper(unpack)
  74.  
  75.         // client.remap(clientRemapper)
  76.         // gl.remap(glRemapper)
  77.         // loader.remap(loaderRemapper)
  78.         // signLink.remap(signLinkRemapper)
  79.         // unpack.remap(unpackRemapper)
  80.         // unpackClass.remap(unpackClassRemapper)
  81.  
  82.         // bundle libraries together into a common classpath
  83.         val runtime = ClassLoader.getPlatformClassLoader()
  84.         val classPath = ClassPath(
  85.             runtime,
  86.             dependencies = emptyList(),
  87.             libraries = listOf(runescape)//listOf(client, gl, loader, signLink, unpack, unpackClass)
  88.         )
  89.  
  90.         // deobfuscate
  91.         logger.info { "Transforming client" }
  92.         for (transformer in transformers) {
  93.             logger.info { "Running transformer ${transformer.javaClass.simpleName}" }
  94.             transformer.transform(classPath)
  95.         }
  96.  
  97.         // strip class name prefixes
  98.         // client.remap(StripClassNamePrefixRemapper)
  99.         // gl.remap(StripClassNamePrefixRemapper)
  100.         // loader.remap(StripClassNamePrefixRemapper)
  101.         // signLink.remap(StripClassNamePrefixRemapper)
  102.         // unpack.remap(StripClassNamePrefixRemapper)
  103.         // unpackClass.remap(StripClassNamePrefixRemapper)
  104.  
  105.         // write output jars
  106.         logger.info { "Writing output jars" }
  107.  
  108.         Files.createDirectories(output)
  109.  
  110.         runescape.write(output.resolve("runescape-out.jar"), JarLibraryWriter, classPath)
  111.         // client.write(output.resolve("client.jar"), JarLibraryWriter, classPath)
  112.         // gl.write(output.resolve("gl.jar"), JarLibraryWriter, classPath)
  113.         // loader.write(output.resolve("loader.jar"), JarLibraryWriter, classPath)
  114.         // signLink.write(output.resolve("signlink.jar"), JarLibraryWriter, classPath)
  115.         // unpack.write(output.resolve("unpack.jar"), JarLibraryWriter, classPath)
  116.         // unpackClass.write(output.resolve("unpackclass.jar"), JarLibraryWriter, classPath)
  117.     }
  118.  
  119.     private companion object {
  120.         private val logger = InlineLogger()
  121.     }
  122. }
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement