refactor(native): hook def

This commit is contained in:
rhunk 2024-02-13 13:21:51 +01:00
parent 2b85856be0
commit ba655a0e67
7 changed files with 18 additions and 23 deletions

View File

@ -1,11 +1,11 @@
#pragma once
#include <android/asset_manager.h>
namespace AssetHook {
jmethodID native_lib_on_asset_load;
AAsset* (*AAssetManager_open_original)(AAssetManager*, const char*, int) = nullptr;
AAsset* AAssetManager_open_hook(AAssetManager* mgr, const char* filename, int mode) {
HOOK_DEF(AAsset*, AAssetManager_open_hook, AAssetManager* mgr, const char* filename, int mode) {
if (common::native_config->hook_asset_open) {
JNIEnv *env = nullptr;
common::java_vm->GetEnv((void **)&env, JNI_VERSION_1_6);
@ -15,11 +15,11 @@ namespace AssetHook {
}
}
return AAssetManager_open_original(mgr, filename, mode);
return AAssetManager_open_hook_original(mgr, filename, mode);
}
void init(JNIEnv *env) {
native_lib_on_asset_load = env->GetMethodID(env->GetObjectClass(common::native_lib_object), "shouldLoadAsset", "(Ljava/lang/String;)Z");
DobbyHook((void *) AAssetManager_open, (void *) AAssetManager_open_hook, (void **) &AAssetManager_open_original);
DobbyHook((void *) AAssetManager_open, (void *) AAssetManager_open_hook, (void **) &AAssetManager_open_hook_original);
}
}

View File

@ -1,9 +1,7 @@
#pragma once
namespace FstatHook {
auto fstat_original = (int (*)(int, struct stat *)) nullptr;
int fstat_hook(int fd, struct stat *buf) {
HOOK_DEF(int, fstat_hook, int fd, struct stat *buf) {
char name[256];
memset(name, 0, sizeof(name));
snprintf(name, sizeof(name), "/proc/self/fd/%d", fd);
@ -20,10 +18,10 @@ namespace FstatHook {
return -1;
}
return fstat_original(fd, buf);
return fstat_hook_original(fd, buf);
}
void init() {
DobbyHook((void *)DobbySymbolResolver("libc.so", "fstat"), (void *)fstat_hook, (void **)&fstat_original);
DobbyHook((void *)DobbySymbolResolver("libc.so", "fstat"), (void *)fstat_hook, (void **)&fstat_hook_original);
}
}

View File

@ -15,10 +15,9 @@ namespace SqliteMutexHook {
} sqlite3;
static std::map<std::string, sqlite3_mutex *> mutex_map = {};
static int (*sqlite3_open_original)(const char *, sqlite3 **, unsigned int, const char *) = nullptr;
int sqlite3_open_hook(const char *filename, sqlite3 **ppDb, unsigned int flags, const char *zVfs) {
auto result = sqlite3_open_original(filename, ppDb, flags, zVfs);
HOOK_DEF(int, sqlite3_open_hook, const char *filename, sqlite3 **ppDb, unsigned int flags, const char *zVfs) {
auto result = sqlite3_open_hook_original(filename, ppDb, flags, zVfs);
if (result == 0) {
auto mutex = (*ppDb)->mutex;
if (mutex == nullptr) return result;
@ -42,6 +41,6 @@ namespace SqliteMutexHook {
LOGE("sqlite3 openDatabase sig not found");
return;
}
DobbyHook((void *) open_database_sig, (void *) sqlite3_open_hook, (void **) &sqlite3_open_original);
DobbyHook((void *) open_database_sig, (void *) sqlite3_open_hook, (void **) &sqlite3_open_hook_original);
}
}

View File

@ -19,14 +19,13 @@ namespace UnaryCallHook {
} grpc_byte_buffer;
}
static void *(*unaryCall_original)(void *, const char *, grpc::grpc_byte_buffer **, void *, void *, void *);
static jmethodID native_lib_on_unary_call_method;
void *unaryCall_hook(void *unk1, const char *uri, grpc::grpc_byte_buffer **buffer_ptr, void *unk4, void *unk5, void *unk6) {
HOOK_DEF(void *, unaryCall_hook, void *unk1, const char *uri, grpc::grpc_byte_buffer **buffer_ptr, void *unk4, void *unk5, void *unk6) {
// request without reference counter can be hooked using xposed ig
auto slice_buffer = (*buffer_ptr)->slice_buffer;
if (slice_buffer->ref_counter == 0) {
return unaryCall_original(unk1, uri, buffer_ptr, unk4, unk5, unk6);
return unaryCall_hook_original(unk1, uri, buffer_ptr, unk4, unk5, unk6);
}
JNIEnv *env = nullptr;
@ -67,7 +66,7 @@ namespace UnaryCallHook {
slice_buffer->data = (uint8_t *)((uintptr_t)new_ref_counter + ref_counter_struct_size);
}
return unaryCall_original(unk1, uri, buffer_ptr, unk4, unk5, unk6);
return unaryCall_hook_original(unk1, uri, buffer_ptr, unk4, unk5, unk6);
}
void init(JNIEnv *env) {
@ -80,9 +79,9 @@ namespace UnaryCallHook {
native_lib_on_unary_call_method = env->GetMethodID(env->GetObjectClass(common::native_lib_object), "onNativeUnaryCall", "(Ljava/lang/String;[B)L" BUILD_NAMESPACE "/NativeRequestData;");
if (unaryCall_func != 0) {
DobbyHook((void *)unaryCall_func, (void *)unaryCall_hook, (void **)&unaryCall_original);
DobbyHook((void *)unaryCall_func, (void *)unaryCall_hook, (void **)&unaryCall_hook_original);
} else {
LOGE("can't find unaryCall signature");
LOGE("Can't find unaryCall signature");
}
}
}

View File

@ -2,7 +2,6 @@
#include <string>
#include <dobby.h>
#include <vector>
#include <android/asset_manager.h>
#include "logger.h"
#include "common.h"
@ -24,7 +23,7 @@ void JNICALL init(JNIEnv *env, jobject clazz) {
return;
}
LOGD("libclient.so base=0x%0lx, size=0x%0lx", client_module.base, client_module.size);
LOGD("libclient.so base=0x%lx, size=0x%zx", client_module.base, client_module.size);
AssetHook::init(env);
UnaryCallHook::init(env);

View File

@ -5,7 +5,5 @@
#define LOG_TAG "SnapEnhanceNative"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)

View File

@ -2,6 +2,8 @@
#include <unistd.h>
#define HOOK_DEF(ret, func, ...) ret (*func##_original)(__VA_ARGS__); ret func(__VA_ARGS__)
namespace util {
typedef struct {
uintptr_t base;