2021-08-11 00:00:21 -07:00
|
|
|
#include <dlfcn.h>
|
2021-01-08 00:53:24 -08:00
|
|
|
#include <xhook.h>
|
|
|
|
#include <utils.hpp>
|
|
|
|
#include <flags.hpp>
|
2021-01-12 03:28:00 -08:00
|
|
|
#include <daemon.hpp>
|
2021-01-08 00:53:24 -08:00
|
|
|
|
|
|
|
#include "inject.hpp"
|
2021-08-01 14:35:16 -07:00
|
|
|
#include "memory.hpp"
|
2021-08-11 22:56:18 -07:00
|
|
|
#include "api.hpp"
|
2021-01-08 00:53:24 -08:00
|
|
|
|
|
|
|
using namespace std;
|
2021-08-01 14:35:16 -07:00
|
|
|
using jni_hook::hash_map;
|
|
|
|
using jni_hook::tree_map;
|
|
|
|
using xstring = jni_hook::string;
|
|
|
|
|
2021-08-11 22:56:18 -07:00
|
|
|
namespace {
|
2021-08-02 03:20:19 -07:00
|
|
|
|
2021-08-01 14:35:16 -07:00
|
|
|
struct HookContext {
|
|
|
|
int pid;
|
|
|
|
bool do_hide;
|
2021-08-02 03:20:19 -07:00
|
|
|
union {
|
|
|
|
SpecializeAppProcessArgs *args;
|
|
|
|
ForkSystemServerArgs *server_args;
|
|
|
|
void *raw_args;
|
|
|
|
};
|
2021-08-01 14:35:16 -07:00
|
|
|
};
|
|
|
|
|
2021-08-11 22:56:18 -07:00
|
|
|
// Global variables
|
|
|
|
vector<tuple<const char *, const char *, void **>> *xhook_list;
|
|
|
|
vector<JNINativeMethod> *jni_hook_list;
|
|
|
|
hash_map<xstring, tree_map<xstring, tree_map<xstring, void *>>> *jni_method_map;
|
2021-01-08 00:53:24 -08:00
|
|
|
|
2021-08-11 22:56:18 -07:00
|
|
|
HookContext *current_ctx;
|
|
|
|
JavaVM *g_jvm;
|
|
|
|
const JNINativeInterface *old_functions;
|
|
|
|
JNINativeInterface *new_functions;
|
2021-01-09 20:39:59 -08:00
|
|
|
|
|
|
|
#define DCL_JNI_FUNC(name) \
|
2021-08-11 22:56:18 -07:00
|
|
|
void *name##_orig; \
|
|
|
|
void name##_pre(HookContext *ctx, JNIEnv *env, jclass clazz); \
|
|
|
|
void name##_post(HookContext *ctx, JNIEnv *env, jclass clazz);
|
2021-01-08 05:25:44 -08:00
|
|
|
|
2021-01-09 17:41:25 -08:00
|
|
|
// JNI method declarations
|
2021-01-09 20:39:59 -08:00
|
|
|
DCL_JNI_FUNC(nativeForkAndSpecialize)
|
|
|
|
DCL_JNI_FUNC(nativeSpecializeAppProcess)
|
|
|
|
DCL_JNI_FUNC(nativeForkSystemServer)
|
2021-08-11 22:56:18 -07:00
|
|
|
|
|
|
|
#undef DCL_JNI_FUNC
|
|
|
|
|
|
|
|
// JNI method definitions
|
|
|
|
// Includes all method signatures of all supported Android versions
|
|
|
|
#include "jni_hooks.hpp"
|
2021-01-09 17:41:25 -08:00
|
|
|
|
2021-01-09 20:39:59 -08:00
|
|
|
#define HOOK_JNI(method) \
|
2021-08-02 03:20:19 -07:00
|
|
|
if (methods[i].name == #method##sv) { \
|
2021-08-01 14:35:16 -07:00
|
|
|
jni_hook_list->push_back(methods[i]); \
|
2021-08-02 03:20:19 -07:00
|
|
|
method##_orig = methods[i].fnPtr; \
|
2021-08-01 14:35:16 -07:00
|
|
|
for (int j = 0; j < method##_methods_num; ++j) { \
|
|
|
|
if (strcmp(methods[i].signature, method##_methods[j].signature) == 0) { \
|
|
|
|
newMethods[i] = method##_methods[j]; \
|
|
|
|
LOGI("hook: replaced #" #method "\n"); \
|
|
|
|
++hooked; \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
continue; \
|
2021-01-08 05:25:44 -08:00
|
|
|
}
|
|
|
|
|
2021-08-11 22:56:18 -07:00
|
|
|
unique_ptr<JNINativeMethod[]> hookAndSaveJNIMethods(
|
2021-01-08 00:53:24 -08:00
|
|
|
JNIEnv *env, const char *className, const JNINativeMethod *methods, int numMethods) {
|
2021-01-08 05:25:44 -08:00
|
|
|
if (g_jvm == nullptr) {
|
|
|
|
// Save for later unhooking
|
|
|
|
env->GetJavaVM(&g_jvm);
|
|
|
|
}
|
2021-01-08 00:53:24 -08:00
|
|
|
|
2021-08-01 14:35:16 -07:00
|
|
|
unique_ptr<JNINativeMethod[]> newMethods;
|
|
|
|
int hooked = numeric_limits<int>::max();
|
2021-01-08 05:25:44 -08:00
|
|
|
if (className == "com/android/internal/os/Zygote"sv) {
|
2021-08-01 14:35:16 -07:00
|
|
|
hooked = 0;
|
2021-01-09 20:39:59 -08:00
|
|
|
newMethods = make_unique<JNINativeMethod[]>(numMethods);
|
|
|
|
memcpy(newMethods.get(), methods, sizeof(JNINativeMethod) * numMethods);
|
2021-08-01 14:35:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
auto &class_map = (*jni_method_map)[className];
|
|
|
|
for (int i = 0; i < numMethods; ++i) {
|
|
|
|
class_map[methods[i].name][methods[i].signature] = methods[i].fnPtr;
|
2021-08-02 03:20:19 -07:00
|
|
|
if (hooked < 3) {
|
|
|
|
HOOK_JNI(nativeForkAndSpecialize);
|
|
|
|
HOOK_JNI(nativeSpecializeAppProcess);
|
|
|
|
HOOK_JNI(nativeForkSystemServer);
|
|
|
|
}
|
2021-01-08 05:25:44 -08:00
|
|
|
}
|
2021-08-11 00:00:21 -07:00
|
|
|
return newMethods;
|
|
|
|
}
|
|
|
|
|
2021-08-11 22:56:18 -07:00
|
|
|
#undef HOOK_JNI
|
|
|
|
|
|
|
|
jclass gClassRef;
|
|
|
|
jmethodID class_getName;
|
|
|
|
string get_class_name(JNIEnv *env, jclass clazz) {
|
2021-08-11 00:00:21 -07:00
|
|
|
if (!gClassRef) {
|
|
|
|
jclass cls = env->FindClass("java/lang/Class");
|
|
|
|
gClassRef = (jclass) env->NewGlobalRef(cls);
|
|
|
|
env->DeleteLocalRef(cls);
|
|
|
|
class_getName = env->GetMethodID(gClassRef, "getName", "()Ljava/lang/String;");
|
|
|
|
}
|
|
|
|
auto nameRef = (jstring) env->CallObjectMethod(clazz, class_getName);
|
|
|
|
const char *name = env->GetStringUTFChars(nameRef, nullptr);
|
|
|
|
string className(name);
|
|
|
|
env->ReleaseStringUTFChars(nameRef, name);
|
|
|
|
std::replace(className.begin(), className.end(), '.', '/');
|
|
|
|
return className;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------
|
|
|
|
|
2021-08-11 22:56:18 -07:00
|
|
|
#define DCL_HOOK_FUNC(ret, func, ...) \
|
|
|
|
ret (*old_##func)(__VA_ARGS__); \
|
|
|
|
ret new_##func(__VA_ARGS__)
|
|
|
|
|
2021-08-12 06:41:59 -07:00
|
|
|
jint jniRegisterNatives(
|
2021-08-11 00:00:21 -07:00
|
|
|
JNIEnv *env, jclass clazz, const JNINativeMethod *methods, jint numMethods) {
|
|
|
|
auto className = get_class_name(env, clazz);
|
|
|
|
LOGD("hook: JNIEnv->RegisterNatives %s\n", className.data());
|
|
|
|
auto newMethods = hookAndSaveJNIMethods(env, className.data(), methods, numMethods);
|
|
|
|
return old_functions->RegisterNatives(env, clazz, newMethods.get() ?: methods, numMethods);
|
|
|
|
}
|
2021-01-08 05:25:44 -08:00
|
|
|
|
2021-08-11 00:00:21 -07:00
|
|
|
DCL_HOOK_FUNC(int, jniRegisterNativeMethods,
|
|
|
|
JNIEnv *env, const char *className, const JNINativeMethod *methods, int numMethods) {
|
|
|
|
LOGD("hook: jniRegisterNativeMethods %s\n", className);
|
|
|
|
auto newMethods = hookAndSaveJNIMethods(env, className, methods, numMethods);
|
2021-01-08 05:25:44 -08:00
|
|
|
return old_jniRegisterNativeMethods(env, className, newMethods.get() ?: methods, numMethods);
|
2021-01-08 00:53:24 -08:00
|
|
|
}
|
|
|
|
|
2021-01-10 01:25:30 -08:00
|
|
|
DCL_HOOK_FUNC(int, fork) {
|
2021-08-02 03:20:19 -07:00
|
|
|
return current_ctx ? current_ctx->pid : old_fork();
|
2021-01-10 01:25:30 -08:00
|
|
|
}
|
|
|
|
|
2021-01-12 03:28:00 -08:00
|
|
|
DCL_HOOK_FUNC(int, selinux_android_setcontext,
|
|
|
|
uid_t uid, int isSystemServer, const char *seinfo, const char *pkgname) {
|
|
|
|
if (current_ctx && current_ctx->do_hide) {
|
|
|
|
// Ask magiskd to hide ourselves before switching context
|
|
|
|
// because magiskd socket is not accessible on Android 8.0+
|
|
|
|
remote_request_hide();
|
|
|
|
LOGD("hook: process successfully hidden\n");
|
|
|
|
}
|
|
|
|
return old_selinux_android_setcontext(uid, isSystemServer, seinfo, pkgname);
|
|
|
|
}
|
|
|
|
|
2021-08-11 00:00:21 -07:00
|
|
|
// -----------------------------------------------------------------
|
|
|
|
|
2021-08-12 06:41:59 -07:00
|
|
|
// The original android::AppRuntime virtual table
|
|
|
|
void **gAppRuntimeVTable;
|
2021-08-11 22:56:18 -07:00
|
|
|
|
2021-08-11 00:00:21 -07:00
|
|
|
// This method is a trampoline for hooking JNIEnv->RegisterNatives
|
2021-08-12 06:41:59 -07:00
|
|
|
void onVmCreated(void *self, JNIEnv* env) {
|
2021-08-11 00:00:21 -07:00
|
|
|
LOGD("hook: AppRuntime::onVmCreated\n");
|
|
|
|
|
2021-08-12 06:41:59 -07:00
|
|
|
// Restore virtual table
|
|
|
|
auto new_table = *reinterpret_cast<void***>(self);
|
|
|
|
*reinterpret_cast<void***>(self) = gAppRuntimeVTable;
|
|
|
|
delete[] new_table;
|
2021-08-11 00:00:21 -07:00
|
|
|
|
|
|
|
new_functions = new JNINativeInterface();
|
|
|
|
memcpy(new_functions, env->functions, sizeof(*new_functions));
|
2021-08-12 06:41:59 -07:00
|
|
|
new_functions->RegisterNatives = &jniRegisterNatives;
|
|
|
|
|
|
|
|
// Replace the function table in JNIEnv to hook RegisterNatives
|
|
|
|
old_functions = env->functions;
|
2021-08-11 00:00:21 -07:00
|
|
|
env->functions = new_functions;
|
2021-08-12 06:41:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
template<int N>
|
|
|
|
void vtable_entry(void *self, JNIEnv* env) {
|
|
|
|
// The first invocation will be onVmCreated
|
|
|
|
onVmCreated(self, env);
|
|
|
|
// Call original function
|
|
|
|
reinterpret_cast<decltype(&onVmCreated)>(gAppRuntimeVTable[N])(self, env);
|
2021-08-11 00:00:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// This method is a trampoline for swizzling android::AppRuntime vtable
|
2021-08-11 22:56:18 -07:00
|
|
|
bool swizzled = false;
|
2021-08-11 00:00:21 -07:00
|
|
|
DCL_HOOK_FUNC(void, setArgv0, void *self, const char *argv0, bool setProcName) {
|
|
|
|
if (swizzled) {
|
|
|
|
old_setArgv0(self, argv0, setProcName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOGD("hook: AndroidRuntime::setArgv0\n");
|
|
|
|
|
2021-08-12 06:41:59 -07:00
|
|
|
// We don't know which entry is onVmCreated, so overwrite every one
|
|
|
|
// We also don't know the size of the vtable, but 8 is more than enough
|
|
|
|
auto new_table = new void*[8];
|
|
|
|
new_table[0] = reinterpret_cast<void*>(&vtable_entry<0>);
|
|
|
|
new_table[1] = reinterpret_cast<void*>(&vtable_entry<1>);
|
|
|
|
new_table[2] = reinterpret_cast<void*>(&vtable_entry<2>);
|
|
|
|
new_table[3] = reinterpret_cast<void*>(&vtable_entry<3>);
|
|
|
|
new_table[4] = reinterpret_cast<void*>(&vtable_entry<4>);
|
|
|
|
new_table[5] = reinterpret_cast<void*>(&vtable_entry<5>);
|
|
|
|
new_table[6] = reinterpret_cast<void*>(&vtable_entry<6>);
|
|
|
|
new_table[7] = reinterpret_cast<void*>(&vtable_entry<7>);
|
|
|
|
|
2021-08-11 00:00:21 -07:00
|
|
|
// Swizzle C++ vtable to hook virtual function
|
2021-08-12 06:41:59 -07:00
|
|
|
gAppRuntimeVTable = *reinterpret_cast<void***>(self);
|
|
|
|
*reinterpret_cast<void***>(self) = new_table;
|
2021-08-11 00:00:21 -07:00
|
|
|
swizzled = true;
|
|
|
|
|
|
|
|
old_setArgv0(self, argv0, setProcName);
|
2021-01-10 01:25:30 -08:00
|
|
|
}
|
|
|
|
|
2021-08-11 22:56:18 -07:00
|
|
|
#undef DCL_HOOK_FUNC
|
|
|
|
|
2021-01-10 01:25:30 -08:00
|
|
|
// -----------------------------------------------------------------
|
|
|
|
|
2021-08-11 22:56:18 -07:00
|
|
|
void nativeSpecializeAppProcess_pre(HookContext *ctx, JNIEnv *env, jclass clazz) {
|
2021-01-12 03:28:00 -08:00
|
|
|
current_ctx = ctx;
|
2021-08-02 03:20:19 -07:00
|
|
|
const char *process = env->GetStringUTFChars(ctx->args->nice_name, nullptr);
|
2021-01-12 03:28:00 -08:00
|
|
|
LOGD("hook: %s %s\n", __FUNCTION__, process);
|
2021-01-09 17:41:25 -08:00
|
|
|
|
2021-08-02 03:20:19 -07:00
|
|
|
if (ctx->args->mount_external != 0 /* TODO: Handle MOUNT_EXTERNAL_NONE cases */
|
|
|
|
&& remote_check_hide(ctx->args->uid, process)) {
|
2021-01-12 03:28:00 -08:00
|
|
|
ctx->do_hide = true;
|
|
|
|
LOGI("hook: [%s] should be hidden\n", process);
|
|
|
|
}
|
2021-01-10 01:25:30 -08:00
|
|
|
|
2021-08-02 03:20:19 -07:00
|
|
|
env->ReleaseStringUTFChars(ctx->args->nice_name, process);
|
2021-01-12 03:28:00 -08:00
|
|
|
}
|
2021-01-10 01:25:30 -08:00
|
|
|
|
2021-08-11 22:56:18 -07:00
|
|
|
void nativeSpecializeAppProcess_post(HookContext *ctx, JNIEnv *env, jclass clazz) {
|
2021-08-02 03:20:19 -07:00
|
|
|
current_ctx = nullptr;
|
2021-01-09 17:41:25 -08:00
|
|
|
LOGD("hook: %s\n", __FUNCTION__);
|
2021-01-10 01:25:30 -08:00
|
|
|
|
2021-01-12 03:28:00 -08:00
|
|
|
if (ctx->do_hide)
|
2021-01-09 17:41:25 -08:00
|
|
|
self_unload();
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------
|
|
|
|
|
2021-08-11 22:56:18 -07:00
|
|
|
int sigmask(int how, int signum) {
|
2021-08-11 00:00:21 -07:00
|
|
|
sigset_t set;
|
|
|
|
sigemptyset(&set);
|
|
|
|
sigaddset(&set, signum);
|
|
|
|
return sigprocmask(how, &set, nullptr);
|
|
|
|
}
|
|
|
|
|
2021-08-02 03:20:19 -07:00
|
|
|
// Do our own fork before loading any 3rd party code
|
|
|
|
// First block SIGCHLD, unblock after original fork is done
|
|
|
|
#define PRE_FORK() \
|
|
|
|
current_ctx = ctx; \
|
|
|
|
sigmask(SIG_BLOCK, SIGCHLD); \
|
|
|
|
ctx->pid = old_fork(); \
|
|
|
|
if (ctx->pid != 0) \
|
2021-01-12 03:28:00 -08:00
|
|
|
return;
|
|
|
|
|
2021-08-02 03:20:19 -07:00
|
|
|
// Unblock SIGCHLD in case the original method didn't
|
|
|
|
#define POST_FORK() \
|
|
|
|
current_ctx = nullptr; \
|
|
|
|
sigmask(SIG_UNBLOCK, SIGCHLD); \
|
|
|
|
if (ctx->pid != 0)\
|
|
|
|
return;
|
|
|
|
|
2021-08-11 22:56:18 -07:00
|
|
|
void nativeForkAndSpecialize_pre(HookContext *ctx, JNIEnv *env, jclass clazz) {
|
2021-08-02 03:20:19 -07:00
|
|
|
PRE_FORK();
|
|
|
|
nativeSpecializeAppProcess_pre(ctx, env, clazz);
|
2021-01-09 17:41:25 -08:00
|
|
|
}
|
|
|
|
|
2021-08-11 22:56:18 -07:00
|
|
|
void nativeForkAndSpecialize_post(HookContext *ctx, JNIEnv *env, jclass clazz) {
|
2021-08-02 03:20:19 -07:00
|
|
|
POST_FORK();
|
2021-01-12 03:28:00 -08:00
|
|
|
nativeSpecializeAppProcess_post(ctx, env, clazz);
|
2021-01-09 17:41:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------
|
|
|
|
|
2021-08-11 22:56:18 -07:00
|
|
|
void nativeForkSystemServer_pre(HookContext *ctx, JNIEnv *env, jclass clazz) {
|
2021-08-11 00:00:21 -07:00
|
|
|
if (env->functions == new_functions) {
|
|
|
|
// Restore JNIEnv
|
|
|
|
env->functions = old_functions;
|
|
|
|
if (gClassRef) {
|
|
|
|
env->DeleteGlobalRef(gClassRef);
|
|
|
|
gClassRef = nullptr;
|
|
|
|
class_getName = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-02 03:20:19 -07:00
|
|
|
PRE_FORK();
|
2021-01-09 17:41:25 -08:00
|
|
|
LOGD("hook: %s\n", __FUNCTION__);
|
|
|
|
}
|
|
|
|
|
2021-08-11 22:56:18 -07:00
|
|
|
void nativeForkSystemServer_post(HookContext *ctx, JNIEnv *env, jclass clazz) {
|
2021-08-02 03:20:19 -07:00
|
|
|
POST_FORK();
|
2021-01-09 17:41:25 -08:00
|
|
|
LOGD("hook: %s\n", __FUNCTION__);
|
|
|
|
}
|
|
|
|
|
2021-08-02 03:20:19 -07:00
|
|
|
#undef PRE_FORK
|
|
|
|
#undef POST_FORK
|
|
|
|
|
2021-01-09 20:39:59 -08:00
|
|
|
// -----------------------------------------------------------------
|
|
|
|
|
2021-08-11 22:56:18 -07:00
|
|
|
bool hook_refresh() {
|
2021-01-08 00:53:24 -08:00
|
|
|
if (xhook_refresh(0) == 0) {
|
|
|
|
xhook_clear();
|
|
|
|
LOGI("hook: xhook success\n");
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
LOGE("hook: xhook failed\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-11 22:56:18 -07:00
|
|
|
int hook_register(const char *path, const char *symbol, void *new_func, void **old_func) {
|
2021-01-08 00:53:24 -08:00
|
|
|
int ret = xhook_register(path, symbol, new_func, old_func);
|
|
|
|
if (ret != 0) {
|
|
|
|
LOGE("hook: Failed to register hook \"%s\"\n", symbol);
|
|
|
|
return ret;
|
|
|
|
}
|
2021-01-10 05:07:17 -08:00
|
|
|
xhook_list->emplace_back(path, symbol, old_func);
|
2021-01-08 00:53:24 -08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-08-11 22:56:18 -07:00
|
|
|
} // namespace
|
|
|
|
|
2021-08-11 00:00:21 -07:00
|
|
|
template<class T>
|
|
|
|
static inline void default_new(T *&p) { p = new T(); }
|
|
|
|
|
|
|
|
#define XHOOK_REGISTER_SYM(PATH_REGEX, SYM, NAME) \
|
|
|
|
hook_register(PATH_REGEX, SYM, (void*) new_##NAME, (void **) &old_##NAME)
|
|
|
|
|
2021-01-08 00:53:24 -08:00
|
|
|
#define XHOOK_REGISTER(PATH_REGEX, NAME) \
|
2021-08-11 00:00:21 -07:00
|
|
|
XHOOK_REGISTER_SYM(PATH_REGEX, #NAME, NAME)
|
|
|
|
|
|
|
|
#define ANDROID_RUNTIME ".*/libandroid_runtime.so$"
|
|
|
|
#define APP_PROCESS "^/system/bin/app_process.*"
|
2021-01-08 00:53:24 -08:00
|
|
|
|
|
|
|
void hook_functions() {
|
|
|
|
#ifdef MAGISK_DEBUG
|
|
|
|
xhook_enable_debug(1);
|
|
|
|
xhook_enable_sigsegv_protection(0);
|
|
|
|
#endif
|
2021-08-11 00:00:21 -07:00
|
|
|
default_new(xhook_list);
|
|
|
|
default_new(jni_hook_list);
|
|
|
|
default_new(jni_method_map);
|
2021-01-10 05:07:17 -08:00
|
|
|
|
2021-08-11 00:00:21 -07:00
|
|
|
XHOOK_REGISTER(ANDROID_RUNTIME, fork);
|
|
|
|
XHOOK_REGISTER(ANDROID_RUNTIME, selinux_android_setcontext);
|
|
|
|
XHOOK_REGISTER(ANDROID_RUNTIME, jniRegisterNativeMethods);
|
2021-01-08 00:53:24 -08:00
|
|
|
hook_refresh();
|
2021-08-11 00:00:21 -07:00
|
|
|
|
|
|
|
// Remove unhooked methods
|
|
|
|
xhook_list->erase(
|
|
|
|
std::remove_if(xhook_list->begin(), xhook_list->end(),
|
|
|
|
[](auto &t) { return *std::get<2>(t) == nullptr;}),
|
|
|
|
xhook_list->end());
|
|
|
|
|
|
|
|
if (old_jniRegisterNativeMethods == nullptr) {
|
|
|
|
LOGD("hook: jniRegisterNativeMethods not used\n");
|
|
|
|
|
|
|
|
// android::AndroidRuntime::setArgv0(const char *, bool)
|
|
|
|
XHOOK_REGISTER_SYM(APP_PROCESS, "_ZN7android14AndroidRuntime8setArgv0EPKcb", setArgv0);
|
|
|
|
hook_refresh();
|
|
|
|
|
|
|
|
// We still need old_jniRegisterNativeMethods as other code uses it
|
|
|
|
// android::AndroidRuntime::registerNativeMethods(_JNIEnv*, const char *, const JNINativeMethod *, int)
|
|
|
|
constexpr char sig[] = "_ZN7android14AndroidRuntime21registerNativeMethodsEP7_JNIEnvPKcPK15JNINativeMethodi";
|
|
|
|
*(void **) &old_jniRegisterNativeMethods = dlsym(RTLD_DEFAULT, sig);
|
|
|
|
}
|
2021-01-08 00:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool unhook_functions() {
|
2021-01-08 05:25:44 -08:00
|
|
|
JNIEnv* env;
|
|
|
|
if (g_jvm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK)
|
|
|
|
return false;
|
|
|
|
|
2021-08-01 14:35:16 -07:00
|
|
|
// Do NOT call any destructors
|
|
|
|
operator delete(jni_method_map);
|
|
|
|
// Directly unmap the whole memory block
|
|
|
|
jni_hook::memory_block::release();
|
|
|
|
|
2021-01-09 17:41:25 -08:00
|
|
|
// Unhook JNI methods
|
2021-08-01 14:35:16 -07:00
|
|
|
if (!jni_hook_list->empty() && old_jniRegisterNativeMethods(env,
|
2021-01-08 05:25:44 -08:00
|
|
|
"com/android/internal/os/Zygote",
|
2021-08-01 14:35:16 -07:00
|
|
|
jni_hook_list->data(), jni_hook_list->size()) != 0) {
|
2021-01-10 05:07:17 -08:00
|
|
|
LOGE("hook: Failed to register JNI hook\n");
|
2021-01-08 05:25:44 -08:00
|
|
|
return false;
|
|
|
|
}
|
2021-08-01 14:35:16 -07:00
|
|
|
delete jni_hook_list;
|
2021-01-08 05:25:44 -08:00
|
|
|
|
2021-01-09 17:41:25 -08:00
|
|
|
// Unhook xhook
|
2021-01-10 05:07:17 -08:00
|
|
|
for (auto &[path, sym, old_func] : *xhook_list) {
|
2021-01-08 00:53:24 -08:00
|
|
|
if (xhook_register(path, sym, *old_func, nullptr) != 0) {
|
2021-01-08 05:25:44 -08:00
|
|
|
LOGE("hook: Failed to register hook \"%s\"\n", sym);
|
2021-01-08 00:53:24 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2021-01-10 05:07:17 -08:00
|
|
|
delete xhook_list;
|
2021-01-08 00:53:24 -08:00
|
|
|
return hook_refresh();
|
|
|
|
}
|