博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(转)IAT Hook
阅读量:5302 次
发布时间:2019-06-14

本文共 3557 字,大约阅读时间需要 11 分钟。

// IATHook02.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "IATHook02.h"#ifdef _DEBUG#define new DEBUG_NEW#endif// The one and only application objectCWinApp theApp;using namespace std;//HWND HookProc(void);BOOL IATHook(LPCSTR pDLLName, PDWORD pOldAddr, PDWORD pNewAddr);//int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]){    int nRetCode = 0;    // initialize MFC and print and error on failure    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))    {        // TODO: change error code to suit your needs        _tprintf(_T("Fatal Error: MFC initialization failed\n"));        nRetCode = 1;    }    else    {        // TODO: code your application's behavior here.    }    HMODULE    hmod = GetModuleHandle("USER32.dll");    FARPROC    hold = GetProcAddress(hmod, "GetForegroundWindow");    if (IATHook("USER32.dll", (PDWORD)hold, (PDWORD)HookProc))    {        GetForegroundWindow();    }    else        MessageBox(NULL, "Not Hook", "MesageBox", MB_OK);    return nRetCode;}//HWND HookProc(void){    MessageBox(NULL, "I have hooked by IAT", "IAT HOOK", MB_OK);    return NULL;}//BOOL IATHook(LPCSTR pDLLName, PDWORD pOldAddr, PDWORD pNewAddr){    HMODULE                hModule = NULL;    DWORD                OldProtect;    LPVOID                lpaddr;    LPSTR                pModuleLabel = NULL;    PIMAGE_THUNK_DATA    pThunkData = NULL;    PIMAGE_DOS_HEADER    pIMAGE_DOS_HEADER = NULL;    PIMAGE_NT_HEADERS    pNTHeader = NULL;    PIMAGE_OPTIONAL_HEADER32    pOptionalHeader = NULL;    PIMAGE_DATA_DIRECTORY        DataDirectory = NULL;    PIMAGE_IMPORT_DESCRIPTOR    pImportHeader = NULL;    PIMAGE_IMPORT_DESCRIPTOR    pDllModule = NULL;    hModule = GetModuleHandle(NULL);    pIMAGE_DOS_HEADER = (PIMAGE_DOS_HEADER)hModule;    if (pIMAGE_DOS_HEADER->e_magic == IMAGE_DOS_SIGNATURE)    {        pNTHeader = (PIMAGE_NT_HEADERS)((DWORD)pIMAGE_DOS_HEADER + (DWORD)pIMAGE_DOS_HEADER->e_lfanew);        if (pNTHeader->Signature == IMAGE_NT_SIGNATURE)        {            pOptionalHeader = (PIMAGE_OPTIONAL_HEADER32)&(pNTHeader->OptionalHeader);            DataDirectory = pOptionalHeader->DataDirectory;            pImportHeader = (PIMAGE_IMPORT_DESCRIPTOR)((DWORD)hModule +                                 DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);        }        else            return FALSE;    }    else        return FALSE;    while (pImportHeader->Name != NULL)    {        pModuleLabel = (LPSTR)((DWORD)hModule + (DWORD)pImportHeader->Name);        if (*pModuleLabel == *pDLLName)        {            pDllModule = pImportHeader;            pThunkData = (PIMAGE_THUNK_DATA)((DWORD)hModule + (DWORD)pDllModule->FirstThunk);            while (pThunkData->u1.Function != NULL)            {                if (pOldAddr == (PVOID)pThunkData->u1.Function)                {                    MEMORY_BASIC_INFORMATION  mbi;                    lpaddr = &pThunkData->u1.Function;                    VirtualQuery(lpaddr, &mbi,sizeof(mbi));                    VirtualProtect(lpaddr, sizeof(PDWORD), PAGE_READWRITE, &OldProtect);                    WriteProcessMemory(GetCurrentProcess(), lpaddr, &pNewAddr, sizeof(PDWORD), NULL);                    VirtualProtect(&pThunkData->u1.Function, sizeof(PDWORD), OldProtect, &OldProtect);                    return TRUE;                }                else                    pThunkData++;            }        }        pImportHeader++;    }    return FALSE;}

 

转载于:https://www.cnblogs.com/himessage/archive/2012/12/20/2826084.html

你可能感兴趣的文章
1. Two Sum
查看>>
C++语言-03-类与对象
查看>>
字符串小结1
查看>>
Kth Ancestor 第k个祖先问题
查看>>
把数组排成最小的数
查看>>
代码优先-Code First
查看>>
责任链模式-- chainResponsibility
查看>>
[BZOJ1821][JSOI2010]部落划分
查看>>
JAVA_OA(十四):SSM练手项目bug-JSP页面传递参数的编码问题
查看>>
jQuary总结9:html()的常见用法
查看>>
软件工程原理-第二次作业
查看>>
go语言,struct and interface(golang)(二)
查看>>
[leetcode]12. Integer to Roman
查看>>
HTML CSS微信CSS显示一些总结
查看>>
C# PDF转Image图片
查看>>
在项目中使用Google Closure Compiler
查看>>
php-fpm打开错误日志的配置
查看>>
k近邻算法
查看>>
ThinkPHP5实现定时任务
查看>>
uva1599 Ideal path(已AC)
查看>>