Commit 67982050 authored by swift_gan's avatar swift_gan

Merge remote-tracking branch 'origin/master'

parents 8aa1f24a 64575661
...@@ -30,7 +30,7 @@ ext { ...@@ -30,7 +30,7 @@ ext {
userOrg = 'ganyao114' userOrg = 'ganyao114'
groupId = 'com.swift.sandhook' groupId = 'com.swift.sandhook'
repoName = 'SandHook' repoName = 'SandHook'
publishVersion = '3.0.2' publishVersion = '3.2.0'
desc = 'android art hook' desc = 'android art hook'
website = 'https://github.com/ganyao114/SandHook' website = 'https://github.com/ganyao114/SandHook'
licences = ['Apache-2.0'] licences = ['Apache-2.0']
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include "includes/trampoline_manager.h" #include "includes/trampoline_manager.h"
#include "includes/hide_api.h" #include "includes/hide_api.h"
#include "includes/cast_compiler_options.h" #include "includes/cast_compiler_options.h"
#include "includes/log.h"
SandHook::TrampolineManager trampolineManager; SandHook::TrampolineManager trampolineManager;
...@@ -17,21 +18,6 @@ enum HookMode { ...@@ -17,21 +18,6 @@ enum HookMode {
HookMode gHookMode = AUTO; HookMode gHookMode = AUTO;
extern "C"
JNIEXPORT jboolean JNICALL
Java_com_swift_sandhook_SandHook_initNative(JNIEnv *env, jclass type, jint sdk, jboolean debug) {
// TODO
SDK_INT = sdk;
DEBUG = debug;
initHideApi(env);
SandHook::CastArtMethod::init(env);
SandHook::CastCompilerOptions::init(env);
trampolineManager.init(SandHook::CastArtMethod::entryPointQuickCompiled->getOffset());
return JNI_TRUE;
}
void ensureMethodCached(art::mirror::ArtMethod *hookMethod, art::mirror::ArtMethod *backupMethod) { void ensureMethodCached(art::mirror::ArtMethod *hookMethod, art::mirror::ArtMethod *backupMethod) {
if (SDK_INT >= ANDROID_P) if (SDK_INT >= ANDROID_P)
return; return;
...@@ -144,6 +130,22 @@ bool doHookWithInline(JNIEnv* env, ...@@ -144,6 +130,22 @@ bool doHookWithInline(JNIEnv* env,
return true; return true;
} }
extern "C"
JNIEXPORT jboolean JNICALL
Java_com_swift_sandhook_SandHook_initNative(JNIEnv *env, jclass type, jint sdk, jboolean debug) {
// TODO
SDK_INT = sdk;
DEBUG = debug;
initHideApi(env);
SandHook::CastArtMethod::init(env);
SandHook::CastCompilerOptions::init(env);
trampolineManager.init(SandHook::CastArtMethod::entryPointQuickCompiled->getOffset());
return JNI_TRUE;
}
extern "C" extern "C"
JNIEXPORT jint JNICALL JNIEXPORT jint JNICALL
Java_com_swift_sandhook_SandHook_hookMethod(JNIEnv *env, jclass type, jobject originMethod, Java_com_swift_sandhook_SandHook_hookMethod(JNIEnv *env, jclass type, jobject originMethod,
...@@ -319,24 +321,132 @@ Java_com_swift_sandhook_SandHook_disableVMInline(JNIEnv *env, jclass type) { ...@@ -319,24 +321,132 @@ Java_com_swift_sandhook_SandHook_disableVMInline(JNIEnv *env, jclass type) {
extern "C" extern "C"
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_com_swift_sandhook_test_TestClass_jni_1test(JNIEnv *env, jobject instance) { Java_com_swift_sandhook_ClassNeverCall_neverCallNative(JNIEnv *env, jobject instance) {
int a = 1 + 1; int a = 1 + 1;
int b = a + 1; int b = a + 1;
} }
extern "C" extern "C"
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_com_swift_sandhook_ClassNeverCall_neverCallNative(JNIEnv *env, jobject instance) { Java_com_swift_sandhook_ClassNeverCall_neverCallNative2(JNIEnv *env, jobject instance) {
int a = 4 + 3;
int b = 9 + 6;
}
// TODO
extern "C"
JNIEXPORT void JNICALL
Java_com_swift_sandhook_test_TestClass_jni_1test(JNIEnv *env, jobject instance) {
int a = 1 + 1; int a = 1 + 1;
int b = a + 1; int b = a + 1;
}
static JNINativeMethod jniSandHook[] = {
{
"initNative",
"(IZ)Z",
(void *) Java_com_swift_sandhook_SandHook_initNative
},
{
"hookMethod",
"(Ljava/lang/reflect/Member;Ljava/lang/reflect/Method;Ljava/lang/reflect/Method;I)I",
(void *) Java_com_swift_sandhook_SandHook_hookMethod
},
{
"ensureMethodCached",
"(Ljava/lang/reflect/Method;Ljava/lang/reflect/Method;)V",
(void *) Java_com_swift_sandhook_SandHook_ensureMethodCached
},
{
"compileMethod",
"(Ljava/lang/reflect/Member;)Z",
(void *) Java_com_swift_sandhook_SandHook_compileMethod
},
{
"deCompileMethod",
"(Ljava/lang/reflect/Member;Z)Z",
(void *) Java_com_swift_sandhook_SandHook_deCompileMethod
},
{
"getObjectNative",
"(JJ)Ljava/lang/Object;",
(void *) Java_com_swift_sandhook_SandHook_getObjectNative
},
{
"canGetObject",
"()Z",
(void *) Java_com_swift_sandhook_SandHook_canGetObject
},
{
"setHookMode",
"(I)V",
(void *) Java_com_swift_sandhook_SandHook_setHookMode
},
{
"setInlineSafeCheck",
"(Z)V",
(void *) Java_com_swift_sandhook_SandHook_setInlineSafeCheck
},
{
"skipAllSafeCheck",
"(Z)V",
(void *) Java_com_swift_sandhook_SandHook_skipAllSafeCheck
},
{
"is64Bit",
"()Z",
(void *) Java_com_swift_sandhook_SandHook_is64Bit
},
{
"disableVMInline",
"()Z",
(void *) Java_com_swift_sandhook_SandHook_disableVMInline
}
};
static JNINativeMethod jniNeverCall[] = {
{
"neverCallNative",
"()V",
(void *) Java_com_swift_sandhook_ClassNeverCall_neverCallNative
},
{
"neverCallNative2",
"()V",
(void *) Java_com_swift_sandhook_ClassNeverCall_neverCallNative2
}
};
static bool registerNativeMethods(JNIEnv *env, const char *className, JNINativeMethod *jniMethods, int methods) {
jclass clazz = env->FindClass(className);
if (clazz == NULL) {
return false;
}
return env->RegisterNatives(clazz, jniMethods, methods) >= 0;
} }
extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
JNIEXPORT void JNICALL
Java_com_swift_sandhook_ClassNeverCall_neverCallNative2(JNIEnv *env, jobject instance) { const char* CLASS_SAND_HOOK = "com/swift/sandhook/SandHook";
int a = 4 + 3; const char* CLASS_NEVER_CALL = "com/swift/sandhook/ClassNeverCall";
int b = 9 + 6;
int jniMethodSize = sizeof(JNINativeMethod);
JNIEnv *env = NULL;
if (vm->GetEnv((void **) &env, JNI_VERSION_1_6) != JNI_OK) {
return -1;
}
if (!registerNativeMethods(env, CLASS_SAND_HOOK, jniSandHook, sizeof(jniSandHook) / jniMethodSize)) {
return -1;
}
if (!registerNativeMethods(env, CLASS_NEVER_CALL, jniNeverCall, sizeof(jniNeverCall) / jniMethodSize)) {
return -1;
}
LOGW("JNI Loaded");
return JNI_VERSION_1_6;
} }
\ No newline at end of file
...@@ -40,11 +40,7 @@ public class SandHook { ...@@ -40,11 +40,7 @@ public class SandHook {
public static int testAccessFlag; public static int testAccessFlag;
static { static {
if (SandHookConfig.libSandHookPath == null || SandHookConfig.libSandHookPath.length() == 0) { SandHookConfig.libLoader.loadLib();
System.loadLibrary("sandhook");
} else {
System.load(SandHookConfig.libSandHookPath);
}
init(); init();
} }
...@@ -259,7 +255,11 @@ public class SandHook { ...@@ -259,7 +255,11 @@ public class SandHook {
if (artMethodClass != null) if (artMethodClass != null)
return true; return true;
try { try {
if (SandHookConfig.initClassLoader == null) {
artMethodClass = Class.forName("java.lang.reflect.ArtMethod"); artMethodClass = Class.forName("java.lang.reflect.ArtMethod");
} else {
artMethodClass = Class.forName("java.lang.reflect.ArtMethod", true, SandHookConfig.initClassLoader);
}
return true; return true;
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
return false; return false;
......
package com.swift.sandhook; package com.swift.sandhook;
import android.annotation.SuppressLint;
import android.os.Build; import android.os.Build;
import com.swift.sandhook.lib.BuildConfig; import com.swift.sandhook.lib.BuildConfig;
public class SandHookConfig { public class SandHookConfig {
public volatile static int SDK_INT = Build.VERSION.SDK_INT; public volatile static int SDK_INT = Build.VERSION.SDK_INT;
public volatile static String libSandHookPath;
public volatile static boolean DEBUG = BuildConfig.DEBUG; public volatile static boolean DEBUG = BuildConfig.DEBUG;
public volatile static boolean compiler = true; public volatile static boolean compiler = true;
public volatile static ClassLoader initClassLoader;
public volatile static String libSandHookPath;
public volatile static LibLoader libLoader = new LibLoader() {
@SuppressLint("UnsafeDynamicallyLoadedCode")
@Override
public void loadLib() {
if (SandHookConfig.libSandHookPath == null) {
System.loadLibrary("sandhook");
} else {
System.load(SandHookConfig.libSandHookPath);
}
}
};
public interface LibLoader {
void loadLib();
}
} }
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