本帖最后由 d173220523 于 2020-5-3 15:14 编辑
[C] 纯文本查看 复制代码 #include "iostream"
#include "stdio.h"
#include <stdlib.h>
#include <windows.h>
LPVOID ReadPEFile(LPSTR lpszFile)
{
FILE *pFile = NULL;
DWORD fileSize = 0;
LPVOID pFileBuffer = NULL;
if ( (pFile = fopen(lpszFile, "rb")) == NULL )
puts("Fail to open file!");
fseek(pFile,0,SEEK_END);
fileSize=ftell(pFile);
pFileBuffer = malloc(fileSize);
fseek(pFile,0,SEEK_SET);
if(pFileBuffer == NULL)
puts("申请失败");
size_t n = fread(pFileBuffer, fileSize, 1, pFile);
if(!n)
{
printf(" 读取数据失败! ");
free(pFileBuffer);
fclose(pFile);
return NULL;
}
fclose(pFile);
return pFileBuffer;
}
VOID PrintNTHeaders()
{
LPVOID pFileBuffer = NULL;
PIMAGE_DOS_HEADER pDosHeader = NULL;
PIMAGE_NT_HEADERS pNTHeader = NULL;
PIMAGE_FILE_HEADER pPEHeader = NULL;
PIMAGE_OPTIONAL_HEADER32 pOptionHeader = NULL;
PIMAGE_SECTION_HEADER pSectionHeader = NULL;
pFileBuffer = ReadPEFile("C:\\Windows\\system32\\notepad.exe");
if(*((PWORD)pFileBuffer) != IMAGE_DOS_SIGNATURE)
{
printf("不是有效的MZ标志\n");
free(pFileBuffer);
return ;
}
pDosHeader = (PIMAGE_DOS_HEADER)pFileBuffer;
printf("********************DOC头********************\n");
printf("MZ标志:%x\n",pDosHeader->e_magic);
printf("PE偏移:%x\n",pDosHeader->e_lfanew);
//if(*((PWORD)pFileBuffer+pDosHeader->e_lfanew) != IMAGE_NT_SIGNATURE)
if(*((PDWORD)((DWORD)pFileBuffer+pDosHeader->e_lfanew)) != IMAGE_NT_SIGNATURE)
{
printf("不是有效的PE标志\n");
free(pFileBuffer);
return ;
}
pNTHeader=(PIMAGE_NT_HEADERS)((DWORD)pFileBuffer+pDosHeader->e_lfanew);
//printf("********************NT头********************\n");
//printf("Signature:%x\n",pNTHeader->Signature);
pPEHeader=(PIMAGE_FILE_HEADER)(((DWORD)pNTHeader)+4);
// printf("********************PE头********************\n");
//printf("Machine:%x\n",pPEHeader->Machine);
// printf("NumberOfSections:%x\n",pPEHeader->NumberOfSections);
// printf("SizeOfOptionalHeader:%x\n",pPEHeader->SizeOfOptionalHeader);
pOptionHeader=(PIMAGE_OPTIONAL_HEADER32)((DWORD)pNTHeader+0x18);
// printf("********************OPTIONAL_PE头********************\n");
// printf("OPTION_PE:%x\n",pOptionHeader->Magic);
// printf("AddressOfEntryPoint:%x\n",pOptionHeader->AddressOfEntryPoint);
// printf("ImageBase:%x\n",pOptionHeader->ImageBase);
// printf("SectionAlignment:%x\n",pOptionHeader->SectionAlignment);
// printf("FileAlignment:%x\n",pOptionHeader->FileAlignment);
pSectionHeader=(PIMAGE_SECTION_HEADER)((DWORD)pOptionHeader+pPEHeader->SizeOfOptionalHeader);
printf("********************节表********************\n");
for(int i=0;i<pPEHeader->NumberOfSections;i++)
{
printf("Name:%s\n",pSectionHeader->Name);
printf("Misc:%x\n",pSectionHeader->Misc);
printf("VirtualAddress:%x\n",pSectionHeader->VirtualAddress);
printf("SizeOfRawData:%x\n",pSectionHeader->SizeOfRawData);
printf("PointerToRawData:%x\n",pSectionHeader->PointerToRawData);
printf("Characteristics:%x\n",pSectionHeader->Characteristics);
printf("\n");
pSectionHeader+=0x28;//这里应该加什么?
}
free(pFileBuffer);
}
int main()
{
PrintNTHeaders();
return 0;
}
|