1 #ifndef __CPEUTIL_H__
 2 #define __CPEUTIL_H__
 3 #include<Windows.h>
 4 #include<iostream>
 5 class CPeUtil
 6 {
 7 public:
 8     CPeUtil();
 9     ~CPeUtil();
10     //加载文件
11     BOOL LoadFile(const char* patch);
12     //初始化PE信息
13     BOOL InitPeInfo();
14     //遍历区段头
15     void PrintSectionHeaders();
16 private:
17     //创建缓冲区来存储文件信息
18     char* FileBuff;
19     //获取文件大小
20     DWORD FileSize;
21     PIMAGE_DOS_HEADER pDosHeader;
22     PIMAGE_NT_HEADERS pNtHeader;
23     PIMAGE_FILE_HEADER pFileHeader;
24     PIMAGE_OPTIONAL_HEADER pOptionalHeader;
25 };
26 #endif

 

  1 #include"CpeUtil.h"
  2 CPeUtil::CPeUtil()
  3 {
  4     this->FileBuff = NULL;
  5     this->FileSize = 0;
  6     this->pDosHeader = NULL;
  7     this->pFileHeader = NULL;
  8     this->pOptionalHeader = NULL;
  9     this->pNtHeader = NULL;
 10 }
 11 
 12 CPeUtil::~CPeUtil()
 13 {
 14     if (FileBuff != NULL)
 15     {
 16         delete[]FileBuff;
 17         FileBuff = NULL;
 18     }
 19 }
 20 
 21 BOOL CPeUtil::LoadFile(const char* patch)
 22 {
 23     //拿到文件句柄
 24     HANDLE hFile = CreateFileA(patch,
 25         GENERIC_READ,
 26         0x00000000,
 27         NULL,
 28         OPEN_EXISTING,
 29         FILE_ATTRIBUTE_NORMAL,
 30         NULL
 31     );
 32     if (hFile == 0)
 33     {
 34         std::cout << "打开文件失败" << std::endl;
 35         return false;
 36     }
 37     //获取文件大小
 38     this->FileSize = GetFileSize(hFile, NULL);
 39 
 40     //创建缓冲区
 41     this->FileBuff = new char[this->FileSize]{ 0 };
 42 
 43     DWORD RealSize = 0;
 44     //复制文件内容给缓冲区
 45     BOOL ret =    ReadFile(hFile,
 46         this->FileBuff,
 47         this->FileSize,
 48         &RealSize,
 49         NULL
 50     );
 51     if (ret == 0)
 52     {
 53         std::cout << "读取文件失败" << std::endl;
 54         return false;
 55     }
 56 
 57     std::cout << "实际读取的文件大小为:" << RealSize << std::endl;
 58     //初始化PE文件结构体
 59     if (InitPeInfo())
 60     {
 61         //读取完文件后关闭掉文件句柄
 62         CloseHandle(hFile);
 63         return true;
 64     }
 65 
 66     return false;
 67 }
 68 
 69 
 70 BOOL CPeUtil::InitPeInfo()
 71 {
 72     this->pDosHeader = (PIMAGE_DOS_HEADER)this->FileBuff;
 73     if(this->pDosHeader->e_magic != 0x5A4D)
 74     {
 75         std::cout << "不是有效的PE文件" << std::endl;
 76         return false;
 77     }
 78     this->pNtHeader = (PIMAGE_NT_HEADERS)(this->FileBuff + this->pDosHeader->e_lfanew);
 79     if (this->pNtHeader->Signature != IMAGE_NT_SIGNATURE)
 80     {
 81         std::cout << "不是有效的PE文件" << std::endl;
 82         return false;
 83     }
 84     this->pFileHeader = (PIMAGE_FILE_HEADER)&this->pNtHeader->FileHeader;
 85     this->pOptionalHeader = (PIMAGE_OPTIONAL_HEADER)&this->pNtHeader->OptionalHeader;
 86     return true;
 87 }
 88 void CPeUtil::PrintSectionHeaders()
 89 {
 90     PIMAGE_SECTION_HEADER pSectionHeader = IMAGE_FIRST_SECTION(pNtHeader);
 91     for (int i = 0; i < this->pFileHeader->NumberOfSections; i++)
 92     {
 93         char name[9]{ 0 };
 94         for (int i = 0; i < 8; i++)
 95         {
 96             name[i] = pSectionHeader->Name[i];
 97         }
 98         std::cout << "区段名称:" << name<<std::endl;
 99         pSectionHeader++;
100     }
101 
102 
103 }
 1 #include"CpeUtil.h"
 2 
 3 int main()
 4 {
 5     CPeUtil peUtil;
 6     BOOL ifSuccess = peUtil.LoadFile("E:\\Project_Sum\\CC++\\test\\test\\Thread_syn.exe");
 7     if (ifSuccess)
 8     {
 9         peUtil.PrintSectionHeaders();
10         return    0;
11     }
12     std::cout << "加载PE文件失败" << std::endl;
13     return 0;
14 }