有没有办法用 python pefile 检查 exe 是否是点 NET?

Is there a way to check if an exe is dot NET with python pefile?

我正在尝试编写一个简单的 python 脚本;最好使用可以告诉我 exe 或 dll 文件是否编译为 .NET 的 pefile。我知道我可以查找字符串 'BSJB' 来查看该程序是否是用 .NET 编写的,但我试图以比使用 grep 和字符串更 pythonic 的方式来执行此操作。 运行 pefile.PE('my.exe').dump_info() 给了我一些很好的信息,但不足以确定它实际上是 dot Net 还是什么版本的 dot Net。

谢谢!

您可以通过检查 IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR 是否已填写(即其 VirtualAddress 和 Size 不为零)来识别 .NET 程序集。该条目的名称令人困惑,但它是用于 .NET 元数据的名称;参见 Names of PE directories

如果您需要程序集所需的框架版本,那么您将不得不自己解析元数据结构,pefile 似乎不支持。如果你能做到这一点,那么根据 http://www.ntcore.com/files/dotnetformat.htm 你会在那里找到名为 MajorRuntimeVersion 和 MinorRuntimeVersion 的字段,尽管我不确定应该如何解释这些字段。

最终代码为:

isDotNet = pe.OPTIONAL_HEADER.DATA_DIRECTORY[14]
if isDotNet.VirtualAddress == 0 and isDotNet.Size == 0:
    print colors.RED + 'Not a .NET executable'
else:
    print colors.BLUE + 'Is a .NET executable'