2018-12-13 09:35:50 +00:00
|
|
|
package com.topjohnwu.magisk.utils;
|
2018-08-01 16:41:10 +00:00
|
|
|
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
2019-03-07 08:41:24 +00:00
|
|
|
import androidx.core.app.NotificationCompat;
|
|
|
|
|
2019-01-30 08:10:12 +00:00
|
|
|
import com.topjohnwu.magisk.App;
|
2018-12-02 20:28:18 +00:00
|
|
|
import com.topjohnwu.magisk.BuildConfig;
|
2019-01-30 08:10:12 +00:00
|
|
|
import com.topjohnwu.magisk.Config;
|
|
|
|
import com.topjohnwu.magisk.Const;
|
2018-08-01 16:41:10 +00:00
|
|
|
import com.topjohnwu.magisk.R;
|
2019-01-31 04:23:49 +00:00
|
|
|
import com.topjohnwu.magisk.uicomponents.Notifications;
|
2019-01-30 08:10:12 +00:00
|
|
|
import com.topjohnwu.signing.JarMap;
|
|
|
|
import com.topjohnwu.signing.SignAPK;
|
2019-02-03 10:16:29 +00:00
|
|
|
import com.topjohnwu.superuser.Shell;
|
2018-08-01 16:41:10 +00:00
|
|
|
|
2018-12-08 02:42:53 +00:00
|
|
|
import java.io.BufferedOutputStream;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileOutputStream;
|
2018-10-26 06:50:45 +00:00
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
import java.nio.ByteOrder;
|
|
|
|
import java.nio.CharBuffer;
|
2018-12-03 14:52:41 +00:00
|
|
|
import java.nio.IntBuffer;
|
2018-08-01 16:41:10 +00:00
|
|
|
import java.security.SecureRandom;
|
2018-12-31 06:55:03 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2018-08-01 16:41:10 +00:00
|
|
|
import java.util.jar.JarEntry;
|
|
|
|
|
|
|
|
public class PatchAPK {
|
|
|
|
|
2018-10-26 06:50:45 +00: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-01 16:41:10 +00: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-12-31 06:55:03 +00:00
|
|
|
char next, prev = prefix.charAt(prefix.length() - 1);
|
2018-08-01 16:41:10 +00:00
|
|
|
for (int i = 0; i < length; ++i) {
|
2018-10-26 06:50:45 +00:00
|
|
|
if (prev == '.' || i == length - 1) {
|
|
|
|
next = ALPHA.charAt(random.nextInt(ALPHA.length()));
|
2018-08-01 16:41:10 +00:00
|
|
|
} else {
|
2018-10-26 06:50:45 +00:00
|
|
|
next = ALPHANUMDOTS.charAt(random.nextInt(ALPHANUMDOTS.length()));
|
2018-08-01 16:41:10 +00:00
|
|
|
}
|
|
|
|
builder.append(next);
|
|
|
|
prev = next;
|
|
|
|
}
|
|
|
|
return builder.toString();
|
|
|
|
}
|
|
|
|
|
2018-12-31 06:55:03 +00:00
|
|
|
private static boolean findAndPatch(byte xml[], String from, String to) {
|
|
|
|
if (from.length() != to.length())
|
2018-10-26 06:50:45 +00:00
|
|
|
return false;
|
|
|
|
CharBuffer buf = ByteBuffer.wrap(xml).order(ByteOrder.LITTLE_ENDIAN).asCharBuffer();
|
2018-12-31 06:55:03 +00:00
|
|
|
List<Integer> offList = new ArrayList<>();
|
|
|
|
for (int i = 0; i < buf.length() - from.length(); ++i) {
|
2018-08-01 16:41:10 +00:00
|
|
|
boolean match = true;
|
2018-12-31 06:55:03 +00:00
|
|
|
for (int j = 0; j < from.length(); ++j) {
|
|
|
|
if (buf.get(i + j) != from.charAt(j)) {
|
2018-08-01 16:41:10 +00:00
|
|
|
match = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-12-31 06:55:03 +00:00
|
|
|
if (match) {
|
|
|
|
offList.add(i);
|
|
|
|
i += from.length();
|
2018-08-01 16:41:10 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-31 06:55:03 +00:00
|
|
|
if (offList.isEmpty())
|
2018-08-01 16:41:10 +00:00
|
|
|
return false;
|
2018-12-31 06:55:03 +00:00
|
|
|
for (int off : offList) {
|
|
|
|
buf.position(off);
|
|
|
|
buf.put(to);
|
|
|
|
}
|
2018-08-01 16:41:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-03 14:52:41 +00: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-01 16:41:10 +00:00
|
|
|
private static boolean patchAndHide() {
|
2018-12-13 09:35:50 +00:00
|
|
|
App app = App.self;
|
2018-08-01 16:41:10 +00:00
|
|
|
|
|
|
|
// Generate a new app with random package name
|
2018-12-13 09:35:50 +00:00
|
|
|
File repack = new File(app.getFilesDir(), "patched.apk");
|
2018-12-02 20:28:18 +00:00
|
|
|
String pkg = genPackageName("com.", BuildConfig.APPLICATION_ID.length());
|
2018-08-01 16:41:10 +00:00
|
|
|
|
2018-12-31 07:53:24 +00:00
|
|
|
if (!patch(app.getPackageCodePath(), repack.getPath(), pkg))
|
2018-08-01 16:41:10 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Install the application
|
2018-12-08 02:42:53 +00:00
|
|
|
repack.setReadable(true, false);
|
2019-02-03 10:16:29 +00:00
|
|
|
if (!Shell.su("pm install " + repack).exec().isSuccess())
|
2018-12-31 06:55:03 +00:00
|
|
|
return false;
|
2018-08-01 16:41:10 +00:00
|
|
|
|
2019-01-21 20:49:03 +00:00
|
|
|
Config.set(Config.Key.SU_MANAGER, pkg);
|
|
|
|
Config.export();
|
2018-12-02 20:28:18 +00:00
|
|
|
RootUtils.rmAndLaunch(BuildConfig.APPLICATION_ID, pkg);
|
2018-08-01 16:41:10 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-31 07:53:24 +00:00
|
|
|
public static boolean patch(String in, String out, String pkg) {
|
2018-08-01 16:41:10 +00:00
|
|
|
try {
|
2018-12-31 07:53:24 +00:00
|
|
|
JarMap jar = new JarMap(in);
|
2018-12-31 06:55:03 +00:00
|
|
|
JarEntry je = jar.getJarEntry(Const.ANDROID_MANIFEST);
|
|
|
|
byte xml[] = jar.getRawData(je);
|
2018-08-01 16:41:10 +00:00
|
|
|
|
2018-12-08 02:42:53 +00:00
|
|
|
if (!findAndPatch(xml, BuildConfig.APPLICATION_ID, pkg) ||
|
2018-12-03 14:52:41 +00:00
|
|
|
!findAndPatch(xml, R.string.app_name, R.string.re_app_name))
|
2018-12-31 07:53:24 +00:00
|
|
|
return false;
|
2018-08-01 16:41:10 +00:00
|
|
|
|
|
|
|
// Write in changes
|
2018-12-31 06:55:03 +00:00
|
|
|
jar.getOutputStream(je).write(xml);
|
2018-12-31 07:53:24 +00:00
|
|
|
SignAPK.sign(jar, new BufferedOutputStream(new FileOutputStream(out)));
|
2018-08-01 16:41:10 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
2018-12-31 07:53:24 +00:00
|
|
|
return false;
|
2018-08-01 16:41:10 +00:00
|
|
|
}
|
2018-12-31 07:53:24 +00:00
|
|
|
return true;
|
2018-08-01 16:41:10 +00:00
|
|
|
}
|
|
|
|
|
2018-12-03 07:24:07 +00:00
|
|
|
public static void hideManager() {
|
2019-02-02 07:50:49 +00:00
|
|
|
App.THREAD_POOL.execute(() -> {
|
2018-12-13 09:35:50 +00:00
|
|
|
App app = App.self;
|
2018-12-03 07:24:07 +00:00
|
|
|
NotificationCompat.Builder progress =
|
2018-12-13 09:35:50 +00:00
|
|
|
Notifications.progress(app.getString(R.string.hide_manager_title));
|
2018-12-31 06:55:03 +00:00
|
|
|
Notifications.mgr.notify(Const.ID.HIDE_MANAGER_NOTIFICATION_ID, progress.build());
|
2019-02-02 07:50:49 +00:00
|
|
|
if(!patchAndHide())
|
|
|
|
Utils.toast(R.string.hide_manager_fail_toast, Toast.LENGTH_LONG);
|
2018-12-31 06:55:03 +00:00
|
|
|
Notifications.mgr.cancel(Const.ID.HIDE_MANAGER_NOTIFICATION_ID);
|
2018-08-01 16:41:10 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|