首页
社区
课程
招聘
Ring3 EAT Hook
发表于: 2010-4-17 12:15 8180

Ring3 EAT Hook

2010-4-17 12:15
8180
// (c) Code By Extreme
// Description:Ring3 Api hook engine(by EAT)
// Last update:2010-4-16

#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <tlhelp32.h>

HANDLE WINAPI MyOpenProcess( DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwProcessId)
{
    MessageBox(NULL, "Call!", "wonderful!", 0);
}

DWORD GetDllBaseAddr(char *DllName)
{
    HANDLE hToolHelp;
    MODULEENTRY32 ModEntry;    
    
    hToolHelp = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, GetCurrentProcessId());
    if (hToolHelp == INVALID_HANDLE_VALUE)
    {
        return 0;
    }
    
    if (!Module32First(hToolHelp, &ModEntry))
    {
        return 0;
    }
    
    while (Module32Next(hToolHelp, &ModEntry))
    {
        if (!strcmp(ModEntry.szModule, DllName))
        {
            CloseHandle(hToolHelp);
            return (DWORD)ModEntry.modBaseAddr;
        }
        
    }
    
    return 0;
    CloseHandle(hToolHelp);
    
}

PVOID GetEAT(char *DllName, char *ExpName)
{
    PVOID Pointer;
    DWORD DllBaseAddr;
    PIMAGE_EXPORT_DIRECTORY ExpDir;
    
    PDWORD AddrOfNames;
    PDWORD AddrOfFuncs;
    PUSHORT AddrOfOrdinals;
    
    DWORD Cnt;
    DWORD Max;
    PDWORD AddrToWrite;
    DWORD dwTmp; 
    BYTE DataToWrite[4];

    DllBaseAddr = GetDllBaseAddr(DllName);
    if (DllBaseAddr == 0)
    {
        return 0;
    }
    
    // IMAGE_DOS_HEADER
    Pointer = (PVOID)DllBaseAddr;
    if (Pointer == 0)
    {
        return 0;
    }
    
    // IMAGE_NT_HEADERS
    Pointer = (PVOID)((DWORD)((PIMAGE_DOS_HEADER)Pointer)->e_lfanew + DllBaseAddr);
    if (Pointer == 0)
    {
        return 0;
    }
    // IMAGE_EXPORT_DIRECTORY
    ExpDir = (PIMAGE_EXPORT_DIRECTORY)((DWORD)(((PIMAGE_NT_HEADERS)(Pointer))->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress) + DllBaseAddr);
    AddrOfNames = (PDWORD)((DWORD)ExpDir->AddressOfNames + DllBaseAddr);
    AddrOfFuncs = (PDWORD)((DWORD)ExpDir->AddressOfFunctions + DllBaseAddr);
    AddrOfOrdinals = (PWORD)((DWORD)ExpDir->AddressOfNameOrdinals + DllBaseAddr);
    
    Max = ExpDir->NumberOfNames;
    for (Cnt = 0; Cnt < Max; ++Cnt)
    {
        if (!strcmp((PUCHAR)(AddrOfNames[Cnt] + DllBaseAddr), ExpName))
        {
            dwTmp = (DWORD)MyOpenProcess - DllBaseAddr;
            memcpy(DataToWrite, &dwTmp, sizeof(DWORD));
            AddrToWrite = &AddrOfFuncs[AddrOfOrdinals[Cnt]];
            if (!VirtualProtect(AddrToWrite, sizeof(DWORD), PAGE_EXECUTE_READWRITE, &dwTmp))
            {
                return 0;
            }
            
            WriteProcessMemory(GetCurrentProcess(), (PDWORD)AddrToWrite, DataToWrite, sizeof(DWORD), &dwTmp);
            if (dwTmp != sizeof(DWORD))
            {
                return 0;
            }
            return 0;
        }
    }
    return 0;
}

int main()
{
    PVOID Addr;
    Addr = GetEAT("kernel32.dll", "OpenProcess");
    Addr = (DWORD)OpenProcess;
    return 0;
}



Hook 后内存写入成功,但获得的地址不变。看了《Windows 核心编程》中关于IAT Hook 的章节,自己按原方法改为Hook EAT,但为什么不起作用?

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 0
支持
分享
最新回复 (4)
雪    币: 75
活跃值: (698)
能力值: ( LV6,RANK:90 )
在线值:
发帖
回帖
粉丝
2
IAT的初始化是在程序加载时就已经完成了,Addr = (DWORD)OpenProcess中的OpenProcess在你的GetEAT("kernel32.dll", "OpenProcess")还未运行之前,就已经通过IAT映射到原kernel32.dll中的地址.
即使把kernel32.dll中相应的导出项改成自己的函数地址,在这里木已成舟,无所作为了.
2010-4-17 12:37
0
雪    币: 35
活跃值: (11)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
3
这里面的这个GetDllBaseAddr你写的的这个用LoadLIbraryA一下就实现了,而且兼容性还相当的不错 你写了那么多 太浪费金钱和时间了 时间是用来享受生活的 明天估计会出太阳 有空跟我去钓鱼吧 哈哈
2010-4-17 21:28
0
雪    币: 35
活跃值: (11)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
4
修改的是当前进程 为什么不用指针操作呢
2010-4-17 21:32
0
雪    币: 35
活跃值: (11)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
5
[QUOTE=wjwmz;792416] AddrToWrite = &AddrOfFuncs[AddrOfOrdinals[Cnt]];
[/QUOTE]这里忽略了函数转发,话说多了 赶快跑到
2010-4-17 21:48
0
游客
登录 | 注册 方可回帖
返回
//