From a898b5c5b58762e88973653c588fac6dd16a9f92 Mon Sep 17 00:00:00 2001 From: StackOverflowExcept1on <109800286+StackOverflowExcept1on@users.noreply.github.com> Date: Fri, 30 Aug 2024 18:20:11 +0300 Subject: [PATCH] add attach mode to script --- .github/workflows/ci.yml | 5 ++++- Bootstrapper/CMakeLists.txt | 2 +- Bootstrapper/src/library.cpp | 31 +++++++++++++++++++++++++- README.md | 5 +++++ _run.bat | 3 ++- _run.sh | 42 ++++++++++++++++++++++++++++-------- 6 files changed, 75 insertions(+), 13 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a39ed0e..25c1560 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,9 +18,12 @@ jobs: - name: Build project run: ./_build.sh - - name: Run project + - name: Run project without root run: ./_run.sh + - name: Run project with root + run: ./_run.sh -a + windows-build: runs-on: windows-latest diff --git a/Bootstrapper/CMakeLists.txt b/Bootstrapper/CMakeLists.txt index b748e48..5a1992e 100644 --- a/Bootstrapper/CMakeLists.txt +++ b/Bootstrapper/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.20) project(Bootstrapper) -set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD_REQUIRED ON) add_library(${PROJECT_NAME} SHARED src/library.cpp) diff --git a/Bootstrapper/src/library.cpp b/Bootstrapper/src/library.cpp index 5896653..46fc4c4 100644 --- a/Bootstrapper/src/library.cpp +++ b/Bootstrapper/src/library.cpp @@ -4,7 +4,7 @@ #else #define EXPORT __attribute__((visibility("default"))) #include -#include +#include #endif /// This class helps to manage shared libraries @@ -113,3 +113,32 @@ extern "C" EXPORT InitializeResult bootstrapper_load_assembly( return InitializeResult::Success; } + +#ifndef _WIN32 +std::string getEnvVar(const char *name) { + auto val = std::getenv(name); + return val == nullptr ? std::string() : std::string(val); +} + +__attribute__((constructor)) +void initialize_library() { + auto runtime_config_path = getEnvVar("RUNTIME_CONFIG_PATH"); + auto assembly_path = getEnvVar("ASSEMBLY_PATH"); + auto type_name = getEnvVar("TYPE_NAME"); + auto method_name = getEnvVar("METHOD_NAME"); + + if (!runtime_config_path.empty() && !assembly_path.empty() && !type_name.empty() && !method_name.empty()) { + std::thread thread([=] { + sleep(1); + auto ret = bootstrapper_load_assembly( + runtime_config_path.c_str(), + assembly_path.c_str(), + type_name.c_str(), + method_name.c_str() + ); + printf("[+] api.inject() => %d\n", (uint32_t) ret); + }); + thread.detach(); + } +} +#endif diff --git a/README.md b/README.md index 6602c72..b9b4c5e 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,11 @@ It will build This script should produce output like the GIF above - `_run.sh` on Linux + + > [!NOTE] + > If you want to attach to an existing process on Linux, this requires root privileges. In this case, use + `_run.sh -a` (attach). + - `_run.bat` on Windows ### Internal documentation diff --git a/_run.bat b/_run.bat index b1a022f..a610219 100644 --- a/_run.bat +++ b/_run.bat @@ -4,4 +4,5 @@ DemoApplication.exe ^ Bootstrapper\build\Release\Bootstrapper.dll ^ RuntimePatcher\dist\RuntimePatcher.runtimeconfig.json ^ RuntimePatcher\dist\RuntimePatcher.dll ^ -"RuntimePatcher.Main, RuntimePatcher" "InitializePatches" +"RuntimePatcher.Main, RuntimePatcher" ^ +"InitializePatches" diff --git a/_run.sh b/_run.sh index d260a66..5e68713 100755 --- a/_run.sh +++ b/_run.sh @@ -1,11 +1,35 @@ #!/usr/bin/env bash -set -ex +set -e -sudo sysctl kernel.yama.ptrace_scope=0 -./DemoApplication/dist/DemoApplication & -npm start -- inject \ -DemoApplication \ -Bootstrapper/build/libBootstrapper.so \ -RuntimePatcher/dist/RuntimePatcher.runtimeconfig.json \ -RuntimePatcher/dist/RuntimePatcher.dll \ -"RuntimePatcher.Main, RuntimePatcher" "InitializePatches" +while getopts "a" OPTION 2> /dev/null; do + case ${OPTION} in + a) + DO_ATTACH="yes" + ;; + \?) + break + ;; + esac +done + +if [ "$DO_ATTACH" == "yes" ]; then + set -m + sudo sysctl kernel.yama.ptrace_scope=0 + + ./DemoApplication/dist/DemoApplication & + npm start -- inject \ + DemoApplication \ + Bootstrapper/build/libBootstrapper.so \ + RuntimePatcher/dist/RuntimePatcher.runtimeconfig.json \ + RuntimePatcher/dist/RuntimePatcher.dll \ + "RuntimePatcher.Main, RuntimePatcher" \ + "InitializePatches" + fg %1 +else + LD_PRELOAD=./Bootstrapper/build/libBootstrapper.so \ + RUNTIME_CONFIG_PATH="$(pwd)/RuntimePatcher/dist/RuntimePatcher.runtimeconfig.json" \ + ASSEMBLY_PATH="$(pwd)/RuntimePatcher/dist/RuntimePatcher.dll" \ + TYPE_NAME="RuntimePatcher.Main, RuntimePatcher" \ + METHOD_NAME="InitializePatches" \ + ./DemoApplication/dist/DemoApplication +fi