Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.22 KB | None | 0 0
  1. package com.resource.hooking;
  2.  
  3. import com.google.gson.GsonBuilder;
  4. import com.google.gson.reflect.TypeToken;
  5.  
  6. import java.io.*;
  7. import java.util.ArrayList;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11.  
  12. /**
  13.  * @author Daniel
  14.  *
  15.  * appends the {@link ReflectionMapper#file} into a parsable .json format
  16.  * mostly in a pretty terrible way currently, but it works for now until
  17.  * we figure out the exact format we want to use with the reflection hooks
  18.  */
  19. public class ReflectionMapper {
  20.  
  21.     /**
  22.      * The {@link File} to parse our {@link ReflectionMapper#classes} & {@link ReflectionMapper#hooks} from
  23.      */
  24.     private final File file = new File("./proguard/Mapping.txt");
  25.    
  26.     /**
  27.      * The {@link java.util.Map} of the Obfuscated class names
  28.      */
  29.     private final Map<String, String> classes = new HashMap<String, String>();
  30.  
  31.     /**
  32.      * The {@link java.util.Map} of the Obfuscated class names along side their respective methods & fields
  33.      */
  34.     private final Map<String, List<Obfuscated>> hooks = new HashMap<String, List<Obfuscated>>();
  35.  
  36.     /**
  37.      * Constructor
  38.      */
  39.     public ReflectionMapper() {
  40.         mapClasses();
  41.         mapChildren();
  42.         write();
  43.     }
  44.  
  45.     /**
  46.      * Creates a {@link java.util.Map} of the classes {@link ReflectionMapper#classes} in order to properly {@link java.util.Map} the hooks to the {@link ReflectionMapper#hooks}
  47.      */
  48.     private void mapClasses() {
  49.         try {
  50.             BufferedReader reader = new BufferedReader(new FileReader(file));
  51.             String line;
  52.            
  53.             while ((line = reader.readLine()) != null) {
  54.                 String[] split = line.split(" -> ");
  55.                 if (!line.startsWith("    ")) {
  56.                     classes.put(split[0], split[1].substring(0, split[1].length() - 1));
  57.                 }
  58.             }
  59.             reader.close();
  60.         } catch (Exception ex) {
  61.             ex.printStackTrace();
  62.         }
  63.     }
  64.  
  65.     /**
  66.      * Creates a {@link java.util.Map} in order to store the {@link ReflectionMapper#hooks} into a parsable .json file
  67.      */
  68.     private void mapChildren() {
  69.         try {
  70.             BufferedReader reader = new BufferedReader(new FileReader(file));
  71.             String line;
  72.  
  73.             String cor = "";
  74.             String cob = "";
  75.  
  76.             while ((line = reader.readLine()) != null) {
  77.                 String[] split = line.split(" -> ");
  78.                 if (line.startsWith("    ")) {
  79.                     String parameters = "";
  80.                     if (split[0].endsWith(")")) {
  81.                         parameters = split[0].substring(split[0].lastIndexOf("("), split[0].length());
  82.                     }
  83.                     String value = cor + ":" + cob;
  84.                     if (!hooks.containsKey(value)) {
  85.                         hooks.put(value, new ArrayList<Obfuscated>());
  86.                     }
  87.                     if (!parameters.trim().isEmpty()) {
  88.                         String trimmed = parameters.substring(1, parameters.length() - 1);
  89.                         if (trimmed.contains(",")) {
  90.                             StringBuilder sb = new StringBuilder();
  91.                             sb.append("(");
  92.                             String[] parts = trimmed.split(",");
  93.                             for (int index = 0; index < parts.length; index++) {
  94.                                 String clazz = classes.get(parts[index]);
  95.                                 sb.append(clazz != null ? clazz : parts[index]);
  96.                                 if (index != parts.length - 1) {
  97.                                     sb.append(",");
  98.                                 }
  99.                             }
  100.                             sb.append(")");
  101.                             parameters = sb.toString();
  102.                         } else {
  103.                             String clazz = classes.get(trimmed);
  104.                             if (clazz != null) {
  105.                                 parameters = "(" + clazz + ")";
  106.                             }
  107.                         }
  108.                     }
  109.                     hooks.get(value).add(new Obfuscated(split[0].trim().split(" ")[1], split[1] + parameters));
  110.                     //System.out.println("\t[Child] ->\n\t\t[Original]:\t" + split[0] + "\n\t\t[Obfuscated]:\t" + split[1] + parameters + "\n");
  111.                 } else {
  112.                     cor = split[0];
  113.                     cob = split[1].substring(0, split[1].length() - 1);
  114.                     String value = cor + ":" + cob;
  115.                     if (!hooks.containsKey(value)) {
  116.                         hooks.put(value, new ArrayList<Obfuscated>());
  117.                     }
  118.                     //System.out.println("[Original]:\t\t" + split[0] + "\n[Obfuscated]:\t" + split[1].substring(0, split[1].length() - 1));
  119.                 }
  120.             }
  121.             reader.close();
  122.         } catch (Exception ex) {
  123.             ex.printStackTrace();
  124.         }
  125.     }
  126.  
  127.     /**
  128.      * Stores the {@link ReflectionMapper#hooks} into a parsable .json format
  129.      */
  130.     private void write() {
  131.         try {
  132.             BufferedWriter writer = new BufferedWriter(new FileWriter(new File("./proguard/Mapping.json")));
  133.             writer.write(new GsonBuilder().setPrettyPrinting().create().toJson(hooks, new TypeToken<Map<String, List<Obfuscated>>>() {}.getType()));
  134.             writer.newLine();
  135.             writer.flush();
  136.             writer.close();
  137.         } catch (Exception ex) {
  138.             ex.printStackTrace();
  139.         }
  140.     }
  141.  
  142.     /**
  143.      * Inner class to get what we need done for now
  144.      */
  145.     private class Obfuscated {
  146.         /**
  147.          * original type name
  148.          */
  149.         private final String original;
  150.         /**
  151.          * obfuscated type name
  152.          */
  153.         private final String modified;
  154.  
  155.         /**
  156.          * {@link ReflectionMapper.Obfuscated} Constructor
  157.          *
  158.          * @param original - {@link ReflectionMapper.Obfuscated#original}
  159.          * @param modified - {@link ReflectionMapper.Obfuscated#modified}
  160.          */
  161.         public Obfuscated(String original, String modified) {
  162.             this.original = original;
  163.             this.modified = modified;
  164.         }
  165.     }
  166.  
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement