Allow component classname obfuscation

This commit is contained in:
topjohnwu
2019-10-16 04:38:31 -04:00
parent c7033dd757
commit 43bda2d4a4
22 changed files with 132 additions and 63 deletions

View File

@@ -0,0 +1,32 @@
package com.topjohnwu.magisk;
import java.util.HashMap;
import java.util.Map;
class ComponentMap {
private static Map<String, String> map = new HashMap<>(6);
// This mapping will be sent into the guest app
static Map<String, String> inverseMap;
static {
map.put(a.z.class.getName(), "a.c");
map.put("a.x", "a.f");
map.put("a.o", "a.b");
map.put("a.g", "a.m");
map.put(a.w.class.getName(), "a.h");
map.put("a.v", "a.j");
map.put("a.s", "androidx.work.impl.WorkManagerInitializer");
inverseMap = new HashMap<>(map.size());
for (Map.Entry<String, String> e : map.entrySet()) {
inverseMap.put(e.getValue(), e.getKey());
}
}
static String get(String name) {
String n = map.get(name);
return n != null ? n : name;
}
}

View File

@@ -13,6 +13,7 @@ import com.topjohnwu.magisk.utils.DynamicClassLoader;
import java.io.File;
import java.lang.reflect.Method;
import java.util.Map;
import static com.topjohnwu.magisk.DownloadActivity.TAG;
@@ -44,7 +45,8 @@ public class DelegateApplication extends Application {
Object df = cl.loadClass("a.a").newInstance();
// Create the delegate Application
delegate = (Application) cl.loadClass("a.e").newInstance();
delegate = (Application) cl.loadClass("a.e").getConstructor(Map.class)
.newInstance(ComponentMap.inverseMap);
// Call attachBaseContext without ContextImpl to show it is being wrapped
Method m = ContextWrapper.class.getDeclaredMethod("attachBaseContext", Context.class);

View File

@@ -8,12 +8,15 @@ import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ContentProvider;
import android.content.Intent;
import android.util.Log;
import com.topjohnwu.magisk.dummy.DummyActivity;
import com.topjohnwu.magisk.dummy.DummyProvider;
import com.topjohnwu.magisk.dummy.DummyReceiver;
import com.topjohnwu.magisk.dummy.DummyService;
import static com.topjohnwu.magisk.DownloadActivity.TAG;
@SuppressLint("NewApi")
public class DelegateComponentFactory extends AppComponentFactory {
@@ -22,39 +25,45 @@ public class DelegateComponentFactory extends AppComponentFactory {
@Override
public Application instantiateApplication(ClassLoader cl, String className) {
loader = cl;
if (loader == null) loader = cl;
Log.d(TAG, className);
return new DelegateApplication(this);
}
@Override
public Activity instantiateActivity(ClassLoader cl, String className, Intent intent)
throws ClassNotFoundException, IllegalAccessException, InstantiationException {
Log.d(TAG, className);
if (delegate != null)
return delegate.instantiateActivity(loader, className, intent);
return delegate.instantiateActivity(loader, ComponentMap.get(className), intent);
return create(className, DummyActivity.class);
}
@Override
public BroadcastReceiver instantiateReceiver(ClassLoader cl, String className, Intent intent)
throws ClassNotFoundException, IllegalAccessException, InstantiationException {
Log.d(TAG, className);
if (delegate != null)
return delegate.instantiateReceiver(loader, className, intent);
return delegate.instantiateReceiver(loader, ComponentMap.get(className), intent);
return create(className, DummyReceiver.class);
}
@Override
public Service instantiateService(ClassLoader cl, String className, Intent intent)
throws ClassNotFoundException, IllegalAccessException, InstantiationException {
Log.d(TAG, className);
if (delegate != null)
return delegate.instantiateService(loader, className, intent);
return delegate.instantiateService(loader, ComponentMap.get(className), intent);
return create(className, DummyService.class);
}
@Override
public ContentProvider instantiateProvider(ClassLoader cl, String className)
throws ClassNotFoundException, IllegalAccessException, InstantiationException {
Log.d(TAG, className);
if (loader == null) loader = cl;
if (delegate != null)
return delegate.instantiateProvider(loader, className);
return delegate.instantiateProvider(loader, ComponentMap.get(className));
return create(className, DummyProvider.class);
}
@@ -63,6 +72,7 @@ public class DelegateComponentFactory extends AppComponentFactory {
*/
private <T> T create(String name, Class<? extends T> dummy)
throws InstantiationException, IllegalAccessException {
Log.d(TAG, "create " + name);
try {
return (T) loader.loadClass(name).newInstance();
} catch (IllegalAccessException | InstantiationException | ClassNotFoundException ignored) {