Skip to content

Commit

Permalink
fixed getExecutableFolderPath and readded important export that EasyH…
Browse files Browse the repository at this point in the history
…ook is looking for
  • Loading branch information
KeinNiemand committed Sep 18, 2024
1 parent 78df03e commit 3d8d05a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
29 changes: 16 additions & 13 deletions Injector/Injector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ std::wstring getExecutableFolderPath() {

//NTFS technical max path length, I know this is overkill (normally MAX_PATH 260 is enough)
//but large path aware applications can have longer paths so this makes it easier to enable large path support if I want to in the future
constexpr size_t bufferSize = 65535;
auto buffer = std::unique_ptr<wchar_t[]>(new wchar_t[](bufferSize));
constexpr size_t bufferSize = 65535;
auto buffer = std::make_unique<wchar_t[]>(bufferSize);
DWORD size = GetModuleFileNameW(NULL, buffer.get(), bufferSize);
if (size == 0) {
// Handle error, GetLastError() can be used to get error details
Expand All @@ -43,9 +43,10 @@ std::wstring getExecutableFolderPath() {
// Find the last backslash in the path to separate the folder path
size_t pos = path.find_last_of(L"\\/");
if (pos != std::wstring::npos) {
return path.substr(0, pos);
return path.substr(0, pos + 1);
}

std::wcerr << L"Error when trying to get executable folder path" << L"\r\n";
return nullptr;
}

Expand All @@ -68,14 +69,16 @@ int wmain(int argc, wchar_t* argv[])

//Change Current Directory to Executuabel Folder this is probably not the best solution but might work
//TODO: instead of changing direcotry maybe use absolute paths instead (including a way run the exe from in exectuable folder if a relative path is passed into the config) or rdstore path
{
std::wstring executableFolderPath = getExecutableFolderPath();
_wchdir(executableFolderPath.c_str());
}

std::wstring executableFolderPath = getExecutableFolderPath();
_wchdir(executableFolderPath.c_str());

//Load config
Configuration config;
config.loadFromFile(".\\LargePageInjectorMods.toml");

Logger::Log(Logger::Level::Debug, L"exe path = " + config.LaunchPath);

const WCHAR* exeName = config.LaunchPath.c_str();
ULONG procIdVar = 0;
ULONG* procId = &procIdVar;
Expand All @@ -89,11 +92,11 @@ int wmain(int argc, wchar_t* argv[])

//Setup enviroment variables from config
for (auto& [key, value] : config.environment) {

//Use windows API to set enviroment variables make sure child procceses inherit
SetEnvironmentVariableA(key.c_str(), value.c_str());
}

//Run the process and inject the dll
NTSTATUS nt = RhCreateAndInject(
(WCHAR*)exeName,
Expand All @@ -106,15 +109,15 @@ int wmain(int argc, wchar_t* argv[])
0, //0 Pass trough size
procId //Store Created Proc Id
);

//TODO: Replace all these verbosity checks with a simple logger
if (nt != 0)
{
Logger::Log(Logger::Level::Error, "RhInjectLibrary failed with error code = " + std::to_string(nt));
PWCHAR err = RtlGetLastErrorString();
Logger::Log(Logger::Level::Error, std::wstring(err));
}
else if(config.verbosity >= 4)
else if (config.verbosity >= 4)
{
Logger::Log(Logger::Level::Info, L"Library injected successfully.");
}
Expand Down Expand Up @@ -142,7 +145,7 @@ int wmain(int argc, wchar_t* argv[])
Logger::Log(Logger::Level::Info, L"Pipe Closed");

}


return 0;
}
4 changes: 4 additions & 0 deletions MiMallocReplacer/PerformHooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
#include <fcntl.h>
#include <io.h>

// EasyHook will be looking for this export to support DLL injection. If not found then
// DLL injection will fail.
extern "C" void __declspec(dllexport) __stdcall NativeInjectionEntryPoint(REMOTE_ENTRY_INFO* inRemoteInfo);

import MallocSigmatch;
import Configuration;
import Logger;
Expand Down

0 comments on commit 3d8d05a

Please sign in to comment.