Skip to content

Instantly share code, notes, and snippets.

@Xummer
Last active January 31, 2019 03:23
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 Xummer/088770eb01cbcc30316bd0eef8fbf986 to your computer and use it in GitHub Desktop.
Save Xummer/088770eb01cbcc30316bd0eef8fbf986 to your computer and use it in GitHub Desktop.
Check mach-o is 32/64 bit
# -*- coding: utf-8 -*-
import os,sys
def checkMachO(path):
# print path
is_path_exists = os.path.exists(path)
if is_path_exists:
with open(path, 'rb') as f:
headMagic = ""
for i in range(0, 4):
s = f.read(1)
byte = ord(s)
headMagic = headMagic + hex(byte)[2:]
# print hex(byte)
pass
# fat:0xbebafeca 32: 0xfeedface 64: 0xfeedfacf
# print headMagic
if headMagic == "cafebabe":
# Is Fat Mach-o
# Jump 3
f.read(3)
t = f.read(1)
archiveCount = ord(t)
# Jump 8
f.read(8)
# print archiveCount
arrOffsets = []
offsetDelta = 0
# for i in range(0, archiveCount):
while 1:
offset = 0
for j in range(0, 4):
r = f.read(1)
# print ord(r)
offset = offset + ord(r) * (256 ** (3-j))
if offset == 0:
offsetDelta = 4
break;
arrOffsets.append(offset)
# print offset
# Jump 16
f.read(16)
pass
# currentOffset = 0x10 + archiveCount * 20
currentOffset = 0x10 + len(arrOffsets) * 20 + offsetDelta
msg = ""
is_32bit = 0
is_64bit = 0
# print arrOffsets
for of in arrOffsets:
delta = of - currentOffset
f.read(delta)
magic = ""
for i in range(0, 4):
s = f.read(1)
byte = ord(s)
magic = magic + hex(byte)[2:]
pass
# ap = ""
# if len(msg) > 0:
# ap = "&"
# pass
# print magic
if is_64bit == 0 and magic == "cffaedfe":
is_64bit = 1
# msg = msg + ap + "arm64"
# print "arm64"
else:
if is_32bit == 0 and magic == "cefaedfe":
is_32bit = 1
# msg = msg + ap + "arm32"
# print "arm32"
currentOffset = of + 4
if is_32bit == 1 and is_64bit == 1:
print "arm32&arm64"
else:
if is_32bit == 1:
print "arm32"
else:
if is_64bit == 1:
print "arm64"
pass
pass
# print msg
else:
if headMagic == "cffaedfe":
print "arm64"
pass
else:
if headMagic == "cefaedfe":
print "arm32"
pass
pass
pass
pass
if __name__ == '__main__':
checkMachO(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment