hook api的一例子 C+/VC

來源:互聯網
上載者:User
 hook api的一例子 C+/VC[ 2008-7-30 22:33:00 | 發表者 : zihe ]
幫同學寫了個hook api的例子程式,順便post到這兒,免得將來到處找.一個簡單的console工程,vc6,vc7在win2k pro,server上調試通過.#include <stdio.h>#include <windows.h>#include <dbghelp.h>#pragma comment(lib,"dbghelp.lib")#pragma comment(lib,"user32.lib")typedef int (__stdcall *old_messagebox)( hwnd hwnd, lpctstr lptext, lpctstr lpcaption,uint utype );old_messagebox g_procoldmessagebox = null;int __stdcall hook_messagebox( hwnd hwnd, lpctstr lptext, lpctstr lpcaption,uint utype){ printf("%s/t%d/r/n",__function__,__line__); if (null != g_procoldmessagebox)  return g_procoldmessagebox(hwnd,lptext,"不好意思,hook到了!",utype);  else  return messagebox(hwnd,lptext,lpcaption,utype); ;}
int replace_iat(const char *pdllname,const char *papiname,bool breplace){ handle hprocess = ::getmodulehandle (null); dword dwsize = 0; pimage_import_descriptor pimageimport = (pimage_import_descriptor)imagedirectoryentrytodata(hprocess,true,  image_directory_entry_import,&dwsize); if (null == pimageimport)  return 1; pimage_import_by_name pimageimportbyname = null; pimage_thunk_data  pimagethunkoriginal = null; pimage_thunk_data  pimagethunkreal  = null; while (pimageimport->name) {  if (0 == strcmpi((char*)((pbyte)hprocess+pimageimport->name),pdllname))  {   break;  }  ++pimageimport; } if (! pimageimport->name)  return 2; pimagethunkoriginal = (pimage_thunk_data)((pbyte)hprocess+pimageimport->originalfirstthunk  ); pimagethunkreal = (pimage_thunk_data)((pbyte)hprocess+pimageimport->firstthunk   ); while (pimagethunkoriginal->u1.function) {  if ((pimagethunkoriginal->u1 .ordinal & image_ordinal_flag) != image_ordinal_flag)  {   pimageimportbyname = (pimage_import_by_name)((pbyte)hprocess+pimagethunkoriginal->u1 .addressofdata );   if (0 == strcmpi(papiname,(char*)pimageimportbyname->name))   {    memory_basic_information mbi_thunk;    virtualquery(pimagethunkreal, &mbi_thunk, sizeof(memory_basic_information));     virtualprotect(mbi_thunk.baseaddress,mbi_thunk.regionsize, page_readwrite, &mbi_thunk.protect);     if (true == breplace)    {     g_procoldmessagebox =(old_messagebox) pimagethunkreal->u1.function;      pimagethunkreal->u1.function = (dword)hook_messagebox;    }    else     pimagethunkreal->u1.function = (dword)g_procoldmessagebox;    dword dwoldprotect;     virtualprotect(mbi_thunk.baseaddress, mbi_thunk.regionsize, mbi_thunk.protect, &dwoldprotect);    break;   }  }  ++pimagethunkoriginal;  ++pimagethunkreal; } return 0;}int main(){ replace_iat("user32.dll","messageboxa",true); messagebox(null,"enumiat user32.dll messageboxa true;","",mb_ok); replace_iat("user32.dll","messageboxa",false); messagebox(null,"enumiat user32.dll messageboxa false;","",mb_ok); return getchar();}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.