Skip to content

Commit

Permalink
add dlopen as fallback to xdl_open (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
lico-n authored Nov 24, 2023
1 parent 170e5ea commit 510e927
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 20 deletions.
4 changes: 2 additions & 2 deletions module.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ext {
moduleName = "ZygiskFrida"
moduleAuthor = "lico-n"
moduleDescription = "Injects frida gadget via zygisk."
moduleVersion = "v1.5.0"
moduleVersion = "v1.6.0"
moduleVersionCode = 7

// Riru
Expand All @@ -13,5 +13,5 @@ ext {
moduleMaxRiruApiVersion = 26

// Frida
fridaVersion = "16.1.4"
fridaVersion = "16.1.7"
}
18 changes: 8 additions & 10 deletions module/src/jni/child_gating.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <vector>

#include "config.h"
#include "inject.h"

static std::string child_gating_mode; // NOLINT
static std::vector<std::string> injected_libraries;
Expand All @@ -31,30 +32,27 @@ pid_t fork_replacement() {

child_pid = getpid();

auto logContext = "[child_gating][pid " + std::to_string(child_pid) + "] ";

if (child_gating_mode == "kill") {
LOGI("[child_gating][pid %d] killing child process", child_pid);
LOGI("%skilling child process", logContext.c_str());
exit(0);
}

if (child_gating_mode == "freeze") {
LOGI("[child_gating][pid %d] freezing child process", child_pid);
LOGI("%sfreezing child process", logContext.c_str());
std::promise<void>().get_future().wait();
return 0;
}

if (child_gating_mode != "inject") {
LOGI("[child_gating][pid %d] unknown child_gating_mode %s", child_pid, child_gating_mode.c_str());
LOGI("%sunknown child_gating_mode %s", logContext.c_str(), child_gating_mode.c_str());
return 0;
}

for (auto &lib_path : injected_libraries) {
LOGI("[child_gating][pid %d] Injecting %s", child_pid, lib_path.c_str());
auto *handle = xdl_open(lib_path.c_str(), XDL_TRY_FORCE_LOAD);
if (handle) {
LOGI("[child_gating][pid %d] Injected %s with handle %p", child_pid, lib_path.c_str(), handle);
} else {
LOGE("[child_gating][pid %d]Failed to inject %s : %s", child_pid, lib_path.c_str(), dlerror());
}
LOGI("%sInjecting %s", logContext.c_str(), lib_path.c_str());
inject_lib(lib_path, logContext);
}

return 0;
Expand Down
32 changes: 24 additions & 8 deletions module/src/jni/inject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,28 @@ static void delay_start_up(uint64_t start_up_delay_ms) {
}
}

static void inject_libs(target_config const& cfg) {
void inject_lib(std::string const &lib_path, std::string const &logContext) {
auto *handle = xdl_open(lib_path.c_str(), XDL_TRY_FORCE_LOAD);
if (handle) {
LOGI("%sInjected %s with handle %p", logContext.c_str(), lib_path.c_str(), handle);
return;
}

auto xdl_err = dlerror();

handle = dlopen(lib_path.c_str(), RTLD_NOW);
if (handle) {
LOGI("%sInjected %s with handle %p (dlopen)", logContext.c_str(), lib_path.c_str(), handle);
return;
}

auto dl_err = dlerror();

LOGE("%sFailed to inject %s (xdl_open): %s", logContext.c_str(), lib_path.c_str(), xdl_err);
LOGE("%sFailed to inject %s (dlopen): %s", logContext.c_str(), lib_path.c_str(), dl_err);
}

static void inject_libs(target_config const &cfg) {
// We need to wait for process initialization to complete.
// Loading the gadget before that will freeze the process
// before the init has completed. This make the process
Expand All @@ -75,14 +96,9 @@ static void inject_libs(target_config const& cfg) {

delay_start_up(cfg.start_up_delay_ms);

for (auto & lib_path : cfg.injected_libraries) {
for (auto &lib_path : cfg.injected_libraries) {
LOGI("Injecting %s", lib_path.c_str());
auto *handle = xdl_open(lib_path.c_str(), XDL_TRY_FORCE_LOAD);
if (handle) {
LOGI("Injected %s with handle %p", lib_path.c_str(), handle);
} else {
LOGE("Failed to inject %s : %s", lib_path.c_str(), dlerror());
}
inject_lib(lib_path, "");
}
}

Expand Down
1 change: 1 addition & 0 deletions module/src/jni/inject.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <string>

void inject_lib(std::string const& lib_path, std::string const& logContext);
bool check_and_inject(std::string const& app_name);

#endif // ZYGISKFRIDA_INJECT_H

0 comments on commit 510e927

Please sign in to comment.