|
| 1 | +package aSD.transformers; |
| 2 | + |
| 3 | +import java.util.AbstractSet; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.Collections; |
| 6 | +import java.util.Iterator; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Set; |
| 9 | + |
| 10 | +import javax.annotation.Nonnull; |
| 11 | + |
| 12 | +import org.jf.dexlib2.DexFileFactory; |
| 13 | +import org.jf.dexlib2.Opcode; |
| 14 | +import org.jf.dexlib2.Opcodes; |
| 15 | +import org.jf.dexlib2.ReferenceType; |
| 16 | +import org.jf.dexlib2.builder.BuilderInstruction; |
| 17 | +import org.jf.dexlib2.builder.MutableMethodImplementation; |
| 18 | +import org.jf.dexlib2.builder.instruction.BuilderInstruction10x; |
| 19 | +import org.jf.dexlib2.builder.instruction.BuilderInstruction21c; |
| 20 | +import org.jf.dexlib2.builder.instruction.BuilderInstruction31c; |
| 21 | +import org.jf.dexlib2.iface.ClassDef; |
| 22 | +import org.jf.dexlib2.iface.DexFile; |
| 23 | +import org.jf.dexlib2.iface.Method; |
| 24 | +import org.jf.dexlib2.iface.MethodImplementation; |
| 25 | +import org.jf.dexlib2.iface.instruction.Instruction; |
| 26 | +import org.jf.dexlib2.iface.instruction.OneRegisterInstruction; |
| 27 | +import org.jf.dexlib2.iface.instruction.ReferenceInstruction; |
| 28 | +import org.jf.dexlib2.immutable.ImmutableClassDef; |
| 29 | +import org.jf.dexlib2.immutable.ImmutableMethod; |
| 30 | +import org.jf.dexlib2.immutable.ImmutableMethodImplementation; |
| 31 | +import org.jf.dexlib2.immutable.instruction.ImmutableInstruction11x; |
| 32 | +import org.jf.dexlib2.immutable.reference.ImmutableMethodReference; |
| 33 | +import org.jf.dexlib2.immutable.reference.ImmutableStringReference; |
| 34 | + |
| 35 | +import com.google.common.collect.ImmutableList; |
| 36 | + |
| 37 | +/** |
| 38 | + * @author Gorav Gupta |
| 39 | + * |
| 40 | + */ |
| 41 | +public class Transformers { |
| 42 | + |
| 43 | + public static void decryptStrings(String inDex, String outDex, int API, String callMethodRef, boolean removeCall, |
| 44 | + ImmutableMethodReference immutableMethodReference) throws Exception { |
| 45 | + |
| 46 | + DexFile dexFile = DexFileFactory.loadDexFile(inDex, Opcodes.forApi(API)); |
| 47 | + |
| 48 | + List<ClassDef> classes = new ArrayList<>(); |
| 49 | + |
| 50 | + for (ClassDef classDef : dexFile.getClasses()) { |
| 51 | + |
| 52 | + List<Method> methods = new ArrayList<>(); |
| 53 | + boolean modifiedMethod = false; |
| 54 | + |
| 55 | + for (Method method : classDef.getMethods()) { |
| 56 | + MethodImplementation implementation = method.getImplementation(); |
| 57 | + |
| 58 | + if (!removeCall && method.equals(immutableMethodReference)) { |
| 59 | + modifiedMethod = true; |
| 60 | + methods.add(new ImmutableMethod(method.getDefiningClass(), method.getName(), method.getParameters(), |
| 61 | + method.getReturnType(), method.getAccessFlags(), method.getAnnotations(), |
| 62 | + new ImmutableMethodImplementation(1, |
| 63 | + ImmutableList.of(new ImmutableInstruction11x(Opcode.RETURN_OBJECT, 0)), null, |
| 64 | + null))); |
| 65 | + |
| 66 | + } else if (implementation != null && methodNeedsModification(implementation)) { |
| 67 | + modifiedMethod = true; |
| 68 | + StringDecryptor.method.add(method); |
| 69 | + methods.add(new ImmutableMethod(method.getDefiningClass(), method.getName(), method.getParameters(), |
| 70 | + method.getReturnType(), method.getAccessFlags(), method.getAnnotations(), |
| 71 | + modifyMethod(implementation, callMethodRef, removeCall))); |
| 72 | + StringDecryptor.method.remove(0); |
| 73 | + } else { |
| 74 | + methods.add(method); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + if (!modifiedMethod) { |
| 79 | + classes.add(classDef); |
| 80 | + } else { |
| 81 | + classes.add(new ImmutableClassDef(classDef.getType(), classDef.getAccessFlags(), |
| 82 | + classDef.getSuperclass(), classDef.getInterfaces(), classDef.getSourceFile(), |
| 83 | + classDef.getAnnotations(), classDef.getFields(), methods)); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + writeDexFile(classes, outDex, API); |
| 88 | + System.out.println("https://github.com/Modify24x7"); |
| 89 | + } |
| 90 | + |
| 91 | + static boolean methodNeedsModification(@Nonnull MethodImplementation implementation) { |
| 92 | + for (Instruction instruction : implementation.getInstructions()) { |
| 93 | + if (instruction instanceof ReferenceInstruction) { |
| 94 | + if (((ReferenceInstruction) instruction).getReferenceType() == ReferenceType.STRING) { |
| 95 | + return true; |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + static MethodImplementation modifyMethod(@Nonnull MethodImplementation implementation, String callMethodRef, |
| 103 | + boolean removeCall) { |
| 104 | + |
| 105 | + MutableMethodImplementation mutableImplementation = new MutableMethodImplementation(implementation); |
| 106 | + List<BuilderInstruction> instructions = mutableImplementation.getInstructions(); |
| 107 | + ArrayList<Integer> callList = new ArrayList<>(); |
| 108 | + |
| 109 | + // Analyze |
| 110 | + for (int i = 0; i < instructions.size(); i++) { |
| 111 | + Instruction instruction = instructions.get(i); |
| 112 | + if (instruction instanceof ReferenceInstruction) { |
| 113 | + String call = ((ReferenceInstruction) instruction).getReference().toString(); |
| 114 | + if (call.equals(callMethodRef)) { |
| 115 | + callList.add(i); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + // Decode and replace string |
| 121 | + for (int i = 0; i < instructions.size(); i++) { |
| 122 | + Instruction instruction = instructions.get(i); |
| 123 | + if (instruction instanceof ReferenceInstruction) { |
| 124 | + if (((ReferenceInstruction) instruction).getReferenceType() == ReferenceType.STRING) { |
| 125 | + if (repTrue(callList, i)) { |
| 126 | + String str = ((ReferenceInstruction) instruction).getReference().toString(); |
| 127 | + Opcode opcode = instruction.getOpcode(); |
| 128 | + |
| 129 | + int register = ((OneRegisterInstruction) instruction).getRegisterA(); |
| 130 | + |
| 131 | + if (opcode == Opcode.CONST_STRING_JUMBO) |
| 132 | + mutableImplementation.replaceInstruction(i, new BuilderInstruction31c(opcode, register, |
| 133 | + new ImmutableStringReference(StringDecryptor.decryptor(str)))); |
| 134 | + else |
| 135 | + mutableImplementation.replaceInstruction(i, new BuilderInstruction21c(opcode, register, |
| 136 | + new ImmutableStringReference(StringDecryptor.decryptor(str)))); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + if (removeCall) { |
| 143 | + // Replace Method Call to NOP |
| 144 | + for (int i = 0; i < instructions.size(); i++) { |
| 145 | + Instruction instruction = instructions.get(i); |
| 146 | + if (instruction instanceof ReferenceInstruction) { |
| 147 | + String call = ((ReferenceInstruction) instruction).getReference().toString(); |
| 148 | + if (call.equals(callMethodRef)) { |
| 149 | + mutableImplementation.replaceInstruction(i, new BuilderInstruction10x(Opcode.NOP)); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + // Replace MOVE_RESULT_OBJECT to NOP |
| 155 | + for (int i = 0; i < instructions.size(); i++) { |
| 156 | + if (repTrue2(callList, i)) { |
| 157 | + mutableImplementation.replaceInstruction(i, new BuilderInstruction10x(Opcode.NOP)); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + return mutableImplementation; |
| 163 | + } |
| 164 | + |
| 165 | + // Replace String |
| 166 | + static boolean repTrue(ArrayList<Integer> callList, int ins) { |
| 167 | + for (int i = 0; i < callList.size(); i++) { |
| 168 | + if (ins == callList.get(i) - 1) { |
| 169 | + return true; |
| 170 | + } |
| 171 | + } |
| 172 | + return false; |
| 173 | + } |
| 174 | + |
| 175 | + // Replace MOVE_RESULT_OBJECT |
| 176 | + static boolean repTrue2(ArrayList<Integer> callList, int ins) { |
| 177 | + for (int i = 0; i < callList.size(); i++) { |
| 178 | + if (ins == callList.get(i) + 1) { |
| 179 | + return true; |
| 180 | + } |
| 181 | + } |
| 182 | + return false; |
| 183 | + } |
| 184 | + |
| 185 | + // Write Output Dex |
| 186 | + static void writeDexFile(List<ClassDef> classes, String outDex, int API) throws Exception { |
| 187 | + |
| 188 | + Collections.sort(classes); |
| 189 | + |
| 190 | + DexFileFactory.writeDexFile(outDex, new DexFile() { |
| 191 | + |
| 192 | + @Nonnull |
| 193 | + @Override |
| 194 | + public Set<? extends ClassDef> getClasses() { |
| 195 | + // TODO Auto-generated method stub |
| 196 | + return new AbstractSet<ClassDef>() { |
| 197 | + |
| 198 | + @Override |
| 199 | + public Iterator<ClassDef> iterator() { |
| 200 | + // TODO Auto-generated method stub |
| 201 | + return classes.iterator(); |
| 202 | + } |
| 203 | + |
| 204 | + @Override |
| 205 | + public int size() { |
| 206 | + // TODO Auto-generated method stub |
| 207 | + return classes.size(); |
| 208 | + } |
| 209 | + |
| 210 | + }; |
| 211 | + } |
| 212 | + |
| 213 | + @Override |
| 214 | + public Opcodes getOpcodes() { |
| 215 | + // TODO Auto-generated method stub |
| 216 | + return Opcodes.forApi(API); |
| 217 | + } |
| 218 | + }); |
| 219 | + } |
| 220 | + |
| 221 | +} |
0 commit comments