mirror of
https://github.com/xiaozhou233/ptrace-inject-cpp.git
synced 2026-12-05 13:05:54 +08:00
Add a C ABI library (libptrace_inject), a thin CLI (ptrace-inject), and C/C++ examples that inject a .so into a running x86-64 Linux process. Injection flow: - stop every thread with PTRACE_ATTACH, re-scanning /proc/<pid>/task for threads created while attaching - patch a 6-byte shellcode (nop; nop; call r9; int3) into a private, file-backed executable mapping, then call target malloc/dlopen/free via PTRACE_SETREGS + PTRACE_CONT and read the result from rax - write the library path with /proc/<pid>/mem, free it, restore the original bytes and registers, then PTRACE_DETACH all threads Safety: - validate before modifying anything: library path, libc/libdl build identity via device:inode, relocated symbol addresses are executable, patch site avoids stopped threads' RIPs, stack pointer is writable - only accept the SIGTRAP whose RIP is shellcode+6 - forward unexpected signals back to the target on detach - best-effort rollback of code and registers on failure - shared library exports only the two public entry points API: ptrace_inject_by_pid() and ptrace_inject_by_name(), with an optional log callback and an error buffer; silent by default. Build with CMake (no install rules); docs in README.md (EN) and README.ZH.md (ZH).
73 lines
2.2 KiB
CMake
73 lines
2.2 KiB
CMake
cmake_minimum_required(VERSION 3.16)
|
|
project(ptrace_inject VERSION 1.0.0 LANGUAGES C CXX)
|
|
|
|
set(CMAKE_CXX_STANDARD 17)
|
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
|
set(CMAKE_CXX_EXTENSIONS OFF)
|
|
|
|
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
|
|
set(CMAKE_BUILD_TYPE Release)
|
|
endif()
|
|
|
|
option(PTRACE_INJECT_BUILD_CLI "Build the command line tool" ON)
|
|
option(PTRACE_INJECT_BUILD_EXAMPLES "Build the examples" ON)
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Library
|
|
# ---------------------------------------------------------------------------
|
|
|
|
add_library(ptrace_inject
|
|
src/library.cpp
|
|
src/log.cpp
|
|
src/util.cpp
|
|
src/maps.cpp
|
|
src/memory.cpp
|
|
src/ptrace_utils.cpp
|
|
src/symbols.cpp
|
|
src/inject.cpp
|
|
src/process_lookup.cpp
|
|
)
|
|
|
|
add_library(ptrace_inject::ptrace_inject ALIAS ptrace_inject)
|
|
|
|
target_include_directories(ptrace_inject
|
|
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include
|
|
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src
|
|
)
|
|
|
|
target_compile_options(ptrace_inject PRIVATE -Wall -Wextra -Wpedantic)
|
|
target_link_libraries(ptrace_inject PRIVATE dl)
|
|
|
|
set_target_properties(ptrace_inject PROPERTIES
|
|
OUTPUT_NAME ptrace_inject
|
|
VERSION ${PROJECT_VERSION}
|
|
SOVERSION ${PROJECT_VERSION_MAJOR}
|
|
CXX_VISIBILITY_PRESET hidden
|
|
VISIBILITY_INLINES_HIDDEN ON
|
|
POSITION_INDEPENDENT_CODE ON
|
|
)
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# CLI and examples
|
|
# ---------------------------------------------------------------------------
|
|
|
|
if(PTRACE_INJECT_BUILD_CLI)
|
|
add_executable(ptrace_inject_cli src/main.cpp)
|
|
set_target_properties(ptrace_inject_cli PROPERTIES OUTPUT_NAME ptrace-inject)
|
|
target_link_libraries(ptrace_inject_cli PRIVATE ptrace_inject)
|
|
endif()
|
|
|
|
if(PTRACE_INJECT_BUILD_EXAMPLES)
|
|
add_executable(example_by_pid examples/inject_by_pid.c)
|
|
set_target_properties(example_by_pid PROPERTIES LINKER_LANGUAGE CXX)
|
|
target_link_libraries(example_by_pid PRIVATE ptrace_inject)
|
|
|
|
add_executable(example_by_name examples/inject_by_name.cpp)
|
|
target_link_libraries(example_by_name PRIVATE ptrace_inject)
|
|
|
|
add_library(sample_library SHARED examples/sample_library.c)
|
|
set_target_properties(sample_library PROPERTIES
|
|
OUTPUT_NAME ptrace_inject_sample
|
|
)
|
|
endif()
|