Commit ff09c694 authored by swift_gan's avatar swift_gan

pre add hooker stubs

parent e0845316
...@@ -17,6 +17,7 @@ extern "C" { ...@@ -17,6 +17,7 @@ extern "C" {
void suspendVM(); void suspendVM();
void resumeVM(); void resumeVM();
bool canGetObject();
jobject getJavaObject(JNIEnv* env, void* thread, void* address); jobject getJavaObject(JNIEnv* env, void* thread, void* address);
} }
......
...@@ -286,6 +286,19 @@ Java_com_swift_sandhook_SandHook_compileMethod(JNIEnv *env, jclass type, jobject ...@@ -286,6 +286,19 @@ Java_com_swift_sandhook_SandHook_compileMethod(JNIEnv *env, jclass type, jobject
} }
extern "C"
JNIEXPORT jobject JNICALL
Java_com_swift_sandhook_SandHook_getObjectNative(JNIEnv *env, jclass type, jlong thread,
jlong address) {
return getJavaObject(env, reinterpret_cast<void *>(thread), reinterpret_cast<void *>(address));
}
extern "C"
JNIEXPORT jboolean JNICALL
Java_com_swift_sandhook_SandHook_canGetObject(JNIEnv *env, jclass type) {
return static_cast<jboolean>(canGetObject());
}
extern "C" extern "C"
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_com_swift_sandhook_SandHook_setHookMode(JNIEnv *env, jclass type, jint mode) { Java_com_swift_sandhook_SandHook_setHookMode(JNIEnv *env, jclass type, jint mode) {
...@@ -303,10 +316,16 @@ JNIEXPORT void JNICALL ...@@ -303,10 +316,16 @@ JNIEXPORT void JNICALL
Java_com_swift_sandhook_SandHook_skipAllSafeCheck(JNIEnv *env, jclass type, jboolean skip) { Java_com_swift_sandhook_SandHook_skipAllSafeCheck(JNIEnv *env, jclass type, jboolean skip) {
trampolineManager.skipAllCheck = skip; trampolineManager.skipAllCheck = skip;
} }
extern "C"
JNIEXPORT jboolean JNICALL
Java_com_swift_sandhook_SandHook_is64Bit(JNIEnv *env, jclass type) {
return BYTE_POINT == 8;
}
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_test_TestClass_jni_1test(JNIEnv *env, jobject instance) {
int a = 1 + 1; int a = 1 + 1;
int b = a + 1; int b = a + 1;
} }
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
// Created by swift on 2019/1/21. // Created by swift on 2019/1/21.
// //
#include "../includes/hide_api.h" #include "../includes/hide_api.h"
#include "../includes/arch.h"
extern int SDK_INT; extern int SDK_INT;
...@@ -23,7 +24,7 @@ extern "C" { ...@@ -23,7 +24,7 @@ extern "C" {
//init compile //init compile
if (SDK_INT >= 24) { if (SDK_INT >= 24) {
void *jit_lib; void *jit_lib;
if (sizeof(void*) == 8) { if (BYTE_POINT == 8) {
jit_lib = fake_dlopen("/system/lib64/libart-compiler.so", RTLD_NOW); jit_lib = fake_dlopen("/system/lib64/libart-compiler.so", RTLD_NOW);
} else { } else {
jit_lib = fake_dlopen("/system/lib/libart-compiler.so", RTLD_NOW); jit_lib = fake_dlopen("/system/lib/libart-compiler.so", RTLD_NOW);
...@@ -36,7 +37,7 @@ extern "C" { ...@@ -36,7 +37,7 @@ extern "C" {
//init suspend //init suspend
void* art_lib; void* art_lib;
const char* art_lib_path; const char* art_lib_path;
if (sizeof(void*) == 8) { if (BYTE_POINT == 8) {
art_lib_path = "/system/lib64/libart.so"; art_lib_path = "/system/lib64/libart.so";
} else { } else {
art_lib_path = "/system/lib/libart.so"; art_lib_path = "/system/lib/libart.so";
...@@ -61,7 +62,7 @@ extern "C" { ...@@ -61,7 +62,7 @@ extern "C" {
"_ZN3art9JavaVMExt16AddWeakGlobalRefEPNS_6ThreadEPNS_6mirror6ObjectE"); "_ZN3art9JavaVMExt16AddWeakGlobalRefEPNS_6ThreadEPNS_6mirror6ObjectE");
} else { } else {
void *handle; void *handle;
if (sizeof(void *) == 8) { if (BYTE_POINT == 8) {
handle = fake_dlopen("/system/lib64/libart.so", RTLD_NOW); handle = fake_dlopen("/system/lib64/libart.so", RTLD_NOW);
} else { } else {
handle = fake_dlopen("/system/lib/libart.so", RTLD_NOW); handle = fake_dlopen("/system/lib/libart.so", RTLD_NOW);
...@@ -94,6 +95,10 @@ extern "C" { ...@@ -94,6 +95,10 @@ extern "C" {
innerResumeVM(); innerResumeVM();
} }
bool canGetObject() {
return addWeakGlobalRef != nullptr;
}
jobject getJavaObject(JNIEnv* env, void* thread, void* address) { jobject getJavaObject(JNIEnv* env, void* thread, void* address) {
if (addWeakGlobalRef == nullptr) if (addWeakGlobalRef == nullptr)
......
...@@ -4,6 +4,7 @@ import android.os.Build; ...@@ -4,6 +4,7 @@ import android.os.Build;
import android.util.Log; import android.util.Log;
import com.swift.sandhook.annotation.HookMode; import com.swift.sandhook.annotation.HookMode;
import com.swift.sandhook.utils.Unsafe;
import com.swift.sandhook.wrapper.HookErrorException; import com.swift.sandhook.wrapper.HookErrorException;
import com.swift.sandhook.wrapper.HookWrapper; import com.swift.sandhook.wrapper.HookWrapper;
...@@ -196,6 +197,17 @@ public class SandHook { ...@@ -196,6 +197,17 @@ public class SandHook {
} }
} }
public static Object getObject(long address) {
long threadSelf = getThreadId();
if (threadSelf == 0)
return null;
return getObjectNative(threadSelf, address);
}
public static long getObjectAddress(Object object) {
return Unsafe.getObjectAddress(object);
}
private static void initTestOffset() { private static void initTestOffset() {
// make test methods sure resolved! // make test methods sure resolved!
ArtMethodSizeTest.method1(); ArtMethodSizeTest.method1();
...@@ -290,6 +302,11 @@ public class SandHook { ...@@ -290,6 +302,11 @@ public class SandHook {
public static native void compileMethod(Member member); public static native void compileMethod(Member member);
public static native boolean canGetObject();
public static native Object getObjectNative(long thread, long address);
public static native boolean is64Bit();
@FunctionalInterface @FunctionalInterface
public interface HookModeCallBack { public interface HookModeCallBack {
int hookMode(Member originMethod); int hookMode(Member originMethod);
......
package com.swift.sandhook.utils;
import com.swift.sandhook.SandHook;
public class ParamWrapper {
public static boolean support(Class objectType) {
if (SandHook.is64Bit()) {
return objectType != float.class && objectType != double.class;
} else {
return objectType != float.class && objectType != double.class && objectType != long.class;
}
}
public static Object addressToObject(Class objectType, long address) {
if (SandHook.is64Bit()) {
return addressToObject64(objectType, address);
} else {
return addressToObject32(objectType, (int) address);
}
}
public static Object addressToObject64(Class objectType, long address) {
if (objectType == null)
return null;
if (objectType.isPrimitive()) {
if (objectType == int.class) {
return (int)address;
} else if (objectType == long.class) {
return address;
} else if (objectType == short.class) {
return (short)address;
} else if (objectType == byte.class) {
return (byte)address;
} else if (objectType == char.class) {
return (char)address;
} else if (objectType == boolean.class) {
return address == 0;
} else {
throw new RuntimeException("unknown type: " + objectType.toString());
}
} else {
return SandHook.getObject(address);
}
}
public static Object addressToObject32(Class objectType, int address) {
if (objectType == null)
return null;
if (objectType.isPrimitive()) {
if (objectType == int.class) {
return address;
} else if (objectType == short.class) {
return (short)address;
} else if (objectType == byte.class) {
return (byte)address;
} else if (objectType == char.class) {
return (char)address;
} else if (objectType == boolean.class) {
return address == 0;
} else {
throw new RuntimeException("unknown type: " + objectType.toString());
}
} else {
return SandHook.getObject(address);
}
}
public static long objectToAddress(Object object) {
if (object == null) {
return 0;
} else {
return SandHook.getObjectAddress(object);
}
}
}
/*
* Copyright 2014-2015 Marvin Wißfeld
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.swift.sandhook.utils;
import android.util.Log;
import java.lang.reflect.Field;
public final class Unsafe {
private static final String TAG = "Unsafe";
private static Object unsafe;
private static Class unsafeClass;
static {
try {
unsafeClass = Class.forName("sun.misc.Unsafe");
Field theUnsafe = unsafeClass.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
unsafe = theUnsafe.get(null);
} catch (Exception e) {
try {
final Field theUnsafe = unsafeClass.getDeclaredField("THE_ONE");
theUnsafe.setAccessible(true);
unsafe = theUnsafe.get(null);
} catch (Exception e2) {
Log.w(TAG, "Unsafe not found o.O");
}
}
}
private Unsafe() {
}
@SuppressWarnings("unchecked")
public static int arrayBaseOffset(Class cls) {
try {
return (int) unsafeClass.getDeclaredMethod("arrayBaseOffset", Class.class).invoke(unsafe, cls);
} catch (Exception e) {
Log.w(TAG, e);
return 0;
}
}
@SuppressWarnings("unchecked")
public static int arrayIndexScale(Class cls) {
try {
return (int) unsafeClass.getDeclaredMethod("arrayIndexScale", Class.class).invoke(unsafe, cls);
} catch (Exception e) {
Log.w(TAG, e);
return 0;
}
}
@SuppressWarnings("unchecked")
public static long objectFieldOffset(Field field) {
try {
return (long) unsafeClass.getDeclaredMethod("objectFieldOffset", Field.class).invoke(unsafe, field);
} catch (Exception e) {
Log.w(TAG, e);
return 0;
}
}
@SuppressWarnings("unchecked")
public static int getInt(Object array, long offset) {
try {
return (int) unsafeClass.getDeclaredMethod("getInt", Object.class, long.class).invoke(unsafe, array, offset);
} catch (Exception e) {
Log.w(TAG, e);
return 0;
}
}
@SuppressWarnings("unchecked")
public static long getLong(Object array, long offset) {
try {
return (long) unsafeClass.getDeclaredMethod("getLong", Object.class, long.class).invoke(unsafe, array, offset);
} catch (Exception e) {
Log.w(TAG, e);
return 0;
}
}
@SuppressWarnings("unchecked")
public static void putLong(Object array, long offset, long value) {
try {
unsafeClass.getDeclaredMethod("putLongVolatile", Object.class, long.class, long.class).invoke(unsafe, array, offset, value);
} catch (Exception e) {
try {
unsafeClass.getDeclaredMethod("putLong", Object.class, long.class, long.class).invoke(unsafe, array, offset, value);
} catch (Exception e1) {
Log.w(TAG, e);
}
}
}
@SuppressWarnings("unchecked")
public static void putInt(Object array, long offset, int value) {
try {
unsafeClass.getDeclaredMethod("putIntVolatile", Object.class, long.class, int.class).invoke(unsafe, array, offset, value);
} catch (Exception e) {
try {
unsafeClass.getDeclaredMethod("putIntVolatile", Object.class, long.class, int.class).invoke(unsafe, array, offset, value);
} catch (Exception e1) {
Log.w(TAG, e);
}
}
}
public static long getObjectAddress(Object obj) {
try {
Object[] array = new Object[]{obj};
if (arrayIndexScale(Object[].class) == 8) {
return getLong(array, arrayBaseOffset(Object[].class));
} else {
return 0xffffffffL & getInt(array, arrayBaseOffset(Object[].class));
}
} catch (Exception e) {
Log.w(TAG, e);
return -1;
}
}
}
package com.swift.sandhook.xposedcompat.hookstub;
public interface CallOriginCallBack {
Object call(long... args) throws Throwable;
}
package com.swift.sandhook.xposedcompat.hookstub;
import com.swift.sandhook.utils.ParamWrapper;
public class HookMethodInfo {
public Class[] parType;
public Class retType;
public Object getArg(int index, long address) {
return ParamWrapper.addressToObject(parType[index], address);
}
public boolean isVoid() {
return Void.TYPE.equals(retType);
}
}
package com.swift.sandhook.xposedcompat.hookstub;
import java.lang.reflect.Member;
public class HookStubManager {
public final static int MAX_STUB_COUNT = 100;
public static Member[] originMethods = new Member[MAX_STUB_COUNT];
public static String getHookMethodName(int index) {
return "stub_hook_" + index;
}
public static String getbackupMethodName(int index) {
return "stub_backup_" + index;
}
public static Object hookBridge(int index, CallOriginCallBack callOrigin, long... stubArgs) throws Throwable {
Member originMethod = originMethods[index];
return null;
}
}
package com.swift.sandhook.xposedcompat.hookstub;
public class MethodHookerStubs {
}
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