Skip to content

Commit d067781

Browse files
committed
Make code more robust.
1 parent 6628dc7 commit d067781

File tree

5 files changed

+62
-36
lines changed

5 files changed

+62
-36
lines changed

RemoteInput/Injection/Injector_Arm.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,9 @@ bool Injector::Inject(std::string module_path, std::int32_t pid, void* bootstrap
419419
ptrace(PTRACE_DETACH, pid, nullptr, nullptr);
420420
return false;
421421
}
422+
423+
ptrace(PTRACE_DETACH, pid, nullptr, nullptr);
424+
return true;
422425
}
423426

424427
ptrace(PTRACE_DETACH, pid, nullptr, nullptr);

RemoteInput/Injection/Injector_Linux.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,9 @@ bool Injector::Inject(std::string module_path, std::int32_t pid, void* bootstrap
391391
ptrace(PTRACE_DETACH, pid, nullptr, nullptr);
392392
return false;
393393
}
394+
395+
ptrace(PTRACE_DETACH, pid, nullptr, nullptr);
396+
return true;
394397
}
395398

396399
ptrace(PTRACE_DETACH, pid, nullptr, nullptr);

RemoteInput/JVM.cxx

+1-2
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,14 @@ JVM::JVM() noexcept : vm(nullptr), createdVM(false), loadedJNI(false), module(nu
4040
}
4141
#endif
4242

