Skip to content

Commit 03bb5fe

Browse files
committed
DLL injection path bug fix
1 parent 9a9b3f1 commit 03bb5fe

File tree

4 files changed

+31
-25
lines changed

4 files changed

+31
-25
lines changed

include/keyreaper/config.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Config {
99
public:
1010
static Config& Instance(); // Singleton accessor
1111

12-
error_handling::ProgramResult Load(const std::string& filename);
12+
error_handling::ProgramResult Load(const std::wstring& filename);
1313

1414
std::wstring GetKeyExtractorDLLPath() const;
1515

@@ -20,7 +20,7 @@ class Config {
2020
Config(const Config&) = delete;
2121
Config& operator=(const Config&) = delete;
2222

23-
std::string key_extractor_dll_;
23+
std::wstring key_extractor_dll_;
2424
};
2525

2626
#endif // CONFIG_H_

src/config.cc

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,52 +11,50 @@ Config& Config::Instance() {
1111
return instance;
1212
}
1313

14-
wstring StringToWString(const string& str) {
15-
if (str.empty()) return wstring();
16-
17-
int size_needed = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, nullptr, 0);
18-
if (size_needed == 0) return wstring(); // Conversion failed
19-
20-
wstring wstr(size_needed, 0);
21-
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, &wstr[0], size_needed);
22-
23-
return wstr;
14+
wstring ExePath() {
15+
wchar_t buffer[MAX_PATH] = { 0 };
16+
GetModuleFileNameW(NULL, buffer, MAX_PATH);
17+
wstring fullPath(buffer);
18+
wstring::size_type pos = fullPath.find_last_of(L"\\/");
19+
return fullPath.substr(0, pos);
2420
}
2521

26-
27-
bool FileExists(string filename) {
22+
bool FileExists(wstring filename) {
2823
ifstream file_check(filename);
2924
if (file_check) return true;
3025
else return false;
3126
}
3227

33-
ProgramResult Config::Load(const string& filename) {
28+
ProgramResult Config::Load(const wstring& filename) {
3429
if (!FileExists(filename)) return ErrorResult("Could not find config file");
3530

3631
toml::table config;
3732
try {
3833
config = toml::parse_file(filename);
3934

4035
} catch (const toml::parse_error& err) {
41-
cerr << err << endl;
42-
return ErrorResult("Failed to parse config file " + filename);
36+
wcerr << L"Error while parsing " << filename;
37+
cerr << ": " << err << endl;
38+
return ErrorResult("Failed to parse config file ");
4339
}
4440

41+
auto current_path = ExePath();
42+
4543
#if _WIN64
46-
string default_dll = "injectable_server_x64.dll";
44+
wstring default_dll = current_path + L"\\injectable_server_x64.dll";
4745
#else
48-
string default_dll = "injectable_server_x86.dll";
46+
wstring default_dll = current_path + L"\\injectable_server_x86.dll";
4947
#endif
5048
key_extractor_dll_ = config["Settings"]["key_extractor_dll_path"].value_or(default_dll);
5149
if (!FileExists(key_extractor_dll_)) {
52-
cerr << "Could not find a default\n";
50+
cerr << "Default DLL not set in the config file\n";
5351
if (!FileExists(default_dll)) return ErrorResult("Could not find a valid DLL");
54-
cerr << "Specified path to DLL does not exist. Defaulting to " << default_dll << endl;
52+
wcerr << "Specified path to DLL does not exist. Defaulting to " << default_dll << endl;
5553
}
5654

5755
return OkResult("Configuration successfully loaded");
5856
}
5957

6058
std::wstring Config::GetKeyExtractorDLLPath() const {
61-
return StringToWString(key_extractor_dll_);
59+
return key_extractor_dll_;
6260
}

src/injection/injector.cc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ using namespace error_handling;
1414

1515
namespace injection {
1616

17+
wstring DllName(wstring dll_path) {
18+
std::wstring::size_type pos = dll_path.find_last_of(L"\\/");
19+
if (pos == std::wstring::npos)
20+
return dll_path; // No path separator found, return entire string
21+
22+
return dll_path.substr(pos + 1);
23+
}
24+
1725
bool IsDLLLoadedOnProcess(DWORD pid, wstring w_dll_path, HANDLE process_handle) {
1826
if (!process_handle) return false;
1927

@@ -23,7 +31,7 @@ bool IsDLLLoadedOnProcess(DWORD pid, wstring w_dll_path, HANDLE process_handle)
2331
for (size_t i = 0; i < (bytes_needed / sizeof(HMODULE)); i++) {
2432
WCHAR module_name[MAX_PATH];
2533
if (GetModuleBaseNameW(process_handle, module_array[i], module_name, sizeof(module_name) / sizeof(WCHAR))) {
26-
if (_wcsicmp(module_name, w_dll_path.c_str()) == 0) {
34+
if (_wcsicmp(module_name, DllName(w_dll_path).c_str()) == 0) {
2735
printf(" [i] DLL found in process\n");
2836
return true;
2937
}

src/main.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ std::string GetChoices(const std::map<std::string, EnumClass>& options_map, cons
5656
}
5757

5858
bool LoadConfig() {
59-
string config_file = "config.toml";
59+
wstring config_file = L"config.toml";
6060
if (!fs::exists(config_file)) {
61-
cout << "[!] Config file not found, creating a new one with the name: " << config_file << endl;
61+
wcout << "[!] Config file not found, creating a new one with the name: " << config_file << endl;
6262
ofstream empty_file(config_file);
6363
}
6464

0 commit comments

Comments
 (0)