#!/usr/bin/env python3
"""极简Frida测试 - PID 16816"""
import frida, time, sys, os

OUT = "/tmp/frida_out.txt"
f = open(OUT, "w")

def log(msg):
    f.write(msg + "\n")
    f.flush()
    print(msg)
    sys.stdout.flush()

try:
    log("[1] Connect to 192.168.0.12:27042")
    d = frida.get_device_manager().add_remote_device("192.168.0.12:27042")
    log(f"[2] Device: {d.name}")
    log("[3] Attach PID 16816")
    s = d.attach(16816)
    log("[4] Create script")
    sc = s.create_script('rpc.exports={ping:function(){return "pong";}};')
    sc.on("message", lambda m, d: None)
    log("[5] Loading...")
    sc.load()
    time.sleep(1)
    log("[6] Loaded! Calling exports...")
    e = sc.exports_sync
    methods = [x for x in dir(e) if not x.startswith("_")]
    log(f"[7] Methods: {methods}")
    log(f"[8] ping() = {e.ping()}")
    sc.unload()
    s.detach()
    log("[DONE]")
except Exception as ex:
    log(f"[ERROR] {ex}")
    import traceback
    traceback.print_exc(file=f)
finally:
    f.close()
