154 lines
4.9 KiB
Java
Raw Normal View History

2017-08-22 03:01:54 +08:00
package com.topjohnwu.magisk.asyncs;
import android.app.Activity;
import android.app.ProgressDialog;
2017-08-22 03:01:54 +08:00
import android.widget.Toast;
import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.R;
2017-11-06 04:41:23 +08:00
import com.topjohnwu.magisk.utils.Const;
2018-06-02 22:00:52 +08:00
import com.topjohnwu.magisk.utils.RootUtils;
2017-08-22 03:01:54 +08:00
import com.topjohnwu.magisk.utils.ZipUtils;
2018-01-21 06:07:24 +08:00
import com.topjohnwu.superuser.Shell;
2018-02-12 23:07:35 +08:00
import com.topjohnwu.superuser.ShellUtils;
import com.topjohnwu.superuser.io.SuFile;
import com.topjohnwu.superuser.io.SuFileOutputStream;
import com.topjohnwu.utils.JarMap;
2017-08-22 03:01:54 +08:00
2017-10-21 22:54:47 +08:00
import java.io.FileInputStream;
2017-10-15 23:54:34 +08:00
import java.security.SecureRandom;
2017-10-04 22:27:14 +08:00
import java.util.jar.JarEntry;
2017-08-22 03:01:54 +08:00
public class HideManager extends ParallelTask<Void, Void, Boolean> {
private ProgressDialog dialog;
public HideManager(Activity activity) {
super(activity);
}
2017-10-15 23:54:34 +08:00
private String genPackageName(String prefix, int length) {
StringBuilder builder = new StringBuilder(length);
builder.append(prefix);
length -= prefix.length();
SecureRandom random = new SecureRandom();
String base = "abcdefghijklmnopqrstuvwxyz";
String alpha = base + base.toUpperCase();
String full = alpha + "0123456789..........";
char next, prev = '\0';
for (int i = 0; i < length; ++i) {
if (prev == '.' || i == length - 1 || i == 0) {
next = alpha.charAt(random.nextInt(alpha.length()));
} else {
next = full.charAt(random.nextInt(full.length()));
}
builder.append(next);
prev = next;
}
return builder.toString();
}
2017-10-31 22:48:48 +08:00
private int findOffset(byte buf[], byte pattern[]) {
2017-10-21 22:54:47 +08:00
int offset = -1;
2017-10-31 22:48:48 +08:00
for (int i = 0; i < buf.length - pattern.length; ++i) {
2017-10-21 22:54:47 +08:00
boolean match = true;
2017-10-31 22:48:48 +08:00
for (int j = 0; j < pattern.length; ++j) {
if (buf[i + j] != pattern[j]) {
2017-10-21 22:54:47 +08:00
match = false;
break;
}
}
if (match) {
offset = i;
break;
}
}
2017-10-31 22:48:48 +08:00
return offset;
}
/* It seems that AAPT sometimes generate another type of string format */
private boolean fallbackPatch(byte xml[], String from, String to) {
byte[] target = new byte[from.length() * 2 + 2];
for (int i = 0; i < from.length(); ++i) {
target[i * 2] = (byte) from.charAt(i);
}
int offset = findOffset(xml, target);
2017-10-21 22:54:47 +08:00
if (offset < 0)
return false;
2017-10-31 22:48:48 +08:00
byte[] dest = new byte[target.length - 2];
for (int i = 0; i < to.length(); ++i) {
dest[i * 2] = (byte) to.charAt(i);
}
System.arraycopy(dest, 0, xml, offset, dest.length);
return true;
}
2017-10-21 22:54:47 +08:00
2017-10-31 22:48:48 +08:00
private boolean findAndPatch(byte xml[], String from, String to) {
byte target[] = (from + '\0').getBytes();
int offset = findOffset(xml, target);
if (offset < 0)
return fallbackPatch(xml, from, to);
2017-10-21 22:54:47 +08:00
System.arraycopy(to.getBytes(), 0, xml, offset, to.length());
return true;
}
2017-08-22 03:01:54 +08:00
@Override
protected void onPreExecute() {
dialog = ProgressDialog.show(getActivity(),
getActivity().getString(R.string.hide_manager_toast),
getActivity().getString(R.string.hide_manager_toast2));
2017-08-22 03:01:54 +08:00
}
@Override
protected Boolean doInBackground(Void... voids) {
2017-10-16 00:54:48 +08:00
MagiskManager mm = MagiskManager.get();
2017-08-22 03:01:54 +08:00
// Generate a new unhide app with random package name
SuFile repack = new SuFile("/data/local/tmp/repack.apk", true);
2017-11-06 04:41:23 +08:00
String pkg = genPackageName("com.", Const.ORIG_PKG_NAME.length());
2017-10-04 22:27:14 +08:00
try {
2017-10-21 22:54:47 +08:00
// Read whole APK into memory
JarMap apk = new JarMap(new FileInputStream(mm.getPackageCodePath()));
2017-11-06 04:41:23 +08:00
JarEntry je = new JarEntry(Const.ANDROID_MANIFEST);
2017-10-21 22:54:47 +08:00
byte xml[] = apk.getRawData(je);
2017-11-06 04:41:23 +08:00
if (!findAndPatch(xml, Const.ORIG_PKG_NAME, pkg))
2017-10-21 22:54:47 +08:00
return false;
2017-11-06 04:41:23 +08:00
if (!findAndPatch(xml, Const.ORIG_PKG_NAME + ".provider", pkg + ".provider"))
2017-10-04 22:27:14 +08:00
return false;
2017-10-21 22:54:47 +08:00
// Write in changes
apk.getOutputStream(je).write(xml);
2017-10-04 22:27:14 +08:00
// Sign the APK
ZipUtils.signZip(apk, new SuFileOutputStream(repack));
2017-10-04 22:27:14 +08:00
} catch (Exception e) {
e.printStackTrace();
return false;
}
2017-08-22 03:01:54 +08:00
// Install the application
2018-02-13 03:27:27 +08:00
if (!ShellUtils.fastCmdResult(Shell.getShell(), "pm install " + repack))
2018-02-12 23:07:35 +08:00
return false;
2017-10-21 22:54:47 +08:00
repack.delete();
2017-08-22 03:01:54 +08:00
2018-04-06 01:54:09 +08:00
mm.mDB.setStrings(Const.Key.SU_REQUESTER, pkg);
2018-06-02 22:00:52 +08:00
mm.dumpPrefs();
RootUtils.uninstallPkg(Const.ORIG_PKG_NAME);
2017-08-22 03:01:54 +08:00
return true;
}
@Override
protected void onPostExecute(Boolean b) {
dialog.dismiss();
2017-08-22 03:01:54 +08:00
if (!b) {
2017-10-16 00:54:48 +08:00
MagiskManager.toast(R.string.hide_manager_fail_toast, Toast.LENGTH_LONG);
2017-08-22 03:01:54 +08:00
}
super.onPostExecute(b);
}
}