简体   繁体   English

如何将对 ole32.dll 的调用重定向到我自己的代理 DLL?

[英]How do I redirect calls to ole32.dll to my own proxy DLL?

I'm trying to detect all calls to CoCreateInstance in some process I'm starting (ideally, I'm able to detect calls in child processes as well).我正在尝试在我开始的某个进程中检测对CoCreateInstance的所有调用(理想情况下,我也能够检测到子进程中的调用)。

To achieve this, using Microsoft Visual Studio 2008 on Windows 7, I create a proxy DLL which forwards all but one call in the standard ole32.dll library as described in various articles, eg Intercepted: Windows Hacking via DLL Redirection . To achieve this, using Microsoft Visual Studio 2008 on Windows 7, I create a proxy DLL which forwards all but one call in the standard ole32.dll library as described in various articles, eg Intercepted: Windows Hacking via DLL Redirection . The resulting DLL looks fine, but I just can't make existing programs (I'm using the standard ActiveX Control Test Container (tstcon32.exe) as a test application) pick up my proxy DLL.生成的 DLL 看起来不错,但我无法让现有程序(我使用标准ActiveX 控制测试容器 (tstcon32.exe)作为测试应用程序)拿起我的代理 DLL。 No matter what I do, the programs always seem to pick up C:\Windows\SysWow64\ole32.dll according to Process Explorer .无论我做什么,程序似乎总是根据Process Explorer选择C:\Windows\SysWow64\ole32.dll I tried a few things so far:到目前为止,我尝试了几件事:

  1. Prepend the directory which contains my proxy DLL to the PATH and then invoke the program;将包含我的代理 DLL 的目录添加到PATH中,然后调用程序; didn't seem to have any effect.似乎没有任何效果。
  2. Copy my proxy DLL into the same directory as the invoked program;将我的代理 DLL 复制到与调用程序相同的目录中; no luck.没运气。
  3. Create a .local file in the same directory as the invoked program as described in the Dynamic-Link Library Redirection article and put my proxy DLL into the same directory - didn't work either.动态链接库重定向文章中所述,在与调用程序相同的目录中创建一个.local文件,并将我的代理 DLL 放入同一目录 - 也不起作用。 But then, I read that this stopped working on more recent Windows versions.但是后来,我读到这停止了对更新的 Windows 版本的工作。 Additionally, ole32.dll is a "known DLL" according to the HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\KnownDLLs registry setting, so .local -based redirection is probably not going to work anyway.此外,根据HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\KnownDLLs注册表设置, ole32.dll是“已知 DLL”,因此基于.local的重定向可能无论如何都不起作用。
  4. Use manifest-based redirection as described eg in the DLL redirection using manifests question, but that didn't seem to have any effect either.使用基于清单的重定向,如DLL 重定向使用清单问题中所述,但这似乎也没有任何效果。 However, this approach seems to be non-trivial, so chances are I did something wrong.但是,这种方法似乎很重要,所以我很可能做错了什么。

