使用C ++ dll通过注入不拦截api的IAT钩子

时间:2016-09-07 18:40:40

标签: c++ delphi dev-c++ api-hook

我正在尝试在MessageBoxA api中使用IAT hook tecnique在我创建的远程进程中创建一个钩子。我正在使用 Process Hacker 软件将我的dll文件注入我的进程,直到这里工作正常。

我唯一的麻烦是,当我从我的应用程序中调用此API时,MessageBoxA永远不会被截获。

我发现这个api不能在我的进程导入表中找到。

所以,我想知道关于如何在桌面上找到这个api的任何建议。

以下是我使用的代码:

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

void HookFunction(char* funcName, LPDWORD function);
LPDWORD FoundIAT(char* funcName);

int WINAPI HookMessageBoxA(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType);  

BOOL APIENTRY DllMain (HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved) 
{
    if(dwReason == DLL_PROCESS_ATTACH)
    {
        MessageBox(NULL, "Injeted with success!", "Hello", NULL);
        HookFunction("MessageBoxA", (LPDWORD)&HookMessageBoxA);
    }
    return TRUE;
}

void HookFunction(char* funcName, LPDWORD function)
{
    LPDWORD pOldFunction = FoundIAT(funcName);

    DWORD accessProtectionValue , accessProtec;

    int vProtect = VirtualProtect(pOldFunction, sizeof(LPDWORD), PAGE_EXECUTE_READWRITE, &accessProtectionValue);

    *pOldFunction = (DWORD)function;

    vProtect = VirtualProtect(pOldFunction, sizeof(LPDWORD), accessProtectionValue, &accessProtec);
}

int WINAPI HookMessageBoxA(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType)
{
    return MessageBoxA(hWnd, "Hello", "DLL answering here!", uType);
}

LPDWORD FoundIAT(char* funcName)
{
    DWORD test = 0;

    LPVOID pMapping = GetModuleHandle(NULL);

    if (pMapping == NULL)

        exit(-1);

    PIMAGE_DOS_HEADER DosHeader = (PIMAGE_DOS_HEADER) pMapping;

    if (DosHeader->e_magic != IMAGE_DOS_SIGNATURE)

        exit(-1);

    PIMAGE_NT_HEADERS NtHeaders = (PIMAGE_NT_HEADERS) ((char*) DosHeader + DosHeader->e_lfanew);

    if (NtHeaders->Signature != IMAGE_NT_SIGNATURE)

        exit(-1);

    PIMAGE_DATA_DIRECTORY DataDirectory = &NtHeaders->OptionalHeader.DataDirectory[1];

    PIMAGE_IMPORT_DESCRIPTOR ImportDescriptor = (PIMAGE_IMPORT_DESCRIPTOR) ((char*) DosHeader + DataDirectory->VirtualAddress);

    PIMAGE_THUNK_DATA32 OriginalFirstThunk = (PIMAGE_THUNK_DATA32)((char*) DosHeader + ImportDescriptor->OriginalFirstThunk);

    while(OriginalFirstThunk != 0)
    {
        DWORD name = (DWORD)((char*) pMapping + ImportDescriptor->Name);

        OriginalFirstThunk = (PIMAGE_THUNK_DATA32)((char*) DosHeader + ImportDescriptor->OriginalFirstThunk);

        PIMAGE_THUNK_DATA32 FirstThunk = (PIMAGE_THUNK_DATA32)((char*) DosHeader + ImportDescriptor->FirstThunk);

        while(OriginalFirstThunk->u1.AddressOfData != 0)
        {
            PIMAGE_IMPORT_BY_NAME NameImg = (PIMAGE_IMPORT_BY_NAME)((char*) DosHeader + (DWORD)OriginalFirstThunk->u1.AddressOfData);

            test = (DWORD)OriginalFirstThunk->u1.Function & (DWORD)IMAGE_ORDINAL_FLAG32;

            if (test == 0)
            {
                if(strcmp(funcName, (const char*)NameImg->Name) == 0)
                {
                    MessageBox(NULL, NameImg->Name, "", NULL);
                    return (LPDWORD)&(FirstThunk->u1.Function);
                }
            }
            OriginalFirstThunk++;
            FirstThunk++;
        }
        ImportDescriptor++;
    }
    return 0;
}

Source

编辑1:

我已经看到更多的数据,现在这段代码能够找到MessageBoxA api:D,但是当我从我的应用程序(用Delphi制作)中调用这个api时,钩子不工作。 : - (

我的Delphi代码:

procedure TForm1.btn1Click(Sender: TObject);
begin
 MessageBoxA(Application.Handle, 'Delphi application here!', 'Hello',0);
end;

编辑2:

This可以是最终解决方案,但链接处于离线状态。有人可以帮我解决一下如何适应上面的代码吗?

0 个答案:

没有答案