139 lines
4.6 KiB
Java
Raw Normal View History

2017-08-22 03:01:54 +08:00
package com.topjohnwu.magisk.asyncs;
import android.content.Context;
import android.content.pm.PackageManager;
2017-08-27 01:04:55 +08:00
import android.os.Environment;
2017-08-22 03:01:54 +08:00
import android.widget.Toast;
import com.topjohnwu.jarsigner.JarMap;
2017-08-22 03:01:54 +08:00
import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.R;
2017-09-30 03:04:23 +08:00
import com.topjohnwu.magisk.container.Policy;
2017-10-15 23:02:44 +08:00
import com.topjohnwu.magisk.utils.Shell;
2017-08-22 03:01:54 +08:00
import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.magisk.utils.ZipUtils;
import java.io.File;
2017-10-15 23:54:34 +08:00
import java.security.SecureRandom;
2017-08-22 03:01:54 +08:00
import java.util.List;
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> {
2017-10-04 22:27:14 +08:00
private static final String UNHIDE_APK = "unhide.apk";
private static final String ANDROID_MANIFEST = "AndroidManifest.xml";
private static final byte[] UNHIDE_PKG_NAME = "com.topjohnwu.unhide\0".getBytes();
2017-08-22 03:01:54 +08:00
public HideManager(Context context) {
super(context);
}
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-08-22 03:01:54 +08:00
@Override
protected void onPreExecute() {
getMagiskManager().toast(R.string.hide_manager_toast, Toast.LENGTH_SHORT);
}
@Override
protected Boolean doInBackground(Void... voids) {
2017-09-03 15:35:14 +08:00
MagiskManager mm = getMagiskManager();
if (mm == null)
2017-08-22 03:01:54 +08:00
return false;
// Generate a new unhide app with random package name
2017-08-27 01:04:55 +08:00
File unhideAPK = new File(Environment.getExternalStorageDirectory() + "/MagiskManager", "unhide.apk");
unhideAPK.getParentFile().mkdirs();
2017-10-04 22:27:14 +08:00
String pkg;
try {
JarMap asset = new JarMap(mm.getAssets().open(UNHIDE_APK));
JarEntry je = new JarEntry(ANDROID_MANIFEST);
byte xml[] = asset.getRawData(je);
int offset = -1;
// Linear search pattern offset
for (int i = 0; i < xml.length - UNHIDE_PKG_NAME.length; ++i) {
boolean match = true;
for (int j = 0; j < UNHIDE_PKG_NAME.length; ++j) {
if (xml[i + j] != UNHIDE_PKG_NAME[j]) {
match = false;
break;
}
}
if (match) {
offset = i;
break;
}
}
if (offset < 0)
return false;
// Patch binary XML with new package name
2017-10-15 23:54:34 +08:00
pkg = genPackageName("com.", UNHIDE_PKG_NAME.length - 1);
2017-10-04 22:27:14 +08:00
System.arraycopy(pkg.getBytes(), 0, xml, offset, pkg.length());
asset.getOutputStream(je).write(xml);
// Sign the APK
ZipUtils.signZip(mm, asset, unhideAPK, false);
} catch (Exception e) {
e.printStackTrace();
return false;
}
2017-08-22 03:01:54 +08:00
// Install the application
2017-10-15 23:02:44 +08:00
List<String> ret = Shell.su("pm install " + unhideAPK + ">/dev/null && echo true || echo false");
2017-08-22 03:01:54 +08:00
unhideAPK.delete();
if (!Utils.isValidShellResponse(ret) || !Boolean.parseBoolean(ret.get(0)))
return false;
try {
// Allow the application to gain root by default
2017-09-03 15:35:14 +08:00
PackageManager pm = mm.getPackageManager();
2017-08-22 03:01:54 +08:00
int uid = pm.getApplicationInfo(pkg, 0).uid;
Policy policy = new Policy(uid, pm);
policy.policy = Policy.ALLOW;
policy.notification = false;
policy.logging = false;
2017-09-03 15:35:14 +08:00
mm.suDB.addPolicy(policy);
2017-08-22 03:01:54 +08:00
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return false;
}
// Hide myself!
2017-10-15 23:02:44 +08:00
Shell.su_raw("pm hide " + mm.getPackageName());
2017-08-22 03:01:54 +08:00
return true;
}
@Override
protected void onPostExecute(Boolean b) {
2017-09-03 15:35:14 +08:00
MagiskManager mm = getMagiskManager();
if (mm == null)
2017-08-22 03:01:54 +08:00
return;
if (!b) {
2017-09-03 15:35:14 +08:00
mm.toast(R.string.hide_manager_fail_toast, Toast.LENGTH_LONG);
2017-08-22 03:01:54 +08:00
}
super.onPostExecute(b);
}
}