Python:从PE文件中删除节

Python:从PE文件中删除节,python,reverse-engineering,portable-executable,Python,Reverse Engineering,Portable Executable,我正在使用Python和pefile库处理PE二进制文件。对于从二进制文件中读取信息和重写某些字节,这个库做得非常好。但是现在我想从文件中完全删除一个部分 我该怎么做?我找到的关于此任务的唯一代码是返回的函数pop_。但是这段代码只删除最后一节,而我需要能够删除任何一节 我想,我可以用如下方法删除原始的部分数据 dead_sect_start = dead_sect.PointerToRawData dead_sect_ed = dead_sect.PointerToRawData + dead

我正在使用Python和pefile库处理PE二进制文件。对于从二进制文件中读取信息和重写某些字节,这个库做得非常好。但是现在我想从文件中完全删除一个部分

我该怎么做?我找到的关于此任务的唯一代码是返回的函数pop_。但是这段代码只删除最后一节,而我需要能够删除任何一节

我想,我可以用如下方法删除原始的部分数据

dead_sect_start = dead_sect.PointerToRawData
dead_sect_ed = dead_sect.PointerToRawData + dead_sect.SizeOfRawData
pe.__data__ = pe.__data__[:dead_sect_start] + pe.__data__[dead_sect_end:])
其中,pe是我解析的二进制文件,dead_sect是我要删除的部分

但是,如何修复节标题?我不认为我会得到它的权利,如果我开始与我自己的单头字节混乱。pefile库中是否有对此的支持?或者一些代码,比我更有能力的人写的


提前谢谢

您已经有了pop_back函数的源代码,只需修改它即可满足您的需要:

def remove(self, index):
    """Removes a section of the section table.
       Deletes the section header in the section table, the data of the section
       in the file, removes the section in the sections list of pefile and adjusts
       the sizes in the optional header.
    """

    # Checking if the section list is long enough to actually remove index.
    if (self.pe.FILE_HEADER.NumberOfSections > index
        and self.pe.FILE_HEADER.NumberOfSections == len(self.pe.sections)):

        # Stripping the data of the section from the file.
        if self.pe.sections[index].SizeOfRawData != 0:
            self.pe.__data__ = 
                (self.pe.__data__[:self.pe.sections[index].PointerToRawData] +
                 self.pe.__data__[self.pe.sections[index].PointerToRawData +
                 self.pe.sections[index].SizeOfRawData:])

        # Overwriting the section header in the binary with nulls.
        # Getting the address of the section table and manually overwriting
        # the header with nulls unfortunally didn't work out.
        self.pe.sections[index].Name = '\x00'*8
        self.pe.sections[index].Misc_VirtualSize = 0x00000000
        self.pe.sections[index].VirtualAddress = 0x00000000
        self.pe.sections[index].SizeOfRawData = 0x00000000
        self.pe.sections[index].PointerToRawData = 0x00000000
        self.pe.sections[index].PointerToRelocations = 0x00000000
        self.pe.sections[index].PointerToLinenumbers = 0x00000000
        self.pe.sections[index].NumberOfRelocations = 0x0000
        self.pe.sections[index].NumberOfLinenumbers = 0x0000
        self.pe.sections[index].Characteristics = 0x00000000

        del self.pe.sections[index]

        self.pe.FILE_HEADER.NumberOfSections -= 1

        self.__adjust_optional_header()
    else:
        raise SectionDoublePError("There's no section to remove.")
您可以按原样将该函数添加到类SectionDoubleP,或仅使用SectionDoubleP对象上的显式self调用它:


在后一种情况下,我会选择一个比删除更好的名称,不过:

不幸的是,这不起作用。如果我使用此代码删除了上一个以外的任何部分,则LordPE会显示一个没有名称且所有值都设置为0的部分。以下所有节的偏移量与原始文件保持不变,这是不正确的,因为我们删除了节数据。最后一个部分消失了,因为我们减少了sections的数量。如果您可以重新添加部分,您可以尝试通过pop_back将所有部分重新删除到索引中,然后将它们全部添加到索引中,不包括要重新删除的部分。否则,您可能必须深入研究PEF文件库的内部工作机制。
remove(your_section_double_p, index_of_section_to_remove)