Skip to content

Commit

Permalink
Load and Hook dll
Browse files Browse the repository at this point in the history
Loads system dll and then redirects it to the local copy of the dll.
  • Loading branch information
elishacloud committed Oct 7, 2018
1 parent 997b54c commit 43b8a7e
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Stub/BuildNo.rc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define BUILD_NUMBER 204
#define BUILD_NUMBER 205
1 change: 1 addition & 0 deletions Stub/Logging/Logging.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include <iostream>
#include <fstream>

// Send logging to standard out
namespace Logging
Expand Down
52 changes: 52 additions & 0 deletions Stub/Stub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,26 @@
#include "..\Settings\ReadParse.h"
#include "..\Wrappers\wrapper.h"
#include "Logging\Logging.h"
#include "..\Hooking\Hook.h"

std::ofstream Logging::Log::LOG("stub.log");

std::string RealDllPath; // Manually set Dll to wrap
std::string WrapperMode; // Name of dxwrapper

const char *HookedDllName = "quartz.dll";

#define VISIT_PROCS_HOOKED_DLL(visit) \
visit(AMGetErrorTextA) \
visit(AMGetErrorTextW) \
visit(AmpFactorToDB) \
visit(DBToAmpFactor) \
visit(DllCanUnloadNow) \
visit(DllGetClassObject) \
visit(DllRegisterServer) \
visit(DllUnregisterServer) \
visit(GetProxyDllInfo)

// Set booloean value from string (file)
bool IsValueEnabled(char* name)
{
Expand Down Expand Up @@ -83,6 +97,44 @@ bool APIENTRY DllMain(HMODULE hModule, DWORD fdwReason, LPVOID lpReserved)

// Start wrapper
proxy_dll = Wrapper::CreateWrapper((RealDllPath.size()) ? RealDllPath.c_str() : nullptr, (WrapperMode.size()) ? WrapperMode.c_str() : nullptr, WrapperName.c_str());

// Load system dll
Logging::Log() << "Loading '" << HookedDllName << "' from System32...";
char path[MAX_PATH];
GetSystemDirectoryA(path, MAX_PATH);
strcat_s(path, MAX_PATH, "\\");
strcat_s(path, MAX_PATH, HookedDllName);
HMODULE sys_dll = LoadLibraryA(path);
if (!sys_dll)
{
Logging::Log() << "Error: Failed to load dll from System32...";
return true;
}

// Load local dll
Logging::Log() << "Loading local '" << HookedDllName << "'...";
GetModuleFileNameA(hModule, path, MAX_PATH);
strcpy_s(strrchr(path, '\\'), MAX_PATH - strlen(path), "\\");
strcat_s(path, MAX_PATH, HookedDllName);
Logging::Log() << "Loading dll from: " << path << "";
HMODULE loc_dll = LoadLibraryA(path);
if (!loc_dll)
{
Logging::Log() << "Error: Failed to load dll locally...";
return true;
}

// Hooking system dll
FARPROC procAddr = nullptr;

#define HOOK_PROC(procName) \
procAddr = Hook::GetProcAddress(loc_dll, #procName); \
if (procAddr) \
{ \
Hook::HotPatch(Hook::GetProcAddress(sys_dll, #procName), #procName, procAddr, true); \
}

VISIT_PROCS_HOOKED_DLL(HOOK_PROC);
}
break;
case DLL_PROCESS_DETACH:
Expand Down
5 changes: 4 additions & 1 deletion Stub/stub.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ copy /Y "$(TargetDir)$(TargetName).dll" "$(TargetDir)Build\Stub\winmmbase.dll" &
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\External\MemoryModule\MemoryModule.c" />
<ClCompile Include="..\Hooking\Hook.cpp" />
<ClCompile Include="..\Hooking\HotPatch.cpp" />
<ClCompile Include="..\Hooking\IATPatch.cpp" />
<ClCompile Include="..\Settings\ReadParse.cpp" />
<ClCompile Include="..\Wrappers\wrapper.cpp" />
<ClCompile Include="stub.cpp" />
Expand All @@ -141,6 +143,7 @@ copy /Y "$(TargetDir)$(TargetName).dll" "$(TargetDir)Build\Stub\winmmbase.dll" &
<ResourceCompile Include="stub.rc" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\Hooking\Hook.h" />
<ClInclude Include="..\Wrappers\bcrypt.h" />
<ClInclude Include="..\Wrappers\cryptsp.h" />
<ClInclude Include="..\Wrappers\d2d1.h" />
Expand Down
16 changes: 15 additions & 1 deletion Stub/stub.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@
<ClCompile Include="..\Wrappers\wrapper.cpp">
<Filter>Wrappers</Filter>
</ClCompile>
<ClCompile Include="..\External\MemoryModule\MemoryModule.c" />
<ClCompile Include="..\Hooking\Hook.cpp">
<Filter>Hooking</Filter>
</ClCompile>
<ClCompile Include="..\Hooking\HotPatch.cpp">
<Filter>Hooking</Filter>
</ClCompile>
<ClCompile Include="..\Hooking\IATPatch.cpp">
<Filter>Hooking</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="stub.h" />
Expand Down Expand Up @@ -85,6 +93,9 @@
<ClInclude Include="Logging\Logging.h">
<Filter>Logging</Filter>
</ClInclude>
<ClInclude Include="..\Hooking\Hook.h">
<Filter>Hooking</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="BuildNo.rc" />
Expand All @@ -97,6 +108,9 @@
<Filter Include="Logging">
<UniqueIdentifier>{45e2f2d8-f49d-43e6-8fa0-10e67d6c4c44}</UniqueIdentifier>
</Filter>
<Filter Include="Hooking">
<UniqueIdentifier>{5114cd5d-904b-4e8a-ae6f-08c062d7cde9}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<None Include="..\Wrappers\wrapper.def">
Expand Down

0 comments on commit 43b8a7e

Please sign in to comment.