如何使用pefile从PE文件中获取.text部分

Far*_*eer 4 python portable-executable python-2.7

如何.text通过使用pefile模块从PE 获取部分(或任何其他部分)的内容?

ndp*_*dpu 11

sections PE类实例的attr是段列表,每个项都有get_data函数:

>>> import pefile
>>> pe = pefile.PE('./psfile.exe')

>>> for section in pe.sections:
...   print (section.Name, hex(section.VirtualAddress),
...     hex(section.Misc_VirtualSize), section.SizeOfRawData )
... 
('.text\x00\x00\x00', '0x1000', '0xd3e4', 57344)
('.rdata\x00\x00', '0xf000', '0x5504', 24576)
('.data\x00\x00\x00', '0x15000', '0x3684', 8192)
('.rsrc\x00\x00\x00', '0x19000', '0x444', 4096)
Run Code Online (Sandbox Code Playgroud)

例如,来自文本部分数据的10个第一个字节:

>>> pe.sections[0].get_data()[:10]
'\x81\xec\x90\x00\x00\x00j>\x8dD'
Run Code Online (Sandbox Code Playgroud)

  • @webtobesocial我不知道为什么,但你可以使用`rstrip`摆脱'\ x00':''.text\x00\x00\x00'.rstrip('\ x00')`将只返回`.text` (2认同)