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/Section and payloads

From ReverseEngineering.wiki

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 load a DLL.

The .detour section

The section name is .detour (creatwth.cpp writes it into a helper image, image.cpp inserts it when you edit a binary with setdll). The names .detourd and .detourc are not payload sections: disasm.cpp only uses #pragma data_seg(".detourd") and const_seg(".detourc") to place the disassembler's tables of the Detours library itself in sections of that name. Finding them in a module means that module contains the Detours code.

Contents: Fact:Microsoft Detours/struct/DETOUR_SECTION_HEADER, then cbPrePE bytes of saved data, then payload records (Fact:Microsoft Detours/struct/DETOUR_SECTION_RECORD), each tagged by a GUID. DetourFindPayload(hModule, guid, &size) walks the records of a module's .detour section; DetourEnumerateExports and friends are in the same API. See Fact:Microsoft Detours/string/Payload GUIDs.

DetourCreateProcessWithDll(Ex)

Creates the process suspended (the flag is added to your creation flags), then for same-bitness targets edits the target's in-memory image: it saves the original import table, bound imports, IAT, size of image and CLR flags in a DETOUR_EXE_RESTORE blob (copied into the process under the restore GUID), builds a new import table in a new block that lists your DLL first, and resumes. The injected DLL is imported by ordinal 1: when src/image.cpp inserts the import it allocates the lookup and bound thunks as IMAGE_ORDINAL_FLAG+1 (lines 1834-1836, microsoft/Detours commit 1dbd4ea281d73d478aa8d09a8b83fbd265f715f7), so the DLL has to export something at ordinal 1. The helper command line uses the same ordinal: src/creatwth.cpp builds rundll32.exe "%s",#1 (lines 1385 and 1490). Unverified: that the Detours sample DLLs export the dummy Detoured at that ordinal (not found by grep in src/). In the DLL's DllMain, DetourRestoreAfterWith() restores the original import table so the program continues normally.

For a target of different bitness, Detours starts a helper: rundll32.exe "<dll>",#1 from sysnative/syswow64/system32 (command line built in creatwth.cpp), passing the DLL list as a payload under DETOUR_EXE_HELPER_GUID; the helper entry point is DetourFinishHelperProcess, which the 32-bit/64-bit Detours DLL must export as ordinal 1. DetourIsHelperProcess() tells your DllMain it was loaded this way.

Identification hints

  • A .detour section in an exe usually means it was patched with setdll or a similar tool; the saved import table lets you recover the original one.
  • A DLL exporting a function at ordinal 1 whose name is Detoured or similar, calling DetourTransactionBegin, is a Detours injection module.
  • Related technique pages: Technique:Function hooking, Technique:DLL proxy loading.

Unverified: details of the import table rewrite beyond what the function names show were not traced line by line.