Commit 1ca453e2 authored by swift_gan's avatar swift_gan

tweak code

parent 99c556c2
......@@ -9,19 +9,18 @@ public class HookBlackList {
public static Set<String> methodBlackList = new HashSet<>();
public static Set<Class> classBlackList = new HashSet<>();
public static Set<String> methodUseInHookBridge = new HashSet<>();
public static Set<String> methodUseInHookStub = new HashSet<>();
static {
methodBlackList.add("java.lang.reflect.Member.getDeclaringClass");
methodBlackList.add("java.lang.reflect.Method.invoke");
methodBlackList.add("java.lang.Class.getDeclaredField");
methodBlackList.add("java.util.Map.get");
methodBlackList.add("java.util.concurrent.ConcurrentHashMap.get");
methodBlackList.add("java.lang.reflect.AccessibleObject.setAccessible");
methodBlackList.add("java.lang.reflect.Member.getModifiers");
methodBlackList.add("java.lang.reflect.InvocationTargetException.getCause");
methodBlackList.add("java.lang.reflect.Method.hashCode");
methodBlackList.add("java.lang.reflect.Class.getName");
methodBlackList.add("java.lang.String.hashCode");
methodBlackList.add("java.lang.String.length");
methodUseInHookBridge.add("java.lang.reflect.Member.getDeclaringClass");
methodUseInHookBridge.add("java.lang.Class.getDeclaredField");
methodUseInHookBridge.add("java.lang.reflect.InvocationTargetException.getCause");
methodUseInHookStub.add("java.lang.Object.equals");
methodUseInHookStub.add("java.lang.Class.isPrimitive");
}
public final static boolean canNotHook(Member origin) {
......@@ -31,4 +30,14 @@ public class HookBlackList {
return methodBlackList.contains(name);
}
public final static boolean canNotHookByBridge(Member origin) {
String name = origin.getDeclaringClass().getName() + "." + origin.getName();
return methodUseInHookBridge.contains(name);
}
public final static boolean canNotHookByStub(Member origin) {
String name = origin.getDeclaringClass().getName() + "." + origin.getName();
return methodUseInHookStub.contains(name);
}
}
......@@ -18,6 +18,8 @@ package com.swift.sandhook.utils;
import android.util.Log;
import com.swift.sandhook.HookLog;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
......@@ -75,7 +77,6 @@ public final class Unsafe {
try {
return (int) arrayBaseOffsetMethod.invoke(unsafe, cls);
} catch (Exception e) {
Log.w(TAG, e);
return 0;
}
}
......@@ -85,7 +86,6 @@ public final class Unsafe {
try {
return (int) arrayIndexScaleMethod.invoke(unsafe, cls);
} catch (Exception e) {
Log.w(TAG, e);
return 0;
}
}
......@@ -95,7 +95,6 @@ public final class Unsafe {
try {
return (int) getIntMethod.invoke(unsafe, array, offset);
} catch (Exception e) {
Log.w(TAG, e);
return 0;
}
}
......@@ -105,7 +104,6 @@ public final class Unsafe {
try {
return (long) getLongMethod.invoke(unsafe, array, offset);
} catch (Exception e) {
Log.w(TAG, e);
return 0;
}
}
......@@ -119,7 +117,7 @@ public final class Unsafe {
return 0xffffffffL & getInt(array, arrayBaseOffset(objectArrayClass));
}
} catch (Exception e) {
Log.w(TAG, e);
HookLog.e("get object address error", e);
return -1;
}
}
......
......@@ -16,6 +16,8 @@ import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedInit;
import de.robv.android.xposed.callbacks.XC_LoadPackage;
import static com.swift.sandhook.xposedcompat.utils.DexMakerUtils.MD5;
public class XposedCompat {
public static File cacheDir;
......@@ -79,10 +81,22 @@ public class XposedCompat {
}
}
public static File getCacheDir() {
if (cacheDir == null) {
if (context == null) {
context = ApplicationUtils.currentApplication();
}
if (context != null) {
cacheDir = new File(context.getCacheDir(), MD5(processName != null ? processName : ProcessUtils.getProcessName(context)));
}
}
return cacheDir;
}
public static boolean clearCache() {
try {
FileUtils.delete(cacheDir);
cacheDir.mkdirs();
FileUtils.delete(getCacheDir());
getCacheDir().mkdirs();
return true;
} catch (Throwable throwable) {
return false;
......
......@@ -16,10 +16,13 @@ public class HookMethodEntity {
public Class[] parType;
public Class retType;
public boolean isStatic;
public HookMethodEntity(Member origin, Method hook, Method backup) {
this.origin = origin;
this.hook = hook;
this.backup = backup;
this.isStatic = Modifier.isStatic(origin.getModifiers());
}
public Object[] getArgs(long... addresses) {
......@@ -28,7 +31,7 @@ public class HookMethodEntity {
if (parType == null || parType.length == 0)
return new Object[0];
int argStart = 0;
if (!isStatic()) {
if (!isStatic) {
argStart = 1;
}
Object[] args = new Object[parType.length];
......@@ -43,7 +46,7 @@ public class HookMethodEntity {
return new long[0];
long[] addresses;
int argStart = 0;
if (!isStatic()) {
if (!isStatic) {
argStart = 1;
addresses = new long[oldAddress.length + 1];
addresses[0] = oldAddress[0];
......@@ -57,7 +60,7 @@ public class HookMethodEntity {
}
public Object getThis(long address) {
if (isStatic())
if (isStatic)
return null;
return SandHook.getObject(address);
}
......@@ -86,8 +89,4 @@ public class HookMethodEntity {
return origin instanceof Constructor;
}
public boolean isStatic() {
return Modifier.isStatic(origin.getModifiers());
}
}
......@@ -3,6 +3,7 @@ package com.swift.sandhook.xposedcompat.methodgen;
import android.os.Trace;
import com.swift.sandhook.SandHook;
import com.swift.sandhook.blacklist.HookBlackList;
import com.swift.sandhook.wrapper.HookWrapper;
import com.swift.sandhook.xposedcompat.XposedCompat;
import com.swift.sandhook.xposedcompat.hookstub.HookMethodEntity;
......@@ -23,7 +24,7 @@ import de.robv.android.xposed.XposedBridge;
public final class DynamicBridge {
private static HookMaker hookMaker = XposedCompat.useNewCallBackup ? new HookerDexMakerNew() : new HookerDexMaker();
private static HookMaker defaultHookMaker = XposedCompat.useNewCallBackup ? new HookerDexMakerNew() : new HookerDexMaker();
private static final AtomicBoolean dexPathInited = new AtomicBoolean(false);
private static File dexDir;
......@@ -46,7 +47,7 @@ public final class DynamicBridge {
try {
if (dexPathInited.compareAndSet(false, true)) {
try {
String fixedAppDataDir = XposedCompat.cacheDir.getAbsolutePath();
String fixedAppDataDir = XposedCompat.getCacheDir().getAbsolutePath();
dexDir = new File(fixedAppDataDir, "/sandxposed/");
if (!dexDir.exists())
dexDir.mkdirs();
......@@ -57,13 +58,19 @@ public final class DynamicBridge {
Trace.beginSection("SandHook-Xposed");
long timeStart = System.currentTimeMillis();
HookMethodEntity stub = null;
if (XposedCompat.useInternalStub) {
if (XposedCompat.useInternalStub && !HookBlackList.canNotHookByStub(hookMethod) && !HookBlackList.canNotHookByBridge(hookMethod)) {
stub = HookStubManager.getHookMethodEntity(hookMethod, additionalHookInfo);
}
if (stub != null) {
SandHook.hook(new HookWrapper.HookEntity(hookMethod, stub.hook, stub.backup, false));
entityMap.put(hookMethod, stub);
} else {
HookMaker hookMaker;
if (HookBlackList.canNotHookByBridge(hookMethod)) {
hookMaker = new HookerDexMaker();
} else {
hookMaker = defaultHookMaker;
}
hookMaker.start(hookMethod, additionalHookInfo,
XposedCompat.classLoader, dexDir == null ? null : dexDir.getAbsolutePath());
hookedInfo.put(hookMethod, hookMaker.getCallBackupMethod());
......@@ -76,7 +83,7 @@ public final class DynamicBridge {
}
public static void clearOatFile() {
String fixedAppDataDir = XposedCompat.cacheDir.getAbsolutePath();
String fixedAppDataDir = XposedCompat.getCacheDir().getAbsolutePath();
File dexOatDir = new File(fixedAppDataDir, "/sandxposed/oat/");
if (!dexOatDir.exists())
return;
......
......@@ -2,7 +2,7 @@ package com.swift.sandhook.xposedcompat.utils;
import android.util.Log;
import com.swift.sandhook.SandHookConfig;
import com.swift.sandhook.HookLog;
import java.lang.reflect.Member;
......@@ -11,7 +11,7 @@ public class DexLog {
public static final String TAG = "SandXposed";
public static boolean DEBUG = SandHookConfig.DEBUG;
public static boolean DEBUG = HookLog.DEBUG;
public static int v(String s) {
return Log.v(TAG, s);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment