当前位置:   article > 正文

PE文件(三)节表作业

PE文件(三)节表作业

本次作业以notepad进行演示,如下是其在硬盘上的内存

1.手动解析节表

由标准pe头可知,一共由7个节也就是7个节表,可选pe头的大小是0X00F0,即240字节大小

根据上述我们所获取的信息,找到节表的首地址为0x01F8

.text

#define IMAGE_SIZEOF_SHORT_NAME  8 //宏定义    
typedef struct _IMAGE_SECTION_HEADER{
    BYTE Name[IMAGE_SIZEOF_SHORT_NAME];  0X000000747865742E
    union{
        DWORD   PhysicalAddress;
        DWORD   VirtualSize;
    }Misc;  0X00027D02
    DWORD VirtualAddress;  0X00001000
    DWORD SizeOfRawData;  0X00028000
    DWORD PointerToRawData; 0X00001000
    DWORD PointerToRelocations; 0X00000000
    DWORD PointerToLinenumbers;0X00000000
    WORD NumberOfRelocations; 0X0000
    WORD NumberOfLinenumbers;0X0000
    DWORD Characteristics;  0X60000020
} IMAGE_SECTION_HEADER, *PIMAGE_SECTION_HEADER;
 

.rdata

#define IMAGE_SIZEOF_SHORT_NAME 8 
typedef struct _IMAGE_SECTION_HEADER{
    BYTE Name[IMAGE_SIZEOF_SHORT_NAME];  0X000061746164722E
    union{
        DWORD   PhysicalAddress;
        DWORD   VirtualSize;
    }Misc;  0X0000A608
    DWORD VirtualAddress;  0X00029000
    DWORD SizeOfRawData; 0X0000B000
    DWORD PointerToRawData; 0X00029000
    DWORD PointerToRelocations; 0X00000000
    DWORD PointerToLinenumbers; 0X00000000
    WORD NumberOfRelocations; 0X0000
    WORD NumberOfLinenumbers; 0X0000
    DWORD Characteristics;  0X40000040
} IMAGE_SECTION_HEADER, *PIMAGE_SECTION_HEADER;
 

.data

#define IMAGE_SIZEOF_SHORT_NAME 8
typedef struct _IMAGE_SECTION_HEADER{
    BYTE Name[IMAGE_SIZEOF_SHORT_NAME]; 0X000000617464642E
    union{
        DWORD   PhysicalAddress;
        DWORD   VirtualSize;
    }Misc;  000026C0
    DWORD VirtualAddress;  0X00034000
    DWORD SizeOfRawData; 0X00001000
    DWORD PointerToRawData; 0X00034000
    DWORD PointerToRelocations; 0X00000000
    DWORD PointerToLinenumbers; 0X00000000
    WORD NumberOfRelocations; 0X0000
    WORD NumberOfLinenumbers;0X0000
    DWORD Characteristics; 0XC0000040
} IMAGE_SECTION_HEADER, *PIMAGE_SECTION_HEADER;
 

由于该文件所有节过多,此处不再多演示,具体操作都是一样的。

2.控制台输出解析所有节表

  1. #include<stdio.h>
  2. #include<Windows.h>
  3. char* ReadPEFile(const char* lpszFile)
  4. {
  5. FILE* pFile = NULL;
  6. pFile = fopen(lpszFile, "rb");
  7. DWORD fileSize = 0;
  8. char* pFileBuffer = NULL;
  9. if (!pFile)
  10. {
  11. printf("无法打开EXE文件");
  12. return NULL;
  13. }
  14. fseek(pFile, 0, SEEK_END);
  15. fileSize = ftell(pFile);
  16. fseek(pFile, 0, SEEK_SET);
  17. pFileBuffer = (char*)malloc(sizeof(char)*fileSize);
  18. if (!pFileBuffer)
  19. {
  20. printf("分配空间失败");
  21. fclose(pFile);
  22. return NULL;
  23. }
  24. size_t i = fread(pFileBuffer, fileSize, 1, pFile);
  25. if (!i)
  26. {
  27. printf("读取数据失败!");
  28. free(pFileBuffer);
  29. fclose(pFile);
  30. return NULL;
  31. }
  32. fclose(pFile);
  33. return pFileBuffer;
  34. }
  35. BOOL PrintNTHeaders(const char* lpszFile)
  36. {
  37. IMAGE_DOS_HEADER *pDosHeader;
  38. IMAGE_NT_HEADERS *pNTHeader;
  39. IMAGE_FILE_HEADER *pPEHeader;
  40. IMAGE_SECTION_HEADER *pSecHeader;
  41. int i = 0;
  42. char* pFileBuffer = ReadPEFile(lpszFile);
  43. pDosHeader = (IMAGE_DOS_HEADER*)pFileBuffer;
  44. if (pDosHeader->e_magic != IMAGE_DOS_SIGNATURE)
  45. {
  46. printf("不是有效MZ标志,打印结束\n");
  47. free(pFileBuffer);
  48. pFileBuffer = NULL;
  49. return FALSE;
  50. }
  51. pNTHeader = (IMAGE_NT_HEADERS*)((char*)pFileBuffer + pDosHeader->e_lfanew);
  52. if (pNTHeader->Signature != IMAGE_NT_SIGNATURE)
  53. {
  54. printf("不是有效的PE标志,打印结束\n");
  55. free(pFileBuffer);
  56. pFileBuffer = NULL;
  57. return FALSE;
  58. }
  59. pPEHeader = (IMAGE_FILE_HEADER*)((char*)pNTHeader + 4);
  60. printf("NumberOfSections(节表的数量):%04X\n", pPEHeader->NumberOfSections);
  61. printf("SizeOfOptionalHeader(可选pe头的大小):%04X\n", pPEHeader->SizeOfOptionalHeader);
  62. printf("\n");
  63. printf("节表信息解析开始");
  64. pSecHeader = (IMAGE_SECTION_HEADER*)((char*)pPEHeader + sizeof(_IMAGE_FILE_HEADER) + pPEHeader->SizeOfOptionalHeader);
  65. for (int i = 0; i < pPEHeader->NumberOfSections; i++)
  66. {
  67. char SecName[9] = "\0";
  68. printf("这是第%d个节表\n", i+1);
  69. char* Name = (char*)pSecHeader->Name;
  70. strcpy(SecName, Name);
  71. printf("Name:%s\n", SecName);
  72. printf("VirtualSize:%08X\n", pSecHeader->Misc.VirtualSize);
  73. printf("VirtualAddress:%08X\n", pSecHeader->VirtualAddress);
  74. printf("SizeOfRawData:%08X\n", pSecHeader->SizeOfRawData);
  75. printf("PointerToRawData:%08X\n", pSecHeader->PointerToRawData);
  76. printf("Characteristics:%08X\n", pSecHeader->Characteristics);
  77. printf("\n");
  78. pSecHeader++;
  79. }
  80. free(pFileBuffer);
  81. pFileBuffer = NULL;
  82. return TRUE;
  83. }
  84. int main(int argc, char* argv[])
  85. {
  86. const char* lpszFile = "C:\\Windows\\notepad.exe";
  87. PrintNTHeaders(lpszFile);
  88. return 0;
  89. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/507774
推荐阅读
相关标签
  

闽ICP备14008679号