1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
#include "stdafx.h"
#include <stdio.h>
#include <shlwapi.h> // For types
typedef struct _PE_FILE_HEADER
{
ULONG signature;
IMAGE_FILE_HEADER header;
IMAGE_OPTIONAL_HEADER optionalHeader;
}PE_FILE_HEADER;
ULONG main(ULONG argc, PCHAR argv[])
{
IMAGE_DOS_HEADER dosHeader;
PE_FILE_HEADER peHeader;
PIMAGE_SECTION_HEADER pSections;
FILE* file;
errno_t errno;
PCHAR szFilename = "toto.exe";
if(argc == 2)
{
szFilename = argv[1];
printf("Using %s as file\n", szFilename);
}
memset(&dosHeader, 0, sizeof(IMAGE_DOS_HEADER));
memset(&peHeader, 0, sizeof(PE_FILE_HEADER));
if((errno = fopen_s(&file, szFilename, "r")) != 0)
{
perror("Error while opening file");
return -1;
}
if (fread(&dosHeader, sizeof(IMAGE_DOS_HEADER), 1, file) != 1)
{
printf("Error while reading IMAGE_DOS_HEADER\n");
return -1;
}
if(fseek(file, dosHeader.e_lfanew, 0) != 0)
{
printf("Error while seeking PE header start\n");
return -1;
}
if(fread(&peHeader, sizeof(PE_FILE_HEADER), 1, file) != 1)
{
printf("Error while reading IMAGE_DOS_HEADER\n");
return -1;
}
printf("Number of sections : %d\n", peHeader.header.NumberOfSections);
printf("Entry point : %#8lx\n", peHeader.optionalHeader.AddressOfEntryPoint);
printf("File alignment: %#8lx\n", peHeader.optionalHeader.FileAlignment);
if((pSections = (PIMAGE_SECTION_HEADER)calloc(peHeader.header.NumberOfSections, sizeof(IMAGE_SECTION_HEADER))) == NULL)
{
printf("Error while allocation memory for the sections\n");
return -1;
}
printf("%#8lx\n",ftell(file));
size_t readSize;
if((readSize = fread(pSections, sizeof(IMAGE_SECTION_HEADER), peHeader.header.NumberOfSections, file)) != peHeader.header.NumberOfSections)
{
printf("Error while reading IMAGE_SECTION_HEADER\n");
printf("Only %d sections read\n", readSize);
return -1;
}
for(int i = 0; i < peHeader.header.NumberOfSections; i++)
{
printf("Section %s : \n", pSections[i].Name);
printf("\tCharacteristics : %#8lx\n", pSections[i].Characteristics);
printf("\tMisc : %#8lx\n", pSections[i].Misc);
printf("\tNumber of line numbers : %#x\n", pSections[i].NumberOfLinenumbers);
printf("\tPointer to line numbers : %#x\n", pSections[i].PointerToLinenumbers);
printf("\tNumber of relocations : %#x\n", pSections[i].NumberOfRelocations);
printf("\tPointer to relocations : %#8lx\n", pSections[i].PointerToRelocations);
printf("\tSize of raw data : %#8lx\n", pSections[i].SizeOfRawData);
printf("\tPointer to raw data : %#8lx\n", pSections[i].PointerToRawData);
printf("\tVirtual address : %#8lx\n", pSections[i].VirtualAddress);
printf("\tEnd of raw data : %#8lx\n", pSections[i].SizeOfRawData + pSections[i].PointerToRawData);
}
return 0;
} |
Partager