Skip to content

Commit

Permalink
Make code more robust.
Browse files Browse the repository at this point in the history
Added Injector and Fixed crashes on Linux
Use same injector on all platforms.
  • Loading branch information
Brandon-T committed Nov 5, 2024
1 parent d067781 commit d8367c3
Show file tree
Hide file tree
Showing 34 changed files with 7,181 additions and 76 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ IF(WIN32)
set(EXTRA_LIBRARIES
user32
opengl32
gdi32)
gdi32
dbghelp) # Needed for ThirdParty/kubo/injector
ELSEIF(APPLE)
find_library(FOUNDATION Foundation)
find_library(COCOA Cocoa)
Expand Down
2 changes: 0 additions & 2 deletions RemoteInput/Injection/Injector.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
#ifndef REMOTEINPUT_INJECTOR_HXX
#define REMOTEINPUT_INJECTOR_HXX

#include <utility>
#include <cstdint>
#include <string>
#include <sys/types.h>

class Injector
{
Expand Down
1 change: 1 addition & 0 deletions RemoteInput/Injection/Injector_Darwin.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#if defined(__APPLE__)
#include <dlfcn.h>
#include <sys/sysctl.h>
#include <sys/types.h>
#include <mach/mach.h>
#include <mach/mach_vm.h>
#include <mach-o/loader.h>
Expand Down
33 changes: 31 additions & 2 deletions RemoteInput/Platform/Platform_Darwin.mm
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
#endif

#include <Thirdparty/Hook.hxx>
#include <Injection/Injector.hxx>
#if defined(CUSTOM_INJECTOR)
#include "Injection/Injector.hxx"
#else
#include "Thirdparty/Injector.hxx"
#endif
#include <signal.h>
#include <libproc.h>
#include <sys/syscall.h>
Expand Down Expand Up @@ -123,10 +127,35 @@ bool IsThreadAlive(std::int32_t tid) noexcept
std::string path = std::string(PATH_MAX, '\0');
if (realpath(info.dli_fname, &path[0]))
{
if (Injector::Inject(info.dli_fname, pid, nullptr))
#if defined(CUSTOM_INJECTOR)
if (Injector::Inject(path.c_str(), pid, nullptr))
{
return pid;
}
#else
extern std::vector<std::unique_ptr<Injector>> injectors;

for (auto& injector : injectors)
{
if (injector && injector->get_pid() == pid)
{
if (injector->is_injected())
{
return pid;
}

return injector->Inject(path.c_str()) ? pid : -1;
}
}

std::unique_ptr<Injector> injector = std::make_unique<Injector>(pid);
if (injector)
{
bool result = injector->Inject(path.c_str());
injectors.push_back(std::move(injector));
return result ? pid : -1;
}
#endif
}
}
return -1;
Expand Down
84 changes: 35 additions & 49 deletions RemoteInput/Platform/Platform_Linux.cxx
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#include "Platform.hxx"

#if defined(__linux__)
#if defined(CUSTOM_INJECTOR)
#include "Injection/Injector.hxx"
#else
#include "Thirdparty/Injector.hxx"
#endif

#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>
Expand Down Expand Up @@ -197,10 +202,35 @@ std::int32_t InjectProcess(std::int32_t pid) noexcept
std::string path = std::string(PATH_MAX, '\0');
if (realpath(info.dli_fname, &path[0]))
{
if (Injector::Inject(info.dli_fname, pid, nullptr))
#if defined(CUSTOM_INJECTOR)
if (Injector::Inject(path.c_str(), pid, nullptr))
{
return pid;
}
#else
extern std::vector<std::unique_ptr<Injector>> injectors;

for (auto& injector : injectors)
{
if (injector && injector->get_pid() == pid)
{
if (injector->is_injected())
{
return pid;
}

return injector->Inject(path.c_str()) ? pid : -1;
}
}

std::unique_ptr<Injector> injector = std::make_unique<Injector>(pid);
if (injector)
{
bool result = injector->Inject(path.c_str());
injectors.push_back(std::move(injector));
return result ? pid : -1;
}
#endif
}
}
return -1;
Expand Down Expand Up @@ -455,8 +485,8 @@ void* GetModuleHandle(const char* module_name) noexcept
}
}
return 0;
}, reinterpret_cast<void*>(&module_info));
return module_info.result ?: dlopen(module_name, RTLD_NOLOAD);
}, &module_info);
return module_info.result ? module_info.result : dlopen(module_name, RTLD_NOLOAD);
}
#endif

