{"id":133,"date":"2015-07-05T16:32:45","date_gmt":"2015-07-05T16:32:45","guid":{"rendered":""},"modified":"2015-07-05T16:32:45","modified_gmt":"2015-07-05T16:32:45","slug":"","status":"publish","type":"post","link":"http:\/\/weizn.net\/?p=133","title":{"rendered":"Inline Hook"},"content":{"rendered":"<p>\n\t\/*&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;apihook.h&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;*\/\n<\/p>\n<p>\n\t#ifndef APIHOOK_H_INCLUDED<br \/>\n#define APIHOOK_H_INCLUDED\n<\/p>\n<p>\n\t#include &lt;windows.h&gt;\n<\/p>\n<p>\n\tclass APIHook<br \/>\n{<br \/>\npublic:<br \/>\n&nbsp;&nbsp;&nbsp; APIHook();<br \/>\n&nbsp;&nbsp;&nbsp; ~APIHook();\n<\/p>\n<p>\n\t&nbsp;&nbsp;&nbsp; \/\/Set API Info<br \/>\n&nbsp;&nbsp;&nbsp; int SetHookAPI(LPSTR _pModuleName,LPSTR _pAPIName,PROC _func);<br \/>\n&nbsp;&nbsp;&nbsp; \/\/HOOK API<br \/>\n&nbsp;&nbsp;&nbsp; int Hook();<br \/>\n&nbsp;&nbsp;&nbsp; \/\/Clear HOOK<br \/>\n&nbsp;&nbsp;&nbsp; int UnHook();\n<\/p>\n<p>\n\tprivate:<br \/>\n&nbsp;&nbsp;&nbsp; LPSTR pModuleName;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/API\u6240\u5728\u94fe\u63a5\u5e93\u540d\u79f0<br \/>\n&nbsp;&nbsp;&nbsp; LPSTR pAPIName;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/API\u540d\u79f0<br \/>\n&nbsp;&nbsp;&nbsp; PROC pAPIAddress;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/API\u5730\u5740<br \/>\n&nbsp;&nbsp;&nbsp; PROC func;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/\u56de\u8c03\u7684\u51fd\u6570<br \/>\n&nbsp;&nbsp;&nbsp; DWORD Previous;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/\u4fdd\u5b58\u5185\u5b58\u9875\u8001\u7684\u4fdd\u62a4\u65b9\u5f0f<br \/>\n&nbsp;&nbsp;&nbsp; BYTE oldByteData[5];&nbsp;&nbsp;&nbsp; \/\/\u539f\u51fd\u6570\u5165\u53e3\u4ee3\u7801<br \/>\n&nbsp;&nbsp;&nbsp; BYTE newByteData[5];&nbsp;&nbsp;&nbsp; \/\/\u66f4\u6539\u540e\u7684\u51fd\u6570\u5165\u53e3\u4ee3\u7801<br \/>\n};\n<\/p>\n<p>\n\t#endif \/\/ APIHOOK_H_INCLUDED\n<\/p>\n<p>\n\t\/*&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-apihook.cpp&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;*\/\n<\/p>\n<p>\n\t&nbsp;\n<\/p>\n<p>\n\t#include &#8220;apihook.h&#8221;\n<\/p>\n<p>\n\tAPIHook::APIHook()<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp; pModuleName=NULL;<br \/>\n&nbsp;&nbsp;&nbsp; pAPIName=NULL;<br \/>\n&nbsp;&nbsp;&nbsp; pAPIAddress=NULL;<br \/>\n&nbsp;&nbsp;&nbsp; memset(oldByteData,NULL,5);<br \/>\n&nbsp;&nbsp;&nbsp; memset(newByteData,NULL,5);\n<\/p>\n<p>\n\t&nbsp;&nbsp;&nbsp; return;<br \/>\n}\n<\/p>\n<p>\n\tAPIHook::~APIHook()<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp; if(pModuleName!=NULL)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delete pModuleName;<br \/>\n&nbsp;&nbsp;&nbsp; if(pAPIName!=NULL)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delete pAPIName;\n<\/p>\n<p>\n\t&nbsp;&nbsp;&nbsp; return;<br \/>\n}\n<\/p>\n<p>\n\tint APIHook::SetHookAPI(LPSTR _pModuleName,LPSTR _pAPIName,PROC _func)<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp; if(pModuleName!=NULL)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return -1;<br \/>\n&nbsp;&nbsp;&nbsp; pModuleName=new char(strlen(_pModuleName)+1);<br \/>\n&nbsp;&nbsp;&nbsp; pAPIName=new char(strlen(_pAPIName)+1);<br \/>\n&nbsp;&nbsp;&nbsp; if(pModuleName==NULL || pAPIName==NULL)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return -2;<br \/>\n&nbsp;&nbsp;&nbsp; memset(pModuleName,NULL,strlen(_pModuleName)+1);<br \/>\n&nbsp;&nbsp;&nbsp; memset(pAPIName,NULL,strlen(_pAPIName)+1);\n<\/p>\n<p>\n\t&nbsp;&nbsp;&nbsp; strcat(pModuleName,_pModuleName);<br \/>\n&nbsp;&nbsp;&nbsp; strcat(pAPIName,_pAPIName);\n<\/p>\n<p>\n\t&nbsp;&nbsp;&nbsp; this-&gt;func=_func;\n<\/p>\n<p>\n\t&nbsp;&nbsp;&nbsp; return 0;<br \/>\n}\n<\/p>\n<p>\n\tint APIHook::Hook()<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp; DWORD dwNum=0;\n<\/p>\n<p>\n\t&nbsp;&nbsp;&nbsp; \/\/\u83b7\u53d6\u6307\u5b9a\u6a21\u5757\u4e2d\u7684API\u5730\u5740<br \/>\n&nbsp;&nbsp;&nbsp; pAPIAddress=(PROC)GetProcAddress(GetModuleHandle(pModuleName),pAPIName);<br \/>\n&nbsp;&nbsp;&nbsp; if(pAPIAddress==NULL)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return -1;\n<\/p>\n<p>\n\t&nbsp;&nbsp;&nbsp; \/\/\u6539\u53d8\u5185\u5b58\u9875\u4fdd\u62a4\u5c5e\u6027<br \/>\n&nbsp;&nbsp;&nbsp; VirtualProtect((void *)pAPIAddress,5,PAGE_EXECUTE_READWRITE,&amp;Previous);\n<\/p>\n<p>\n\t&nbsp;&nbsp;&nbsp; \/\/\u4fdd\u5b58\u8be5\u5730\u5740\u59045\u4e2a\u5b57\u8282\u7684\u5185\u5bb9<br \/>\n&nbsp;&nbsp;&nbsp; if(ReadProcessMemory(GetCurrentProcess(),(LPCVOID)pAPIAddress,oldByteData,5,&amp;dwNum)==0)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return -2;<br \/>\n&nbsp;&nbsp;&nbsp; if(dwNum!=5)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return -2;\n<\/p>\n<p>\n\t&nbsp;&nbsp;&nbsp; \/\/\u6784\u9020JMP\u6307\u4ee4<br \/>\n&nbsp;&nbsp;&nbsp; newByteData[0]=0xE9;&nbsp; \/\/jmp code<br \/>\n&nbsp;&nbsp;&nbsp; *(DWORD *)(newByteData+1)=(DWORD)func-(DWORD)pAPIAddress-5;\n<\/p>\n<p>\n\t&nbsp;&nbsp;&nbsp; \/\/\u5c06\u6784\u9020\u597d\u7684JMP\u5730\u5740\u5199\u5165\u5230\u8be5\u5904<br \/>\n&nbsp;&nbsp;&nbsp; if(WriteProcessMemory(GetCurrentProcess(),(LPVOID)pAPIAddress,newByteData,5,&amp;dwNum)==0)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return -3;\n<\/p>\n<p>\n\t&nbsp;&nbsp;&nbsp; \/\/\u8fd8\u539f\u9875\u9762\u4fdd\u62a4\u5c5e\u6027<br \/>\n&nbsp;&nbsp;&nbsp; VirtualProtect((void *)pAPIAddress,5,Previous,&amp;Previous);<br \/>\n&nbsp;&nbsp;&nbsp; FlushInstructionCache(GetCurrentProcess(),0,0);\n<\/p>\n<p>\n\t&nbsp;&nbsp;&nbsp; if(dwNum!=5)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return -3;\n<\/p>\n<p>\n\t&nbsp;&nbsp;&nbsp; return 0;<br \/>\n}\n<\/p>\n<p>\n\tint APIHook::UnHook()<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp; DWORD dwNum=0;\n<\/p>\n<p>\n\t&nbsp;&nbsp;&nbsp; if(pAPIAddress==NULL)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 0;\n<\/p>\n<p>\n\t&nbsp;&nbsp;&nbsp; VirtualProtect((void *) pAPIAddress,5,PAGE_EXECUTE_READWRITE,&amp;Previous);<br \/>\n&nbsp;&nbsp;&nbsp; WriteProcessMemory(GetCurrentProcess(),(LPVOID)pAPIAddress,oldByteData,5,&amp;dwNum);<br \/>\n&nbsp;&nbsp;&nbsp; VirtualProtect((void *)pAPIAddress,5,Previous,&amp;Previous);\n<\/p>\n<p>\n\t&nbsp;&nbsp;&nbsp; return 0;<br \/>\n}\n<\/p>\n<p>\n\t&nbsp;\n<\/p>\n<p>\n\t<br \/>\n&nbsp;\n<\/p>\n<p>\n\t&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\n\t\/*&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-apihook.h&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;*\/\n<\/p>\n<p>\n\t#ifndef APIHOOK_H_INCLUDED<br \/>\n#define APIHOOK_H_INCLUDED\n<\/p>\n<p>\n\t#include &lt;windows.h&gt;\n<\/p>\n<p>\n\t&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[324],"tags":[],"class_list":["post-133","post","type-post","status-publish","format-standard","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Inline Hook - Wayne&#039;s Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/weizn.net\/?p=133\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Inline Hook - Wayne&#039;s Blog\" \/>\n<meta property=\"og:description\" content=\"\/*----------------------------------------apihook.h--------------------------------------------------*\/    #ifndef APIHOOK_H_INCLUDED #define APIHOOK_H_INCLUDED    #include &lt;windows.h&gt;    ...\" \/>\n<meta property=\"og:url\" content=\"http:\/\/weizn.net\/?p=133\" \/>\n<meta property=\"og:site_name\" content=\"Wayne&#039;s Blog\" \/>\n<meta property=\"article:published_time\" content=\"2015-07-05T16:32:45+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"zinan\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"http:\/\/weizn.net\/#website\",\"url\":\"http:\/\/weizn.net\/\",\"name\":\"Wayne&#039;s Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/weizn.net\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/weizn.net\/?p=133#webpage\",\"url\":\"http:\/\/weizn.net\/?p=133\",\"name\":\"Inline Hook - Wayne&#039;s Blog\",\"isPartOf\":{\"@id\":\"http:\/\/weizn.net\/#website\"},\"datePublished\":\"2015-07-05T16:32:45+00:00\",\"dateModified\":\"2015-07-05T16:32:45+00:00\",\"breadcrumb\":{\"@id\":\"http:\/\/weizn.net\/?p=133#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/weizn.net\/?p=133\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/weizn.net\/?p=133#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\\u9996\\u9875\",\"item\":\"http:\/\/weizn.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Inline Hook\"}]},{\"@type\":\"Article\",\"@id\":\"http:\/\/weizn.net\/?p=133#article\",\"isPartOf\":{\"@id\":\"http:\/\/weizn.net\/?p=133#webpage\"},\"author\":{\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\"},\"headline\":\"Inline Hook\",\"datePublished\":\"2015-07-05T16:32:45+00:00\",\"dateModified\":\"2015-07-05T16:32:45+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/weizn.net\/?p=133#webpage\"},\"wordCount\":550,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\"},\"articleSection\":[\"C\/C++\"],\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"http:\/\/weizn.net\/?p=133#respond\"]}]},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\",\"name\":\"zinan\",\"logo\":{\"@id\":\"http:\/\/weizn.net\/#personlogo\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Inline Hook - Wayne&#039;s Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/weizn.net\/?p=133","og_locale":"zh_CN","og_type":"article","og_title":"Inline Hook - Wayne&#039;s Blog","og_description":"\/*----------------------------------------apihook.h--------------------------------------------------*\/    #ifndef APIHOOK_H_INCLUDED #define APIHOOK_H_INCLUDED    #include &lt;windows.h&gt;    ...","og_url":"http:\/\/weizn.net\/?p=133","og_site_name":"Wayne&#039;s Blog","article_published_time":"2015-07-05T16:32:45+00:00","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"zinan","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"http:\/\/weizn.net\/#website","url":"http:\/\/weizn.net\/","name":"Wayne&#039;s Blog","description":"","publisher":{"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/weizn.net\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"WebPage","@id":"http:\/\/weizn.net\/?p=133#webpage","url":"http:\/\/weizn.net\/?p=133","name":"Inline Hook - Wayne&#039;s Blog","isPartOf":{"@id":"http:\/\/weizn.net\/#website"},"datePublished":"2015-07-05T16:32:45+00:00","dateModified":"2015-07-05T16:32:45+00:00","breadcrumb":{"@id":"http:\/\/weizn.net\/?p=133#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["http:\/\/weizn.net\/?p=133"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/weizn.net\/?p=133#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"http:\/\/weizn.net\/"},{"@type":"ListItem","position":2,"name":"Inline Hook"}]},{"@type":"Article","@id":"http:\/\/weizn.net\/?p=133#article","isPartOf":{"@id":"http:\/\/weizn.net\/?p=133#webpage"},"author":{"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264"},"headline":"Inline Hook","datePublished":"2015-07-05T16:32:45+00:00","dateModified":"2015-07-05T16:32:45+00:00","mainEntityOfPage":{"@id":"http:\/\/weizn.net\/?p=133#webpage"},"wordCount":550,"commentCount":0,"publisher":{"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264"},"articleSection":["C\/C++"],"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["http:\/\/weizn.net\/?p=133#respond"]}]},{"@type":["Person","Organization"],"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264","name":"zinan","logo":{"@id":"http:\/\/weizn.net\/#personlogo"}}]}},"_links":{"self":[{"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/posts\/133","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=133"}],"version-history":[{"count":0,"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/posts\/133\/revisions"}],"wp:attachment":[{"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=133"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}