#!/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 dumparr(p){try{if(p.isNull())return null;var len=p.add(0xc).readS32();if(len<0||len>4000)return {n:len};return {n:len,h:hexb(p.add(0x10),Math.min(len,120))};}catch(e){return {n:-2};}}
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});}}
 // WebSocket::send(this, Opcode, byte[]) and sendBytes(this, byte[])
 T(function(){Interceptor.attach(b.add(0x8f1b28),{onEnter:function(a){
   // scan a[1],a[2],a[3] for a byte[]
   for(var i=1;i<4;i++){var d=dumparr(a[i]);if(d&&d.n>0&&d.n<4000){send({g:"WS.send arg"+i+" ["+d.n+"] "+d.h});break;}}
 }});});
 T(function(){Interceptor.attach(b.add(0x8eef58),{onEnter:function(a){var d=dumparr(a[1]);if(d)send({g:"sendBytes ["+d.n+"] "+d.h});}});});
 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)
seen=set()
def om(m,d):
    p=m.get("payload",m)
    if isinstance(p,dict): print(p,flush=True)
sc.on("message",om); sc.load(); time.sleep(36)
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(10); print("=== done ===",flush=True)
