Skip to content

Commit

Permalink
add attach mode to script
Browse files Browse the repository at this point in the history
  • Loading branch information
StackOverflowExcept1on committed Aug 30, 2024
1 parent 672f029 commit a898b5c
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 13 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion Bootstrapper/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
31 changes: 30 additions & 1 deletion Bootstrapper/src/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#else
#define EXPORT __attribute__((visibility("default")))
#include <dlfcn.h>
#include <cstdio>
#include <thread>
#endif

/// This class helps to manage shared libraries
Expand Down Expand Up @@ -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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion _run.bat
Original file line number Diff line number Diff line change
Expand Up @@ -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"
42 changes: 33 additions & 9 deletions _run.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a898b5c

Please sign in to comment.