Support restarting app when obfuscated

This commit is contained in:
topjohnwu 2019-11-03 02:55:12 -05:00
parent 14ba002cbc
commit 0c9feedb37
14 changed files with 46 additions and 57 deletions

View File

@ -53,18 +53,12 @@ fun Context.wrapJob(): Context = object : GlobalResContext(this) {
} }
} }
fun Class<*>.cmp(pkg: String = BuildConfig.APPLICATION_ID): ComponentName { fun Class<*>.cmp(pkg: String): ComponentName {
val name = ClassMap[this].name val name = ClassMap[this].name
return ComponentName(pkg, Info.stub?.componentMap?.get(name) ?: name) return ComponentName(pkg, Info.stub?.componentMap?.get(name) ?: name)
} }
fun Context.intent(c: Class<*>): Intent { inline fun <reified T> Context.intent() = Intent().setComponent(T::class.java.cmp(packageName))
val cls = ClassMap[c]
return Info.stub?.let {
val className = it.componentMap.getOrElse(cls.name) { cls.name }
Intent().setComponent(ComponentName(this, className))
} ?: Intent(this, cls)
}
private open class GlobalResContext(base: Context) : ContextWrapper(base) { private open class GlobalResContext(base: Context) : ContextWrapper(base) {
open val mRes: Resources get() = ResourceMgr.resource open val mRes: Resources get() = ResourceMgr.resource
@ -192,8 +186,9 @@ object ClassMap {
UpdateCheckService::class.java to a.g::class.java, UpdateCheckService::class.java to a.g::class.java,
GeneralReceiver::class.java to a.h::class.java, GeneralReceiver::class.java to a.h::class.java,
DownloadService::class.java to a.j::class.java, DownloadService::class.java to a.j::class.java,
SuRequestActivity::class.java to a.m::class.java SuRequestActivity::class.java to a.m::class.java,
ProcessPhoenix::class.java to a.r::class.java
) )
operator fun get(c: Class<*>) = map.getOrElse(c) { throw IllegalArgumentException() } operator fun get(c: Class<*>) = map.getOrElse(c) { c }
} }

View File

