Skip to content

Instantly share code, notes, and snippets.

@scr34m
Created March 29, 2024 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scr34m/5f3bfc80cf9bef62a2f23cb0ecd6c489 to your computer and use it in GitHub Desktop.
Save scr34m/5f3bfc80cf9bef62a2f23cb0ecd6c489 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/python
import sys
import struct
if len(sys.argv)-1 < 1:
print('usage: {} <binary>'.format(sys.argv[0]))
sys.exit(1)
f = open(sys.argv[1], 'rb')
data = f.read()
f.close()
offset = 0
# FAT header
h = struct.unpack_from('<LL', data, offset)
offset += 8
if h[0] != 0xcafebabe:
print('FAT_MAGIC is wrong')
sys.exit(1)
print('Number of structs: {}'.format(h[1]))
for x in range(0, h[1]):
s = struct.unpack_from('<LLLLL', data, offset)
offset += 20
print('CPU type 0x{:x} and sub type 0x{:x}'.format(s[0], s[1]))
print('Offset {} and size {}'.format(s[2], s[3]))
if (s[0] & 0x1000007 == 0x1000007): # CPU_TYPE_X86_64
suffix = 'x64'
elif (s[0] & 0x100000C == 0x100000C): # CPU_TYPE_ARM64
suffix = 'arm64'
else:
suffix = str(s[0])
begining = s[2]
end = s[2] + s[3]
f = open(sys.argv[1] + '_' + suffix, "wb")
f.write(data[begining:end])
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment