mirror of
https://github.com/xiaozhou233/JuiceAgent.git
synced 2026-12-05 13:05:53 +08:00
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
This commit is contained in:
1 parent
416e83f36e
commit
483a12cd3c
4 files changed
+122
-11
No files matched your search
@@ -18,6 +18,11 @@ endif()
|
|||||||
# libraries
|
# libraries
|
||||||
if (WIN32)
|
if (WIN32)
|
||||||
add_subdirectory(${CMAKE_SOURCE_DIR}/includes/ReflectiveDLLInjection)
|
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()
|
endif()
|
||||||
|
|
||||||
# modules
|
# modules
|
||||||
|
|||||||
@@ -2,5 +2,6 @@
|
|||||||
if(WIN32)
|
if(WIN32)
|
||||||
add_executable(injector injector.c)
|
add_executable(injector injector.c)
|
||||||
else()
|
else()
|
||||||
add_executable(injector injector_linux.c)
|
add_executable(injector injector_linux.cpp)
|
||||||
|
target_link_libraries(injector PRIVATE ptrace_inject::ptrace_inject)
|
||||||
endif()
|
endif()
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,115 @@
|
|||||||
|
#include <limits.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <exception>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#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) <library.so>\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<pid_t>(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;
|
||||||
|
}
|
||||||
Reference in new issue
Block a user