@ -140,7 +140,7 @@ open class DownloadService : RemoteFileService() {
inline operator fun invoke(context: Context, argBuilder: Builder.() -> Unit) { inline operator fun invoke(context: Context, argBuilder: Builder.() -> Unit) {
val app = context.applicationContext val app = context.applicationContext
val builder = Builder().apply(argBuilder) val builder = Builder().apply(argBuilder)
val intent = app.intent(DownloadService::class.java).putExtra(ARG_URL, builder.subject) val intent = app.intent<DownloadService>().putExtra(ARG_URL, builder.subject)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
app.startForegroundService(intent) app.startForegroundService(intent)

View File

@ -38,7 +38,7 @@ private fun RemoteFileService.upgrade(apk: File, id: Int) {
patch(apk, id) patch(apk, id)
} else { } else {
// Simply relaunch the app // Simply relaunch the app
ProcessPhoenix.triggerRebirth(this) ProcessPhoenix.triggerRebirth(this, intent<ProcessPhoenix>())
} }
} else { } else {
patch(apk, id) patch(apk, id)

View File

@ -63,7 +63,7 @@ open class GeneralReceiver : BaseReceiver() {
} }
when (action) { when (action) {
REQUEST -> { REQUEST -> {
val i = context.intent(SuRequestActivity::class.java) val i = context.intent<SuRequestActivity>()
.setAction(action) .setAction(action)
.putExtra("socket", intent.getStringExtra("socket")) .putExtra("socket", intent.getStringExtra("socket"))
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

View File

@ -60,7 +60,7 @@ open class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>(), Na
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
if (!SplashActivity.DONE) { if (!SplashActivity.DONE) {
startActivity(intent(SplashActivity::class.java)) startActivity(intent<SplashActivity>())
finish() finish()
} }

View File

@ -40,7 +40,7 @@ open class SplashActivity : Activity() {
Config.suManager = "" Config.suManager = ""
Shell.su("pm uninstall $pkg").submit() Shell.su("pm uninstall $pkg").submit()
} }
if (TextUtils.equals(pkg, packageName)) { if (pkg == packageName) {
runCatching { runCatching {
// We are the manager, remove com.topjohnwu.magisk as it could be malware // We are the manager, remove com.topjohnwu.magisk as it could be malware
packageManager.getApplicationInfo(BuildConfig.APPLICATION_ID, 0) packageManager.getApplicationInfo(BuildConfig.APPLICATION_ID, 0)
@ -60,10 +60,9 @@ open class SplashActivity : Activity() {
// Setup shortcuts // Setup shortcuts
Shortcuts.setup(this) Shortcuts.setup(this)
val intent = intent(MainActivity::class.java)
intent.putExtra(Const.Key.OPEN_SECTION, getIntent().getStringExtra(Const.Key.OPEN_SECTION))
DONE = true DONE = true
startActivity(intent)
startActivity(intent<MainActivity>().apply { intent?.also { putExtras(it) } })
finish() finish()
} }

View File

@ -60,7 +60,7 @@ open class FlashActivity : BaseActivity<FlashViewModel, ActivityFlashBinding>()
companion object { companion object {
private fun intent(context: Context) = context.intent(FlashActivity::class.java) private fun intent(context: Context) = context.intent<FlashActivity>()
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
private fun intent(context: Context, file: File) = intent(context).setData(file.toUri()) private fun intent(context: Context, file: File) = intent(context).setData(file.toUri())

View File

@ -28,7 +28,7 @@ class ModulesFragment : BaseFragment<ModuleViewModel, FragmentModulesBinding>()
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == Const.ID.FETCH_ZIP && resultCode == Activity.RESULT_OK && data != null) { if (requestCode == Const.ID.FETCH_ZIP && resultCode == Activity.RESULT_OK && data != null) {
// Get the URI of the selected file // Get the URI of the selected file
val intent = activity.intent(FlashActivity::class.java) val intent = activity.intent<FlashActivity>()
intent.setData(data.data).putExtra(Const.Key.FLASH_ACTION, Const.Value.FLASH_ZIP) intent.setData(data.data).putExtra(Const.Key.FLASH_ACTION, Const.Value.FLASH_ZIP)
startActivity(intent) startActivity(intent)
} }

View File

@ -47,7 +47,7 @@ object Notifications {
} }
fun magiskUpdate(context: Context) { fun magiskUpdate(context: Context) {
val intent = context.intent(SplashActivity::class.java) val intent = context.intent<SplashActivity>()
.putExtra(Const.Key.OPEN_SECTION, "magisk") .putExtra(Const.Key.OPEN_SECTION, "magisk")
val stackBuilder = TaskStackBuilder.create(context) val stackBuilder = TaskStackBuilder.create(context)
stackBuilder.addParentStack(SplashActivity::class.java.cmp(context.packageName)) stackBuilder.addParentStack(SplashActivity::class.java.cmp(context.packageName))
@ -65,7 +65,7 @@ object Notifications {
} }
fun managerUpdate(context: Context) { fun managerUpdate(context: Context) {
val intent = context.intent(GeneralReceiver::class.java) val intent = context.intent<GeneralReceiver>()
.setAction(Const.Key.BROADCAST_MANAGER_UPDATE) .setAction(Const.Key.BROADCAST_MANAGER_UPDATE)
.putExtra(Const.Key.INTENT_SET_APP, Info.remote.app) .putExtra(Const.Key.INTENT_SET_APP, Info.remote.app)
@ -82,7 +82,7 @@ object Notifications {
} }
fun dtboPatched(context: Context) { fun dtboPatched(context: Context) {
val intent = context.intent(GeneralReceiver::class.java) val intent = context.intent<GeneralReceiver>()
.setAction(Const.Key.BROADCAST_REBOOT) .setAction(Const.Key.BROADCAST_REBOOT)
val pendingIntent = PendingIntent.getBroadcast(context, val pendingIntent = PendingIntent.getBroadcast(context,
Const.ID.DTBO_NOTIFICATION_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT) Const.ID.DTBO_NOTIFICATION_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT)

View File

@ -7,6 +7,7 @@ import android.content.pm.ShortcutManager
import android.graphics.drawable.Icon import android.graphics.drawable.Icon
import android.os.Build import android.os.Build
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import androidx.core.content.getSystemService
import androidx.core.graphics.drawable.toAdaptiveIcon import androidx.core.graphics.drawable.toAdaptiveIcon
import androidx.core.graphics.drawable.toIcon import androidx.core.graphics.drawable.toIcon
import com.topjohnwu.magisk.* import com.topjohnwu.magisk.*
@ -19,7 +20,7 @@ object Shortcuts {
fun setup(context: Context) { fun setup(context: Context) {
if (Build.VERSION.SDK_INT >= 25) { if (Build.VERSION.SDK_INT >= 25) {
val manager = context.getSystemService(ShortcutManager::class.java) val manager = context.getSystemService<ShortcutManager>()
manager?.dynamicShortcuts = getShortCuts(context) manager?.dynamicShortcuts = getShortCuts(context)
} }
} }
@ -28,7 +29,7 @@ object Shortcuts {
private fun getShortCuts(context: Context): List<ShortcutInfo> { private fun getShortCuts(context: Context): List<ShortcutInfo> {
val shortCuts = mutableListOf<ShortcutInfo>() val shortCuts = mutableListOf<ShortcutInfo>()
val root = Shell.rootAccess() val root = Shell.rootAccess()
val intent = context.intent(SplashActivity::class.java) val intent = context.intent<SplashActivity>()
fun getIcon(id: Int): Icon { fun getIcon(id: Int): Icon {
return if (Build.VERSION.SDK_INT >= 26) return if (Build.VERSION.SDK_INT >= 26)

View File

@ -11,7 +11,7 @@ import static android.os.Build.VERSION.SDK_INT;
public class DynAPK { public class DynAPK {
private static final int STUB_VERSION = 3; private static final int STUB_VERSION = 4;
// Indices of the object array // Indices of the object array
private static final int STUB_VERSION_ENTRY = 0; private static final int STUB_VERSION_ENTRY = 0;

View File

@ -22,7 +22,6 @@ import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK; import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK; import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
@ -37,27 +36,11 @@ import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
* Trigger process recreation by calling {@link #triggerRebirth} with a {@link Context} instance. * Trigger process recreation by calling {@link #triggerRebirth} with a {@link Context} instance.
*/ */
public class ProcessPhoenix extends Activity { public class ProcessPhoenix extends Activity {
private static final String KEY_RESTART_INTENTS = "phoenix_restart_intents"; private static final String KEY_RESTART_INTENT = "phoenix_restart_intent";
/** public static void triggerRebirth(Context context, Intent intent) {
* Call to restart the application process using the {@linkplain Intent#CATEGORY_DEFAULT default}
* activity as an intent.
* <p>
* Behavior of the current process after invoking this method is undefined.
*/
public static void triggerRebirth(Context context) {
triggerRebirth(context, getRestartIntent(context));
}
/**
* Call to restart the application process using the specified intents.
* <p>
* Behavior of the current process after invoking this method is undefined.
*/
public static void triggerRebirth(Context context, Intent... nextIntents) {
Intent intent = new Intent(context, a.r.class);
intent.addFlags(FLAG_ACTIVITY_NEW_TASK); // In case we are called with non-Activity context. intent.addFlags(FLAG_ACTIVITY_NEW_TASK); // In case we are called with non-Activity context.
intent.putParcelableArrayListExtra(KEY_RESTART_INTENTS, new ArrayList<>(Arrays.asList(nextIntents))); intent.putExtra(KEY_RESTART_INTENT, getRestartIntent(context));
context.startActivity(intent); context.startActivity(intent);
if (context instanceof Activity) { if (context instanceof Activity) {
((Activity) context).finish(); ((Activity) context).finish();
@ -73,17 +56,15 @@ public class ProcessPhoenix extends Activity {
return defaultIntent; return defaultIntent;
} }
throw new IllegalStateException("Unable to determine default activity for " throw new IllegalStateException();
+ packageName
+ ". Does an activity specify the DEFAULT category in its intent filter?");
} }
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
ArrayList<Intent> intents = getIntent().getParcelableArrayListExtra(KEY_RESTART_INTENTS); Intent intent = getIntent().getParcelableExtra(KEY_RESTART_INTENT);
startActivities(intents.toArray(new Intent[0])); startActivity(intent);
finish(); finish();
Runtime.getRuntime().exit(0); Runtime.getRuntime().exit(0);
} }

View File

@ -4,6 +4,8 @@ import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.Application; import android.app.Application;
import android.app.ProgressDialog; import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
@ -11,6 +13,7 @@ import android.util.Log;
import com.topjohnwu.magisk.net.ErrorHandler; import com.topjohnwu.magisk.net.ErrorHandler;
import com.topjohnwu.magisk.net.Networking; import com.topjohnwu.magisk.net.Networking;
import com.topjohnwu.magisk.net.ResponseListener; import com.topjohnwu.magisk.net.ResponseListener;
import com.topjohnwu.magisk.obfuscate.Mapping;
import com.topjohnwu.magisk.obfuscate.RawData; import com.topjohnwu.magisk.obfuscate.RawData;
import com.topjohnwu.magisk.utils.APKInstall; import com.topjohnwu.magisk.utils.APKInstall;
@ -48,7 +51,11 @@ public class DownloadActivity extends Activity {
// Download and relaunch the app // Download and relaunch the app
Networking.get(apkLink) Networking.get(apkLink)
.setErrorHandler(err) .setErrorHandler(err)
.getAsFile(MANAGER_APK, f -> ProcessPhoenix.triggerRebirth(this)); .getAsFile(MANAGER_APK, apk -> {
Intent intent = new Intent()
.setComponent(new ComponentName(this, Mapping.inverse("a.r")));
ProcessPhoenix.triggerRebirth(this, intent);
});
} else { } else {
// Download and upgrade the app // Download and upgrade the app
Application app = getApplication(); Application app = getApplication();

View File

@ -7,9 +7,14 @@ import static com.topjohnwu.magisk.DynAPK.Data;
public class Mapping { public class Mapping {
private static Map<String, String> map = new HashMap<>(); private static Map<String, String> map = new HashMap<>();
private static Map<String, String> inverseMap;
static { static {
map.put("a.x", "androidx.work.impl.background.systemjob.SystemJobService"); map.put("a.x", "androidx.work.impl.background.systemjob.SystemJobService");
inverseMap = new HashMap<>(map.size());
for (Map.Entry<String, String> e : map.entrySet()) {
inverseMap.put(e.getValue(), e.getKey());
}
} }
public static String get(String name) { public static String get(String name) {
@ -17,13 +22,14 @@ public class Mapping {
return n != null ? n : name; return n != null ? n : name;
} }
public static String inverse(String name) {
String n = inverseMap.get(name);
return n != null ? n : name;
}
public static Data data() { public static Data data() {
Map<String, String> componentMap = new HashMap<>(map.size());
for (Map.Entry<String, String> e : map.entrySet()) {
componentMap.put(e.getValue(), e.getKey());
}
Data data = new Data(); Data data = new Data();
data.componentMap = componentMap; data.componentMap = inverseMap;
return data; return data;
} }