c++ - 添加了破坏 .text 部分的 pe 可执行部分

标签 c++ portable-executable

我正在尝试向 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

关于c++ - 添加了破坏 .text 部分的 pe 可执行部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26283438/

相关文章:

c++ - 在 C++ 中读取、操作和写入 SQLServer DateTimeOffset 字段

c - PEB(进程环境 block )无效的 DllBase 地址

c++ - 将操作码添加到现有二进制文件需要什么?

linker - 链接器如何工作

c++ - 具有模板成员的模板类,检查实例化

c++ - 只是一些简单的错误

C++ 模板理解

linux - 如何在linux中将PE(Portable Executable)格式转换为ELF

c++ - 如何使用 C++ 和 Window.h 解析 EXE 文件并从 IMAGE_DOS_HEADER 结构中获取数据?

仅限 Eclipse 外部的 C++ 段错误