157 lines
5.4 KiB
Java
Raw Normal View History

2018-08-02 00:41:10 +08:00
package com.topjohnwu.magisk.asyncs;
import android.os.AsyncTask;
import android.widget.Toast;
2018-12-02 15:28:18 -05:00
import com.topjohnwu.magisk.BuildConfig;
2018-08-02 00:41:10 +08:00
import com.topjohnwu.magisk.Const;
import com.topjohnwu.magisk.Data;
import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.R;
2018-12-03 02:24:07 -05:00
import com.topjohnwu.magisk.components.Notifications;
2018-08-02 00:41:10 +08:00
import com.topjohnwu.magisk.utils.RootUtils;
import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.superuser.ShellUtils;
import com.topjohnwu.utils.JarMap;
import com.topjohnwu.utils.SignAPK;
2018-08-02 00:41:10 +08:00
2018-12-07 21:42:53 -05:00
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
2018-10-26 02:50:45 -04:00
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.CharBuffer;
2018-12-03 09:52:41 -05:00
import java.nio.IntBuffer;
2018-08-02 00:41:10 +08:00
import java.security.SecureRandom;
import java.util.jar.JarEntry;
2018-12-03 02:24:07 -05:00
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
2018-08-02 00:41:10 +08:00
public class PatchAPK {
2018-10-26 02:50:45 -04:00
public static final String LOWERALPHA = "abcdefghijklmnopqrstuvwxyz";
public static final String UPPERALPHA = LOWERALPHA.toUpperCase();
public static final String ALPHA = LOWERALPHA + UPPERALPHA;
public static final String DIGITS = "0123456789";
public static final String ALPHANUM = ALPHA + DIGITS;
public static final String ALPHANUMDOTS = ALPHANUM + "............";
2018-08-02 00:41:10 +08:00
private static String genPackageName(String prefix, int length) {
StringBuilder builder = new StringBuilder(length);
builder.append(prefix);
length -= prefix.length();
SecureRandom random = new SecureRandom();
2018-10-26 02:50:45 -04:00
char next, prev = '.';
2018-08-02 00:41:10 +08:00
for (int i = 0; i < length; ++i) {
2018-10-26 02:50:45 -04:00
if (prev == '.' || i == length - 1) {
next = ALPHA.charAt(random.nextInt(ALPHA.length()));
2018-08-02 00:41:10 +08:00
} else {
2018-10-26 02:50:45 -04:00
next = ALPHANUMDOTS.charAt(random.nextInt(ALPHANUMDOTS.length()));
2018-08-02 00:41:10 +08:00
}
builder.append(next);
prev = next;
}
return builder.toString();
}
2018-10-26 02:50:45 -04:00
private static boolean findAndPatch(byte xml[], String a, String b) {
if (a.length() != b.length())
return false;
char[] from = a.toCharArray(), to = b.toCharArray();
CharBuffer buf = ByteBuffer.wrap(xml).order(ByteOrder.LITTLE_ENDIAN).asCharBuffer();
2018-08-02 00:41:10 +08:00
int offset = -1;
2018-10-26 02:50:45 -04:00
for (int i = 0; i < buf.length() - from.length; ++i) {
2018-08-02 00:41:10 +08:00
boolean match = true;
2018-10-26 02:50:45 -04:00
for (int j = 0; j < from.length; ++j) {
if (buf.get(i + j) != from[j]) {
2018-08-02 00:41:10 +08:00
match = false;
break;
}
}
2018-10-26 02:50:45 -04:00
// Make sure it is null terminated
if (match && buf.get(i + from.length) == '\0') {
2018-08-02 00:41:10 +08:00
offset = i;
break;
}
}
if (offset < 0)
return false;
2018-10-26 02:50:45 -04:00
buf.position(offset);
buf.put(to);
2018-08-02 00:41:10 +08:00
return true;
}
2018-12-03 09:52:41 -05:00
private static boolean findAndPatch(byte xml[], int a, int b) {
IntBuffer buf = ByteBuffer.wrap(xml).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer();
int len = xml.length / 4;
for (int i = 0; i < len; ++i) {
if (buf.get(i) == a) {
buf.put(i, b);
return true;
}
}
return false;
}
2018-08-02 00:41:10 +08:00
private static boolean patchAndHide() {
MagiskManager mm = Data.MM();
// Generate a new app with random package name
2018-12-07 21:42:53 -05:00
File repack = new File(mm.getFilesDir(), "patched.apk");
2018-12-02 15:28:18 -05:00
String pkg = genPackageName("com.", BuildConfig.APPLICATION_ID.length());
2018-08-02 00:41:10 +08:00
try {
JarMap apk = new JarMap(mm.getPackageCodePath());
2018-12-07 21:42:53 -05:00
if (!patch(apk, pkg))
2018-08-02 00:41:10 +08:00
return false;
2018-12-07 21:42:53 -05:00
SignAPK.sign(apk, new BufferedOutputStream(new FileOutputStream(repack)));
2018-08-02 00:41:10 +08:00
} catch (Exception e) {
return false;
}
// Install the application
2018-12-07 21:42:53 -05:00
repack.setReadable(true, false);
2018-08-02 00:41:10 +08:00
if (!ShellUtils.fastCmdResult("pm install " + repack))
2018-12-07 21:42:53 -05:00
return false;;
2018-08-02 00:41:10 +08:00
mm.mDB.setStrings(Const.Key.SU_MANAGER, pkg);
Data.exportPrefs();
2018-12-02 15:28:18 -05:00
RootUtils.rmAndLaunch(BuildConfig.APPLICATION_ID, pkg);
2018-08-02 00:41:10 +08:00
return true;
}
2018-12-07 21:42:53 -05:00
public static boolean patch(JarMap apk, String pkg) {
2018-08-02 00:41:10 +08:00
try {
JarEntry je = apk.getJarEntry(Const.ANDROID_MANIFEST);
byte xml[] = apk.getRawData(je);
2018-12-07 21:42:53 -05:00
if (!findAndPatch(xml, BuildConfig.APPLICATION_ID, pkg) ||
!findAndPatch(xml, BuildConfig.APPLICATION_ID + ".provider", pkg + ".provider") ||
2018-12-03 09:52:41 -05:00
!findAndPatch(xml, R.string.app_name, R.string.re_app_name))
2018-08-02 00:41:10 +08:00
return false;
// Write in changes
apk.getOutputStream(je).write(xml);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
2018-12-03 02:24:07 -05:00
public static void hideManager() {
2018-08-02 00:41:10 +08:00
AsyncTask.THREAD_POOL_EXECUTOR.execute(() -> {
2018-12-03 02:24:07 -05:00
MagiskManager mm = Data.MM();
NotificationCompat.Builder progress =
Notifications.progress(mm.getString(R.string.hide_manager_title));
NotificationManagerCompat mgr = NotificationManagerCompat.from(mm);
2018-12-03 02:28:20 -05:00
mgr.notify(Const.ID.HIDE_MANAGER_NOTIFICATION_ID, progress.build());
2018-08-02 00:41:10 +08:00
boolean b = patchAndHide();
2018-12-03 02:28:20 -05:00
mgr.cancel(Const.ID.HIDE_MANAGER_NOTIFICATION_ID);
2018-12-03 02:24:07 -05:00
if (!b) Utils.toast(R.string.hide_manager_fail_toast, Toast.LENGTH_LONG);
2018-08-02 00:41:10 +08:00
});
}
}