From 75e300532ef8122a936682670cff4651b21cc66c Mon Sep 17 00:00:00 2001 From: JingMatrix Date: Fri, 30 Aug 2024 14:44:18 +0200 Subject: [PATCH] Use Dobby for native_api LSPlt might not be capable for general native_api hooks Fix Dobby module repo Url chiteroman has deleted his repo, so I should maintain my own fork. --- .gitmodules | 5 ++++- README.md | 4 ++-- core/src/main/jni/CMakeLists.txt | 2 +- core/src/main/jni/src/native_api.cpp | 7 +++---- core/src/main/jni/src/native_api.h | 25 +++++++++++++++++++++++++ external/CMakeLists.txt | 3 ++- external/dobby | 1 + 7 files changed, 38 insertions(+), 9 deletions(-) create mode 160000 external/dobby diff --git a/.gitmodules b/.gitmodules index c5eacef1..1fe76dfd 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,9 +1,12 @@ [submodule "external/lsplant"] path = external/lsplant url = https://github.com/JingMatrix/LSPlant.git +[submodule "external/dobby"] + path = external/dobby + url = https://github.com/JingMatrix/Dobby.git [submodule "external/lsplt"] path = external/lsplt - url = https://github.com/LSPosed/LSPlt + url = https://github.com/LSPosed/LSPlt.git [submodule "external/fmt"] path = external/fmt url = https://github.com/fmtlib/fmt.git diff --git a/README.md b/README.md index 4731d132..d2334337 100644 --- a/README.md +++ b/README.md @@ -58,10 +58,10 @@ You can contribute translation [here](https://crowdin.com/project/lsposed_jingma - [Magisk](https://github.com/topjohnwu/Magisk/): makes all these possible - [Riru](https://github.com/RikkaApps/Riru): provides a way to inject code into zygote process - [XposedBridge](https://github.com/rovo89/XposedBridge): the OG Xposed framework APIs -- [LSPlt](https://github.com/LSPosed/LSPlt): used for inline hooking +- [LSPlt](https://github.com/LSPosed/LSPlt): used for (Android 15) `libart` inline hooking +- [Dobby](https://github.com/JingMatrix/Dobby): used for fallback and `native_api` inline hooking - [LSPlant](https://github.com/JingMatrix/LSPlant): the core ART hooking framework - [EdXposed](https://github.com/ElderDrivers/EdXposed): fork source -- ~[Dobby](https://github.com/chiteroman/Dobby): used for inline hooking~ - ~[SandHook](https://github.com/ganyao114/SandHook/): ART hooking framework for SandHook variant~ - ~[YAHFA](https://github.com/rk700/YAHFA): previous ART hooking framework~ - ~[dexmaker](https://github.com/linkedin/dexmaker) and [dalvikdx](https://github.com/JakeWharton/dalvik-dx): to dynamically generate YAHFA hooker classes~ diff --git a/core/src/main/jni/CMakeLists.txt b/core/src/main/jni/CMakeLists.txt index 62e5b150..263cf6a6 100644 --- a/core/src/main/jni/CMakeLists.txt +++ b/core/src/main/jni/CMakeLists.txt @@ -12,5 +12,5 @@ add_library(${PROJECT_NAME} STATIC ${SRC_LIST}) target_include_directories(${PROJECT_NAME} PUBLIC include) target_include_directories(${PROJECT_NAME} PRIVATE src) -target_link_libraries(${PROJECT_NAME} PUBLIC lsplt_static lsplant_static log fmt-header-only) +target_link_libraries(${PROJECT_NAME} PUBLIC dobby_static lsplt_static lsplant_static log fmt-header-only) target_link_libraries(${PROJECT_NAME} PRIVATE dex_builder_static) diff --git a/core/src/main/jni/src/native_api.cpp b/core/src/main/jni/src/native_api.cpp index ca148045..00bd6f6f 100644 --- a/core/src/main/jni/src/native_api.cpp +++ b/core/src/main/jni/src/native_api.cpp @@ -28,7 +28,6 @@ #include #include #include -#include "native_util.h" #include "elf_util.h" @@ -59,8 +58,8 @@ namespace lspd { const auto[entries] = []() { auto *entries = new(protected_page.get()) NativeAPIEntries{ .version = 2, - .hookFunc = &HookFunction, - .unhookFunc = &UnhookFunction, + .hookFunc = &DobbyHookFunction, + .unhookFunc = &DobbyUnhookFunction, }; mprotect(protected_page.get(), 4096, PROT_READ); @@ -72,7 +71,7 @@ namespace lspd { return InstallNativeAPI({ .inline_hooker = [](auto t, auto r) { void* bk = nullptr; - return HookFunction(t, r, &bk) == 0 ? bk : nullptr; + return DobbyHookFunction(t, r, &bk) == 0 ? bk : nullptr; }, }); }(); diff --git a/core/src/main/jni/src/native_api.h b/core/src/main/jni/src/native_api.h index 8ba4ce1a..41b65179 100644 --- a/core/src/main/jni/src/native_api.h +++ b/core/src/main/jni/src/native_api.h @@ -26,8 +26,11 @@ #define LSPOSED_NATIVE_API_H #include +#include #include +#include +#include "config.h" #include "utils/hook_helper.hpp" typedef int (*HookFunType)(void *func, void *replace, void **backup); @@ -48,6 +51,28 @@ namespace lspd { bool InstallNativeAPI(const lsplant::HookHandler& handler); void RegisterNativeLib(const std::string &library_name); + + inline int DobbyHookFunction(void *original, void *replace, void **backup) { + if constexpr (isDebug) { + Dl_info info; + if (dladdr(original, &info)) + LOGD("Dobby hooking {} ({}) from {} ({})", + info.dli_sname ? info.dli_sname : "(unknown symbol)", info.dli_saddr, + info.dli_fname ? info.dli_fname : "(unknown file)", info.dli_fbase); + } + return DobbyHook(original, reinterpret_cast(replace), reinterpret_cast(backup)); + } + + inline int DobbyUnhookFunction(void *original) { + if constexpr (isDebug) { + Dl_info info; + if (dladdr(original, &info)) + LOGD("Dobby unhooking {} ({}) from {} ({})", + info.dli_sname ? info.dli_sname : "(unknown symbol)", info.dli_saddr, + info.dli_fname ? info.dli_fname : "(unknown file)", info.dli_fbase); + } + return DobbyDestroy(original); + } } #endif //LSPOSED_NATIVE_API_H diff --git a/external/CMakeLists.txt b/external/CMakeLists.txt index 52b67d1e..439c9581 100644 --- a/external/CMakeLists.txt +++ b/external/CMakeLists.txt @@ -104,7 +104,8 @@ link_libraries(cxx) OPTION(LSPLANT_BUILD_SHARED OFF) OPTION(LSPLT_BUILD_SHARED OFF) +add_subdirectory(dobby) +add_subdirectory(fmt) add_subdirectory(lsplant/lsplant/src/main/jni) add_subdirectory(lsplt/lsplt/src/main/jni) -add_subdirectory(fmt) target_compile_definitions(fmt-header-only INTERFACE FMT_STATIC_THOUSANDS_SEPARATOR=1 FMT_USE_FLOAT=0 FMT_USE_DOUBLE=0 FMT_USE_LONG_DOUBLE=0) diff --git a/external/dobby b/external/dobby new file mode 160000 index 00000000..389938ae --- /dev/null +++ b/external/dobby @@ -0,0 +1 @@ +Subproject commit 389938ae5e7f2fbc45fc62f94a20192a29d37435