前一篇主要讲“API Hooking”的原理——修改IAT,这一篇主要讲代码实现和调试。
一、修改IAT
我们要修改IAT,首先要了解导入段的数据结构和操作API。
1,IMAGE_IMPORT_DESCRIPTOR
在winnt.h文件中,定义了这么一个数据结构来描述导入段中的信息单元
typedef struct _IMAGE_IMPORT_DESCRIPTOR {
union {
DWORD Characteristics; // 0 for terminating null import descriptor
DWORD OriginalFirstThunk; // RVA to original unbound IAT (PIMAGE_THUNK_DATA)
} DUMMYUNIONNAME;
DWORD TimeDateStamp; // 0 if not bound,
// -1 if bound, and real date\time stamp
// in IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT (new BIND)
// O.W. date/time stamp of DLL bound to (Old BIND)
DWORD ForwarderChain; // -1 if no forwarders
DWORD Name;
DWORD FirstThunk; // RVA to IAT (if bound this IAT has actual addresses)
} IMAGE_IMPORT_DESCRIPTOR;
typedef IMAGE_IMPORT_DESCRIPTOR UNALIGNED *PIMAGE_IMPORT_DESCRIPTOR;
其中,“Name”表示的是DLL名,如:kernel32.dll。“FirstThunk”指向IAT的第一个项。我们只需要了解这两个成员即可。
如上图,INT是指“Import Name Table”,IAT是指“Import Address Table”。新系统中,一般用IAT即可,老的用INT。
2,ImageDirectoryEntryToDataEx或ImageDirectoryEntryToData
Microsoft提供了这么一个API来获取Image文件的导入段的信息单位。
Locates a directory entry within the image header and returns the address of the data for the directory entry. This function returns the section header for the data located, if one exists.