import json,sys,time,frida
A=json.load(open("/root/ants/sid_addr.json"))
JS=r"""
var GSID=%d;
var strnew=null;
function ins(){var m=Process.findModuleByName("libil2cpp.so");if(!m)return false;var b=m.base;
var e=m.findExportByName?m.findExportByName("il2cpp_string_new"):Module.getGlobalExportByName("il2cpp_string_new");
if(!e){send({t:"e",m:"no string_new"});return false;}
strnew=new NativeFunction(e,'pointer',['pointer']);
send({t:"info",m:"base "+b+" string_new "+e});
Interceptor.attach(b.add(GSID),{
  onLeave:function(rv){
    // if returned string is empty (length 0) or null, substitute
    var empty=true;
    try{ if(!rv.isNull()){var l=rv.add(8).readS32(); empty=(l<=0);} }catch(e){}
    if(empty){
      var buf=Memory.allocUtf8String("revived-session-0001");
      var s=strnew(buf);
      rv.replace(s);
      if(!this.logged){this.logged=true;send({t:"fix",m:"substituted sessionId"});}
    }
  }
});
return true;}
if(!ins()){var iv=setInterval(function(){if(ins())clearInterval(iv);},200);}
"""%A["get_sessionId"]
dev=frida.get_device_manager().add_remote_device("127.0.0.1:27042")
pid=dev.spawn(["se.foglo.svt"]);s=dev.attach(pid);sc=s.create_script(JS)
n=[0]
def om(m,d):
    if m["type"]!="send":print("!!",m);return
    p=m["payload"]
    if p["t"]=="fix":
        n[0]+=1
        if n[0]<=1: print("[FIX] %s"%p["m"])
    else:print("[%s]%s"%(p["t"],p.get("m")))
sc.on("message",om);sc.load();dev.resume(pid)
time.sleep(int(sys.argv[1]) if len(sys.argv)>1 else 30)
print("substitutions:",n[0])
