添加了pe可执行部分损坏的.text部分

Added pe executable section corrupting .text section

本文关键字:损坏 text 部分 执行部 pe 可执行 添加      更新时间:2023-10-16

我正试图将一个节添加到pe可执行文件中,当我添加该节时,它会损坏.text节的前40个字节的内存。我想知道是否有人知道为什么我的函数损坏了.text部分?

当我在CFF资源管理器中检查时,所有的偏移都是正确的,包括新的部分。这种情况在不同的文件中反复发生。

以下是创建添加部分的代码:

int addSection(char* sectionName, DWORD size){
int pos = ntHeader->FileHeader.NumberOfSections;
firstSection[pos].VirtualAddress = align((firstSection[pos - 1].VirtualAddress + firstSection[pos - 1].Misc.VirtualSize), ntHeader->OptionalHeader.SectionAlignment);
firstSection[pos].Misc.VirtualSize = (size);
firstSection[pos].PointerToRawData = align((firstSection[pos - 1].PointerToRawData + firstSection[pos - 1].SizeOfRawData), ntHeader->OptionalHeader.FileAlignment);
firstSection[pos].SizeOfRawData = align(size, ntHeader->OptionalHeader.FileAlignment);
firstSection[pos].NumberOfLinenumbers = 0;
firstSection[pos].NumberOfRelocations = 0;
firstSection[pos].PointerToLinenumbers = 0;
firstSection[pos].PointerToRelocations = 0;
ntHeader->FileHeader.NumberOfSections++;
ntHeader->OptionalHeader.SizeOfImage += align(firstSection[ntHeader->FileHeader.NumberOfSections-1].Misc.VirtualSize, ntHeader->OptionalHeader.SectionAlignment);
return 0;

}

在可移植可执行文件中添加部分:https://github.com/Ge0/PeTools/tree/master/PeAddSection

我发现解决方案是,在节头的末尾没有足够的空间来添加另一个节头,因此它会直接覆盖后面的节。现在我需要找出如何增加文件中的头空间,这样我就可以添加一个不溢出的节头。