Jump to content
Setting up an AI agent? ReverseEngineering.wiki skill (works from the command line, no configuration): https://reverseengineering.wiki/skill/rewiki/SKILL.md · setup for every client, including MCP: https://reverseengineering.wiki/llms.txt · REWiki:Connect

Library:Microsoft Detours

From ReverseEngineering.wiki
Library
Vendor Microsoft
Platforms Windows x86, x64, ARM, ARM64
License MIT
Website https://github.com/microsoft/Detours
On this wiki Facts · Code · Notes · Topics

Detours is Microsoft's MIT-licensed library for intercepting Win32 functions: it rewrites the first bytes of a target with a jump to your function and keeps a trampoline to call the original, and can also start a process with an extra DLL injected through a modified import table.

Overview

Public API (detours.h): DetourTransactionBegin, DetourUpdateThread, DetourAttach(&(PVOID&)pTarget, pDetour), DetourDetach, DetourTransactionCommit. The transaction suspends listed threads, patches all targets, and fixes thread instruction pointers that sit inside overwritten bytes. Version in the source is 4.0.1 (DETOURS_VERSION 0x4c0c1, format 0xMAJORcMINORcPATCH). Compared with Library:MinHook: Detours is the reference implementation, supports ARM/ARM64 and process creation with DLLs, MinHook is smaller and has no transactions. General background: Technique:Function hooking.

Trampoline layout

See Fact:Microsoft Detours/struct/DETOUR_TRAMPOLINE x86 x64. Targets are patched with E9 rel32 (5 bytes, SIZE_OF_JMP). On x64 the trampoline allocator looks for 64 KiB regions within +-2 GiB of the target (and skips the system DLL region 0x70000000 to 0x80000000 range); the rel32 jump goes to the trampoline's rbCodeIn, which is FF 25 jump through pbDetour; the moved instructions end with an FF 25 jump through pbRemain back into the target. Trampolines live in regions of 0x10000 bytes whose first bytes are a header with the signature 'Rrtd' (bytes 64 74 72 52 in memory on little endian, i.e. "dtrR"), a next-region pointer and a free list. detours.h also defines DETOUR_TRAMPOLINE_SIGNATURE 0x21727444 ("Dtr!"); in the version read it is only a constant, not written into the trampoline structure. The hook layout is in Fact:Microsoft Detours/struct/DETOUR_TRAMPOLINE x86 x64.

Identification: when a function starts with E9 into an executable allocation that begins with moved prologue bytes and ends with FF 25, it is probably Detours or a similar library.

Detour section, payloads and DLL injection

See Library:Microsoft Detours/Section and payloads for the .detour PE section, payload GUIDs and DetourCreateProcessWithDll.

Tooling

The samples build withdll.exe (start a program with a DLL), setdll.exe (edit an exe's import table to add a DLL), sleep5, traceapi and others.

Topics

  • Section and payloads — How Detours stores payloads in a PE file or a target process (the .detour section, GUID-tagged records) and how DetourCreateProcessWithDll makes a new process…

Code & repositories

See below.

  • https://github.com/microsoft/Detours @ 1dbd4ea281d73d478aa8d09a8b83fbd265f715f7 · license: MIT
    Microsoft Detours source (MIT): detours.h/.cpp trampolines, creatwth.cpp process creation with DLL, image.cpp .detour section editing.
    Tested on: source read, not run: source read, not run · checked 2026-10-18; last activity 2026-10-14