Skip to content

Commit 43c4dbb

Browse files
committed
Make code more robust.
Added Injector and Fixed crashes on Linux Use same injector on all platforms.
1 parent d067781 commit 43c4dbb

34 files changed

+7179
-74
lines changed

CMakeLists.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ IF(WIN32)
6767
set(EXTRA_LIBRARIES
6868
user32
6969
opengl32
70-
gdi32)
70+
gdi32
71+
dbghelp) # Needed for ThirdParty/kubo/injector
7172
ELSEIF(APPLE)
7273
find_library(FOUNDATION Foundation)
7374
find_library(COCOA Cocoa)

RemoteInput/Injection/Injector.hxx

-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
#ifndef REMOTEINPUT_INJECTOR_HXX
66
#define REMOTEINPUT_INJECTOR_HXX
77

8-
#include <utility>
98
#include <cstdint>
109
#include <string>
11-
#include <sys/types.h>
1210

1311
class Injector
1412
{

RemoteInput/Injection/Injector_Darwin.cxx

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#if defined(__APPLE__)
88
#include <dlfcn.h>
99
#include <sys/sysctl.h>
10+
#include <sys/types.h>
1011
#include <mach/mach.h>
1112
#include <mach/mach_vm.h>
1213
#include <mach-o/loader.h>

RemoteInput/Platform/Platform_Darwin.mm

+30-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
#endif
1414

1515
#include <Thirdparty/Hook.hxx>
16-
#include <Injection/Injector.hxx>
16+
#if defined(CUSTOM_INJECTOR)
17+
#include "Injection/Injector.hxx"
18+
#else
19+
#include "Thirdparty/Injector.hxx"
20+
#endif
1721
#include <signal.h>
1822
#include <libproc.h>
1923
#include <sys/syscall.h>
@@ -123,10 +127,35 @@ bool IsThreadAlive(std::int32_t tid) noexcept
123127
std::string path = std::string(PATH_MAX, '\0');
124128
if (realpath(info.dli_fname, &path[0]))
125129
{
130+
#if defined(CUSTOM_INJECTOR)
126131
if (Injector::Inject(info.dli_fname, pid, nullptr))
127132
{
128133
return pid;
129134
}
135+
#else
136+
extern std::vector<std::unique_ptr<Injector>> injectors;
137+
138+
for (auto& injector : injectors)
139+
{
140+
if (injector && injector->get_pid() == pid)
141+
{
142+
if (injector->is_injected())
143+
{
144+
return pid;
145+
}
146+
147+
return injector->Inject(info.dli_fname) ? pid : -1;
148+
}
149+
}
150+
151+
std::unique_ptr<Injector> injector = std::make_unique<Injector>(pid);
152+
if (injector)
153+
{
154+
bool result = injector->Inject(info.dli_fname);
155+
injectors.push_back(std::move(injector));
156+
return result ? pid : -1;
157+
}
158+
#endif
130159
}
131160
}
132161
return -1;

RemoteInput/Platform/Platform_Linux.cxx

+34-48
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
#include "Platform.hxx"
22

33
#if defined(__linux__)
4+
#if defined(CUSTOM_INJECTOR)
45
#include "Injection/Injector.hxx"
6+
#else
7+
#include "Thirdparty/Injector.hxx"
8+
#endif
9+
510
#include <X11/Xlib.h>
611
#include <X11/Xatom.h>
712
#include <X11/Xutil.h>
@@ -197,10 +202,35 @@ std::int32_t InjectProcess(std::int32_t pid) noexcept
197202
std::string path = std::string(PATH_MAX, '\0');
198203
if (realpath(info.dli_fname, &path[0]))
199204
{
205+
#if defined(CUSTOM_INJECTOR)
200206
if (Injector::Inject(info.dli_fname, pid, nullptr))
201207
{
202208
return pid;
203209
}
210+
#else
211+
extern std::vector<std::unique_ptr<Injector>> injectors;
212+
213+
for (auto& injector : injectors)
214+
{
215+
if (injector && injector->get_pid() == pid)
216+
{
217+
if (injector->is_injected())
218+
{
219+
return pid;
220+
}
221+
222+
return injector->Inject(info.dli_fname) ? pid : -1;
223+
}
224+
}
225+
226+
std::unique_ptr<Injector> injector = std::make_unique<Injector>(pid);
227+
if (injector)
228+
{
229+
bool result = injector->Inject(info.dli_fname);
230+
injectors.push_back(std::move(injector));
231+
return result ? pid : -1;
232+
}
233+
#endif
204234
}
205235
}
206236
return -1;
@@ -455,8 +485,8 @@ void* GetModuleHandle(const char* module_name) noexcept
455485
}
456486
}
457487
return 0;
458-
}, reinterpret_cast<void*>(&module_info));
459-
return module_info.result ?: dlopen(module_name, RTLD_NOLOAD);
488+
}, &module_info);
489+
return module_info.result ? module_info.result : dlopen(module_name, RTLD_NOLOAD);
460490
}
461491
#endif
462492

