1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
| public class ShellApplication extends Application { private static final String TAG = "ShellApplication";
public static String getPassword(){ return "abcdefghijklmnop"; }
@Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); AES.init(getPassword()); File apkFile = new File(getApplicationInfo().sourceDir); File unZipFile = getDir("fake_apk", MODE_PRIVATE); File app = new File(unZipFile, "app"); if (!app.exists()) { Zip.unZip(apkFile, app); File[] files = app.listFiles(); for (File file : files) { String name = file.getName(); if (name.equals("classes.dex")) {
} else if (name.endsWith(".dex")) { try { byte[] bytes = getBytes(file); FileOutputStream fos = new FileOutputStream(file); byte[] decrypt = AES.decrypt(bytes); fos.write(decrypt); fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); } } } } List list = new ArrayList<>(); Log.d("FAKE", Arrays.toString(app.listFiles())); for (File file : app.listFiles()) { if (file.getName().endsWith(".dex")) { list.add(file); } }
Log.d("FAKE", list.toString()); try { V19.install(getClassLoader(), list, unZipFile); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } }
private static Field findField(Object instance, String name) throws NoSuchFieldException { Class clazz = instance.getClass();
while (clazz != null) { try { Field e = clazz.getDeclaredField(name); if (!e.isAccessible()) { e.setAccessible(true); }
return e; } catch (NoSuchFieldException var4) { clazz = clazz.getSuperclass(); } }
throw new NoSuchFieldException("Field " + name + " not found in " + instance.getClass()); }
private static Method findMethod(Object instance, String name, Class... parameterTypes) throws NoSuchMethodException { Class clazz = instance.getClass(); while (clazz != null) { try { Method e = clazz.getDeclaredMethod(name, parameterTypes); if (!e.isAccessible()) { e.setAccessible(true); }
return e; } catch (NoSuchMethodException var5) { clazz = clazz.getSuperclass(); } } throw new NoSuchMethodException("Method " + name + " with parameters " + Arrays.asList (parameterTypes) + " not found in " + instance.getClass()); }
private static void expandFieldArray(Object instance, String fieldName, Object[] extraElements) throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException { Field jlrField = findField(instance, fieldName); Object[] original = (Object[]) ((Object[]) jlrField.get(instance)); Object[] combined = (Object[]) ((Object[]) Array.newInstance(original.getClass() .getComponentType(), original.length + extraElements.length)); System.arraycopy(original, 0, combined, 0, original.length); System.arraycopy(extraElements, 0, combined, original.length, extraElements.length); jlrField.set(instance, combined); }
private static final class V19 { private V19() { } private static void install(ClassLoader loader, List<File> additionalClassPathEntries, File optimizedDirectory) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, InvocationTargetException, NoSuchMethodException { Field pathListField = findField(loader, "pathList"); Object dexPathList = pathListField.get(loader); ArrayList suppressedExceptions = new ArrayList(); Log.d(TAG, "Build.VERSION.SDK_INT " + Build.VERSION.SDK_INT); if (Build.VERSION.SDK_INT >= 23) { expandFieldArray(dexPathList, "dexElements", makePathElements(dexPathList, new ArrayList(additionalClassPathEntries), optimizedDirectory, suppressedExceptions)); } else { expandFieldArray(dexPathList, "dexElements", makeDexElements(dexPathList, new ArrayList(additionalClassPathEntries), optimizedDirectory, suppressedExceptions)); } if (suppressedExceptions.size() > 0) { Iterator suppressedExceptionsField = suppressedExceptions.iterator();
while (suppressedExceptionsField.hasNext()) { IOException dexElementsSuppressedExceptions = (IOException) suppressedExceptionsField.next(); Log.w("MultiDex", "Exception in makeDexElement", dexElementsSuppressedExceptions); } Field suppressedExceptionsField1 = findField(loader, "dexElementsSuppressedExceptions"); IOException[] dexElementsSuppressedExceptions1 = (IOException[]) ((IOException[]) suppressedExceptionsField1.get(loader)); if (dexElementsSuppressedExceptions1 == null) { dexElementsSuppressedExceptions1 = (IOException[]) suppressedExceptions .toArray(new IOException[suppressedExceptions.size()]); } else { IOException[] combined = new IOException[suppressedExceptions.size() + dexElementsSuppressedExceptions1.length]; suppressedExceptions.toArray(combined); System.arraycopy(dexElementsSuppressedExceptions1, 0, combined, suppressedExceptions.size(), dexElementsSuppressedExceptions1.length); dexElementsSuppressedExceptions1 = combined; }
suppressedExceptionsField1.set(loader, dexElementsSuppressedExceptions1); }
}
private static Object[] makeDexElements(Object dexPathList, ArrayList<File> files, File optimizedDirectory, ArrayList<IOException> suppressedExceptions) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
Method makeDexElements = findMethod(dexPathList, "makeDexElements", new Class[]{ArrayList.class, File.class, ArrayList.class}); return ((Object[]) makeDexElements.invoke(dexPathList, new Object[]{files, optimizedDirectory, suppressedExceptions})); } }
private static Object[] makePathElements( Object dexPathList, ArrayList<File> files, File optimizedDirectory, ArrayList<IOException> suppressedExceptions) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { Method makePathElements; try { makePathElements = findMethod(dexPathList, "makePathElements", List.class, File.class, List.class); } catch (NoSuchMethodException e) { Log.e(TAG, "NoSuchMethodException: makePathElements(List,File,List) failure"); try { makePathElements = findMethod(dexPathList, "makePathElements", ArrayList.class, File.class, ArrayList.class); } catch (NoSuchMethodException e1) { Log.e(TAG, "NoSuchMethodException: makeDexElements(ArrayList,File,ArrayList) failure"); try { Log.e(TAG, "NoSuchMethodException: try use v19 instead"); return V19.makeDexElements(dexPathList, files, optimizedDirectory, suppressedExceptions); } catch (NoSuchMethodException e2) { Log.e(TAG, "NoSuchMethodException: makeDexElements(List,File,List) failure"); throw e2; } } } return (Object[]) makePathElements.invoke(dexPathList, files, optimizedDirectory, suppressedExceptions); }
private byte[] getBytes(File file) throws Exception { RandomAccessFile r = new RandomAccessFile(file, "r"); byte[] buffer = new byte[(int) r.length()]; r.readFully(buffer); r.close(); return buffer; } }
|