erocarrera/pefile

GitHub: erocarrera/pefile

一个用 Python 实现的跨平台 PE 文件解析模块,帮助程序化读取与分析 Portable Executable 格式。

Stars: 2032 | Forks: 540

# pefile [![PyPI version](https://badge.fury.io/py/pefile.svg)](https://badge.fury.io/py/pefile) ![pefile test](https://static.pigsec.cn/wp-content/uploads/repos/2026/04/6f4ceb5299093231.svg) ![Coverage](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/erocarrera/2150adbc4ea8c61e381fdb9da0943723/raw/covbadge.json) [![OpenSSF Scorecard](https://api.scorecard.dev/projects/github.com/erocarrera/pefile/badge)](https://scorecard.dev/viewer/?uri=github.com/erocarrera/pefile) [![Contributors](https://img.shields.io/github/contributors/erocarrera/pefile)](https://github.com/erocarrera/pefile/graphs/contributors) [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black) [![Downloads](https://static.pepy.tech/badge/pefile/month)](https://pepy.tech/project/pefile) [![Downloads](https://static.pepy.tech/badge/pefile)](https://pepy.tech/project/pefile) _pefile_ 是一个用于解析和使用 [Portable Executable (PE) 文件](https://en.wikipedia.org/wiki/Portable_Executable) 的多平台 Python 模块。PE 文件头中包含的大部分信息都是可访问的,所有节的详细信息与数据也都可被获取。 在 Windows 头文件中定义的结构将作为属性在 PE 实例中可用。字段/属性的命名将尽量遵循这些头文件中的命名约定。仅为了方便而添加的快捷方式会偏离这一约定。 _pefile_ 需要一些关于 PE 文件布局的基本理解——有了它,就可以探索 PE 文件格式的几乎每一个特性。 ### 安装 要通过 pip 安装 pefile: `pip3 install pefile` ## 功能 pefile 可以实现以下部分任务: * 检查头部信息 * 分析节数据 * 检索嵌入的数据 * [从资源中读取字符串](https://github.com/erocarrera/pefile/blob/wiki/ReadingResourceStrings.md) * 对可疑和格式错误的值发出警告 * 对 PE 进行基本修改,例如 [写入某些字段](https://github.com/erocarrera/pefile/blob/wiki/UsageExamples.md#reading-and-writing-standard-header-members) 和 [其他部分](https://github.com/erocarrera/pefile/blob/wiki/ModifyingPEImageData.md) * 此功能不会重新排列 PE 文件结构以腾出新字段的空间,因此请谨慎使用。 * 覆盖字段通常是安全的。 * 使用 [PEiD 的签名](https://github.com/erocarrera/pefile/blob/wiki/PEiDSignatures.md) 进行打包器检测 * [生成 PEiD 签名](https://github.com/erocarrera/pefile/blob/wiki/PEiDSignatures.md) 请参考 [使用示例](https://github.com/erocarrera/pefile/blob/wiki/UsageExamples.md#introduction) 以获取演示如何使用 _pefile_ 的代码片段。 以下是使用 _pefile_ 生成的某些文件转储示例: * [已打包文件](https://github.com/erocarrera/pefile/blob/wiki/FullDump0x90.md) * [kernel32.dll](https://github.com/erocarrera/pefile/blob/wiki/FullDumpKernel32.md) * [TinyPE](https://github.com/erocarrera/pefile/blob/wiki/FullDumpTinyPE.md) 要处理经过身份验证的二进制文件(包括 **Authenticode 签名**),请查看项目 [verify-sigs](https://code.google.com/archive/p/verify-sigs/)。 _pefile_ 每天在多个流水线中扫描数十万份新的 PE 文件,并且虽然并不完美,但随着时间的推移它已经变得相当健壮。也就是说,偶尔仍会发现小问题。如果您遇到似乎无法正确处理的 PE 文件,请务必报告!这将帮助使 pefile 更加强大。 ## 依赖项 _pefile_ 是自包含的。该模块没有依赖项;它与字节序无关;并且可在 Windows、macOS 和 Linux 上运行。 ## 使用 _pefile_ 的项目和产品 * Didier Stevens 的 [pecheck](https://blog.didierstevens.com/2018/06/12/update-pecheck-py-version-0-7-3/),一个用于显示 PE 文件信息的工具,对 PEiD 文件的支持比 _pefile_ 更好。 * [MAEC](https://maecproject.github.io/),一种用于编码和传达基于行为、工件和攻击模式等属性的高保真恶意软件信息的标准化语言。MAEC [转换](https://github.com/MAECProject/pefile-to-maec) _pefile_ 的输出为其 XML 格式。 * [Qiew](https://github.com/mtivadar/qiew),一个十六进制/文件格式查看器。 * [VirusTotal](https://www.virustotal.com/) * [bbfreeze](https://pypi.org/project/bbfreeze/) * **pyemu**: [下载](https://www.openrce.org/repositories/browse/codypierce),[GitHub](https://github.com/codypierce/pyemu),[白皮书](https://www.blackhat.com/presentations/bh-usa-07/Pierce/Whitepaper/bh-usa-07-pierce-WP.pdf) * [Immunity Debugger 1.1](https://www.openrce.org/blog/view/882/Immunity_Debugger_v1.1_Release) * [Cuckoo](https://github.com/cuckoosandbox/cuckoo) * [CAPE](https://github.com/kevoreilly/CAPEv2) * [MultiScanner](https://github.com/mitre/multiscanner) * [PE Tree](https://github.com/blackberry/pe_tree) * [icoextract](https://github.com/jlu5/icoextract) ## 额外资源 展示 PE 文件格式的海报 PDF: * [Portable Executable Format Layout](https://drive.google.com/file/d/0B3_wGJkuWLytbnIxY1J5WUs4MEk/view?usp=sharing&resourcekey=0-n5zZ2UW39xVTH8ZSu6C2aQ) 显示了 PE 格式定义的头部和结构的完整视图。 * [Portable Executable Header Walkthrough](https://drive.google.com/file/d/0B3_wGJkuWLytQmc2di0wajB1Xzg/view?resourcekey=0-coPypA_IwxaOCPwl1_4u2g) 显示了带有 PE 格式字段布局的可执行文件的原始视图。 以下链接提供了关于 PE 格式及其结构的详细信息。 * [corkami 的 PE 格式维基页面](https://web.archive.org/web/20150821170441/https://code.google.com/p/corkami/wiki/PE)(归档)已发展成为关于 PE 格式最深入的仓库之一。 * [corkami 的 PE 怪异宝藏](https://github.com/corkami/pocs/tree/master/PE) * [深入探究 Win32 可移植可执行文件格式](https://learn.microsoft.com/en-us/archive/msdn-magazine/2002/february/inside-windows-win32-portable-executable-file-format-in-detail) * [深入探究 Win32 可移植可执行文件格式,第 2 部分](https://learn.microsoft.com/en-us/archive/msdn-magazine/2002/march/inside-windows-an-in-depth-look-into-the-win32-portable-executable-file-format-part-2) * [Portable Executable File Format](https://web.archive.org/web/20220804184037/http://www.csn.ul.ie/~caolan/publink/winresdump/winresdump/doc/pefile.html)(归档) * [从 Exe 或 DLL 获取图标](https://web.archive.org/web/20250819092225/https://www.codeproject.com/Articles/9303/Get-icons-from-Exe-or-DLL-the-PE-way)(归档) * Solar Eclipse 的 Tiny PE 页面(网址:http://www.phreedom.org/solar/code/tinype/)已不再可用([仅 HTML 归档](https://web.archive.org/web/20111001045025/http://www.phreedom.org/solar/code/tinype/)),corkami 的 TinyPE 可在此处获取[仅代码](https://github.com/corkami/pocs/blob/master/PE/tiny.asm)
标签:DAST, pefile, PE文件, PE结构, Portable Executable, Python, Python模块, Windows可执行文件, 二进制分析, 二进制发布, 云安全监控, 云安全运维, 云资产清单, 区段分析, 可执行文件解析, 字符串提取, 开源工具, 恶意软件分析, 文件头解析, 文件格式解析, 无后门, 漏洞分析, 网络安全, 自动化分析, 资源提取, 跨站脚本, 路径探测, 逆向工具, 逆向工程, 隐私保护, 静态分析