How brahma-adshonor/gohook works
Main Components
• Library API Layer: Represent the core exported functions (Hook, UnHook, HookByIndirectJmp, HookMethod, UnHookMethod) as the public interface. This is the primary entry point for consumers.
• Function Address Discovery: A component that employs the standard reflect library to obtain function addresses.
• Code Injection Engine: The module or set of functions (for instance, the routines in arch_util.go, elf_helper.go, hook.go) that craft and inject the jump instructions. This module is engineering the “hot patching” technique.
• Trampoline Mechanism: A dedicated module or routine (as seen in the examples and trampoline functions) that forms a “shadow” copy of the original function, enabling callbacks.
• Platform Specific Utilities: Highlight the use of utility files tailored to different OSes (utility_darwin.go, utility_linux.go, utility_unix.go, utility_windows.go). This indicates that the hooking process is sensitive to operating system specifics.
• Build & CI/CD Pipelines: Acknowledge the presence of GitHub workflows (.github/workflows/go.yml), azure-pipelines.yml, and perhaps even bug.sh. They are not runtime components but are key for ensuring integrity across multiple environments.
• Calculation/Gathering of CPU/ABI-critical data (possibly implemented with guidance from arch_util.go and elf_helper.go).
• Branching to either a direct jump or an indirect jump approach (the latter handling closures and reflect.MakeFunc() cases via HookByIndirectJmp).
• Parallel to code injection, indicate the creation/setup of the “Trampoline Function”. This sub-module holds a copy of the original function to allow callbacks.
Architectural Patterns and Design Principles
• It utilizes dynamic reflection and binary patching which can be considered “runtime instrumentation.”
• For testability and ease of integration, clearly segregate the test harness and example implementations.
• Use annotations for important caveats (like inlining risks or 32-bit limitations) which are mentioned in the README as side notes.