create MIN_HOOK c wrapper for OLEAUT32.dll GetErrorInfo SysFreeString that does this:…
- User
create MIN_HOOK c wrapper for OLEAUT32.dll GetErrorInfo SysFreeString that does this: ```/* SignToolExDll.dll
- =================
- Hooks the time related functions to automagically permit
- using expired certificates with SignTool.exe. This is the
- detours library function code for changing expiry time to
- make a certificate appear valid and then spoofing the system
- time check to sign a file using an expired certificate.
- -- HF @ https://hacker.house */ #include <windows.h> #include "pch.h"
// Link with detours.lib #pragma comment (lib, "detours.lib")
// Original function pointers static BOOL(WINAPI* TrueFileTimeToSystemTime)(const FILETIME* lpFileTime, LPSYSTEMTIME lpSystemTime) = FileTimeToSystemTime; static LONG(WINAPI* TrueCompareFileTime)(const FILETIME* lpFileTime1, const FILETIME* lpFileTime2) = CompareFileTime;
// Detour functions BOOL WINAPI DetourFileTimeToSystemTime(const FILETIME* lpFileTime, LPSYSTEMTIME lpSystemTime) { BOOL result = TrueFileTimeToSystemTime(lpFileTime, lpSystemTime); if (result) { // In the year 3001, Microsoft code signing is broken. lpSystemTime->wYear = 3000; } return result; }
LONG WINAPI DetourCompareFileTime(const FILETIME* lpFileTime1, const FILETIME* lpFileTime2) { return 0; }
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: { DetourRestoreAfterWith(); DetourTransactionBegin(); DetourUpdateThread(GetCurrentThread()); DetourAttach(&(PVOID&)TrueFileTimeToSystemTime, DetourFileTimeToSystemTime); DetourAttach(&(PVOID&)TrueCompareFileTime, DetourCompareFileTime); DetourTransactionCommit(); break; } case DLL_PROCESS_DETACH: { DetourTransactionBegin(); DetourUpdateThread(GetCurrentThread()); DetourDetach(&(PVOID&)TrueFileTimeToSystemTime, DetourFileTimeToSystemTime); DetourDetach(&(PVOID&)TrueCompareFileTime, DetourCompareFileTime); DetourTransactionCommit(); break; } } return TRUE; }
- Assistant
Here's a C wrapper using MinHook to hook
GetErrorInfoandSysFreeStringfrom OLEAUT32.dll, following a similar structure to your Detours example. MinHook is a lightweight hooking library that is easier to use compared to Detours, and it works well for this purpose.Steps to Create the Wrapper
- Include MinHook: Ensure you have MinHook installed and linked in your project.
- Define Function Pointers: Create pointers to the original functions.
- Implement Hook Functions: Define the detour/hook functions to modify behavior.
- Initialize and Cleanup Hooks: Use
DllMainto initialize and clean up the hooks.
Below is the complete code for the MinHook-based wrapper:
/* OLEAUT32HookDll.c * ================= * Hooks OLEAUT32.dll functions GetErrorInfo and SysFreeString. * This example demonstrates how to use MinHook to intercept and modify * the behavior of these functions. * * -- HF @ https://hacker.house */ #include <windows.h> #include <oleauto.h> #include "MinHook.h" // Include MinHook header // Original function pointers static HRESULT(WINAPI* TrueGetErrorInfo)(ULONG dwReserved, IErrorInfo** ppIErrorInfo) = NULL; static void(WINAPI* TrueSysFreeString)(BSTR bstrString) = NULL; // Detour/hook functions HRESULT WINAPI DetourGetErrorInfo(ULONG dwReserved, IErrorInfo** ppIErrorInfo) { // Example modification: Always return S_OK and set ppIErrorInfo to NULL // This effectively disables error information retrieval if (ppIErrorInfo) { *ppIErrorInfo = NULL; } return S_OK; } void WINAPI DetourSysFreeString(BSTR bstrString) { // Example modification: Do nothing (prevent freeing the string) // This is just for demonstration; be cautious as it may cause memory leaks return; } // Function to initialize hooks static BOOL InitializeHooks() { // Initialize MinHook if (MH_Initialize() != MH_OK) { return FALSE; } // Get the address of the original functions from OLEAUT32.dll HMODULE hOleAut32 = GetModuleHandleW(L"OLEAUT32.dll"); if (!hOleAut32) { hOleAut32 = LoadLibraryW(L"OLEAUT32.dll"); if (!hOleAut32) { return FALSE; } } TrueGetErrorInfo = (HRESULT(WINAPI*)(ULONG, IErrorInfo**))GetProcAddress(hOleAut32, "GetErrorInfo"); TrueSysFreeString = (void(WINAPI*)(BSTR))GetProcAddress(hOleAut32, "SysFreeString"); if (!TrueGetErrorInfo || !TrueSysFreeString) { return FALSE; } // Create hooks for GetErrorInfo and SysFreeString if (MH_CreateHook(TrueGetErrorInfo, DetourGetErrorInfo, (LPVOID*)&TrueGetErrorInfo) != MH_OK || MH_CreateHook(TrueSysFreeString, DetourSysFreeString, (LPVOID*)&TrueSysFreeString) != MH_OK) { return FALSE; } // Enable the hooks if (MH_EnableHook(TrueGetErrorInfo) != MH_OK || MH_EnableHook(TrueSysFreeString) != MH_OK) { return FALSE; } return TRUE; } // Function to cleanup hooks static void CleanupHooks() { // Disable hooks MH_DisableHook(TrueGetErrorInfo); MH_DisableHook(TrueSysFreeString); // Uninitialize MinHook MH_Uninitialize(); } BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: if (!InitializeHooks()) { return FALSE; } break; case DLL_PROCESS_DETACH: CleanupHooks(); break; case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: break; } return TRUE; }Explanation of the Code
-
Function Pointers:
TrueGetErrorInfoandTrueSysFreeStringare pointers to the original functions inOLEAUT32.dll. These are used to call the original functions if needed.
-
Detour Functions:
DetourGetErrorInfo: This example hook always returnsS_OKand sets theIErrorInfopointer toNULL, effectively disabling error information retrieval. You can modify this behavior as needed.DetourSysFreeString: This example hook does nothing, preventing the freeing ofBSTRstrings. Be cautious with this, as it can cause memory leaks. This is just for demonstration; in a real application, you might want to call the original function (TrueSysFreeString) under certain conditions.
-
Hook Initialization:
InitializeHooks: This function initializes MinHook, retrieves the addresses of the original functions, creates hooks, and enables them.- MinHook requires explicit initialization (
MH_Initialize) and cleanup (MH_Uninitialize).
-
Hook Cleanup:
CleanupHooks: This function disables the hooks and uninitializes MinHook.
-
DllMain:
- The hooks are initialized when the DLL is loaded (
DLL_PROCESS_ATTACH) and cleaned up when the DLL is unloaded (DLL_PROCESS_DETACH).
- The hooks are initialized when the DLL is loaded (
Building the Project
To build this DLL, you need to:
-
Install MinHook:
- Download MinHook from its GitHub repository or use a package manager like vcpkg.
- Link against
libMinHook.lib.
-
Link Libraries:
- Link against
oleaut32.libfor the OLE automation functions.
- Link against
-
Compiler Settings:
- Ensure your project is set up to compile as a DLL.
- Use a C compiler (e.g., MSVC).
Example Usage
Once compiled, you can inject this DLL into a target process (e.g., using a DLL injector) to hook
GetErrorInfoandSysFreeString. For example, you might use this to debug or modify the behavior of an application that relies on these functions.Notes and Warnings
- Memory Leaks: The
DetourSysFreeStringexample prevents freeingBSTRstrings, which can cause memory leaks. In a real application, you should carefully consider whether to call the original function. - Stability: Hooking system functions can destabilize applications. Test thoroughly in a controlled environment.
- Security: Modifying system behavior (e.g., disabling error reporting) can have security implications. Use this code responsibly.
If you need further customization or have specific requirements, let me know!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.