@@ -558,50 +588,6 @@ std::unique_ptr<Reflection> GetNativeReflector() noexcept
558588
{
559589
std::unique_ptr<Reflection> reflection;
560590
bool hasReflection = TimeOut(20, [&]{
561-
jclass cls = env->FindClass("java/awt/Frame");
562-
if (!cls)
563-
{
564-
return false;
565-
}
566-
567-
jmethodID method = env->GetStaticMethodID(cls, "getFrames", "()[Ljava/awt/Frame;");
568-
if (!method)
569-
{
570-
return false;
571-
}
572-
573-
jobjectArray frames = static_cast<jobjectArray>(env->CallStaticObjectMethod(cls, method));
574-
env->DeleteLocalRef(cls);
575-
if (!frames)
576-
{
577-
return false;
578-
}
579-
580-
jsize size = env->GetArrayLength(frames);
581-
for (jsize i = 0; i < size; ++i)
582-
{
583-
jobject frame = env->GetObjectArrayElement(frames, i);
584-
if (frame)
585-
{
586-
if (IsValidFrame(env, frame))
587-
{
588-
reflection = Reflection::Create(frame);
589-
if (reflection)
590-
{
591-
env->DeleteLocalRef(frames);
592-
return true;
593-
}
594-
}
595-
596-
env->DeleteLocalRef(frame);
597-
}
598-
}
599-
600-
env->DeleteLocalRef(frames);
601-
return false;
602-
});
603-
604-
bool hasReflection2 = !hasReflection && TimeOut(20, [&]{
605591
if (!ModuleLoaded("libawt_xawt.so"))
606592
{
607593
return false;
@@ -617,7 +603,7 @@ std::unique_ptr<Reflection> GetNativeReflector() noexcept
617603
void* windowFrame = reinterpret_cast<void*>(GetMainWindow());
618604
if (windowFrame)
619605
{
620-
jobject frame = awt_GetComponent(reflection->getEnv(), windowFrame); //java.awt.Frame
606+
jobject frame = awt_GetComponent(env, windowFrame); //java.awt.Frame
621607
if (frame)
622608
{
623609
if (IsValidFrame(env, frame))
@@ -634,7 +620,7 @@ std::unique_ptr<Reflection> GetNativeReflector() noexcept
634620
});
635621
});
636622

637-
if (hasReflection || hasReflection2)
623+
if (hasReflection)
638624
{
639625
return reflection;
640626
}

RemoteInput/Platform/Platform_Windows.cxx

+35-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
#include "Platform.hxx"
2+
3+
#if defined(_WIN32) || defined(_WIN64)
24
#include <string>
35
#include <chrono>
46
#include "Thirdparty/Hook.hxx"
7+
#if defined(CUSTOM_INJECTOR)
58
#include "Injection/Injector.hxx"
9+
#else
10+
#include "Thirdparty/Injector.hxx"
11+
#endif
612

7-
#if defined(_WIN32) || defined(_WIN64)
813
#include <windows.h>
914
#include <tlhelp32.h>
1015
#include <shellscalingapi.h>
@@ -270,16 +275,41 @@ bool InjectSelf(std::int32_t pid) noexcept
270275
{
271276
if (IsProcessAlive(pid))
272277
{
273-
std::string File;
274-
File.resize(MAX_PATH);
278+
std::string file;
279+
file.resize(MAX_PATH);
275280
extern HMODULE module;
276281

277-
if (GetModuleFileName(module, &File[0], MAX_PATH) == 0)
282+
if (GetModuleFileName(module, &file[0], MAX_PATH) == 0)
278283
{
279284
return false;
280285
}
281286

282-
return Injector::Inject(File, pid, nullptr);
287+
#if defined(CUSTOM_INJECTOR)
288+
return Injector::Inject(file, pid, nullptr);
289+
#else
290+
extern std::vector<std::unique_ptr<Injector>> injectors;
291+
292+
for (auto& injector : injectors)
293+
{
294+
if (injector && injector->get_pid() == pid)
295+
{
296+
if (injector->is_injected())
297+
{
298+
return true;
299+
}
300+
301+
return injector->Inject(file);;
302+
}
303+
}
304+
305+
std::unique_ptr<Injector> injector = std::make_unique<Injector>(pid);
306+
if (injector)
307+
{
308+
bool result = injector->Inject(file);
309+
injectors.push_back(std::move(injector));
310+
return true;
311+
}
312+
#endif
283313
}
284314
return false;
285315
}

RemoteInput/Plugin/Plugin.cxx

+2-4
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
#include "EIOS.hxx"
1717
#include "DebugConsole.hxx"
1818
#include "Thirdparty/Hook.hxx"
19+
#include "Thirdparty/Injector.hxx"
1920

2021
#if defined(_WIN32) || defined(_WIN64)
2122
HMODULE module = nullptr;
2223
#endif
2324

25+
std::vector<std::unique_ptr<Injector>> injectors;
2426
std::unique_ptr<ControlCenter> control_center;
2527
std::unique_ptr<DebugConsole> console;
2628

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

142144
std::thread([&] {
143-
#if defined(DEBUG)
144-
console = std::make_unique<DebugConsole>();
145-
#endif
146-
147145
auto main_reflector = GetNativeReflector();
148146
if (main_reflector)
149147
{

RemoteInput/Thirdparty/CMakeLists.txt

+39-8
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,38 @@ project(THIRD_PARTY_LIBRARIES VERSION 1.0.0 DESCRIPTION "Third Party")
44
set(CMAKE_C_STANDARD 11)
55
set(CMAKE_CXX_STANDARD 17)
66

7+
# ----------------------- ENABLE ASSEMBLY CODE -----------------------
8+
enable_language(ASM)
9+
710

811

912
# ----------------------- INCLUDE_DIRECTORIES -----------------------
1013
IF(WIN32)
1114
set(INCLUDE_DIRECTORIES
1215
min_hook/include
1316
min_hook/src
14-
min_hook/src/hde)
17+
min_hook/src/hde
18+
kubo_injector/include
19+
kubo_injector/src/windows)
1520
ELSEIF(APPLE)
1621
set(INCLUDE_DIRECTORIES
1722
rd_route/include
18-
mach_inject/include)
23+
mach_inject/include
24+
kubo_injector/include
25+
kubo_injector/src/macos)
1926
ELSE()
2027
set(INCLUDE_DIRECTORIES
21-
linux_detours/include)
28+
linux_detours/include
29+
kubo_injector/include
30+
kubo_injector/src/linux)
2231
ENDIF()
2332

2433

2534
# ----------------------------- LINKER -----------------------------
2635
IF(WIN32)
2736
set(LIBRARIES_LIST
28-
kernel32)
37+
kernel32
38+
dbghelp)
2939
ELSEIF(APPLE)
3040
set(LIBRARIES_LIST
3141
dl
@@ -52,11 +62,23 @@ IF(WIN32)
5262
min_hook/src/buffer.h
5363
min_hook/src/hook.c
5464
min_hook/src/trampoline.c
55-
min_hook/src/trampoline.h)
65+
min_hook/src/trampoline.h
66+
kubo_injector/include/injector.h
67+
kubo_injector/src/windows/injector.c)
5668
ELSEIF(APPLE)
5769
set(LIB_SRC_LIST
5870
rd_route/include/rd_route.h
59-
rd_route/src/rd_route.c)
71+
rd_route/src/rd_route.c
72+
kubo_injector/include/injector.h
73+
kubo_injector/src/macos/exc_handler.c
74+
kubo_injector/src/macos/injector.c
75+
kubo_injector/src/macos/injector_internal.h
76+
kubo_injector/src/macos/mach.c
77+
kubo_injector/src/macos/mach_exc.h
78+
kubo_injector/src/macos/mach_excServer.c
79+
kubo_injector/src/macos/ptrace.c
80+
kubo_injector/src/macos/remote_call.c
81+
kubo_injector/src/macos/util.c)
6082
ELSE()
6183
set(LIB_SRC_LIST
6284
linux_detours/include/detours.h
@@ -68,13 +90,22 @@ ELSE()
6890
linux_detours/src/disasm.cpp
6991
linux_detours/src/plthook_elf.cpp
7092
linux_detours/src/trampoline_x86.cpp
71-
linux_detours/src/trampoline_arm.cpp)
93+
linux_detours/src/trampoline_arm.cpp
94+
kubo_injector/src/linux/shellcode.S
95+
kubo_injector/src/linux/elf.c
96+
kubo_injector/src/linux/injector.c
97+
kubo_injector/src/linux/injector_internal.h
98+
kubo_injector/src/linux/ptrace.c
99+
kubo_injector/src/linux/remote_call.c
100+
kubo_injector/src/linux/util.c)
72101
ENDIF()
73102

74103
set(SRC_LIST
75104
${LIB_SRC_LIST}
76105
Hook.hxx
77-
Hook.cxx)
106+
Hook.cxx
107+
Injector.cxx
108+
Injector.hxx)
78109

79110

80111
# ---------------------------- COMPILE ----------------------------

0 commit comments

Comments
 (0)