machofile is a module to parse Mach-O binary files
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:
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:
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):
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
pip install machofileIt expects to be supplied with either a file path or a data buffer to parse.
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:
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.
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:
…When working with Universal (FAT) binaries, you can extract each architecture slice into its own standalone Mach-O file using --dump-dir:
# 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.arm64Each dumped file is a valid standalone Mach-O binary. The output directory is created automatically if it does not exist. Output files are named ..
machofile supports JSON output for programmatic consumption of the parsed data. The JSON output comes in two formats:
The default JSON output provides human-readable values with proper formatting applied:
…For applications that need to process raw numeric values, use the --raw flag:
% 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"
]
}-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.
Development of machofile is sponsored by RationalEdge.
Those are the people that I would like to thank for being the inspiration that led me to write this module:
No open issues yet, or sync has not completed.