2012-05-10 106 views
0

我試圖用下面的代碼列出PE文件的導入DLL,但它沒有工作,Windows說我運行它時exe已停止工作。在代碼中,我使用CreateFileMapping函數將給定的exe文件映射到內存中,然後使用Win32 API中給出的適當結構瀏覽器的每個部分。我該如何糾正它?列表導入的PE文件的DLL

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

//add Pointer Values 
#define MakePtr(cast, ptr, addValue) (cast)((unsigned long)(ptr)+(unsigned long)(addValue)) 


int main(int argc , char ** argv) //main method 
{ 
HANDLE hMapObject, hFile;//File Mapping Object 
LPVOID lpBase;//Pointer to the base memory of mapped 

PIMAGE_DOS_HEADER dosHeader;//Pointer to DOS Header 
PIMAGE_NT_HEADERS ntHeader;//Pointer to NT Header 
PIMAGE_IMPORT_DESCRIPTOR importDesc;//Pointer to import descriptor 

hFile = CreateFile(argv[1],GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);//Open the Exe File 
if(hFile == INVALID_HANDLE_VALUE){ 
     printf("\nERROR : Could not open the file specified\n"); 
} 
hMapObject = CreateFileMapping(hFile,NULL,PAGE_READONLY,0,0,NULL); 
lpBase = MapViewOfFile(hMapObject,FILE_MAP_READ,0,0,0);//Mapping Given EXE file to Memory 

dosHeader = (PIMAGE_DOS_HEADER)lpBase;//Get the DOS Header Base 
//verify dos header 
if (dosHeader->e_magic == IMAGE_DOS_SIGNATURE) 
{ 

    ntHeader = MakePtr(PIMAGE_NT_HEADERS, dosHeader, dosHeader->e_lfanew);//Get the NT Header 
      //verify NT header 
    if (ntHeader->Signature == IMAGE_NT_SIGNATURE){ 
     importDesc = MakePtr(PIMAGE_IMPORT_DESCRIPTOR, dosHeader,ntHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress); 

     while (importDesc->Name) 
     { 
      printf("%s\n",MakePtr(char*, dosHeader,importDesc->Name)); 
      importDesc++;    
     } 

    } 
} 

getchar(); 

} 
+0

這是很難做到的。 MS爲此提供了dbghelp DLL。 –

回答

3

您正在查找的列表內容包含在一個部分(如PE圖像中的幾乎所有內容)中。您必須訪問目錄指向的部分。看看Matt Pietrek的代碼(PeDump),看看它是如何工作的。

+0

Matt的代碼很舊,並沒有準備好處理一些PE圖像,但它確實幫助我創建了PeStudio。 – mox

+0

請提供downvoting的原因,並建議糾正任何錯誤!謝謝。 – mox

+1

現在我正在編寫我自己的解析器,PE格式讓我困惑了一下。我正在使用PeStudio來驗證我提取的一些關鍵值!感謝您的支持! – Dio