{"id":315,"date":"2019-01-02T01:13:34","date_gmt":"2019-01-02T01:13:34","guid":{"rendered":"http:\/\/malwrforensics.com\/en\/?p=315"},"modified":"2019-01-02T01:13:34","modified_gmt":"2019-01-02T01:13:34","slug":"basic-api-hooking-using-detours","status":"publish","type":"post","link":"https:\/\/malwrforensics.com\/en\/2019\/01\/02\/basic-api-hooking-using-detours\/","title":{"rendered":"Basic API hooking using detours"},"content":{"rendered":"\n<p>Below is a basic example on how to use the <a href=\"https:\/\/github.com\/Microsoft\/Detours\">detours<\/a> library to hook APIs.<\/p>\n\n\n\n<pre style='color:#000000;background:#ffffff;'><span style='color:#004a43; '>#<\/span><span style='color:#004a43; '>include <\/span><span style='color:#800000; '>&lt;<\/span><span style='color:#40015a; '>stdio.h<\/span><span style='color:#800000; '>><\/span>\n<span style='color:#004a43; '>#<\/span><span style='color:#004a43; '>include <\/span><span style='color:#800000; '>&lt;<\/span><span style='color:#40015a; '>windows.h<\/span><span style='color:#800000; '>><\/span>\n<span style='color:#004a43; '>#<\/span><span style='color:#004a43; '>include <\/span><span style='color:#800000; '>&lt;<\/span><span style='color:#40015a; '>detours.h<\/span><span style='color:#800000; '>><\/span>\n\n<span style='color:#696969; '>\/\/ API that we want to hook <\/span>\n<span style='color:#603000; '>DWORD<\/span> <span style='color:#808030; '>(<\/span><span style='color:#603000; '>WINAPI<\/span> <span style='color:#808030; '>*<\/span> Real_SleepEx<span style='color:#808030; '>)<\/span><span style='color:#808030; '>(<\/span><span style='color:#603000; '>DWORD<\/span> dwMilliseconds<span style='color:#808030; '>,<\/span> <span style='color:#603000; '>BOOL<\/span> bAlertable<span style='color:#808030; '>)<\/span> <span style='color:#808030; '>=<\/span> <span style='color:#400000; '>SleepEx<\/span><span style='color:#800080; '>;<\/span>\n\n<span style='color:#696969; '>\/\/ This function will be called *before* the API.<\/span>\n<span style='color:#696969; '>\/\/ We will modify one of the parameters <\/span>\n<span style='color:#696969; '>\/\/ and then call the real API<\/span>\n<span style='color:#800000; font-weight:bold; '>static<\/span> <span style='color:#603000; '>DWORD<\/span> <span style='color:#603000; '>WINAPI<\/span> Catch_SleepEx<span style='color:#808030; '>(<\/span><span style='color:#603000; '>DWORD<\/span> dwMilliseconds<span style='color:#808030; '>,<\/span> <span style='color:#603000; '>BOOL<\/span> bAlertable<span style='color:#808030; '>)<\/span>\n<span style='color:#800080; '>{<\/span>\n    <span style='color:#603000; '>printf<\/span><span style='color:#808030; '>(<\/span><span style='color:#800000; '>\"<\/span><span style='color:#0000e6; '>SleepEx: <\/span><span style='color:#007997; '>%d<\/span><span style='color:#0f69ff; '>\\n<\/span><span style='color:#800000; '>\"<\/span><span style='color:#808030; '>,<\/span> dwMilliseconds<span style='color:#808030; '>)<\/span><span style='color:#800080; '>;<\/span>\n    dwMilliseconds <span style='color:#808030; '>=<\/span> <span style='color:#008c00; '>10<\/span><span style='color:#800080; '>;<\/span>\n    <span style='color:#603000; '>printf<\/span><span style='color:#808030; '>(<\/span><span style='color:#800000; '>\"<\/span><span style='color:#0000e6; '>SleepEx: new value = <\/span><span style='color:#007997; '>%d<\/span><span style='color:#0f69ff; '>\\n<\/span><span style='color:#800000; '>\"<\/span><span style='color:#808030; '>,<\/span> dwMilliseconds<span style='color:#808030; '>)<\/span><span style='color:#800080; '>;<\/span>    \n    <span style='color:#800000; font-weight:bold; '>return<\/span> Real_SleepEx<span style='color:#808030; '>(<\/span>dwMilliseconds<span style='color:#808030; '>,<\/span> bAlertable<span style='color:#808030; '>)<\/span><span style='color:#800080; '>;<\/span>\n<span style='color:#800080; '>}<\/span>\n\n<span style='color:#800000; font-weight:bold; '>int<\/span> <span style='color:#400000; '>main<\/span><span style='color:#808030; '>(<\/span><span style='color:#800000; font-weight:bold; '>int<\/span> argc<span style='color:#808030; '>,<\/span> <span style='color:#800000; font-weight:bold; '>char<\/span> <span style='color:#808030; '>*<\/span><span style='color:#808030; '>*<\/span>argv<span style='color:#808030; '>)<\/span>\n<span style='color:#800080; '>{<\/span>\n    <span style='color:#808030; '>(<\/span><span style='color:#800000; font-weight:bold; '>void<\/span><span style='color:#808030; '>)<\/span>argc<span style='color:#800080; '>;<\/span>\n    <span style='color:#808030; '>(<\/span><span style='color:#800000; font-weight:bold; '>void<\/span><span style='color:#808030; '>)<\/span>argv<span style='color:#800080; '>;<\/span>\n \n    DetourTransactionBegin<span style='color:#808030; '>(<\/span><span style='color:#808030; '>)<\/span><span style='color:#800080; '>;<\/span>\n    DetourUpdateThread<span style='color:#808030; '>(<\/span><span style='color:#400000; '>GetCurrentThread<\/span><span style='color:#808030; '>(<\/span><span style='color:#808030; '>)<\/span><span style='color:#808030; '>)<\/span><span style='color:#800080; '>;<\/span>\n    DetourAttach<span style='color:#808030; '>(<\/span><span style='color:#808030; '>&amp;<\/span><span style='color:#808030; '>(<\/span><span style='color:#603000; '>PVOID<\/span><span style='color:#808030; '>&amp;<\/span><span style='color:#808030; '>)<\/span>Real_SleepEx<span style='color:#808030; '>,<\/span> Catch_SleepEx<span style='color:#808030; '>)<\/span><span style='color:#800080; '>;<\/span>\n    DetourTransactionCommit<span style='color:#808030; '>(<\/span><span style='color:#808030; '>)<\/span><span style='color:#800080; '>;<\/span>\n\n    <span style='color:#400000; '>SleepEx<\/span><span style='color:#808030; '>(<\/span><span style='color:#008c00; '>5000<\/span><span style='color:#808030; '>,<\/span> <span style='color:#008c00; '>0<\/span><span style='color:#808030; '>)<\/span><span style='color:#800080; '>;<\/span>\n\n    <span style='color:#800000; font-weight:bold; '>return<\/span> <span style='color:#008c00; '>0<\/span><span style='color:#800080; '>;<\/span>\n<span style='color:#800080; '>}<\/span>\n<\/pre>\n<!--Created using ToHtml.com on 2019-01-02 01:10:30 UTC -->\n\n\n\n<p>Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Below is a basic example on how to use the detours library to hook APIs. #include &lt;stdio.h> #include &lt;windows.h> #include &lt;detours.h> \/\/ API that we want to hook DWORD (WINAPI * Real_SleepEx)(DWORD dwMilliseconds, BOOL bAlertable) = SleepEx; \/\/ This function will be called *before* the API. \/\/ We will modify one of the parameters \/\/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[107,109,108,4],"class_list":["post-315","post","type-post","status-publish","format-standard","hentry","category-security","tag-api","tag-detours","tag-hooking","tag-windows"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[]}},"featured_image_urls_v2":{"full":"","thumbnail":"","medium":"","medium_large":"","large":"","1536x1536":"","2048x2048":""},"post_excerpt_stackable_v2":"<p>Below is a basic example on how to use the detours library to hook APIs. #include &lt;stdio.h> #include &lt;windows.h> #include &lt;detours.h> \/\/ API that we want to hook DWORD (WINAPI * Real_SleepEx)(DWORD dwMilliseconds, BOOL bAlertable) = SleepEx; \/\/ This function will be called *before* the API. \/\/ We will modify one of the parameters \/\/ and then call the real API static DWORD WINAPI Catch_SleepEx(DWORD dwMilliseconds, BOOL bAlertable) { printf(&#8220;SleepEx: %d\\n&#8221;, dwMilliseconds); dwMilliseconds = 10; printf(&#8220;SleepEx: new value = %d\\n&#8221;, dwMilliseconds); return Real_SleepEx(dwMilliseconds, bAlertable); } int main(int argc, char **argv) { (void)argc; (void)argv; DetourTransactionBegin(); DetourUpdateThread(GetCurrentThread()); DetourAttach(&amp;(PVOID&amp;)Real_SleepEx, Catch_SleepEx); DetourTransactionCommit(); SleepEx(5000, 0);&hellip;<\/p>\n","category_list_v2":"<a href=\"https:\/\/malwrforensics.com\/en\/category\/security\/\" rel=\"category tag\">Security<\/a>","author_info_v2":{"name":"malwrforensics","url":"https:\/\/malwrforensics.com\/en\/author\/u_malwrforensics\/"},"comments_num_v2":"0 comments","_links":{"self":[{"href":"https:\/\/malwrforensics.com\/en\/wp-json\/wp\/v2\/posts\/315","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/malwrforensics.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/malwrforensics.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/malwrforensics.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/malwrforensics.com\/en\/wp-json\/wp\/v2\/comments?post=315"}],"version-history":[{"count":1,"href":"https:\/\/malwrforensics.com\/en\/wp-json\/wp\/v2\/posts\/315\/revisions"}],"predecessor-version":[{"id":316,"href":"https:\/\/malwrforensics.com\/en\/wp-json\/wp\/v2\/posts\/315\/revisions\/316"}],"wp:attachment":[{"href":"https:\/\/malwrforensics.com\/en\/wp-json\/wp\/v2\/media?parent=315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/malwrforensics.com\/en\/wp-json\/wp\/v2\/categories?post=315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/malwrforensics.com\/en\/wp-json\/wp\/v2\/tags?post=315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}