SandHook: convert all inner hookers to sandhook

This commit is contained in:
swift_gan 2019-03-22 10:27:33 +08:00 committed by solohsu
parent 3570e0eb29
commit b24efed29b
10 changed files with 71 additions and 45 deletions

View File

@ -24,7 +24,7 @@ dependencies {
compileOnly files("libs/framework-stub.jar")
implementation project(':edxp-common')
implementation project(':xposed-bridge')
implementation 'com.swift.sandhook:hooklib:3.2.2'
implementation 'com.swift.sandhook:hooklib:3.2.7'
compileOnly project(':dexmaker')
}

View File

@ -4,7 +4,6 @@ import android.app.AndroidAppHelper;
import android.text.TextUtils;
import com.elderdrivers.riru.edxp.config.EdXpConfigGlobal;
import com.elderdrivers.riru.edxp.util.Utils;
import com.elderdrivers.riru.edxp.sandhook.config.SandHookEdxpConfig;
import com.elderdrivers.riru.edxp.sandhook.config.SandHookProvider;
import com.elderdrivers.riru.edxp.sandhook.core.HookMain;
@ -14,9 +13,9 @@ import com.elderdrivers.riru.edxp.sandhook.entry.bootstrap.SysBootstrapHookInfo;
import com.elderdrivers.riru.edxp.sandhook.entry.bootstrap.SysInnerHookInfo;
import com.elderdrivers.riru.edxp.sandhook.entry.bootstrap.WorkAroundHookInfo;
import com.elderdrivers.riru.edxp.sandhook.entry.hooker.SystemMainHooker;
import com.elderdrivers.riru.edxp.util.Utils;
import com.swift.sandhook.SandHookConfig;
import com.swift.sandhook.xposedcompat.XposedCompat;
import com.swift.sandhook.xposedcompat.methodgen.SandHookXposedBridge;
import java.util.concurrent.atomic.AtomicBoolean;
@ -31,6 +30,12 @@ public class Router {
private static volatile AtomicBoolean bootstrapHooked = new AtomicBoolean(false);
static boolean useSandHook;
static {
useSandHook = EdXpConfigGlobal.getHookProvider() instanceof SandHookProvider;
}
public static void prepare(boolean isSystem) {
// this flag is needed when loadModules
@ -81,33 +86,46 @@ public class Router {
Utils.logD("startBootstrapHook starts: isSystem = " + isSystem);
ClassLoader classLoader = XposedBridge.BOOTCLASSLOADER;
if (isSystem) {
HookMain.doHookDefault(
if (useSandHook) {
XposedCompat.addHookers(classLoader, SysBootstrapHookInfo.hookItems);
} else {
HookMain.doHookDefault(
Router.class.getClassLoader(),
classLoader,
SysBootstrapHookInfo.class.getName());
XposedCompat.addHookers(classLoader, SysBootstrapHookInfo.hookItems);
}
} else {
HookMain.doHookDefault(
Router.class.getClassLoader(),
classLoader,
AppBootstrapHookInfo.class.getName());
XposedCompat.addHookers(classLoader, AppBootstrapHookInfo.hookItems);
if (useSandHook) {
XposedCompat.addHookers(classLoader, AppBootstrapHookInfo.hookItems);
} else {
HookMain.doHookDefault(
Router.class.getClassLoader(),
classLoader,
AppBootstrapHookInfo.class.getName());
}
}
}
public static void startSystemServerHook() {
// HookMain.doHookDefault(
// Router.class.getClassLoader(),
// SystemMainHooker.systemServerCL,
// SysInnerHookInfo.class.getName());
XposedCompat.addHookers(SystemMainHooker.systemServerCL, SysInnerHookInfo.hookItems);
if (useSandHook) {
XposedCompat.addHookers(SystemMainHooker.systemServerCL, SysInnerHookInfo.hookItems);
} else {
HookMain.doHookDefault(
Router.class.getClassLoader(),
SystemMainHooker.systemServerCL,
SysInnerHookInfo.class.getName());
}
}
public static void startWorkAroundHook() {
HookMain.doHookDefault(
Router.class.getClassLoader(),
XposedBridge.BOOTCLASSLOADER,
WorkAroundHookInfo.class.getName());
if (useSandHook) {
XposedCompat.addHookers(XposedBridge.BOOTCLASSLOADER, WorkAroundHookInfo.hookItems);
} else {
HookMain.doHookDefault(
Router.class.getClassLoader(),
XposedBridge.BOOTCLASSLOADER,
WorkAroundHookInfo.class.getName());
}
}
public static void onEnterChildProcess() {

View File

@ -7,11 +7,14 @@ import com.elderdrivers.riru.edxp.sandhook.entry.hooker.OnePlusWorkAroundHooker;
public class AppBootstrapHookInfo implements KeepMembers {
public static String[] hookItemNames = {
OnePlusWorkAroundHooker.class.getName()
OnePlusWorkAroundHooker.class.getName(),
HandleBindAppHooker.class.getName(),
LoadedApkConstructorHooker.class.getName(),
};
public static Class[] hookItems = {
HandleBindAppHooker.class,
LoadedApkConstructorHooker.class
LoadedApkConstructorHooker.class,
OnePlusWorkAroundHooker.class
};
}

View File

@ -8,12 +8,16 @@ import com.elderdrivers.riru.edxp.sandhook.entry.hooker.SystemMainHooker;
public class SysBootstrapHookInfo implements KeepMembers {
public static String[] hookItemNames = {
OnePlusWorkAroundHooker.class.getName()
OnePlusWorkAroundHooker.class.getName(),
HandleBindAppHooker.class.getName(),
SystemMainHooker.class.getName(),
LoadedApkConstructorHooker.class.getName()
};
public static Class[] hookItems = {
HandleBindAppHooker.class,
SystemMainHooker.class,
LoadedApkConstructorHooker.class
LoadedApkConstructorHooker.class,
OnePlusWorkAroundHooker.class
};
}

View File

@ -9,5 +9,6 @@ public class WorkAroundHookInfo implements KeepMembers {
};
public static Class[] hookItems = {
OnePlusWorkAroundHooker.class
};
}

View File

@ -7,14 +7,13 @@ import android.content.pm.ApplicationInfo;
import android.content.res.CompatibilityInfo;
import com.elderdrivers.riru.common.KeepMembers;
import com.elderdrivers.riru.edxp.util.Utils;
import com.elderdrivers.riru.edxp.Main;
import com.elderdrivers.riru.edxp.sandhook.entry.Router;
import com.elderdrivers.riru.edxp.util.Utils;
import com.swift.sandhook.SandHook;
import com.swift.sandhook.annotation.HookClass;
import com.swift.sandhook.annotation.HookMethod;
import com.swift.sandhook.annotation.HookMethodBackup;
import com.swift.sandhook.annotation.HookMode;
import com.swift.sandhook.annotation.Param;
import com.swift.sandhook.annotation.SkipParamCheck;
import com.swift.sandhook.annotation.ThisObject;
@ -27,8 +26,8 @@ import de.robv.android.xposed.XposedInit;
import de.robv.android.xposed.callbacks.XC_LoadPackage;
import static com.elderdrivers.riru.edxp.config.InstallerChooser.INSTALLER_PACKAGE_NAME;
import static com.elderdrivers.riru.edxp.util.ClassLoaderUtils.replaceParentClassLoader;
import static com.elderdrivers.riru.edxp.sandhook.entry.hooker.XposedBlackListHooker.BLACK_LIST_PACKAGE_NAME;
import static com.elderdrivers.riru.edxp.util.ClassLoaderUtils.replaceParentClassLoader;
// normal process initialization (for new Activity, Service, BroadcastReceiver etc.)
@HookClass(ActivityThread.class)
@ -45,7 +44,7 @@ public class HandleBindAppHooker implements KeepMembers {
@HookMethod("handleBindApplication")
public static void hook(@ThisObject ActivityThread thiz, @Param("android.app.ActivityThread$AppBindData") Object bindData) throws Throwable {
if (XposedBlackListHooker.shouldDisableHooks("")) {
SandHook.callOriginByBackup(backup, thiz, bindData);
backup(thiz, bindData);
return;
}
try {
@ -96,10 +95,11 @@ public class HandleBindAppHooker implements KeepMembers {
} catch (Throwable t) {
Router.logE("error when hooking bindApp", t);
} finally {
SandHook.callOriginByBackup(backup, thiz, bindData);
backup(thiz, bindData);
}
}
public static void backup(Object thiz, Object bindData) {
public static void backup(Object thiz, Object bindData) throws Throwable {
SandHook.callOriginByBackup(backup, thiz, bindData);
}
}

View File

@ -47,13 +47,12 @@ public class LoadedApkConstructorHooker implements KeepMembers {
boolean includeCode, boolean registerPackage) throws Throwable {
if (XposedBlackListHooker.shouldDisableHooks("")) {
SandHook.callOriginByBackup(backup, thiz, activityThread, aInfo, compatInfo, baseLoader, securityViolation, includeCode, registerPackage);
backup(thiz, activityThread, aInfo, compatInfo, baseLoader, securityViolation, includeCode, registerPackage);
return;
}
Router.logD("LoadedApk#<init> starts");
SandHook.callOriginByBackup(backup, thiz, activityThread, aInfo, compatInfo, baseLoader, securityViolation, includeCode, registerPackage);
backup(thiz, activityThread, aInfo, compatInfo, baseLoader, securityViolation, includeCode, registerPackage);
try {
LoadedApk loadedApk = (LoadedApk) thiz;
@ -105,7 +104,7 @@ public class LoadedApkConstructorHooker implements KeepMembers {
public static void backup(Object thiz, ActivityThread activityThread,
ApplicationInfo aInfo, CompatibilityInfo compatInfo,
ClassLoader baseLoader, boolean securityViolation,
boolean includeCode, boolean registerPackage) {
boolean includeCode, boolean registerPackage) throws Throwable {
SandHook.callOriginByBackup(backup, thiz, activityThread, aInfo, compatInfo, baseLoader, securityViolation, includeCode, registerPackage);
}
}

View File

@ -3,6 +3,7 @@ package com.elderdrivers.riru.edxp.sandhook.entry.hooker;
import com.elderdrivers.riru.common.KeepMembers;
import com.elderdrivers.riru.edxp.Main;
import com.elderdrivers.riru.edxp.sandhook.entry.Router;
import com.swift.sandhook.SandHook;
import com.swift.sandhook.annotation.HookClass;
import com.swift.sandhook.annotation.HookMethod;
import com.swift.sandhook.annotation.HookMethodBackup;
@ -40,7 +41,7 @@ public class OnePlusWorkAroundHooker implements KeepMembers {
static Method backup;
@HookMethod("inCompatConfigList")
public static boolean hook(int type, String packageName) {
public static boolean hook(int type, String packageName) throws Throwable {
if (XposedBridge.disableHooks || Router.forkCompleted) {
return backup(type, packageName);
}
@ -48,7 +49,7 @@ public class OnePlusWorkAroundHooker implements KeepMembers {
return false;
}
public static boolean backup(int type, String packageName) {
return false;
public static boolean backup(int type, String packageName) throws Throwable {
return (boolean) SandHook.callOriginByBackup(backup, null, type, packageName);
}
}

View File

@ -38,7 +38,7 @@ public class StartBootstrapServicesHooker implements KeepMembers {
public static void hook(@ThisObject Object systemServer) throws Throwable {
if (XposedBridge.disableHooks) {
SandHook.callOriginByBackup(backup, systemServer);
backup(systemServer);
return;
}
@ -71,11 +71,11 @@ public class StartBootstrapServicesHooker implements KeepMembers {
} catch (Throwable t) {
Router.logE("error when hooking startBootstrapServices", t);
} finally {
SandHook.callOriginByBackup(backup, systemServer);
backup(systemServer);
}
}
public static void backup(Object systemServer) {
public static void backup(Object systemServer) throws Throwable {
SandHook.callOriginByBackup(backup, systemServer);
}
}

View File

@ -33,10 +33,10 @@ public class SystemMainHooker implements KeepMembers {
@HookMethod("systemMain")
public static ActivityThread hook() throws Throwable {
if (XposedBridge.disableHooks) {
return (ActivityThread) SandHook.callOriginByBackup(backup, null);
return backup();
}
Router.logD("ActivityThread#systemMain() starts");
ActivityThread activityThread = (ActivityThread) SandHook.callOriginByBackup(backup, null);
ActivityThread activityThread = backup();
try {
// get system_server classLoader
systemServerCL = Thread.currentThread().getContextClassLoader();
@ -49,7 +49,7 @@ public class SystemMainHooker implements KeepMembers {
return activityThread;
}
public static ActivityThread backup() {
return null;
public static ActivityThread backup() throws Throwable {
return (ActivityThread) SandHook.callOriginByBackup(backup, null);
}
}