Detours 的使用

Detours 是一个用于在 ARM, ARM64, X86, X64 和 IA64 机器上拦截二进制函数的库。 Detours 最常用来拦截应用程序中的 win32 api 调用,比如添加调试工具。 拦截代码在运行时动态应用。

Detours 将目标函数的前几个指令替换为无条件跳转到用户提供的 detour 函数

它与 WriteProcessMemory 有所不同

区别:

  • WriteProcessMemory 是 Windows API 中的一个函数,用于将数据写入到指定进程的内存空间中。通常情况下,这个函数被用于与进程间通信,比如注入 DLL、修改其他进程的内存内容等。这个函数能够在一定程度上用于修改其他进程的行为,因此在一些涉及软件修改的场景中被使用。
  • Detours 是 Microsoft Research 开发的一种库,用于在 Windows 平台上修改函数调用的行为。它允许开发者截获特定函数的调用,并将其重定向到自定义的函数中,从而实现对目标函数的修改、监视和扩展。Detours 在软件开发中被广泛用于实现各种目的,包括代码注入、API hooking、性能分析等。

结合使用:

  • 在某些情况下,可以将 WriteProcessMemory 和 Detours 结合使用,以实现对其他进程的函数调用的修改和扩展。例如,可以使用 WriteProcessMemory 在目标进程中注入 DLL,然后使用 Detours 修改目标进程的某些函数调用,以实现一些特定的行为,比如游戏作弊、反作弊等。
  • 通常情况下,Detours 会更适合用于修改目标进程内的函数调用,而不是直接修改其内存内容。因此,结合 WriteProcessMemory 和 Detours 使用时,可能会将 Detours 用于修改函数调用,而使用 WriteProcessMemory 用于注入所需的代码或 DLL。

项目上我们将其用于 hook 播放器声音,利用 UDP 将数据传回给我们的软件用于播放和传输

主要用到 DetourCreateProcessWithDllEx

功能:Create a new process and load a DLL into it. Chooses the appropriate 32-bit or 64-bit DLL based on the target process.

它可以根据目标进程自动加载 32-bit DLL 或者 64-bit DLL

方法:

To support both 32-bit and 64-bit applications on a single system, you must create two DLLs. One DLL must contain 32-bit code, the other DLL must contain 64-bit code. The DLLs must reside in the same directory and must have identical names except that the name of the 32-bit DLL should end with "32" and the name of the 64-bit DLL should end with "64". For example, matching DLLs would be named foo32.dll and foo64.dll.

You should use the DetourCreateProcessWithDllEx or DetourCreateProcessWithDlls API to start a process with your DLL. Furthermore, your DLLs must:

Export the DetourFinishHelperProcess API as export ordinal 1.

Call the DetourIsHelperProcess API in your DllMain function. Immediately return TRUE if DetourIsHelperProcess return TRUE.

Call the DetourCreateProcessWithDllEx or DetourCreateProcessWithDlls API instead of DetourCreateProcessWithDll to create new target processes.

文档参考:https://github.com/microsoft/detours/wiki/OverviewHelpers

demo 参考:https://github.com/microsoft/Detours/blob/main/samples/withdll/withdll.cpp

相关文章:

 

posted @ 2024-02-19 11:15  strive-sun  阅读(74)  评论(0编辑  收藏  举报