// PE节头描述如下,占40个字节
typedef struct _IMAGE_SECTION_HEADER {
BYTE Name[IMAGE_SIZEOF_SHORT_NAME];
union {
DWORD PhysicalAddress;
DWORD VirtualSize;
} Misc;
DWORD VirtualAddress;
DWORD SizeOfRawData;
DWORD PointerToRawData;
DWORD PointerToRelocations;
DWORD PointerToLinenumbers;
WORD NumberOfRelocations;
WORD NumberOfLinenumbers;
DWORD Characteristics;
} IMAGE_SECTION_HEADER, *PIMAGE_SECTION_HEADER;
通常我们只关心以下几个字段:
VirtualSize
VirtualAddress
SizeOfRawData
PointerToRawData
Characteristics
MSDN关于以上几个字段的解释如下:
VirtualSize // Total size of the section when loaded into memory.
// If this value is greater than the SizeOfRawData member,
// the section is filled with zeroes.
// 翻译过来就是该节加载到内存后所占的字节总数,
// 注意哈,这里并没有说明是按内存对其后的大小,
// 即该大小不是对其后的大小
VirtualAddress
// The address of the first byte of the section when loaded into memory,
// relative to the image base.
// 翻译过来就是该节加载到内存后的首地址,
// 注意哈,该地址是个偏移地址RVA,真实地址需要加上基址才能得到
SizeOfRawData
// The size of the initialized data on disk.
// This value must be a multiple of the FileAlignment member of the IMAGE_OPTIONAL_HEADER structure.
// If this value is less than the VirtualSize member,
// the remainder of the section is filled with zeroes.
// If the section contains only uninitialized data, the member is zero.
// 翻译过来就是该节在磁盘中已经初始化了的数据大小,
// 注意哈,该大小是按文件对其后的大小
// 如果该节只包含未初始化的数据,则该大小为0
PointerToRawData // File pointer to the first page within the COFF file.
// This value must be a multiple of the FileAlignment member of the IMAGE_OPTIONAL_HEADER structure.
// If a section contains only uninitialized data, this member is zero.
// 翻译过来就是该节在磁盘文件中的偏移地址,注意,磁盘文件中首地址是0哈
// 该地址是按照文件对其后的地址,
// 如果该节只包含未初始化的数据,则该大小为0
疑问:
1、VirtualSize是该节在磁盘文件中未对齐时的原始大小吗?
答案:否。
原因:
在看某些PE文件时,我们会惊奇的发现,某些节竟然有这种情况:VirtualSize > SizeOfRawData,
要知道SizeOfRawData代表的是该节在磁盘中按照文件对其后的大小,
如果VirtualSize表示该节的原始大小,VirtualSize是不可能大于SizeOfRawData,
看了MSDN后,我才恍然大悟,原来VirtualSize表示该节加载到内存后所占的字节总数(未按内存对齐)。
反正法虽然能说个大概,真正的原因,还有待深入...