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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
| // add start import android.os.Build; import android.util.ArrayMap; import android.util.Log; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.lang.ref.WeakReference; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.lang.reflect.Constructor; import dalvik.system.BaseDexClassLoader; import dalvik.system.DexClassLoader; // add end
// add start public static String CKCatTAG = "CKCatUnPack"; public static Field getClassField(ClassLoader classloader, String className, String fieldName){ try{ Class clazz = classloader.loadClass(className); Field field = clazz.getDeclaredField(fieldName); field.setAccessible(true); return field;
}catch(SecurityException e){ e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return null; }
public static Object getClassFieldObject(ClassLoader classloader, String className, Object obj, String fieldName){ try { Class clazz = classloader.loadClass(className); Field field = clazz.getDeclaredField(fieldName); field.setAccessible(true); Object result = null; result = field.get(obj); return result; } catch (Exception e) { e.printStackTrace(); } return null; }
public static Object invokeStaticMethod(String className, String methodName, Class[] pareType, Object[] pareValues){ try { Class clazz = Class.forName(className); Method method = clazz.getMethod(methodName, pareType); return method.invoke(null, pareValues); } catch(Exception e){ e.printStackTrace(); } return null; }
public static Object getFieldObject(String className, Object obj, String fieldName){ try { Class clazz = Class.forName(className); Field field = clazz.getDeclaredField(fieldName); field.setAccessible(true); return field.get(obj); } catch (Exception e) { e.printStackTrace(); } return null;
}
public static ClassLoader getClassLoader(){ ClassLoader classloader = null; Object currentActivityThread = invokeStaticMethod("android.app.ActivityThread", "currentActivityThread", new Class[]{}, new Object[]{}); Object mBoundApplication = getFieldObject("android.app.ActivityThread", currentActivityThread, "mBoundApplication"); Object loadedApkInfo = getFieldObject("android.app.ActivityThread$AppBindData", mBoundApplication, "info"); Application mApplication = (Application)getFieldObject("android.app.loadedApk", loadedApkInfo, "mApplication"); classloader = mApplication.getClassLoader(); return classloader; }
public static void loadClassAndInvoke(ClassLoader appClassloader, String eachClassName, Method dumpMethodCode_method){ Log.i(CKCatTAG, "go into loadClassAndInvoke->" + "classname:" + eachClassName);
Class retClass = null; try { retClass = appClassloader.loadClass(eachClassName); } catch (Exception e) { e.printStackTrace(); return; } catch (Error e) { e.printStackTrace(); return; }
if (retClass != null) { try { Constructor<?> cons[] = retClass.getDeclaredConstructors(); for (Constructor<?> constructor : cons){ if (dumpMethodCode_method != null) { try { dumpMethodCode_method.invoke(null, constructor); } catch (Exception e) { e.printStackTrace(); continue; } catch (Error e) { e.printStackTrace(); continue; } } else { Log.e(CKCatTAG, "dumpMethodCode_method is null "); } } } catch (Exception e) { e.printStackTrace(); } catch (Error e) { e.printStackTrace(); }
try { Method[] methods = retClass.getDeclaredMethods(); if (methods != null) { for (Method m : methods) { if (dumpMethodCode_method != null) { try { dumpMethodCode_method.invoke(null, m); } catch (Exception e) { e.printStackTrace(); continue; } catch (Error e) { e.printStackTrace(); continue; } } else { Log.e(CKCatTAG, "dumpMethodCode_method is null "); } } } } catch (Exception e) { e.printStackTrace(); } catch (Error e) { e.printStackTrace(); } } }
public static void fart(){ //获取当前 classloader ClassLoader appClassloader = getClassLoader(); List<Object> dexFieldsArray = new ArrayList<Object>();
// 获取 pathList 字段 Field pathList_Field = (Field)getClassField(appClassloader, "dalvik.system.BaseDexClassLoader", "pathList"); Object pathList_object = getFieldObject("dalvik.system.BaseDexClassLoader", appClassloader, "pathList");
// 获取 dexElements Object[] dexElements = (Object[]) getFieldObject("dalvik.system.DexPathList", pathList_object, "dexElements");
Field dexFile_Field = null; try { dexFile_Field = (Field)getClassField(appClassloader, "dalvik.system.DexPathList$Element", "dexFile"); } catch (Exception e) { e.printStackTrace(); }
Class dexFileClazz = null; try { dexFileClazz = appClassloader.loadClass("dalvik.system.DexFile"); } catch (Exception e) { e.printStackTrace(); }
Method getClassNameList_Method = null; Method defineClass_Method = null; Method dumpMethodCode_Method = null; // 通过反射获取获取 dumpMethodCode for (Method method : dexFileClazz.getDeclaredMethods()){ if (method.getName().equals("getClassNameList")) { getClassNameList_Method = method; getClassNameList_Method.setAccessible(true); } if (method.getName().equals("defineClassNative")) { defineClass_Method = method; defineClass_Method.setAccessible(true); } if (method.getName().equals("dumpMethodCode")) { dumpMethodCode_Method = method; dumpMethodCode_Method.setAccessible(true); } }
// 获取 mCookie Field mCookieField = getClassField(appClassloader, "dalvik.system.DexFile", "mCookie"); for (int i = 0; i < dexElements.length; i++) { Object element = dexElements[i]; Object dexfile = null; try { dexfile = (Object)dexFile_Field.get(element); } catch (Exception e) { e.printStackTrace(); }
if (dexfile == null) { continue; }
if(dexfile != null){ dexFieldsArray.add(dexfile); Object mCookie = getClassFieldObject(appClassloader, "dalvik.system.DexFile", dexfile, "mCookie"); if (mCookie == null) { continue; }
String[] classNames = null; try { classNames = (String[])getClassNameList_Method.invoke(dexfile, mCookie); } catch (Exception e) { e.printStackTrace(); continue; } catch (Error e) { e.printStackTrace(); continue; }
if (classNames != null) { for(String eachClassName: classNames){ // 执行主动调用 loadClassAndInvoke(appClassloader, eachClassName, dumpMethodCode_Method); } } } }
}
public static void fartthread(){ new Thread(new Runnable(){
@Override public void run(){ try{ Log.e(CKCatTAG, "start sleep,wait for fartthread start......"); Thread.sleep(1*60*1000); }catch(InterruptedException e){ e.printStackTrace(); } Log.e(CKCatTAG, "sleep over and start fartthread"); fart(); Log.e(CKCatTAG, "fart run over"); } }); } // add end
/** Core implementation of activity launch. */ private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
... // add start fartthread(); // add end return activity; }
|