{"id":31,"date":"2013-04-04T14:22:15","date_gmt":"2013-04-04T06:22:15","guid":{"rendered":"http:\/\/www.yarpee.com\/?p=31"},"modified":"2013-04-04T14:22:15","modified_gmt":"2013-04-04T06:22:15","slug":"inline-hook","status":"publish","type":"post","link":"http:\/\/www.yarpee.cn\/?p=31","title":{"rendered":"Inline Hook"},"content":{"rendered":"<p>Inline Hook\u4e5f\u662f\u5149\u542c\u8bf4\u8fc7\u6ca1\u7ec3\u8fc7\uff0c\u4eca\u5929\u81ea\u5df1\u52a8\u624b\u8fd8\u662f\u9047\u5230\u4e86\u4e0d\u5c11\u95ee\u9898\uff0c\u4e3b\u8981\u662f\u66ff\u6362\u540e\u7684\u51fd\u6570\u5982\u4f55\u8c03\u7528\u539f\u51fd\u6570\u548c\u5e73\u8861\u5176\u8c03\u7528\u5806\u6808\u3002Demo\u91cc\u6ca1\u6709\u5b9e\u73b0\u5982\u4f55\u5148\u6267\u884c\u88abInline Hook\u8986\u76d6\u7684\u4ee3\u7801\u540e\u8df3\u8f6c\u5230\u539f\u51fd\u6570\u7684\u6d41\u7a0b\uff0c\u4ee5\u540e\u5b9e\u73b0\u540e\u8865\u5145\u3002<\/p>\n<p><code>#include &lt;Windows.h&gt;<br \/>\n#include &lt;tchar.h&gt;<\/code><\/p>\n<p>\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<br \/>\n\/\/ Inline Hook<br \/>\n#define JMP_CODE_SIZE 5<br \/>\n#pragma warning(push)<br \/>\n#pragma warning(disable:4311)<br \/>\nBOOL InlineHook(void* lpOldFunction, void* lpNewFunction)<br \/>\n{<br \/>\nBOOL bRet = FALSE;<br \/>\nbyte btCode[JMP_CODE_SIZE] = {0xE9}; \/\/ 5\u5b57\u8282\u76f8\u5bf9\u8df3\u8f6c\u6307\u4ee4<br \/>\ndo<br \/>\n{<br \/>\nif(NULL == lpOldFunction || NULL == lpNewFunction)<br \/>\n{<br \/>\nbreak;<br \/>\n}<\/p>\n<p>DWORD* pdwAddr = (DWORD*)&amp;btCode[1];<br \/>\n*pdwAddr = (DWORD)lpNewFunction &#8211; (DWORD)lpOldFunction &#8211; JMP_CODE_SIZE; \/\/ \u8ba1\u7b97\u76f8\u5bf9\u8df3\u8f6c\u5730\u5740<\/p>\n<p>DWORD dwProtect = 0;<br \/>\nif(VirtualProtect(lpOldFunction, JMP_CODE_SIZE, PAGE_READWRITE, &amp;dwProtect))<br \/>\n{<br \/>\nmemcpy(lpOldFunction, btCode, JMP_CODE_SIZE);<br \/>\nVirtualProtect(lpOldFunction, JMP_CODE_SIZE, dwProtect, &amp;dwProtect);<br \/>\nbRet = TRUE;<br \/>\n}<br \/>\n} while (0);<br \/>\nreturn bRet;<br \/>\n}<br \/>\n#pragma warning(pop)<\/p>\n<p>\/\/ \u7528\u4e8e\u6062\u590dInline Hook<br \/>\nvoid* g_lpMessageBoxA = NULL;<br \/>\nbyte g_btCode[JMP_CODE_SIZE] = {0};<\/p>\n<p>\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<br \/>\n\/\/ \u771f\u6b63\u5b9e\u73b0MessageBoxA, MyMessageBoxA\u53ea\u662f\u4e00\u4e2a\u9a6c\u7532<br \/>\nint WINAPI InternalMessageBoxA(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType)<br \/>\n{<br \/>\nMessageBoxW(NULL, L&#8221;In MyMessageBoxA&#8221;, L&#8221;InlineHook&#8221;, MB_OK);<\/p>\n<p>\/\/ \u8fd8\u539fInline Hook<br \/>\nDWORD dwProtect = 0;<br \/>\nif(VirtualProtect(g_lpMessageBoxA, JMP_CODE_SIZE, PAGE_READWRITE, &amp;dwProtect))<br \/>\n{<br \/>\nmemcpy(g_lpMessageBoxA, g_btCode, JMP_CODE_SIZE);<br \/>\nVirtualProtect(g_lpMessageBoxA, JMP_CODE_SIZE, dwProtect, &amp;dwProtect);<br \/>\n}<\/p>\n<p>\/\/ \u8c03\u56de\u771f\u6b63\u7684MessageBoxA<br \/>\nreturn MessageBoxA(hWnd, lpText, lpCaption, uType);<br \/>\n}<\/p>\n<p>\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<br \/>\n\/\/ Inline Hook\u7528MyMessageBoxA\u66ff\u6362MessageBoxA<br \/>\n\/\/ \u7f16\u8bd1\u5668\u751f\u6210\u4ee3\u7801\u65f6\u4f1a\u5728\u51fd\u6570\u524d\u540e\u589e\u52a0\u4e00\u4e9b\u5806\u6808\u5e73\u8861\u7684\u4ee3\u7801, \u5bfc\u81f4\u76f4\u63a5ret 10h\u5806\u6808\u5e76\u4e0d\u4f1a\u5e73\u8861<br \/>\n\/\/ \u56e0\u6b64\u9700\u8981\u4f7f\u7528_declspec(naked)\u4fee\u9970, \u4f46\u51fd\u6570\u5185\u9700\u8981\u81ea\u5df1\u63a7\u5236\u5bc4\u5b58\u5668<br \/>\n_declspec(naked) int WINAPI MyMessageBoxA(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType)<br \/>\n{<br \/>\n\/\/ \u5806\u6808\u5e73\u8861<br \/>\n_asm<br \/>\n{<br \/>\npush ebp<br \/>\nmov ebp, esp<br \/>\npush dword ptr[ebp+0x14]<br \/>\npush dword ptr[ebp+0x10]<br \/>\npush dword ptr[ebp+0xC]<br \/>\npush dword ptr[ebp+0x8]<br \/>\ncall InternalMessageBoxA<br \/>\npop ebp<br \/>\nret 10h ; \u7531\u4e8e\u6ca1\u6709jmp\u56de\u88abInline Hook\u7684\u51fd\u6570, \u56e0\u6b64\u9700\u8981\u5e73\u8861\u5806\u6808.<br \/>\n}<br \/>\n}<\/p>\n<p>int _tmain(int argc, TCHAR* argv[])<br \/>\n{<br \/>\nHMODULE hLib = LoadLibrary(_T(&#8220;user32.dll&#8221;));<br \/>\nif(hLib != NULL)<br \/>\n{<br \/>\ng_lpMessageBoxA = GetProcAddress(hLib, &#8220;MessageBoxA&#8221;);<br \/>\nmemcpy(g_btCode, g_lpMessageBoxA, JMP_CODE_SIZE); \/\/ \u4fdd\u5b58\u5373\u5c06\u8986\u76d6\u76845\u5b57\u8282\u4ee3\u7801<br \/>\nInlineHook(g_lpMessageBoxA, MyMessageBoxA); \/\/ Inline Hook MessageBoxA\u5230MyMessageBoxA<br \/>\nMessageBoxA(NULL, &#8220;Real MessageBoxA&#8221;, &#8220;InlineHook&#8221;, MB_OK);<br \/>\nFreeLibrary(hLib);<br \/>\n}<br \/>\nreturn 0;<br \/>\n}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Inline Hook\u4e5f\u662f\u5149\u542c\u8bf4\u8fc7\u6ca1\u7ec3\u8fc7\uff0c\u4eca\u5929\u81ea\u5df1\u52a8\u624b\u8fd8\u662f\u9047\u5230\u4e86\u4e0d\u5c11\u95ee\u9898\uff0c\u4e3b\u8981\u662f\u66ff\u6362\u540e\u7684\u51fd\u6570\u5982\u4f55\u8c03\u7528\u539f\u51fd\u6570\u548c\u5e73 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/www.yarpee.cn\/?p=31\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201cInline Hook\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-31","post","type-post","status-publish","format-standard","hentry","category-c_plus_plus"],"_links":{"self":[{"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=\/wp\/v2\/posts\/31"}],"collection":[{"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=31"}],"version-history":[{"count":0,"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=\/wp\/v2\/posts\/31\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=31"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=31"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=31"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}