PE 文件格之导入导出表
先将源码贴上:
先将源码贴上:
#include <stdio.h>
#include "windows.h"
BOOL DirInSecton(PIMAGE_DATA_DIRECTORY ndir,PIMAGE_SECTION_HEADER nsection)
{
if(ndir->VirtualAddress>=nsection->VirtualAddress&&ndir->VirtualAddress<=nsection->VirtualAddress+nsection->Misc.VirtualSize)
{
return TRUE;
}
return FALSE;
}
void AnyImportFromFile(PIMAGE_SECTION_HEADER Section,PIMAGE_NT_HEADERS npe,char *filePath)//导入表
{
IMAGE_DATA_DIRECTORY* IMAGE_Import = &npe->OptionalHeader.DataDirectory[1];
int IMAGE_Import_ID = -1;
//从所有节点中打到,所属于的结点位置
for (int i = 0 ; i < npe->FileHeader.NumberOfSections ; ++i)
{
if(DirInSecton(IMAGE_Import,Section+i))
{
IMAGE_Import_ID = i;break;
}
}
//如果找到了
if(IMAGE_Import_ID>=0)
{
//VirtualAddress 虚拟地址的起始位置,PointerToRawData 是文件中的起始位置。但是,它们的偏移是一样的
//IMAGE_Import->VirtualAddress 要将这个虚拟地址,转化为文件的地址,所以如下。
int n_Import_file=Section[IMAGE_Import_ID].VirtualAddress-Section[IMAGE_Import_ID].PointerToRawData;//相对值,RVA与PTRD想对值
int n_Import=IMAGE_Import->VirtualAddress-n_Import_file;//将位置进行转换
IMAGE_IMPORT_DESCRIPTOR dll;
IMAGE_IMPORT_DESCRIPTOR zero_dll = {0};//主要是为了用空比较
HANDLE file=INVALID_HANDLE_VALUE;
DWORD lpNumberOfBytesRead;
try
{
file=CreateFile(filePath,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_ATTRIBUTE_READONLY,0);//打开文件
if(file==INVALID_HANDLE_VALUE ) return ;//如果错误
int Dllnum = 0;//记录dll个数
SetFilePointer(file,n_Import,0,FILE_BEGIN);//设置位置
while (1)//主要是统计dll个数
{
ReadFile(file,&dll,sizeof(IMAGE_IMPORT_DESCRIPTOR),&lpNumberOfBytesRead,0);//读取,
if(memcmp(&zero_dll,&dll,sizeof(IMAGE_IMPORT_DESCRIPTOR)) == 0)//直到为空就停止
break;
Dllnum ++;
}
if(Dllnum == 0)
throw (1);
PIMAGE_IMPORT_DESCRIPTOR PDll = new IMAGE_IMPORT_DESCRIPTOR[Dllnum];//为其分配空间
int i = 0;
SetFilePointer(file,n_Import,0,FILE_BEGIN);//设置位置
//为得到 dll个数
while (1)
{
ReadFile(file,&PDll[i],sizeof(IMAGE_IMPORT_DESCRIPTOR),&lpNumberOfBytesRead,0);//读取,
if(memcmp(&zero_dll,&PDll[i],sizeof(IMAGE_IMPORT_DESCRIPTOR)) == 0)//直到为空就停止
break;
i++;
}
char Temp[256]={0};
printf("导入表\n");
//枚举
for (i = 0 ; i < Dllnum ; ++i)
{
SetFilePointer(file,PDll[i].Name-n_Import_file,NULL,FILE_BEGIN);
ReadFile(file,Temp,100,&lpNumberOfBytesRead,0);//读取名字
printf("%s ",Temp);
printf(" RVA:%08X\n",PDll[i].Name+npe->OptionalHeader.ImageBase);
char name[MAX_PATH]={0};
int vadd=0;
DWORD nameadd=0;
int j = 0;
//循环,导入表的函数
do
{
//进入
if(PDll[i].OriginalFirstThunk > 0)
SetFilePointer(file,PDll[i].OriginalFirstThunk-n_Import_file+vadd,NULL,FILE_BEGIN);//从原始表
else
SetFilePointer(file,PDll[i].FirstThunk-n_Import_file+vadd,NULL,FILE_BEGIN);//如果上面不成功
ReadFile(file,&nameadd,4,&lpNumberOfBytes