import struct, sys
sys.stdout.reconfigure(encoding='utf-8')
sys.path.insert(0, r'E:\ios\work319')

DRV = r'E:\ios\work319\patch_src\Payload\Driver.app\Driver'
d = open(DRV, 'rb').read()
print('file size', len(d), hex(len(d)))
magic = struct.unpack_from('<I', d, 0)[0]
print('magic', hex(magic))

# parse load commands
ncmds = struct.unpack_from('<I', d, 16)[0]
off = 32
segs = []
symtab = None
for _ in range(ncmds):
    cmd, sz = struct.unpack_from('<II', d, off)
    if cmd == 0x19:  # LC_SEGMENT_64
        name = d[off+8:off+24].split(b'\0')[0].decode()
        vmaddr, vmsize, fileoff, filesize = struct.unpack_from('<QQQQ', d, off+24)
        segs.append((name, vmaddr, vmsize, fileoff, filesize))
    if cmd == 0x2:
        symtab = struct.unpack_from('<IIII', d, off+8)
    off += sz

for s in segs:
    print('%-20s vmaddr=%#x vmsize=%#x fileoff=%#x filesize=%#x' % s)

def va_to_off(va):
    for name, vmaddr, vmsize, fileoff, filesize in segs:
        if vmaddr <= va < vmaddr + vmsize:
            return fileoff + (va - vmaddr)
    return None

print()
print('=== pointer slots ===')
for va in (0x101FD33F0, 0x101B24520, 0x101FB6330, 0x101DCEBA0, 0x101ED0C28):
    o = va_to_off(va)
    print('%#x -> fileoff %s' % (va, hex(o) if o is not None else None))
    if o is not None and 0 <= o <= len(d) - 8:
        v = struct.unpack_from('<Q', d, o)[0]
        print('    u64 = %#x' % v)
        o2 = va_to_off(v)
        if o2 is not None:
            e = d.find(b'\0', o2)
            print('    target cstr: %r' % d[o2:e].decode('latin1', 'replace'))
        else:
            # maybe v is a file offset or unslid pointer
            pass

print()
print('=== symbols matching patterns ===')
if symtab:
    so, ns, st, ss = symtab
    pats = ['abort', 'swift_', 'malloc', 'free', 'fatal', 'assert', 'raise', 'exit', 'getTypeByMangled', 'retain', 'release']
    hits = []
    for k in range(ns):
        strx, typ, sect, desc, val = struct.unpack_from('<IBBHQ', d, so + k * 16)
        e = d.find(b'\0', st + strx)
        nm = d[st + strx:e].decode('latin1', 'replace')
        if any(p in nm for p in pats):
            hits.append((val, nm, typ))
    for val, nm, typ in sorted(hits)[:200]:
        print('  %#x %s (typ %#x)' % (val, nm, typ))
    print('  total hits', len(hits))

print()
print('=== cstring hits ===')
for pat in (b'fatal', b'Fatal', b'assert', b'Assert', b'repack', b'Repack', b'bundle', b'Bundle', b'developer', b'Developer', b'jailbreak', b'Jailbreak', b'signature', b'Signature', b'entitlement', b'team', b'Team', b'configuration', b'Configuration', b'SceneDelegate', b'AppDelegate'):
    idx = []
    start = 0
    while True:
        i = d.find(pat, start)
        if i < 0:
            break
        idx.append(i)
        start = i + 1
        if len(idx) >= 8:
            break
    if idx:
        print('%s: %d hits' % (pat.decode(), len(idx)))
        for i in idx:
            # find string start/end
            s = d.rfind(b'\0', max(0, i-80), i) + 1
            e = d.find(b'\0', i)
            if e - s < 200:
                print('   %#x %r' % (i, d[s:e].decode('latin1', 'replace')))
