{"id":25,"date":"2013-04-03T21:46:06","date_gmt":"2013-04-03T13:46:06","guid":{"rendered":"http:\/\/www.yarpee.com\/?p=25"},"modified":"2013-04-03T21:46:06","modified_gmt":"2013-04-03T13:46:06","slug":"iat-hook","status":"publish","type":"post","link":"http:\/\/www.yarpee.cn\/?p=25","title":{"rendered":"IAT Hook"},"content":{"rendered":"<p>IAT Hook\u65e9\u4e86\u89e3\u8fc7\u4e86\u4e00\u76f4\u6ca1\u6709\u81ea\u5df1\u52a8\u624b\u5b9e\u73b0\u8fc7\uff0c\u5199\u4e86\u4e2ademo\u5374\u4e5f\u82b1\u4e86\u4e0d\u5c11\u65f6\u95f4\uff0c\u8bb0\u4e0b\u6765\u5907\u7528\u3002<\/p>\n<p><code>#include &lt;Windows.h&gt;<br \/>\n#include &lt;tchar.h&gt;<\/code><\/p>\n<p>\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<br \/>\n\/\/ \u7528lpNewFunction\u66ff\u6362hModule\u4e2d\u5bfc\u5165\u7684lpImageName\u7684lpOldFunction<br \/>\nBOOL IATHook(HMODULE hModule, char* lpImageName, void* lpOldFunction, void* lpNewFunction)<br \/>\n{<br \/>\nBOOL bRet = FALSE;<br \/>\ndo<br \/>\n{<br \/>\nif(NULL == hModule || NULL == lpImageName || NULL == lpOldFunction || NULL == lpNewFunction)<br \/>\n{<br \/>\nbreak;<br \/>\n}<\/p>\n<p>PIMAGE_DOS_HEADER pDosHeader = (PIMAGE_DOS_HEADER)hModule;<br \/>\nif(pDosHeader-&gt;e_magic != IMAGE_DOS_SIGNATURE || pDosHeader-&gt;e_lfanew &lt;= 0)<br \/>\n{<br \/>\nbreak;<br \/>\n}<\/p>\n<p>PIMAGE_NT_HEADERS pNtHeaders = (PIMAGE_NT_HEADERS)(pDosHeader-&gt;e_lfanew + (DWORD_PTR)hModule);<br \/>\nif(pNtHeaders-&gt;Signature != IMAGE_NT_SIGNATURE)<br \/>\n{<br \/>\nbreak;<br \/>\n}<\/p>\n<p>\/\/ \u4ece\u5bfc\u5165\u8868\u4e2d\u627e\u5230lpImageName\u7684IMAGE_THUNK_DATA<br \/>\nPIMAGE_IMPORT_DESCRIPTOR pDescriptor = (PIMAGE_IMPORT_DESCRIPTOR)(pNtHeaders-&gt;OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress + (DWORD_PTR)hModule);<br \/>\nPIMAGE_THUNK_DATA pIATThunk = NULL;<br \/>\nwhile(pDescriptor-&gt;Characteristics != 0)<br \/>\n{<br \/>\nchar* lpszName = (char*)(pDescriptor-&gt;Name + (DWORD_PTR)hModule);<br \/>\nif(0 == _stricmp(lpszName, lpImageName))<br \/>\n{<br \/>\npIATThunk = (PIMAGE_THUNK_DATA)(pDescriptor-&gt;FirstThunk + (DWORD_PTR)hModule);<br \/>\nbreak;<br \/>\n}<br \/>\n++pDescriptor;<br \/>\n}<\/p>\n<p>\/\/ \u4eceIMAGE_THUNK_DATA\u627e\u5230\u9700\u8981\u66ff\u6362\u7684\u5177\u4f53\u51fd\u6570<br \/>\nif(NULL == pIATThunk)<br \/>\n{<br \/>\nbreak;<br \/>\n}<br \/>\nwhile(pIATThunk-&gt;u1.Function != 0)<br \/>\n{<br \/>\nif(pIATThunk-&gt;u1.Function == (DWORD_PTR)lpOldFunction)<br \/>\n{<br \/>\nDWORD dwProtect = 0;<br \/>\nif(VirtualProtect(&amp;(pIATThunk-&gt;u1.Function), sizeof(DWORD), PAGE_READWRITE, &amp;dwProtect))<br \/>\n{<br \/>\nbRet = WriteProcessMemory((HANDLE)-1, &amp;(pIATThunk-&gt;u1.Function), &amp;lpNewFunction, sizeof(DWORD), NULL);<br \/>\nVirtualProtect(&amp;(pIATThunk-&gt;u1.Function), sizeof(DWORD), dwProtect, &amp;dwProtect);<br \/>\n}<br \/>\nbreak;<br \/>\n}<br \/>\n++pIATThunk;<br \/>\n}<br \/>\n} while (0);<br \/>\nreturn bRet;<br \/>\n}<\/p>\n<p>\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<br \/>\n\/\/ \u7528MyMessageBox\u66ff\u6362MessageBoxW<br \/>\ntypedef int (WINAPI *MESSAGE_BOX)(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType);<br \/>\nMESSAGE_BOX RealMessageBox = NULL;<br \/>\nint WINAPI MyMessageBox(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType)<br \/>\n{<br \/>\nint nRet = 0;<br \/>\nif(RealMessageBox != NULL)<br \/>\n{<br \/>\nRealMessageBox(NULL, _T(&#8220;In MyMessageBox&#8221;), _T(&#8220;IATHook&#8221;), MB_OK);<br \/>\nnRet = RealMessageBox(hWnd, lpText, lpCaption, uType);<br \/>\n}<br \/>\nreturn nRet;<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 \/>\nRealMessageBox = (MESSAGE_BOX)GetProcAddress(hLib, &#8220;MessageBoxW&#8221;);\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\/\/ \u9700\u8981IAT Hook\u7684\u51fd\u6570\u5730\u5740, \u7528\u4e8e\u5728IAT\u4e2d\u67e5\u627e\u5e76\u66ff\u6362<br \/>\nIATHook(GetModuleHandle(NULL), &#8220;user32.dll&#8221;, RealMessageBox, MyMessageBox);\u00a0\u00a0 \u00a0\/\/ IAT Hook\u672cexe\u6a21\u5757\u4e2d\u7684MessageBoxW<br \/>\nMessageBox(NULL, _T(&#8220;Real MessageBox&#8221;), _T(&#8220;IATHook&#8221;), MB_OK);<br \/>\nFreeLibrary(hLib);<br \/>\n}<br \/>\nreturn 0;<br \/>\n}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>IAT Hook\u65e9\u4e86\u89e3\u8fc7\u4e86\u4e00\u76f4\u6ca1\u6709\u81ea\u5df1\u52a8\u624b\u5b9e\u73b0\u8fc7\uff0c\u5199\u4e86\u4e2ademo\u5374\u4e5f\u82b1\u4e86\u4e0d\u5c11\u65f6\u95f4\uff0c\u8bb0\u4e0b\u6765\u5907\u7528\u3002 #includ &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/www.yarpee.cn\/?p=25\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201cIAT 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-25","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\/25"}],"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=25"}],"version-history":[{"count":0,"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=\/wp\/v2\/posts\/25\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=25"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=25"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=25"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}