Skip to content
KitploitKITPLOIT
工具博客
提交
工具博客
提交

黑客、渗透测试和网络安全工具,武装您的安全武器库!

Kitploit 是一个黑客、网络安全和渗透测试工具的目录。发现最新的项目更新,查找漏洞、分析系统、自动化测试并加强你的安全。

··订阅源·联系·隐私·© 2026 Kitploit

工具目录

分类

查看所有分类
Loading categories
machofile — machofile is a module to parse Mach-O binary files | Kitploit
工具/GitHubGitHub/pstirparo/machofile
Static AnalysisReverse EngineeringForensicsMalware AnalysisBinary Analysis
GitHubpstirparo/machofile

machofile

machofile is a module to parse Mach-O binary files

查看仓库
9956个月前Kitploit 审核通过

最受欢迎

查看全部 →

发现我们社区最常用的工具。

探索所有工具

浏览我们的工具集合

查看所有工具 →
分享
网站

machofile

Downloads

machofile 是一个用于解析 Mach-O 二进制文件的模块,专注于恶意软件分析和逆向工程。

受 Ero Carrera 的 pefile 启发,本模块旨在为 Mach-O 二进制文件提供类似的功能。 用于获取文件格式知识、基本结构和常量的参考资料与文档均来自下面列出的资源。

machofile 是自包含的。该模块没有依赖;它与字节序无关;并且可在 macOS、Windows 和 Linux 上运行。

虽然市面上已有其他 Mach-O 解析模块,但开发本模块的动机是:

  • 首先也是最重要的,对我而言这是深入了解 Mach-O 格式和结构的绝佳方式
  • 提供一种简单的方法来解析 Mach-O 文件以供分析
  • 不依赖外部模块(例如 lief、macholib、macho 等),因为所有内容都直接从文件中提取,并且全部使用纯 Python 实现。

如果你尝试使用或发现了 bug,请告诉我,不过也请...温柔一点 ;) 代码将得到优化,并且会添加更多功能。

当前功能:

  • 解析 Mach-O 头
  • 解析加载命令
  • 解析文件段
  • 解析 Dylib 命令
  • 解析 Dylib 列表
  • 提取导入函数
  • 提取导出符号
  • 哈希:dylib 哈希、导入哈希、导出哈希、授权哈希、symhash
  • 段熵计算
  • 提取入口点
  • 提取 UUID
  • 提取版本信息
  • 解析基本的代码签名信息
  • 支持 FAT(通用)二进制文件
  • 从 FAT(通用)二进制文件转储单个 Mach-O 切片
  • 支持 JSON 输出(人类可读格式和原始格式)

注意:截至目前,该项目已初步针对 x86、x86_64、arm64 和 arm64e Mach-O 样本进行了测试。

接下来要实现的功能(顺序不分先后):

  • 嵌入式字符串
  • 文件属性
  • 可疑库标记
  • 加壳检测
  • ...

用法和示例

你可以从命令行使用它,也可以将其作为模块导入到你的 Python 代码中,单独调用每个函数,只解析你感兴趣的结构。你可以直接通过 pip 安装它,以编程方式或从命令行使用它,也可以将其作为独立脚本使用。

root@kitploit:~
pip install machofile

模块版本

它需要传入文件路径或数据缓冲区来进行解析。

root@kitploit:~
import machofile
macho = machofile.UniversalMachO(file_path='/path/to/machobinary')
macho.parse()

如果数据缓冲区已经可用,则可以直接提供:

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

有关 API 的详细用法,请查看专门的 API 文档页面。

命令行版本

如果你通过 pip 安装了 machofile,也可以直接将其用作 CLI 工具,或者作为独立工具运行 python3 machofile.py。作为模块和命令行工具,所有功能都同样可用。

root@kitploit:~
% machofile -h
usage: machofile [-h] -f FILE [-j] [--raw] [-a] [-d] [-e] [-ep] [-g]
                    [-hdr] [-i] [-l] [-seg] [-sig] [-sim] [-u] [-v]
                    [--arch ARCH] [--dump-dir DUMP_DIR]

Parse Mach-O binary structures. (version 2026.02.04)

options:
  -h, --help          show this help message and exit

required arguments:
  -f, --file FILE     Path to the file to be parsed

output format options:
  -j, --json          Output data in JSON format
  --raw               Output raw values in JSON format (use with -j/--json)

data extraction options:
  -a, --all           Print all info about the file
  -d, --dylib         Print Dylib Command Table and Dylib list
  -e, --exports       Print exported symbols
  -ep, --entry-point  Print entry point information
  -g, --general_info  Print general info about the file
  -hdr, --header      Print Mach-O header info
  -i, --imports       Print imported symbols
  -l, --load_cmd_t    Print Load Command Table and Command list
  -seg, --segments    Print File Segments info
  -sig, --signature   Print code signature and entitlements information
  -sim, --similarity  Print similarity hashes
  -u, --uuid          Print UUID
  -v, --version       Print version information

filter options:
  --arch ARCH         Show info for specific architecture only (for Universal binaries)

dump options:
  --dump-dir DUMP_DIR Dump individual Mach-O slices from a FAT/Universal binary
                      to the specified directory

示例输出:

root@kitploit:~
% machofile -a -f b4f68a58658ceceb368520dafc35b270272ac27b8890d5b3ff0b968170471e2b

[General File Info]
        Filename:         b4f68a58658ceceb368520dafc35b270272ac27b8890d5b3ff0b968170471e2b
        Filesize:         54240
        MD5:              20ffe440e4f557b9e03855b5da2b3c9c
        SHA1:             1bf61ecad8568a774f9fba726a254a9603d09f33
        SHA256:           b4f68a58658ceceb368520dafc35b270272ac27b8890d5b3ff0b968170471e2b

[Mach-O Header]
        magic:            MH_MAGIC (32-bit), 0xFEEDFACE
        cputype:          Intel i386
        cpusubtype:       X86_ALL
        filetype:         EXECUTE
        ncmds:            13
        sizeofcmds:       1180
        flags:            NOUNDEFS, DYLDLINK, TWOLEVEL

[Load Cmd table]
        {'cmd': 'LC_SEGMENT', 'cmdsize': 56}
        {'cmd': 'LC_SEGMENT', 'cmdsize': 192}
        {'cmd': 'LC_SEGMENT', 'cmdsize': 328}
        {'cmd': 'LC_SEGMENT', 'cmdsize': 192}
        {'cmd': 'LC_SEGMENT', 'cmdsize': 56}
        {'cmd': 'LC_SYMTAB', 'cmdsize': 24}
        {'cmd': 'LC_DYSYMTAB', 'cmdsize': 80}
        {'cmd': 'LC_LOAD_DYLINKER', 'cmdsize': 28}
        {'cmd': 'LC_UUID', 'cmdsize': 24}
        {'cmd': 'LC_UNIXTHREAD', 'cmdsize': 80}
        {'cmd': 'LC_LOAD_DYLIB', 'cmdsize': 52}
        {'cmd': 'LC_LOAD_DYLIB', 'cmdsize': 52}
        {'cmd': 'LC_CODE_SIGNATURE', 'cmdsize': 16}

[Load Commands]
        LC_CODE_SIGNATURE
        LC_DYSYMTAB
        LC_LOAD_DYLIB
        LC_LOAD_DYLINKER
        LC_SEGMENT
        LC_SYMTAB
        LC_UNIXTHREAD
        LC_UUID

[File Segments]
        SEGNAME    VADDR VSIZE OFFSET SIZE  MAX_VM_PROTECTION INITIAL_VM_PROTECTION NSECTS FLAGS ENTROPY            
        ------------------------------------------------------------------------------------------------------------
        __PAGEZERO 0     4096  0      0     0                 0                     0      0     0.0                
        __TEXT     4096  28672 0      28672 7                 5                     2      0     5.080680410706916  
        __DATA     32768 4096  28672  4096  7                 3                     4      0     0.1261649636134924 
        __IMPORT   36864 4096  32768  4096  7                 7                     2      0     0.21493796627555234
        __LINKEDIT 40960 20480 36864  17376 7                 1                     0      0     6.637864516225949  

[Dylib Commands]
        DYLIB_NAME_OFFSET DYLIB_TIMESTAMP DYLIB_CURRENT_VERSION DYLIB_COMPAT_VERSION DYLIB_NAME                   
        ----------------------------------------------------------------------------------------------------------
        24                2               65536                 65536                b'/usr/lib/libgcc_s.1.dylib' 
        24                2               7274759               65536                b'/usr/lib/libSystem.B.dylib'

[Dylib Names]
        b'/usr/lib/libgcc_s.1.dylib'
        b'/usr/lib/libSystem.B.dylib'

[UUID]
        d691c242-da49-1081-50d5-4f8991924b06

[Entry Point]
        type:             LC_UNIXTHREAD
        entry_address:    9200
        thread_data_size: 72

[Version Information]
        No version information found

[Code Signature]
        signed:           True
        signing_status:   Apple signed
        certificates_info:
            count:            3
            certificates:
              index:            0
              size:             4815
              subject:          Contains: Developer ID Certification Authority
              issuer:           Unable to parse
              is_apple_cert:    True
              type:             Developer ID Certification Authority

              index:            1
              size:             1215
              subject:          Contains: Apple Root CA
              issuer:           Unable to parse
              is_apple_cert:    True
              type:             Apple Root CA

              index:            2
              size:             1385
              subject:          Contains: Developer ID Application:
              issuer:           Unable to parse
              is_apple_cert:    False
              type:             Developer ID Application Certificate
        entitlements_info:
            count:            0
            entitlements:
        code_directory:
            version:          131328
            flags:            0
            hash_offset:      144
            identifier_offset:48
            special_slots:    3
            signing_flags:
                None
            code_slots:       11
            hash_size:        44640
            hash_type:        335609868
            hash_algorithm:   Unknown (335609868)
            identifier:       onmac.unspecified.installer

[Imported Libraries]
        /usr/lib/libgcc_s.1.dylib
        /usr/lib/libSystem.B.dylib

[Imported Functions]
        (Sources: CF=chained_fixups, BO=bind, WB=weak_bind, LB=lazy_bind, ST=symtab)
        /usr/lib/libSystem.B.dylib:
                __NSGetExecutablePath [ST]
                ___stderrp [ST]
                _dlerror [ST]
                _dlopen [ST]
                _dlsym [ST]
                _exit [ST]
                _fclose [ST]
                _fopen [ST]
                _fprintf [ST]
                _fputs$UNIX2003 [ST]
                _free [ST]
                _fwrite$UNIX2003 [ST]
                _getenv [ST]
                _getpid [ST]
                _getpwnam [ST]
                _lstat [ST]
                _mbstowcs [ST]
                _memcpy [ST]
                _memset [ST]
                _setenv$UNIX2003 [ST]
                _setlocale [ST]
                _snprintf [ST]
                _stat [ST]
                _strchr [ST]
                _strdup [ST]
                _strlen [ST]
                _unsetenv$UNIX2003 [ST]

[Exported Symbols]
        <unknown>:
                _NXArgc
                _NXArgv
                ___progname
                _environ
                _main
                start

[Similarity Hashes]
        dylib_hash:       0556bed5dc31bddaee73f3234b3c577b
        export_hash:      824e359e3d0ad7283d0982bd5da2e8fd
        import_hash:      0bae89995ad3900987c49c0bea1d17fe
        symhash:          15e6c1aeba01be1404901f7152213779

从通用(FAT)二进制文件转储切片

在使用通用(FAT)二进制文件时,你可以使用 --dump-dir 将每个架构切片提取为独立的 Mach-O 文件:

root@kitploit:~
# 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

每个转储的文件都是有效的独立 Mach-O 二进制文件。如果输出目录不存在,则会自动创建。输出文件的命名格式为 <original_filename>.<arch_name>。

JSON 输出

machofile 支持 JSON 输出,便于以编程方式使用解析后的数据。JSON 输出有两种格式:

人类可读 JSON(默认)

默认的 JSON 输出提供经过适当格式化的人类可读值:

root@kitploit:~
% python3 machofile.py -j -hdr -f dec750b9d596b14aeab1ed6f6d6d370022443ceceb127e7d2468b903c2d9477a 
{
  "header": {
    "x86_64": {
      "magic": "MH_MAGIC_64 (64-bit), 0xFEEDFACF",
      "cputype": "x86_64",
      "cpusubtype": "x86_ALL",
      "filetype": "EXECUTE",
      "ncmds": 41,
      "sizeofcmds": 5024,
      "flags": "NOUNDEFS, DYLDLINK, TWOLEVEL, BINDS_TO_WEAK, PIE"
    },
    "arm64": {
      "magic": "MH_MAGIC_64 (64-bit), 0xFEEDFACF",
      "cputype": "ARM 64-bit",
      "cpusubtype": "ARM_ALL",
      "filetype": "EXECUTE",
      "ncmds": 41,
      "sizeofcmds": 5104,
      "flags": "NOUNDEFS, DYLDLINK, TWOLEVEL, BINDS_TO_WEAK, PIE"
    }
  },
  "architectures": [
    "x86_64",
    "arm64"
  ]
}

原始 JSON 输出

对于需要处理原始数值的应用程序,请使用 --raw 标志:

root@kitploit:~
% 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 输出选项

  • -j, --json:以 JSON 格式输出数据(默认为人类可读格式)
  • --raw:输出原始数值而不是格式化字符串(必须与 -j 一起使用)

JSON 输出支持与标准输出(-a、-hd、-l、-sg 等)相同的所有分析选项,并且适用于单架构和通用(FAT)二进制文件。

赞助方

RationalEdge

machofile 的开发由 RationalEdge 赞助。

致谢

以下是我要感谢的人,正是他们的启发促使我编写了这个模块:

  • Ero Carrera(@erocarrera),感谢他编写并维护了 pefile 模块
  • Patrick Wardle(@patrickwardle),感谢他在分享 macOS 恶意软件分析和研究方面的出色工作,以及创办了 OBTS :)
  • Greg Lesnewich(@greg-l.bsky.social)和 Jacob Latonis(@jacoblatonis.me),感谢他们在 Mach-O 相似性方面的工作,以及围绕 Mach-O 二进制格式进行的持续、深入且富有启发性的头脑风暴讨论。可以去看看他们在 YT 上的 OBTS v7 演讲。

参考/文档链接:

  • 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
下载工具