typedef struct _IMAGE_IMPORT_DESCRIPTOR {
union {
DWORD Characteristics // 0 for terminating null import descriptor
DWORD OriginalFirstThunk// RVA to original unbound IAT (PIMAGE_THUNK_DATA)
}
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
//建立一个vc6.0中建立一个win32console工程,打入如下代码。我在我的电脑上测试通过了。#include "手洞stdafx.h"
#include <windows.h>
#include <winbase.h>
#include <stdio.h>
#include <tchar.h>
#include <imagehlp.h>
typedef PVOID (CALLBACK* PFNEXPORTFUNC)(PIMAGE_NT_HEADERS,PVOID,ULONG,PIMAGE_SECTION_HEADER*)
void printAllFuncName(char* fileName)//打印所有函数名称的函数声明
int main(int argc, char* argv[])
{
printAllFuncName("F:\\gdi32.dll")//打印gdi32.dll中所有函数名称,要打印别的dll中的函数,只要改变这个字符串。
getchar()
return 0
}
void printAllFuncName(char* fileName){
LPWIN32_FIND_DATA lpwfd_first=new WIN32_FIND_DATA/毕磨枯/接受findfirstfile的结构指针
HANDLE hFile,hFileMap//文件句柄和内存映射文件句柄
DWORD fileAttrib=0//存储文件属性用,在createfile中用到。
void* mod_base//内存映射文件的起始地址,也是模块的起始地址
//首先取得ImageRvaToVa函数本来只要#include <Dbghelp.h>就可以使用这个函数,但是我的vc没有这个头文件
PFNEXPORTFUNC ImageRvaToVax=NULL
HMODULE hModule=::LoadLibrary("DbgHelp.dll")
if(hModule!=NULL){
ImageRvaToVax=(PFNEXPORTFUNC)::GetProcAddress(hModule,"ImageRvaToVa")
if(ImageRvaToVax==NULL){
::FreeLibrary(hModule)
printf("取得函数失败\n")
return
}
}else{
printf("加载模块失败\n")
return
}
if(FindFirstFile(fileName,lpwfd_first)==NULL){//返回值为NULL,则文件不存在,游野退出
printf("文件不存在: %s ",fileName)
return
}else{
DWORD fileAttrib=lpwfd_first->dwFileAttributes
}
hFile=CreateFile(fileName,GENERIC_READ,0,0,OPEN_EXISTING,fileAttrib,0)
if(hFile==INVALID_HANDLE_VALUE){
printf("打开文件出错!")
return
}
hFileMap=CreateFileMapping(hFile,0,PAGE_READONLY,0,0,0)
if(hFileMap==NULL){
CloseHandle(hFile)
printf("建立内存映射文件出错!")
return
}
mod_base=MapViewOfFile(hFileMap,FILE_MAP_READ,0,0,0)
if (mod_base==NULL)
{
printf("建立内存映射文件出错!")
CloseHandle(hFileMap)
CloseHandle(hFile)
return
}
IMAGE_DOS_HEADER* pDosHeader = (IMAGE_DOS_HEADER*)mod_base
IMAGE_NT_HEADERS * pNtHeader =
(IMAGE_NT_HEADERS *)((BYTE*)mod_base+ pDosHeader->e_lfanew)//得到NT头首址
//IMAGE_NT_HEADERS * pNtHeader =ImageNtHeader (mod_base)
IMAGE_OPTIONAL_HEADER * pOptHeader =
(IMAGE_OPTIONAL_HEADER *)((BYTE*)mod_base + pDosHeader->e_lfanew + 24)//optional头首址
IMAGE_EXPORT_DIRECTORY* pExportDesc = (IMAGE_EXPORT_DIRECTORY*)ImageRvaToVax(pNtHeader,mod_base,pOptHeader->DataDirectory[0].VirtualAddress,0)
//导出表首址
PDWORD nameAddr=(PDWORD)ImageRvaToVax(pNtHeader,mod_base,pExportDesc->AddressOfNames,0)//函数名称表首地址每个DWORD代表一个函数名字字符串的地址
PCHAR func_name = (PCHAR)ImageRvaToVax(pNtHeader,mod_base,(DWORD)nameAddr[0],0)
DWORD i=0
DWORD unti=pExportDesc->NumberOfNames
for(i=0i<untii++){
printf("%s\n",func_name)
func_name = (PCHAR)ImageRvaToVax(pNtHeader,mod_base,(DWORD)nameAddr[i],0)
}
::FreeLibrary(hModule)
CloseHandle(hFileMap)
CloseHandle(hFile)
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)