Skip to content

Commit

Permalink
Fix incorrect path format for LoadLibraryA
Browse files Browse the repository at this point in the history
  • Loading branch information
daedsidog committed Apr 10, 2021
1 parent c1d0ada commit 082a96c
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <list>
#include <windows.h>
#include <filesystem>
#include <regex>

#define BUFSIZE 1024

Expand All @@ -27,14 +28,18 @@ int main(int argc, char **argv) {

// Format passed down to loader to pass to process.
std::stringstream args;
args << target_binary_abs_path.string();
args << "\"" + target_binary_abs_path.string() + "\"";
bool reading_args = false;
for (int i = 2; i < argc; ++i) {
if (!reading_args) {
if (std::string(argv[i]) != "-args") {
std::filesystem::path abs_dll_path =
std::filesystem::absolute(std::filesystem::path(argv[i]));
dlls.push_back(abs_dll_path.string());
std::string abs_dll_path_string = abs_dll_path.string();

// Needs to be converted to this format for Windows.
abs_dll_path_string = std::regex_replace(abs_dll_path_string, std::regex("\\\\"), "/");
dlls.push_back(abs_dll_path_string);
continue;
}
reading_args = true;
Expand All @@ -54,18 +59,16 @@ int main(int argc, char **argv) {
&process_info)) {
std::list<LPVOID> pages;
for (std::string dll_name : dlls) {

LPVOID page =
VirtualAllocEx(process_info.hProcess, nullptr, BUFSIZE,
MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
pages.push_back(page);
LPVOID page = VirtualAllocEx(
process_info.hProcess, nullptr, BUFSIZE,
MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (page == nullptr) {
std::cerr << "VirtualAllocEx error: " << GetLastError()
<< std::endl;
return -1;
}
if (WriteProcessMemory(process_info.hProcess, page,
dll_name.c_str(), sizeof(dll_name),
dll_name.c_str(), dll_name.length(),
nullptr) == 0) {
std::cerr << "WriteProcessMemory error: " << GetLastError()
<< std::endl;
Expand Down

0 comments on commit 082a96c

Please sign in to comment.