#!/usr/bin/env python3
"""Which filesystem view does each root process have? Read a stock system file + jbroot paths."""
import frida, sys, time, json

sys.stdout.reconfigure(encoding='utf-8')

JS = r"""
function readHead(p) {
    try {
        const f = new File(p, 'r');
        const n = Math.min(f.size, 48);
        const b = f.readBytes(n);
        f.close();
        return {ok: true, size: n, head: b.toString()};
    } catch (e) {
        return {ok: false, err: String(e)};
    }
}
const probes = {};
const paths = [
    '/System/Library/CoreServices/SystemVersion.plist',
    '/bin/sh',
    '/usr/lib/libSystem.B.dylib',
    '/var/mobile/Media/fix/step.txt',
    '/var/containers/Bundle/Application/.jbroot-F65244869E171CF3/usr/bin/ls',
    '/var/jb/usr/bin/ls'
];
for (const p of paths) probes[p] = readHead(p);
send({probes: probes});
"""

dev = frida.get_device_manager().add_remote_device('127.0.0.1:7777')
procs = {p.name: p.pid for p in dev.enumerate_processes()}
for target in ['sysdiagd', 'launchd', 'logd', 'securityd']:
    if target not in procs:
        print(target, 'absent')
        continue
    try:
        session = dev.attach(procs[target])
        script = session.create_script(JS)
        script.on('message', lambda m, d: print(target, '->', json.dumps(m.get('payload'))[:900]))
        script.load()
        time.sleep(1.5)
        session.detach()
    except Exception as e:
        print(target, 'ERR', type(e).__name__, str(e)[:80])