Does anybody have experience with redirecting calls to standard DLLs such as ole32.dll using a stub DLL?是否有人有使用存根 DLL 将调用重定向到标准 DLL(例如ole32.dll的经验? How did you force the applications to pick up your stub DLL?您如何强制应用程序获取您的存根 DLL?

I realise this is a little late by about 6 months, but I was trying the same thing and have some additional notes:我意识到这有点晚了大约 6 个月,但我正在尝试同样的事情并有一些额外的注意事项:

  1. You can take ownership of and remove ole32.dll from HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\KnownDLLs .您可以从HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\KnownDLLs获得并删除ole32.dll的所有权。 This allows you to get around the fact Windows has locked these keys.这使您可以绕过 Windows 已锁定这些键的事实。
  2. Creating a key SafeDllSearch with the value 0 in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager is supposed to alter the search path .HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager中创建一个值为0的键SafeDllSearch 应该会改变搜索路径

Having applied both these techniques and rebooting, hooking still did not work.应用了这两种技术并重新启动后,挂钩仍然不起作用。 I went one further, booted up a VM with one of our rescue CDs (a Windows PE based environment) and overwrote the one in system32 .我更进一步,使用我们的一张救援 CD(基于 Windows PE 的环境)启动了一个 VM,并覆盖了system32中的那个。 Windows does not boot as a result - no symbol errors, but I never get as far as LogonUI.exe . Windows 结果无法启动 - 没有符号错误,但我从来没有达到LogonUI.exe It is possible my hooked functions are broken, so this may be the cause.我的钩子函数可能被破坏了,所以这可能是原因。

Anyway, that produced an actual, tangible hook effect - albeit one that screams "broken"., Unfortunately it appears highly difficult to debug, and I may be resorting to the other method of hooking - namely IAT patching .无论如何,这产生了一种实际的、有形的钩子效果——尽管它尖叫着“坏了”。不幸的是,它似乎很难调试,我可能会求助于另一种钩子方法——即IAT 补丁

Edit another experiment I performed was to explicitly load the Dll myself into the target process' address space.编辑我执行的另一个实验是我自己将 Dll 显式加载到目标进程的地址空间中。 A snippet of code that does this looks like this:执行此操作的代码片段如下所示:

wchar_t* TargetPath = argv[1];
wchar_t DllPath[] = L"N:\\experiments\\ole32.dll";
STARTUPINFOW si;
PROCESS_INFORMATION pi;
memset(&si, 0, sizeof(STARTUPINFOW));
memset(&pi, 0, sizeof(PROCESS_INFORMATION));

// create process suspended
BOOL bResult = CreateProcess(NULL, TargetPath, NULL, NULL, FALSE, 
    CREATE_SUSPENDED, NULL, NULL, &si, &pi);

// write DLL name to remote process
void* RemoteAddr = VirtualAllocEx(pi.hProcess, NULL, sizeof(DllPath)+1, 
    MEM_RESERVE | MEM_COMMIT, PAGE_READONLY);
WriteProcessMemory(pi.hProcess, RemoteAddr, DllPath, sizeof(DllPath), &BytesWritten);

// get handle to LoadLibraryW
PTHREAD_START_ROUTINE pfLoadLibrary = (PTHREAD_START_ROUTINE) 
    GetProcAddress(GetModuleHandle(L"kernel32.dll"), "LoadLibraryW");

// create remote thread calling LoadLibraryW
HANDLE hThread = CreateRemoteThread(pi.hProcess, NULL, 
    0, pfLoadLibrary, RemoteAddr, 0, NULL);

// start remote process
ResumeThread(pi.hThread);

Error handling removed for brevity.为简洁起见,删除了错误处理。

Basically, the objective was to force load my ole32.dll into the target's address space before it had chance to load ole32.dll from system32.基本上,目标是在它有机会从 system32 加载ole32.dll之前强制将我的ole32.dll加载到目标的地址空间中。 In my case, ole32.dll was being loaded later on in the application's load routine, so this in theory should have worked.就我而言, ole32.dll稍后会在应用程序的加载例程中加载,所以理论上这应该有效。 In practice, it did not.在实践中,它没有。 I am not sure why.我不确定为什么。

Update My original code failed because the DLL had unresolved symbol warnings at runtime.更新我的原始代码失败,因为 DLL 在运行时出现未解决的符号警告。 This technique does work So apparently, it loads both my ole32.dll AND the one from system32. 这种技术确实有效 所以显然,它加载了我的ole32.dll和来自 system32 的那个。 To ensure the library was loading successfully, I added a LoadLibrary(DllPath) call to the code above.为了确保库加载成功,我在上面的代码中添加了一个LoadLibrary(DllPath)调用。

Perhaps winapioverride can help you.也许winapioverride可以帮助你。 It can log all win api calls without programming anything.它可以记录所有 win api 调用,而无需进行任何编程。 It therefore injects dlls to the process that do the logging.因此,它将 dll 注入到执行日志记录的进程中。 If I recall it correctly it is also possible to inject own custom dlls - even before the process actually executes any code.如果我没记错的话,也可以注入自己的自定义 dll——甚至在进程实际执行任何代码之前。 The documentation has some information about spying com objects.该文档有一些关于监视 com 对象的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM