百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
M

machofile

> 编程语言
开源

machofile 是一个用于解析 Mach-O 二进制文件的模块

99 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

machofile 是一个用于解析 Mach-O 二进制文件的模块

machofile

machofile is a module to parse Mach-O binary files, with a focus on malware analysis and reverse engineering.

Inspired by Ero Carrera's pefile, this module aims to provide similar capabilities but for Mach-O binaries instead. Reference material and documentation used to gain the file format knowledge, the basic structures and constant are taken from the resources listed below.

machofile is self-contained. The module has no dependencies; it is endianness independent; and it works on macOS, Windows, and Linux.

While there are other Mach-o parsing modules out there, the motivations behind developing this one are:

  • first and foremost, for me this was a great way to deep dive and learn more about the Mach-O format and structures
  • to provide a simple way to parse Mach-O files for analysis
  • to not depend on external modules (e.g. lief, macholib, macho, etc.), since everything is directly extracted from the file and is all in pure python.

Let me know if you try it or find bugs but also... be gentle ;) code will be optimized and more features will be added.

Current Features:

  • Parse Mach-O Header
  • Parse Load Commands
  • Parse File Segments
  • Parse Dylib Commands
  • Parse Dylib List
  • Extract imported function
  • Extract Exported Symbols
  • Hashes: dylib hash, import hash, export hash, entitlement hash, symhash
  • Segment entropy calculation
  • Extract Entry point
  • Extract UUID
  • Extract Version Information
  • Parse basic Code Signature information
  • Support for FAT (Universal) Binaries
  • Dump individual Mach-O slices from FAT (Universal) Binaries
  • JSON output support (both human-readable and raw formats)

Note: as of now, this has initially been tested against x86, x86_64, arm64, and arm64e Mach-O samples.

Next features to be implemented (in random order):

  • Embedded strings
  • File Attributes
  • flag for suspicious libraries
  • Packer detection
  • ...

Usage and examples

You can either use it from command line or import it as a module in your python code, and call each function individually to parse only the structures you are interested in. You can install it directly via pip and use it programmatically or from command line, or use it as standalone script

bash
pip install machofile

Module version

It expects to be supplied with either a file path or a data buffer to parse.

python
import machofile
macho = machofile.UniversalMachO(file_path='/path/to/machobinary')
macho.parse()

If the data buffer is already available, it can be supplied directly with:

python
import machofile
with open(file_path, 'rb') as f:
    data = f.read()
macho = machofile.UniversalMachO(data=data)
macho.parse()

For detailed usage of the API, check the dedicated API documentation page.

Command Line version

You can use machofile also directly as a CLI tool if you installed it via pip, or as standalone tool as python3 machofile.py. All the same features are available as module, as well as command line tool.

…

Example output:

…

Dumping Slices from Universal (FAT) Binaries

When working with Universal (FAT) binaries, you can extract each architecture slice into its own standalone Mach-O file using --dump-dir:

bash
# Dump all slices
% machofile -f universal_binary --dump-dir ./output
Dumped x86_64 -> ./output/universal_binary.x86_64
Dumped arm64 -> ./output/universal_binary.arm64

# Dump only a specific architecture (combine with --arch)
% machofile -f universal_binary --dump-dir ./output --arch arm64
Dumped arm64 -> ./output/universal_binary.arm64

Each dumped file is a valid standalone Mach-O binary. The output directory is created automatically if it does not exist. Output files are named ..

JSON Output

machofile supports JSON output for programmatic consumption of the parsed data. The JSON output comes in two formats:

Human-Readable JSON (Default)

The default JSON output provides human-readable values with proper formatting applied:

…

Raw JSON Output

For applications that need to process raw numeric values, use the --raw flag:

bash
% python3 machofile.py -j --raw -hdr -f dec750b9d596b14aeab1ed6f6d6d370022443ceceb127e7d2468b903c2d9477a
{
  "header": {
    "x86_64": {
      "magic": 4277009103,
      "cputype": 16777223,
      "cpusubtype": 3,
      "filetype": 2,
      "ncmds": 41,
      "sizeofcmds": 5024,
      "flags": 2162821
    },
    "arm64": {
      "magic": 4277009103,
      "cputype": 16777228,
      "cpusubtype": 0,
      "filetype": 2,
      "ncmds": 41,
      "sizeofcmds": 5104,
      "flags": 2162821
    }
  },
  "architectures": [
    "x86_64",
    "arm64"
  ]
}

JSON Output Options

  • -j, --json: Output data in JSON format (human-readable by default)
  • --raw: Output raw numeric values instead of formatted strings (must be used with -j)

JSON output supports all the same analysis options as the standard output (-a, -hd, -l, -sg, etc.) and works with both single-architecture and Universal (FAT) binaries.

Sponsored by

Development of machofile is sponsored by RationalEdge.

Credits

Those are the people that I would like to thank for being the inspiration that led me to write this module:

  • Ero Carrera (@erocarrera) for writing and maintaining the pefile module
  • Patrick Wardle (@patrickwardle) for the great work in sharing his macOS malware analysis and research, and bringing to life OBTS :)
  • Greg Lesnewich (@greg-l.bsky.social) and Jacob Latonis (@jacoblatonis.me) for their work on mach-o similarity and the continuous, nerding and enlightening brainstormings sessions on Mach-O binary format. Check you their OBTS v7 presentation on YT.

Reference/Documentation links:

  • https://opensource.apple.com/source/xnu/xnu-2050.18.24/EXTERNAL_HEADERS/mach-o/loader.h
  • https://github.com/apple-oss-distributions/lldb/blob/10de1840defe0dff10b42b9c56971dbc17c1f18c/llvm/include/llvm/Support/MachO.h
  • https://github.com/apple-oss-distributions/dyld/tree/main
  • https://iphonedev.wiki/Mach-O_File_Format
  • https://lowlevelbits.org/parsing-mach-o-files/
  • https://github.com/aidansteele/osx-abi-macho-file-format-reference
  • https://lief-project.github.io/doc/latest/tutorials/11_macho_modification.html
  • https://github.com/VirusTotal/yara/blob/master/libyara/include/yara/macho.h
  • https://github.com/corkami/pics/blob/master/binary/README.md
  • https://github.com/qyang-nj/llios/tree/main
  • https://github.com/threatstream/symhash

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Pythonmach-omacosmalware-analysismalware-research

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言