From 483a12cd3caf2956e8abdc5e5d56b9c2c6491a8e Mon Sep 17 00:00:00 2001 From: xiaozhou233 Date: Tue, 20 Oct 2026 13:35:59 +0800 Subject: [PATCH] Feat: implement the Linux injector with ptrace-inject-cpp - cmake: build the ptrace-inject-cpp submodule on non-Windows with its CLI and examples disabled, and link ptrace_inject::ptrace_inject into the injector - injector: replace the placeholder injector_linux.c with injector_linux.cpp, a CLI that injects a shared library into -p PID or -n NAME and forwards the library's log output to stderr --- CMakeLists.txt | 5 ++ src/injector/CMakeLists.txt | 3 +- src/injector/injector_linux.c | 10 --- src/injector/injector_linux.cpp | 115 ++++++++++++++++++++++++++++++++ 4 files changed, 122 insertions(+), 11 deletions(-) delete mode 100644 src/injector/injector_linux.c create mode 100644 src/injector/injector_linux.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b5305aa..36635ca 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,6 +18,11 @@ endif() # libraries if (WIN32) add_subdirectory(${CMAKE_SOURCE_DIR}/includes/ReflectiveDLLInjection) +else() + set(PTRACE_INJECT_BUILD_CLI OFF CACHE BOOL "" FORCE) + set(PTRACE_INJECT_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) + + add_subdirectory(ptrace-inject-cpp) endif() # modules diff --git a/src/injector/CMakeLists.txt b/src/injector/CMakeLists.txt index e5c2c6d..d5fde87 100644 --- a/src/injector/CMakeLists.txt +++ b/src/injector/CMakeLists.txt @@ -2,5 +2,6 @@ if(WIN32) add_executable(injector injector.c) else() - add_executable(injector injector_linux.c) + add_executable(injector injector_linux.cpp) + target_link_libraries(injector PRIVATE ptrace_inject::ptrace_inject) endif() diff --git a/src/injector/injector_linux.c b/src/injector/injector_linux.c deleted file mode 100644 index 0b9be5f..0000000 --- a/src/injector/injector_linux.c +++ /dev/null @@ -1,10 +0,0 @@ -#include - -int main(int argc, char *argv[]) { - // TODO: Implement Linux injector - // Due to certain reasons, the Injector for Linux is not open source. - // Please wait a moment for the next update. - printf("Due to certain reasons, the Injector for Linux is not open source. Please wait a moment for the next update.\n"); - - return 0; -} \ No newline at end of file diff --git a/src/injector/injector_linux.cpp b/src/injector/injector_linux.cpp new file mode 100644 index 0000000..05fa71c --- /dev/null +++ b/src/injector/injector_linux.cpp @@ -0,0 +1,115 @@ +#include +#include + +#include +#include +#include +#include +#include + +#include "ptrace_inject.h" + +static void print_log(int level, const char* message, void* /*user_data*/) { + const char* prefix = "[*]"; + switch (level) { + case PTRACE_INJECT_LOG_DEBUG: + prefix = "[debug]"; + break; + case PTRACE_INJECT_LOG_INFO: + prefix = "[*]"; + break; + case PTRACE_INJECT_LOG_WARN: + prefix = "[!]"; + break; + case PTRACE_INJECT_LOG_ERROR: + prefix = "[error]"; + break; + default: + break; + } + std::fprintf(stderr, "%s %s\n", prefix, message); +} + +static void usage(const char* program) { + std::fprintf(stderr, + "Usage: %s (-p PID | -n NAME) \n" + "\n" + "Inject a shared library into a running x86-64 Linux process.\n" + "\n" + " -p PID attach to this PID\n" + " -n NAME attach to the first process whose executable name\n" + " or path matches NAME\n" + " -h show this help\n" + "\n" + "Requires the same uid and CAP_SYS_PTRACE, or a suitable\n" + "kernel.yama.ptrace_scope setting.\n" + "\n" + "Example: %s -p 123456 ./libloader.so\n" + "\n" + "Default runtime directory: ${HOME}/.juiceagent/ \n" + "See README for more information.\n", + program, program); +} + +int main(int argc, char** argv) { + pid_t pid = -1; + std::string name; + + int option; + while ((option = getopt(argc, argv, "p:n:h")) != -1) { + switch (option) { + case 'p': { + try { + const long value = std::stol(optarg); + if (value <= 0 || value > INT_MAX) { + throw std::out_of_range("pid"); + } + pid = static_cast(value); + } catch (const std::exception&) { + std::fprintf(stderr, "error: invalid pid '%s'\n", optarg); + return 1; + } + break; + } + case 'n': + name = optarg; + break; + case 'h': + usage(argv[0]); + return 0; + default: + usage(argv[0]); + return 1; + } + } + + if (optind + 1 != argc) { + usage(argv[0]); + return 1; + } + const char* library = argv[optind]; + + if ((pid == -1) == name.empty()) { + std::fprintf(stderr, "error: specify exactly one of -p PID or -n NAME\n"); + usage(argv[0]); + return 1; + } + + ptrace_inject_options options{}; + options.log_fn = print_log; + + char error[512] = {}; + int result; + if (!name.empty()) { + result = ptrace_inject_by_name(name.c_str(), library, &options, nullptr, + error, sizeof(error)); + } else { + result = ptrace_inject_by_pid(pid, library, &options, error, sizeof(error)); + } + + if (result != PTRACE_INJECT_OK) { + std::fprintf(stderr, "error: %s\n", error); + return 1; + } + return 0; +}