Skip to content

Commit

Permalink
Disable WOW64 redirections
Browse files Browse the repository at this point in the history
Disables WOW64 redirections, so it shouldn't matter if you're using the 32 or 64 bit version of DLL Forward. Also refactors a bit and allows for 'unknown' (Non recognized as i386 or amd64) architecture types to go through
  • Loading branch information
itisluiz committed Nov 29, 2023
1 parent 58f8513 commit b5d7099
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 28 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.21)
project("dllforward" VERSION 1.2 LANGUAGES CXX)
project("dllforward" VERSION 1.2.1 LANGUAGES CXX)

## Project's base, containing all that's to be inherited
set(PROJECT_BASE ${PROJECT_NAME}-base)
Expand Down
11 changes: 9 additions & 2 deletions src/builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@ void buildResultHeader(const fs::path& dllPath, const fs::path& outFile, Archite
#include <headerboilerplate/top.inl>
<< '\n';

file << "// Proxy header generated for " << dllPath.filename().string() << " (" << (architecture == Architecture::kI386 ? "32" : "64") << " bit)" "\n";
file << "static_assert(sizeof(void*) == " << (architecture == Architecture::kI386 ? 4 : 8) << ", \"The proxied DLL must match the architecture of the proxy DLL\");" "\n\n";
file << "// Proxy header generated for " << dllPath.filename().string();

if (architecture != Architecture::kUnknown)
{
file << " (" << (architecture == Architecture::kI386 ? "32" : "64") << " bit)" "\n";
file << "static_assert(sizeof(void*) == " << (architecture == Architecture::kI386 ? 4 : 8) << ", \"The proxied DLL must match the architecture of the proxy DLL\");" "\n\n";
}
else
file << "\n\n";

for (const Export& exportEntry : exports)
{
Expand Down
64 changes: 39 additions & 25 deletions src/forwarder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,50 @@
#include <iostream>
#include <fstream>

bool makeHeader(const fs::path& dllPath, const fs::path& outFile)
std::pair<Architecture, std::vector<Export>> acquireArchExports(const fs::path& dllPath)
{
try
Architecture architecture{ parseArchitecture(dllPath) };

if (architecture == Architecture::kNotNT)
throw std::runtime_error("Input file is not an NT binary");

std::cout << "Selected file is of ";

switch (architecture)
{
Architecture architecture{ parseArchitecture(dllPath) };

if (architecture != Architecture::kI386 && architecture != Architecture::kAMD64)
throw std::runtime_error("Bad executable and/or architecture");
case Architecture::kUnknown:
std::cout << "unknown";
break;
case Architecture::kI386:
std::cout << "x86";
break;
case Architecture::kAMD64:
std::cout << "x64";
break;
}

std::cout << "Selected file is a " << (architecture == Architecture::kI386 ? "x86" : "x64") << " binary" "\n";
std::cout << " architecture" "\n";

std::vector<Export> exports{ parseExports(dllPath) };
std::vector<Export> exports{ parseExports(dllPath) };

std::cout << "There are " << exports.size() << " exports:" "\n";
std::cout << "There are " << exports.size() << " exports:" "\n";

for (size_t hint{ 0 }; hint < exports.size(); ++hint)
{
const Export& exportEntry{ exports[hint] };
std::cout << '\t' << exportEntry << '\n';
}
for (size_t hint{ 0 }; hint < exports.size(); ++hint)
{
const Export& exportEntry{ exports[hint] };
std::cout << '\t' << exportEntry << '\n';
}

buildResultHeader(dllPath, outFile, architecture, exports);
return std::make_pair(architecture, exports);
}

bool makeHeader(const fs::path& dllPath, const fs::path& outFile)
{
try
{
std::pair<Architecture, std::vector<Export>> archExports{ acquireArchExports(dllPath) };

buildResultHeader(dllPath, outFile, archExports.first, archExports.second);
std::cout << "Generated output at \"" << fs::absolute(outFile).string() << "\"" "\n";
}
catch (const std::system_error& e)
Expand All @@ -46,17 +68,9 @@ bool makeDefinition(const fs::path& dllPath, const fs::path& outFile)
{
try
{
std::vector<Export> exports{ parseExports(dllPath) };

std::cout << "There are " << exports.size() << " exports:" "\n";

for (size_t hint{ 0 }; hint < exports.size(); ++hint)
{
const Export& exportEntry{ exports[hint] };
std::cout << '\t' << exportEntry << '\n';
}
std::pair<Architecture, std::vector<Export>> archExports{ acquireArchExports(dllPath) };

buildResultDefinition(dllPath, outFile, exports);
buildResultDefinition(dllPath, outFile, archExports.second);
std::cout << "Generated output at \"" << fs::absolute(outFile).string() << "\"" "\n";
}
catch (const std::system_error& e)
Expand Down
16 changes: 16 additions & 0 deletions src/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@ Architecture parseArchitecture(const fs::path& path)
IMAGE_DOS_HEADER dosHeader;
IMAGE_NT_HEADERS32 ntHeader32;

PVOID oldRedirection{ NULL };
if (!Wow64DisableWow64FsRedirection(&oldRedirection))
throw std::system_error(std::error_code(GetLastError(), std::system_category()), "Failed to disable Wow64Fs redirection");

std::ifstream file(path, std::ios::binary);

if (!Wow64RevertWow64FsRedirection(oldRedirection))
throw std::system_error(std::error_code(GetLastError(), std::system_category()), "Failed to revert Wow64Fs redirection");

file.exceptions(std::ifstream::failbit | std::ifstream::badbit);

file.read(reinterpret_cast<char*>(&dosHeader), sizeof(dosHeader));
Expand All @@ -38,9 +46,17 @@ Architecture parseArchitecture(const fs::path& path)
std::vector<Export> parseExports(const fs::path& path)
{
_LOADED_IMAGE LoadedImage;

PVOID oldRedirection{ NULL };
if (!Wow64DisableWow64FsRedirection(&oldRedirection))
throw std::system_error(std::error_code(GetLastError(), std::system_category()), "Failed to disable Wow64Fs redirection");

if (!MapAndLoad(path.u8string().c_str(), nullptr, &LoadedImage, TRUE, TRUE))
throw std::system_error(std::error_code(GetLastError(), std::system_category()), "MapAndLoad failed to load DLL");

if (!Wow64RevertWow64FsRedirection(oldRedirection))
throw std::system_error(std::error_code(GetLastError(), std::system_category()), "Failed to revert Wow64Fs redirection");

std::vector<Export> exportVector;

ULONG cDirSize;
Expand Down

0 comments on commit b5d7099

Please sign in to comment.