From 95ad2d5a324c0173399e5e24b80d22e67856fffe Mon Sep 17 00:00:00 2001 From: GarfieldHan <2652609017@qq.com> Date: Thu, 18 Jul 2024 08:20:39 +0800 Subject: [PATCH] feature: Add customize spoof build vars support Signed-off-by: GarfieldHan <2652609017@qq.com> --- .gitmodules | 3 + module/src/main/cpp/CMakeLists.txt | 2 +- module/src/main/cpp/external/CMakeLists.txt | 17 ++-- module/src/main/cpp/external/glaze | 1 + module/src/main/cpp/zygisk/main.cpp | 94 ++++++++++++++++----- 5 files changed, 88 insertions(+), 29 deletions(-) create mode 160000 module/src/main/cpp/external/glaze diff --git a/.gitmodules b/.gitmodules index a2aeccd..6290cd0 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "module/src/main/cpp/external/LSPlt"] path = module/src/main/cpp/external/LSPlt url = https://github.com/LSPosed/LSPlt +[submodule "module/src/main/cpp/external/glaze"] + path = module/src/main/cpp/external/glaze + url = https://github.com/stephenberry/glaze diff --git a/module/src/main/cpp/CMakeLists.txt b/module/src/main/cpp/CMakeLists.txt index b6f703c..572d282 100644 --- a/module/src/main/cpp/CMakeLists.txt +++ b/module/src/main/cpp/CMakeLists.txt @@ -40,5 +40,5 @@ target_link_libraries(${MODULE_NAME} log binder utils elf_util my_logging) target_compile_options(${MODULE_NAME} PRIVATE -fvisibility=hidden -fvisibility-inlines-hidden) add_library(tszygisk SHARED zygisk/main.cpp) -target_link_libraries(tszygisk log my_logging) +target_link_libraries(tszygisk log my_logging glaze::glaze) target_compile_options(tszygisk PRIVATE -fvisibility=hidden -fvisibility-inlines-hidden) diff --git a/module/src/main/cpp/external/CMakeLists.txt b/module/src/main/cpp/external/CMakeLists.txt index cd62f58..dca30f2 100644 --- a/module/src/main/cpp/external/CMakeLists.txt +++ b/module/src/main/cpp/external/CMakeLists.txt @@ -10,6 +10,8 @@ target_include_directories(lsplt PRIVATE LSPlt/lsplt/src/main/jni) target_link_libraries(lsplt PUBLIC my_logging cxx) # end lsplt +add_subdirectory(glaze) + # cxx set(LIBCXX_SOURCES algorithm.cpp @@ -23,16 +25,16 @@ set(LIBCXX_SOURCES condition_variable_destructor.cpp # debug.cpp exception.cpp - # filesystem/directory_iterator.cpp - # filesystem/int128_builtins.cpp - # filesystem/operations.cpp + filesystem/directory_iterator.cpp + filesystem/int128_builtins.cpp + filesystem/operations.cpp functional.cpp future.cpp hash.cpp - # ios.cpp - # ios.instantiations.cpp - # iostream.cpp - # locale.cpp + ios.cpp + ios.instantiations.cpp + iostream.cpp + locale.cpp memory.cpp mutex.cpp mutex_destructor.cpp @@ -65,7 +67,6 @@ set(LIBCXX_EXPORT_FLAGS -D_LIBCPP_BUILDING_LIBRARY -D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS -D_LIBCXXABI_NO_EXCEPTIONS - -D_LIBCPP_HAS_NO_LOCALIZATION ) set(LIBCXX_FLAGS -fvisibility-global-new-delete-hidden diff --git a/module/src/main/cpp/external/glaze b/module/src/main/cpp/external/glaze new file mode 160000 index 0000000..53b69c4 --- /dev/null +++ b/module/src/main/cpp/external/glaze @@ -0,0 +1 @@ +Subproject commit 53b69c447f98b89f656c0e6bda19833d682bb44b diff --git a/module/src/main/cpp/zygisk/main.cpp b/module/src/main/cpp/zygisk/main.cpp index 22a0e31..0eb812e 100644 --- a/module/src/main/cpp/zygisk/main.cpp +++ b/module/src/main/cpp/zygisk/main.cpp @@ -3,7 +3,9 @@ #include #include #include +#include +#include "glaze/glaze.hpp" #include "logging.hpp" #include "zygisk.hpp" @@ -12,6 +14,21 @@ using zygisk::AppSpecializeArgs; using zygisk::ServerSpecializeArgs; using namespace std::string_view_literals; +struct spoof_config { + std::string manufacturer{"Google"}; + std::string model{"Pixel"}; + std::string fingerprint{"google/sailfish/sailfish:8.1.0/OPM1.171019.011/4448085:user/release-keys"}; + std::string brand{"google"}; + std::string product{"sailfish"}; + std::string device{"sailfish"}; + std::string release{"8.1.0"}; + std::string id{"OPM1.171019.011"}; + std::string incremental{"4448085"}; + std::string security_patch{"2017-12-05"}; + std::string type{"user"}; + std::string tags{"release-keys"}; +}; + class TrickyStore : public zygisk::ModuleBase { public: void onLoad(Api *api, JNIEnv *env) override { @@ -21,15 +38,30 @@ public: void preAppSpecialize(AppSpecializeArgs *args) override { api_->setOption(zygisk::DLCLOSE_MODULE_LIBRARY); - int enabled = 0; + spoof_config spoofConfig{}; { auto fd = api_->connectCompanion(); - if (fd >= 0) { + if (fd >= 0) [[likely]] { + // read enabled read(fd, &enabled, sizeof(enabled)); + if (enabled) { + size_t bufferSize = 0; + std::string buffer; + // read size first + read(fd, &bufferSize, sizeof(bufferSize)); + // resize and receive + buffer.resize(bufferSize); + read(fd, buffer.data(), bufferSize); + // parse + if (glz::read_json(spoofConfig, buffer)) [[unlikely]] { + LOGE("[preAppSpecialize] spoofConfig parse error"); + } + } close(fd); } } + if (!enabled) return; if (args->app_data_dir == nullptr) { return; @@ -47,23 +79,18 @@ public: auto buildClass = env_->FindClass("android/os/Build"); auto buildVersionClass = env_->FindClass("android/os/Build$VERSION"); -#define SET_FIELD(CLAZZ, FIELD, VALUE) ({ \ - auto id = env_->GetStaticFieldID(CLAZZ, FIELD, "Ljava/lang/String;"); \ - env_->SetStaticObjectField(buildClass, id, env_->NewStringUTF(VALUE)); }) - - SET_FIELD(buildClass, "MANUFACTURER", "Google"); - SET_FIELD(buildClass, "MODEL", "Pixel"); - SET_FIELD(buildClass, "FINGERPRINT", - "google/sailfish/sailfish:8.1.0/OPM1.171019.011/4448085:user/release-keys"); - SET_FIELD(buildClass, "BRAND", "google"); - SET_FIELD(buildClass, "PRODUCT", "sailfish"); - SET_FIELD(buildClass, "DEVICE", "sailfish"); - SET_FIELD(buildVersionClass, "RELEASE", "8.1.0"); - SET_FIELD(buildClass, "ID", "OPM1.171019.011"); - SET_FIELD(buildVersionClass, "INCREMENTAL", "4448085"); - SET_FIELD(buildVersionClass, "SECURITY_PATCH", "2017-12-05"); - SET_FIELD(buildClass, "TYPE", "user"); - SET_FIELD(buildClass, "TAGS", "release-keys"); + setField(buildClass, "MANUFACTURER", std::move(spoofConfig.manufacturer)); + setField(buildClass, "MODEL", std::move(spoofConfig.model)); + setField(buildClass, "FINGERPRINT", std::move(spoofConfig.fingerprint)); + setField(buildClass, "BRAND", std::move(spoofConfig.brand)); + setField(buildClass, "PRODUCT", std::move(spoofConfig.product)); + setField(buildClass, "DEVICE", std::move(spoofConfig.device)); + setField(buildVersionClass, "RELEASE", std::move(spoofConfig.release)); + setField(buildClass, "ID", std::move(spoofConfig.id)); + setField(buildVersionClass, "INCREMENTAL", std::move(spoofConfig.incremental)); + setField(buildVersionClass, "SECURITY_PATCH", std::move(spoofConfig.security_patch)); + setField(buildClass, "TYPE", std::move(spoofConfig.type)); + setField(buildClass, "TAGS", std::move(spoofConfig.tags)); } env_->ReleaseStringUTFChars(args->nice_name, nice_name); @@ -77,14 +104,41 @@ public: private: Api *api_; JNIEnv *env_; + + inline void setField(jclass clazz, const char* field, std::string&& value) { + auto id = env_->GetStaticFieldID(clazz, field, "Ljava/lang/String;"); + env_->SetStaticObjectField(clazz, id, env_->NewStringUTF(value.c_str())); + } }; static void companion_handler(int fd) { int enabled = access("/data/adb/tricky_store/spoof_build_vars", F_OK) == 0; write(fd, &enabled, sizeof(enabled)); + + if (!enabled) { + return; + } + + spoof_config spoofConfig{}; + auto ec = glz::read_file_json(spoofConfig, "/data/adb/tricky_store/spoof_build_vars"sv, std::string{}); + if (ec) [[unlikely]] { + LOGW("[companion_handler] Failed to parse spoof_build_vars, writing and using default spoof config..."); + ec = glz::write_file_json(spoofConfig, "/data/adb/tricky_store/spoof_build_vars"sv, std::string{}); + if (ec) [[unlikely]] { + LOGW("[companion_handler] Failed to write spoof_build_vars"); + } + LOGI("[write_spoof_configs@companion_handler] write done!"); + } + + std::string buffer = glz::write_json(spoofConfig).value_or(""); + size_t bufferSize = buffer.size(); + // Send buffer size first + write(fd, &bufferSize, sizeof(bufferSize)); + // client resize string stl and receive buffer + write(fd, buffer.data(), bufferSize); } // Register our module class and the companion handler function REGISTER_ZYGISK_MODULE(TrickyStore) -REGISTER_ZYGISK_COMPANION(companion_handler) +REGISTER_ZYGISK_COMPANION(companion_handler) \ No newline at end of file