Expand Down Expand Up @@ -558,50 +588,6 @@ std::unique_ptr<Reflection> GetNativeReflector() noexcept
{
std::unique_ptr<Reflection> reflection;
bool hasReflection = TimeOut(20, [&]{
jclass cls = env->FindClass("java/awt/Frame");
if (!cls)
{
return false;
}

jmethodID method = env->GetStaticMethodID(cls, "getFrames", "()[Ljava/awt/Frame;");
if (!method)
{
return false;
}

jobjectArray frames = static_cast<jobjectArray>(env->CallStaticObjectMethod(cls, method));
env->DeleteLocalRef(cls);
if (!frames)
{
return false;
}

jsize size = env->GetArrayLength(frames);
for (jsize i = 0; i < size; ++i)
{
jobject frame = env->GetObjectArrayElement(frames, i);
if (frame)
{
if (IsValidFrame(env, frame))
{
reflection = Reflection::Create(frame);
if (reflection)
{
env->DeleteLocalRef(frames);
return true;
}
}

env->DeleteLocalRef(frame);
}
}

env->DeleteLocalRef(frames);
return false;
});

bool hasReflection2 = !hasReflection && TimeOut(20, [&]{
if (!ModuleLoaded("libawt_xawt.so"))
{
return false;
Expand All @@ -617,7 +603,7 @@ std::unique_ptr<Reflection> GetNativeReflector() noexcept
void* windowFrame = reinterpret_cast<void*>(GetMainWindow());
if (windowFrame)
{
jobject frame = awt_GetComponent(reflection->getEnv(), windowFrame); //java.awt.Frame
jobject frame = awt_GetComponent(env, windowFrame); //java.awt.Frame
if (frame)
{
if (IsValidFrame(env, frame))
Expand All @@ -634,7 +620,7 @@ std::unique_ptr<Reflection> GetNativeReflector() noexcept
});
});

if (hasReflection || hasReflection2)
if (hasReflection)
{
return reflection;
}
Expand Down
40 changes: 35 additions & 5 deletions RemoteInput/Platform/Platform_Windows.cxx
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
#include "Platform.hxx"

#if defined(_WIN32) || defined(_WIN64)
#include <string>
#include <chrono>
#include "Thirdparty/Hook.hxx"
#if defined(CUSTOM_INJECTOR)
#include "Injection/Injector.hxx"
#else
#include "Thirdparty/Injector.hxx"
#endif

#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#include <tlhelp32.h>
#include <shellscalingapi.h>
Expand Down Expand Up @@ -270,16 +275,41 @@ bool InjectSelf(std::int32_t pid) noexcept
{
if (IsProcessAlive(pid))
{
std::string File;
File.resize(MAX_PATH);
std::string file;
file.resize(MAX_PATH);
extern HMODULE module;

if (GetModuleFileName(module, &File[0], MAX_PATH) == 0)
if (GetModuleFileName(module, &file[0], MAX_PATH) == 0)
{
return false;
}

return Injector::Inject(File, pid, nullptr);
#if defined(CUSTOM_INJECTOR)
return Injector::Inject(file, pid, nullptr);
#else
extern std::vector<std::unique_ptr<Injector>> injectors;

for (auto& injector : injectors)
{
if (injector && injector->get_pid() == pid)
{
if (injector->is_injected())
{
return true;
}

return injector->Inject(file);;
}
}

std::unique_ptr<Injector> injector = std::make_unique<Injector>(pid);
if (injector)
{
bool result = injector->Inject(file);
injectors.push_back(std::move(injector));
return true;
}
#endif
}
return false;
}
Expand Down
6 changes: 2 additions & 4 deletions RemoteInput/Plugin/Plugin.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
#include "EIOS.hxx"
#include "DebugConsole.hxx"
#include "Thirdparty/Hook.hxx"
#include "Thirdparty/Injector.hxx"

#if defined(_WIN32) || defined(_WIN64)
HMODULE module = nullptr;
#endif

std::vector<std::unique_ptr<Injector>> injectors;
std::unique_ptr<ControlCenter> control_center;
std::unique_ptr<DebugConsole> console;

Expand Down Expand Up @@ -140,10 +142,6 @@ void __exit_process(int exit_code)
void* this_module = dlopen(this_info.dli_fname, RTLD_LAZY);*/

std::thread([&] {
#if defined(DEBUG)
console = std::make_unique<DebugConsole>();
#endif

auto main_reflector = GetNativeReflector();
if (main_reflector)
{
Expand Down
47 changes: 39 additions & 8 deletions RemoteInput/Thirdparty/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,38 @@ project(THIRD_PARTY_LIBRARIES VERSION 1.0.0 DESCRIPTION "Third Party")
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)

# ----------------------- ENABLE ASSEMBLY CODE -----------------------
enable_language(ASM)



# ----------------------- INCLUDE_DIRECTORIES -----------------------
IF(WIN32)
set(INCLUDE_DIRECTORIES
min_hook/include
min_hook/src
min_hook/src/hde)
min_hook/src/hde
kubo_injector/include
kubo_injector/src/windows)
ELSEIF(APPLE)
set(INCLUDE_DIRECTORIES
rd_route/include
mach_inject/include)
mach_inject/include
kubo_injector/include
kubo_injector/src/macos)
ELSE()
set(INCLUDE_DIRECTORIES
linux_detours/include)
linux_detours/include
kubo_injector/include
kubo_injector/src/linux)
ENDIF()


# ----------------------------- LINKER -----------------------------
IF(WIN32)
set(LIBRARIES_LIST
kernel32)
kernel32
dbghelp)
ELSEIF(APPLE)
set(LIBRARIES_LIST
dl
Expand All @@ -52,11 +62,23 @@ IF(WIN32)
min_hook/src/buffer.h
min_hook/src/hook.c
min_hook/src/trampoline.c
min_hook/src/trampoline.h)
min_hook/src/trampoline.h
kubo_injector/include/injector.h
kubo_injector/src/windows/injector.c)
ELSEIF(APPLE)
set(LIB_SRC_LIST
rd_route/include/rd_route.h
rd_route/src/rd_route.c)
rd_route/src/rd_route.c
kubo_injector/include/injector.h
kubo_injector/src/macos/exc_handler.c
kubo_injector/src/macos/injector.c
kubo_injector/src/macos/injector_internal.h
kubo_injector/src/macos/mach.c
kubo_injector/src/macos/mach_exc.h
kubo_injector/src/macos/mach_excServer.c
kubo_injector/src/macos/ptrace.c
kubo_injector/src/macos/remote_call.c
kubo_injector/src/macos/util.c)
ELSE()
set(LIB_SRC_LIST
linux_detours/include/detours.h
Expand All @@ -68,13 +90,22 @@ ELSE()
linux_detours/src/disasm.cpp
linux_detours/src/plthook_elf.cpp
linux_detours/src/trampoline_x86.cpp
linux_detours/src/trampoline_arm.cpp)
linux_detours/src/trampoline_arm.cpp
kubo_injector/src/linux/shellcode.S
kubo_injector/src/linux/elf.c
kubo_injector/src/linux/injector.c
kubo_injector/src/linux/injector_internal.h
kubo_injector/src/linux/ptrace.c
kubo_injector/src/linux/remote_call.c
kubo_injector/src/linux/util.c)
ENDIF()

set(SRC_LIST
${LIB_SRC_LIST}
Hook.hxx
Hook.cxx)
Hook.cxx
Injector.cxx
Injector.hxx)


# ---------------------------- COMPILE ----------------------------
Expand Down
Loading

0 comments on commit d8367c3

Please sign in to comment.