【问题标题】:Python: Deleting a section from a PE filePython:从 PE 文件中删除一个部分
【发布时间】:2014-04-19 11:14:20
【问题描述】:

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

我该怎么做?我发现的有关此任务的唯一代码在函数pop_back()http://goo.gl/YYl5Vb。但是这段代码只删除了最后一部分,而我需要能够删除任何部分。

我想,我可以用类似的东西删除原始部分数据

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 库中没有对此提供一些支持吗?或者一些代码,比我更有能力的人写的?

提前致谢!

【问题讨论】:

    标签: python reverse-engineering portable-executable


    【解决方案1】:

    您有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 调用它:

    remove(your_section_double_p, index_of_section_to_remove)
    

    在后一种情况下,我会选择一个比remove() 更好的名字,不过:)

    【讨论】:

    • 不幸的是,这不起作用。如果我使用此代码删除除最后一个部分之外的任何部分,LordPE 会显示一个没有名称且所有值都设置为 0 的部分。以下所有节的偏移量与原始文件保持不变,这是不正确的,因为我们删除了节数据。最后一部分消失了,因为我们减少了 NumberOfSections。
    • 如果您可以重新添加部分,您可以尝试通过pop_back() 将所有部分删除回index,然后将它们全部添加(不包括要删除的部分)。否则您可能有深入挖掘 pefile 库的内部工作原理。
    猜你喜欢
    • 2012-03-28
    • 2011-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多