用修改IAT法HOOK API

來源:互聯網
上載者:User

#include <windows.h>
#include <stdio.h>
#include <tchar.h>

#define  UNICODE
#define  _UNICODE

 

PIMAGE_DOS_HEADER  pDosHeader;
PIMAGE_NT_HEADERS  pNTHeaders;
PIMAGE_OPTIONAL_HEADER   pOptHeader;
PIMAGE_IMPORT_DESCRIPTOR  pImportDescriptor;
PIMAGE_THUNK_DATA         pThunkData;
PIMAGE_IMPORT_BY_NAME     pImportByName;
HMODULE hMod;

int * addr = (int *)MessageBoxA;        //儲存函數的入口地址
// 定義MessageBoxA函數原型
typedef int (WINAPI *PFNMESSAGEBOX)(HWND, LPCSTR, LPCSTR, UINT uType);
int WINAPI MessageBoxProxy(IN HWND hWnd, IN LPCSTR lpText, IN LPCSTR lpCaption, IN UINT uType);

int * myaddr = (int *)MessageBoxProxy;

int main()
{
        //OutputDebugString(_T("start !"));
        MessageBoxA(NULL, "原函數", "09HookDemo", 0);

        //-------------HOOK部分
        hMod = GetModuleHandle(NULL);

        pDosHeader = (PIMAGE_DOS_HEADER)hMod;
        pNTHeaders = (PIMAGE_NT_HEADERS)((BYTE *)hMod + pDosHeader->e_lfanew);
        pOptHeader = (PIMAGE_OPTIONAL_HEADER)&(pNTHeaders->OptionalHeader);

        pImportDescriptor = (PIMAGE_IMPORT_DESCRIPTOR)((BYTE *)hMod + pOptHeader->DataDirectory[1].VirtualAddress);

        while(pImportDescriptor->FirstThunk)
        {
                char * dllname = (char *)((BYTE *)hMod + pImportDescriptor->Name);
                //printf("函數模組:%s\n",dllname);

                pThunkData = (PIMAGE_THUNK_DATA)((BYTE *)hMod + pImportDescriptor->OriginalFirstThunk);

                int no = 1;
                while(pThunkData->u1.Function)
                {
                        char * funname = (char *)((BYTE *)hMod + (DWORD)pThunkData->u1.AddressOfData + 2);
                        PDWORD lpAddr = (DWORD *)((BYTE *)hMod + (DWORD)pImportDescriptor->FirstThunk) +(no-1);
                       
                        //printf("%4d:  ",no);
                        //printf("%30s",funname);
                        //printf("%8x\n",lpAddr);
                        //printf("%8x\n",*lpAddr);
                        //修改記憶體的部分
                        if((*lpAddr) == (int)addr)
                        {
                                //修改記憶體頁的屬性
                                DWORD dwOLD;
                                MEMORY_BASIC_INFORMATION  mbi;
                                VirtualQuery(lpAddr,&mbi,sizeof(mbi));
                                VirtualProtect(lpAddr,sizeof(DWORD),PAGE_READWRITE,&dwOLD);
                                //寫記憶體
                                WriteProcessMemory(GetCurrentProcess(),
                                                lpAddr, &myaddr, sizeof(DWORD), NULL);
                                //恢複記憶體頁的屬性
                                VirtualProtect(lpAddr,sizeof(DWORD),dwOLD,0);
                        }
            //---------
                        no++;
                        pThunkData++;
                }

                pImportDescriptor++;
        }

        //用於測試的API函數
        MessageBoxA(NULL, "原函數", "09HookDemo", 0);

        getchar();
        return 0;
}

int WINAPI MessageBoxProxy(IN HWND hWnd, IN LPCSTR lpText, IN LPCSTR lpCaption, IN UINT uType)
{
        return         ((PFNMESSAGEBOX)addr)(NULL, "Gxter", "Gxter", 0);
        //用地址調用一個API函數
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.