首页 技术 正文
技术 2022年11月15日
0 收藏 711 点赞 4,762 浏览 3198 个字

#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;

}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,905
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,430
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,247
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,058
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,690
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,727