#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,TEXT("不好意思,hook到了!"),uType); 

    else

    return MessageBox(hWnd,lpText,lpCaption,uType); ;

}



int replace_IAT(const char *pDllName,const char *pApiName,void ** OldApiAddr,void * NewApiAddr,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 == lstrcmpiA((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 == lstrcmpiA(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)

                {

                    *OldApiAddr = (void*)pImageThunkReal->u1.Function; 

                    pImageThunkReal->u1.Function = (DWORD)(NewApiAddr);

                }

                else

                   {

                    pImageThunkReal->u1.Function = (DWORD)(*OldApiAddr);

                        *OldApiAddr  = NULL;

                    }

                DWORD dwOldProtect; 

                VirtualProtect(mbi_thunk.BaseAddress, mbi_thunk.RegionSize, mbi_thunk.Protect, &dwOldProtect); 

                break;

            }

        }

        ++pImageThunkOriginal;

        ++pImageThunkReal;

    }

    return 0;

}



int _tmain(int argc, _TCHAR* argv[])

{

    

    replace_IAT("User32.dll","MessageBoxW",(void**)&g_procOldMessageBox,HOOK_MessageBox,true);

    MessageBox(NULL,TEXT("EnumIAT User32.dll MessageBoxW true;"),TEXT(""),MB_OK);

    replace_IAT("User32.dll","MessageBoxW",(void**)&g_procOldMessageBox,HOOK_MessageBox,false);

    MessageBox(NULL,TEXT("EnumIAT User32.dll MessageBoxW false;"),TEXT("UnHook!"),MB_OK);

    return getchar();



    return 0;

}

最新文章

  1. asp.net 事件加载顺序
  2. Web Components是不是Web的未来
  3. quanpailie quanbianli
  4. SAP 物料基本单位与BOM单位
  5. Shell脚本编程——了解你的Linux系统必须掌握的20个命令
  6. 基于visual Studio2013解决算法导论之050强连通分支
  7. C 函数 strstr 的高效实现
  8. 实体处理模块IEntityModule
  9. springmvc初始化失败问题跟踪
  10. 常识判断-科技-day123
  11. step_by_step_CSRF/ XSRF_问题描述
  12. hdu-2087(kmp)
  13. P2279 [HNOI2003]消防局的设立
  14. CCNA
  15. RF
  16. Android学习之基础知识四-Activity活动1讲
  17. lesson2-cnn-fastai
  18. python 标准日志模块loging 及日志系统实例
  19. ruby lib文件夹作用
  20. omitTermFreqAndPositions设置,词频FQ在打分中默认为1

热门文章

  1. Vue学习笔记【24】——Vue组件(组件切换)
  2. 牛客 某练习赛 Data Structure
  3. C#简单游戏外挂制作(以Warcraft Ⅲ为例)
  4. Sql生成 Insert 语句
  5. dom读写xml
  6. 2019河北省大学生程序设计竞赛(重现赛)J-舔狗 (拓扑排序)
  7. bootstrap Modal 模态框垂直居中
  8. Linux 下通过mail命令发送邮件
  9. php ZipArchive 压缩整个文件夹
  10. 2018-8-10-wpf-DoEvents-