写此文源于前一阵写一个PE修改工具,需要用到内存偏移向文件偏移转化。想着这种简单的代码网上应该一大把,百度了一下,没找到。又google,又没找到,可能是自己搜索的功力太不到家了。着急用,只好自己写了一个函数用来转换。相信很多人做PE开发的时候都会有这个需求,把这个函数贴出来供大家参考,大牛莫耻笑。
原理比较简单:首先判断这个地址是否在PE头中,如果在,文件偏移和内存偏移相等,如果存在于文件的区段中,则利用以下公式:
内存偏移 - 该段起始的RVA(VirtualAddress) = 文件偏移 - 该段的PointerToRawData
内存偏移 = 该段起始的RVA(VirtualAddress) + (文件偏移 - 该段的PointerToRawData)
文件偏移 = 该段的PointerToRawData + (内存偏移 - 该段起始的RVA(VirtualAddress))
代码如下:
原理比较简单:首先判断这个地址是否在PE头中,如果在,文件偏移和内存偏移相等,如果存在于文件的区段中,则利用以下公式:
内存偏移 - 该段起始的RVA(VirtualAddress) = 文件偏移 - 该段的PointerToRawData
内存偏移 = 该段起始的RVA(VirtualAddress) + (文件偏移 - 该段的PointerToRawData)
文件偏移 = 该段的PointerToRawData + (内存偏移 - 该段起始的RVA(VirtualAddress))
代码如下:
01.#include <stdio.h>
02./*
03.Purpose:PE文件的内存偏移与文件偏移相互转换,不考虑系统为对齐填充偏移转换
04.szFileName:文件名
05.dwAddr:需要转换的偏移值
06.bFile2RVA:是否是文件偏移到内存偏移的转换,1 - dwAddr代表的是文件偏移,此函数返回内存偏移
07. 0 - dwAddr代表的是内存偏移,此函数返回文件偏移
08.返回值:相对应的偏移值,失败返回-1
09.*/
10.
11.DWORD AddressConvert(char szFileName[], DWORD dwAddr, BOOL bFile2RVA)
12.{
13. char *lpBase = NULL;
14. DWORD dwRet = -1;
15. //1.首先将文件读入内存
16. if(szFileName[0] == 0)
17. {
18. return -1;
19. }
20.
21. FILE *fp = fopen(szFileName, "rb");
22. if(fp == 0)
23. {
24. return -1;
25. }
26.
27. fseek(fp, 0, SEEK_END);
28. DWORD dwFileSize = ftell(fp);
29. if(dwFileSize == 0)
30. {
31. return -1;
32. }
33.
34. lpBase = new char[dwFileSize];
35. memset(lpBase, 0, dwFileSize);
36. fseek(fp, 0, SEEK_SET);
37. fread(lpBase, 1, dwFileSize, fp);
38. fclose(fp);
39.
40. //2.读取该文件的信息(文件内存对齐方式以及区块数量,并将区块表指针指向区块表第一个区块头)
41. PIMAGE_DOS_HEADER pDosHeader = (PIMAGE_DOS_HEADER)lpBase;
42. PIMAGE_NT_HEADERS pNtHeader = (PIMAGE_NT_HEADERS)((unsigned long)lpBase + pDosHeader->e_lfanew);
43.
44. DWORD dwMemAlign = pNtHeader->OptionalHeader.SectionAlignment;
45. DWORD dwFileAlign = pNtHeader->OptionalHeader.FileAlignment;
46. int dwSecNum = pNtHeader->FileHeader.NumberOfSections;
47. PIMAGE_SECTION_HEADER pSecHeader = (PIMAGE_SECTION_HEADER)((char *)lpBase + pDosHeader->e_lfanew + sizeof(IMAGE_NT_HEADERS));
48. DWORD dwHeaderSize = 0;
49.
50. if(!bFile2RVA) // 内存偏移转换为文件偏移
51. {
52. //看需要转移的偏移是否在PE头内,如果在则两个偏移相同
53. dwHeaderSize = pNtHeader->OptionalHeader.SizeOfHeaders;
54. if(dwAddr <= dwHeaderSize)
55. {
56. delete lpBase;
57. lpBase = NULL;
58. return dwAddr;
59. }
60. else //不再PE头里,查看该地址在哪个区块中
61. {
62. for(int i = 0; i < dwSecNum; i++)
63. {
64. DWORD dwSecSize = pSecHeader[i].Misc.VirtualSize;
65. if((dwAddr >= pSecHeader[i].VirtualAddress) && (dwAddr <= pSecHeader[i].VirtualAddress + dwSecSize))
66. {
67. //3.找到该该偏移,则文件偏移 = 该区块的文件偏移 + (该偏移 - 该区块的内存偏移)
68. dwRet = pSecHeader[i].PointerToRawData + dwAddr - pSecHeader[i].VirtualAddress;
69. }
70. }
71. }
72. }
73. else // 文件偏移转换为内存偏移
74. {
75. dwHeaderSize = pNtHeader->OptionalHeader.SizeOfHeaders;
76. //看需要转移的偏移是否在PE头内,如果在则两个偏移相同
77. if(dwAddr <= dwHeaderSize)
78. {
79. delete lpBase;
80. lpBase = NULL;
81. return dwAddr;
82. }
83. else//不再PE头里,查看该地址在哪个区块中
84. {
85. for(int i = 0; i < dwSecNum; i++)
86. {
87. DWORD dwSecSize = pSecHeader[i].Misc.VirtualSize;
88. if((dwAddr >= pSecHeader[i].PointerToRawData) && (dwAddr <= pSecHeader[i].PointerToRawData + dwSecSize))
89. {
90. //3.找到该该偏移,则内存偏移 = 该区块的内存偏移 + (该偏移 - 该区块的文件偏移)
91. dwRet = pSecHeader[i].VirtualAddress + dwAddr - pSecHeader[i].PointerToRawData;
92. }
93. }
94. }
95. }
96.
97. //5.释放内存
98. delete lpBase;
99. lpBase = NULL;
100. return dwRet;
101.}