#!/usr/bin/env python3
import frida, time, subprocess
ADB="/opt/android-sdk/platform-tools/adb"
JS=r"""
function hexb(p,n){try{var u=new Uint8Array(p.readByteArray(n));return Array.prototype.map.call(u,function(x){return ('0'+x.toString(16)).slice(-2);}).join('');}catch(e){return "<e>";}}
function arr(p,mx){try{if(p.isNull())return null;var len=p.add(0xc).readS32();if(len<0||len>4000)return null;return {n:len,h:hexb(p.add(0x10),Math.min(len,mx||200))};}catch(e){return null;}}
function ins(){var m=Process.findModuleByName("libil2cpp.so");if(!m)return false;var b=m.base;
 function T(f){try{f();}catch(e){send({w:""+e});}}
 T(function(){var a=b.add(0x11f1032);var o=new NativeFunction(a,'int',['pointer','pointer','pointer']);Interceptor.replace(a,new NativeCallback(function(s,p,mi){if(s.isNull())return 0;try{return o(s,p,mi);}catch(e){return 0;}},'int',['pointer','pointer','pointer']));});
 // DH.DeriveSharedKey(this, sharedSecretBytes) -> keyBytes
 T(function(){Interceptor.attach(b.add(0xb576ca),{onEnter:function(a){var d=arr(a[1],96);send({g:"DeriveSharedKey shared="+(d?("["+d.n+"] "+d.h):"?")});},onLeave:function(r){var d=arr(r,64);send({g:"  -> derivedKey="+(d?("["+d.n+"] "+d.h):"?")});}});});
 // EnetPeer::SendDataEncrypted(this, byte[] plaintext, channel) -- plaintext op
 T(function(){Interceptor.attach(b.add(0x5f4a0c),{onEnter:function(a){var d=arr(a[1],200);send({g:"SendDataEncrypted PLAINTEXT="+(d?("["+d.n+"] "+d.h):"?")});}});});
 // Protocol16::SerializeOperationRequest(stream, opCode, params, bool)
 T(function(){Interceptor.attach(b.add(0x604264),{onEnter:function(a){try{send({g:"SerOpReq opCode="+a[2].toInt32()});}catch(e){}}});});
 send({g:"__ready"});return true;}
if(!ins()){var iv=setInterval(function(){if(ins())clearInterval(iv);},120);}
"""
subprocess.run([ADB,"shell","am","force-stop","se.foglo.svt"]); time.sleep(3)
subprocess.run([ADB,"shell","monkey","-p","se.foglo.svt","-c","android.intent.category.LAUNCHER","1"],stdout=-3,stderr=-3)
time.sleep(8)
dev=frida.get_usb_device(timeout=10)
pid=int(subprocess.run([ADB,"shell","pidof","se.foglo.svt"],capture_output=True,text=True).stdout.split()[0])
s=dev.attach(pid); sc=s.create_script(JS)
sc.on("message",lambda m,d: print(m.get("payload",m),flush=True)); sc.load(); time.sleep(34)
for c in [("690","730"),("690","730"),("1350","960"),("1350","960")]:
    subprocess.run([ADB,"shell","input","tap",c[0],c[1]]); time.sleep(4)
time.sleep(12); print("=== done ===",flush=True)