43-
this->createdVM = false;
4443
jint num_vms = 0;
4544
const jint max_vms = 1;
4645
JavaVM* vms[max_vms] = {0};
4746
if (this->JNI_GetCreatedJavaVMs(vms, max_vms, &num_vms) == JNI_OK)
4847
{
4948
for (int i = 0; i < num_vms; ++i)
5049
{
51-
if (vms[i] != NULL)
50+
if (vms[i] != nullptr)
5251
{
5352
this->vm = vms[i];
5453
break;

RemoteInput/Platform/Platform_Darwin.mm

+44-30
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,15 @@
1818
#include <libproc.h>
1919
#include <sys/syscall.h>
2020
#include <mach-o/dyld.h>
21+
#include <filesystem>
2122
#endif // defined
2223

2324
#if defined(__APPLE__)
25+
std::string StripPath(const std::string& path_to_strip)
26+
{
27+
return std::filesystem::path(path_to_strip).filename().string();
28+
}
29+
2430
void GetDesktopResolution(int &width, int &height) noexcept
2531
{
2632
auto get_screen_resolution = [&]{
@@ -163,9 +169,13 @@ bool IsThreadAlive(std::int32_t tid) noexcept
163169
for (std::uint32_t i = 0; i < count; ++i)
164170
{
165171
const char* name = _dyld_get_image_name(i);
166-
if (!strcasestr(name, partial_module_name))
172+
if (name)
167173
{
168-
result.push_back(name);
174+
std::string module_name = StripPath(name);
175+
if (!strcasestr(module_name.c_str(), partial_module_name))
176+
{
177+
result.push_back(name);
178+
}
169179
}
170180
}
171181

@@ -178,43 +188,47 @@ bool IsThreadAlive(std::int32_t tid) noexcept
178188
for (std::uint32_t i = 0; i < count; ++i)
179189
{
180190
const char* name = _dyld_get_image_name(i);
181-
if (strcasestr(name, module_name))
191+
if (name)
182192
{
183-
return dlopen(name, RTLD_NOLOAD);
184-
185-
/*const struct mach_header* header = _dyld_get_image_header(i);
186-
//std::intptr_t offset = _dyld_get_image_vmaddr_slide(i);
187-
188-
const struct load_command* cmd = reinterpret_cast<const struct load_command*>(reinterpret_cast<const char *>(header) + sizeof(struct mach_header));
189-
if (header->magic == MH_MAGIC_64)
193+
std::string stripped_name = StripPath(name);
194+
if (strcasestr(stripped_name.c_str(), module_name))
190195
{
191-
cmd = reinterpret_cast<const struct load_command*>(reinterpret_cast<const char *>(header) + sizeof(struct mach_header_64));
192-
}
196+
return dlopen(name, RTLD_NOLOAD);
193197

194-
for (std::uint32_t j = 0; j < header->ncmds; ++j)
195-
{
196-
if (cmd->cmd == LC_SEGMENT)
197-
{
198-
const struct segment_command* seg = reinterpret_cast<const struct segment_command*>(cmd);
199-
void* base_addr = reinterpret_cast<void*>(seg->vmaddr); //seg->vmaddr + offset;
198+
/*const struct mach_header* header = _dyld_get_image_header(i);
199+
//std::intptr_t offset = _dyld_get_image_vmaddr_slide(i);
200200
201-
Dl_info info = {0};
202-
dladdr(base_addr, &info);
203-
return dlopen(info.dli_fname, RTLD_NOLOAD);
201+
const struct load_command* cmd = reinterpret_cast<const struct load_command*>(reinterpret_cast<const char *>(header) + sizeof(struct mach_header));
202+
if (header->magic == MH_MAGIC_64)
203+
{
204+
cmd = reinterpret_cast<const struct load_command*>(reinterpret_cast<const char *>(header) + sizeof(struct mach_header_64));
204205
}
205206
206-
if (cmd->cmd == LC_SEGMENT_64)
207+
for (std::uint32_t j = 0; j < header->ncmds; ++j)
207208
{
208-
const struct segment_command_64* seg = reinterpret_cast<const struct segment_command_64*>(cmd);
209-
void* base_addr = reinterpret_cast<void*>(seg->vmaddr); //seg->vmaddr + offset;
209+
if (cmd->cmd == LC_SEGMENT)
210+
{
211+
const struct segment_command* seg = reinterpret_cast<const struct segment_command*>(cmd);
212+
void* base_addr = reinterpret_cast<void*>(seg->vmaddr); //seg->vmaddr + offset;
210213
211-
Dl_info info = {0};
212-
dladdr(base_addr, &info);
213-
return dlopen(info.dli_fname, RTLD_NOLOAD);
214-
}
214+
Dl_info info = {0};
215+
dladdr(base_addr, &info);
216+
return dlopen(info.dli_fname, RTLD_NOLOAD);
217+
}
215218
216-
cmd = reinterpret_cast<const struct load_command*>(reinterpret_cast<const char*>(cmd) + cmd->cmdsize);
217-
}*/
219+
if (cmd->cmd == LC_SEGMENT_64)
220+
{
221+
const struct segment_command_64* seg = reinterpret_cast<const struct segment_command_64*>(cmd);
222+
void* base_addr = reinterpret_cast<void*>(seg->vmaddr); //seg->vmaddr + offset;
223+
224+
Dl_info info = {0};
225+
dladdr(base_addr, &info);
226+
return dlopen(info.dli_fname, RTLD_NOLOAD);
227+
}
228+
229+
cmd = reinterpret_cast<const struct load_command*>(reinterpret_cast<const char*>(cmd) + cmd->cmdsize);
230+
}*/
231+
}
218232
}
219233
}
220234
return dlopen(module_name, RTLD_NOLOAD);

RemoteInput/Platform/Platform_Linux.cxx

+11-4
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,15 @@
2323
#include <cstring>
2424
#include <vector>
2525
#include <algorithm>
26+
#include <filesystem>
2627
#endif // defined
2728

2829
#if defined(__linux__)
30+
std::string StripPath(const std::string& path_to_strip)
31+
{
32+
return std::filesystem::path(path_to_strip).filename().string();
33+
}
34+
2935
void GetDesktopResolution(int &width, int &height) noexcept
3036
{
3137
Display* display = XOpenDisplay(nullptr);
@@ -419,10 +425,10 @@ std::vector<std::string> GetLoadedModuleNames(const char* partial_module_name) n
419425
if (info && info->dlpi_name)
420426
{
421427
Module* module_info = static_cast<Module*>(data);
422-
if (strcasestr(info->dlpi_name, module_info->module_name))
428+
std::string module_name = StripPath(info->dlpi_name);
429+
if (strcasestr(module_name.c_str(), module_info->module_name))
423430
{
424431
module_info->result.push_back(info->dlpi_name);
425-
return 1;
426432
}
427433
}
428434
return 0;
@@ -441,10 +447,11 @@ void* GetModuleHandle(const char* module_name) noexcept
441447
if (info && info->dlpi_name)
442448
{
443449
Module* module_info = static_cast<Module*>(data);
444-
if (strcasestr(info->dlpi_name, module_info->module_name))
450+
std::string module_name = StripPath(info->dlpi_name);
451+
if (strcasestr(module_name.c_str(), module_info->module_name))
445452
{
446453
module_info->result = dlopen(module_info->module_name, RTLD_NOLOAD);
447-
return 1;
454+
return module_info->result ? 1 : 0;
448455
}
449456
}
450457
return 0;

0 commit comments

Comments
 (0)