{"id":19430259,"url":"https://github.com/8dcc/libdetour","last_synced_at":"2026-01-28T01:31:32.562Z","repository":{"id":183769659,"uuid":"670716205","full_name":"8dcc/libdetour","owner":"8dcc","description":"Simple C/C++ library for detour hooking in Linux and Windows","archived":false,"fork":false,"pushed_at":"2024-11-29T23:26:37.000Z","size":120,"stargazers_count":24,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-11-11T22:30:58.861Z","etag":null,"topics":["c","c-library","cpp","cpp-library","detour","detour-hook","hacking","hooking","hooking-library","linux","windows"],"latest_commit_sha":null,"homepage":"https://8dcc.github.io/programming/detour-hooking.html","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/8dcc.png","metadata":{"files":{"readme":"README.org","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-07-25T17:07:08.000Z","updated_at":"2025-09-29T13:03:17.000Z","dependencies_parsed_at":null,"dependency_job_id":"a45e87de-1585-4a58-97a9-00a3600e164f","html_url":"https://github.com/8dcc/libdetour","commit_stats":null,"previous_names":["8dcc/32bit-detour","8dcc/detour-lib"],"tags_count":7,"template":false,"template_full_name":"8dcc/c-template","purl":"pkg:github/8dcc/libdetour","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/8dcc%2Flibdetour","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/8dcc%2Flibdetour/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/8dcc%2Flibdetour/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/8dcc%2Flibdetour/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/8dcc","download_url":"https://codeload.github.com/8dcc/libdetour/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/8dcc%2Flibdetour/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28831549,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-27T23:29:49.665Z","status":"ssl_error","status_checked_at":"2026-01-27T23:25:58.379Z","response_time":168,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","c-library","cpp","cpp-library","detour","detour-hook","hacking","hooking","hooking-library","linux","windows"],"created_at":"2024-11-10T14:24:09.602Z","updated_at":"2026-01-28T01:31:32.535Z","avatar_url":"https://github.com/8dcc.png","language":"C","readme":"#+title: Detour hooking library\n#+author: 8dcc\n#+options: toc:nil\n#+startup: showeverything\n\n*Simple C/C++ library for detour hooking in Linux and Windows.*\n\n#+TOC: headlines 2\n\n* Description\n\nThis library is for detour hooking. For more information on how it works, check\nout my [[https://8dcc.github.io/programming/detour-hooking.html][blog entry]]. It supports x86 and x64 architectures.\n\nCurrently, this library supports both windows and unix-like systems, since the\nonly OS-specific function is =protect_addr()=.\n\nThis library was originally made for [[https://github.com/8dcc/hl-cheat][8dcc/hl-cheat]], but I ended up using it in\nmultiple projects (like [[https://github.com/8dcc/devildaggers-re][8dcc/devildaggers-re]]). It was inspired by [[https://guidedhacking.com/threads/simple-linux-windows-detour-class.10580/][this OOP\nabomination]] ([[https://gist.github.com/8dcc/d0cbef32cd46ab9c73c6f830fa71d999][mirror]]).\n\n* Performance\n\nBecause of its simplicity, this library is really fast. Other hooking methods\nlike VMT hooking have basically zero performance impact by design, but their use\ncase is way more specific.\n\nFirst, a note about compiler optimizations. This library works fine in projects\ncompiled with =-O2= and =-O3=, but because all the functions in this example are\ninside =main.c=, the compiler optimizes the calls so the hooking never occurs.\nThis can be proven by moving the =foo= and =my_hook= functions to a separate source,\nso the compiler can't optimize the calls when compiling =main.c=.\n\nThe library only needs to enable write permissions for the memory region of the\nfunction, write 7 or 12 bytes (x86/x64) and remove the write permission. All\nthis is explained in more detail in the article I linked above.\n\n* Building the example\n\nIf you want to use this library, simply copy the =libdetour.c= source and\n=libdetour.h= headers to your project, include the header in your source files and\ncompile the libdetour source with the rest of your code. Please see [[https://github.com/8dcc/libdetour/blob/main/src/main.c][src/main.c]]\nand the /Usage/ section for an example on how to use it.\n\nIf you want to try the example, simply run:\n\n#+begin_src console\n$ git clone https://github.com/8dcc/libdetour\n$ cd libdetour\n$ make\n...\n\n$ ./libdetour-test.out\nmain: hooked, calling `foo'...\nmy_hook: got values 5.0 and 2.0\nmy_hook: calling original with custom values...\nfoo: 9.5 + 1.5 = 11.0\nmy_hook: calling with original values...\nfoo: 5.0 + 2.0 = 7.0\nmy_hook: original returned 7.0\nmy_hook: returning custom value...\nmain: hooked `foo' returned 420.0\n\nmain: unhooked, calling `foo' again...\nfoo: 11.0 + 3.0 = 14.0\nmain: unhooked `foo' returned 14.0\n#+end_src\n\nAlternatively, you can compile the 32-bit test with ~make clean all-32bit~.\n\n* Usage\n\nSince you will probably want to call the original function from your hook, you\nwill need to specify the type and arguments of the original function with the\n=DETOUR_DECL_TYPE= macro. This macro will =typedef= an internal type used by the\n=DETOUR_ORIG_CALL= and =DETOUR_ORIG_GET= macros, so you can skip this step if you\ndon't need to use them.\n\n#+begin_src C\n/* int orig(double a, double b); */\nDETOUR_DECL_TYPE(int, orig, double, double);\n#+end_src\n\nYou will also need to declare a =detour_ctx_t= structure. Again, since you\nprobably want to use this structure from multiple places, it's a good idea to\ndeclare it globally.\n\n#+begin_src C\ndetour_ctx_t detour_ctx;\n#+end_src\n\nThen, initialize the context structure by calling =detour_init= with a pointer to\nthe original function and a pointer to your hook function:\n\n#+begin_src C\nvoid* orig_ptr = \u0026orig;    /* orig(...) */\nvoid* hook_ptr = \u0026my_hook; /* my_hook(...) */\n\n/* Initialize the detour context. */\ndetour_init(\u0026detour_ctx, orig_ptr, hook_ptr);\n\n/* Hook to the original function. */\ndetour_enable(\u0026detour_ctx);\n#+end_src\n\nIf you want to call the original function from =my_hook=, you can use one of the\nfollowing macros:\n\n- =DETOUR_ORIG_CALL=: Calls the original function, ignoring the returned value.\n- =DETOUR_ORIG_GET=: Takes an extra parameter used for storing the return value.\n\n#+begin_src C\ndouble my_hook(double a, double b) {\n    /* Call original ignoring return. */\n    DETOUR_ORIG_CALL(\u0026detour_ctx, orig, a, b);\n\n    /* Store return value in the `result' variable. */\n    double result;\n    DETOUR_ORIG_GET(\u0026detour_ctx, result, orig, a, b);\n\n    /* Our hook can overwrite the value returned by `orig'. */\n    return 123.0;\n}\n#+end_src\n\nOnce we are done, we can call =detour_del= to unhook the =orig= function.\n\n#+begin_src C\n/* Disable the hook. */\ndetour_del(\u0026detour_ctx);\n#+end_src\n\nIf we call the =orig= function again, the =my_hook= function will /not/ be called.\n\nFor a full working example, see [[https://github.com/8dcc/libdetour/blob/main/src/main.c][src/main.c]].\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F8dcc%2Flibdetour","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F8dcc%2Flibdetour","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F8dcc%2Flibdetour/lists"}