Skip to content

Commit 5718491

Browse files
fix: Process::module should be case insensitive
1 parent c693953 commit 5718491

3 files changed

Lines changed: 19 additions & 10 deletions

File tree

include/blook/utils.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "zasm/base/mode.hpp"
55
#include "zasm/decoder/decoder.hpp"
66
#include "zasm/zasm.hpp"
7+
#include <string>
78

89
#ifndef CLASS_MOVE_ONLY
910
#define CLASS_MOVE_ONLY(classname) \
@@ -63,6 +64,9 @@ constexpr auto compileMachineMode() {
6364
static_assert(false, "Unsupported architecture");
6465
}
6566
}
67+
68+
std::string to_lower(const std::string &str);
69+
6670
}; // namespace utils
6771

6872
} // namespace blook

src/platform/windows/process.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "blook/blook.h"
55

6+
#include "blook/utils.h"
67
#include "windows.h"
78

89
#include "psapi.h"
@@ -36,8 +37,7 @@ static HMODULE GetProcessBaseAddress(HANDLE processHandle,
3637
HMODULE *moduleArray;
3738
LPBYTE moduleArrayBytes;
3839
DWORD bytesRequired;
39-
std::transform(modulename.begin(), modulename.end(), modulename.begin(),
40-
[](unsigned char c) { return std::tolower(c); });
40+
modulename = blook::utils::to_lower(modulename);
4141

4242
if (processHandle) {
4343
if (EnumProcessModules(processHandle, NULL, 0, &bytesRequired)) {
@@ -57,10 +57,7 @@ static HMODULE GetProcessBaseAddress(HANDLE processHandle,
5757
char name[MAX_PATH];
5858
if (GetModuleFileNameA(module, name, MAX_PATH)) {
5959
auto filename = std::filesystem::path(name).filename().string();
60-
61-
std::transform(filename.begin(), filename.end(),
62-
filename.begin(),
63-
[](unsigned char c) { return std::tolower(c); });
60+
filename = blook::utils::to_lower(filename);
6461
if (modulename == filename) {
6562
baseAddress = module;
6663
break;
@@ -181,7 +178,8 @@ std::expected<void, std::string> Process::try_write(void *dst, const void *src,
181178
if (safe_memcpy(dst, src, size)) {
182179
return {};
183180
} else {
184-
return std::unexpected(std::format("Failed to write memory {:p}: access violation", dst));
181+
return std::unexpected(
182+
std::format("Failed to write memory {:p}: access violation", dst));
185183
}
186184
} else {
187185
SIZE_T written = 0;
@@ -369,7 +367,7 @@ Process::module(const std::string &name) {
369367
return {};
370368
} else {
371369
auto modules = this->modules();
372-
auto it = modules.find(name);
370+
auto it = modules.find(blook::utils::to_lower(name));
373371
if (it != modules.end())
374372
return it->second;
375373
return {};
@@ -449,8 +447,7 @@ std::map<std::string, std::shared_ptr<Module>> Process::modules() {
449447
if (GetModuleFileNameExA(this->h, hMods[i], szModName,
450448
sizeof(szModName))) {
451449
auto name = std::filesystem::path(szModName).filename().string();
452-
std::transform(name.begin(), name.end(), name.begin(),
453-
[](unsigned char c) { return std::tolower(c); });
450+
name = blook::utils::to_lower(name);
454451
modules[name] = Module::make(
455452
const_cast<Process *>(this)->shared_from_this(), hMods[i]);
456453
}

src/utils.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,13 @@ getreg_fn_t getPEB = (getreg_fn_t)(void *)get_peb_fn_buf;
8181

8282
getreg_fn_t getStackPointer = (getreg_fn_t)(void *)_getStackPointer;
8383

84+
std::string to_lower(const std::string &str) {
85+
std::string result;
86+
result.reserve(str.size());
87+
for (char c : str) {
88+
result += static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
89+
}
90+
return result;
91+
}
8492
} // namespace utils
8593
} // namespace blook

0 commit comments

Comments
 (0)