-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor.py
83 lines (67 loc) · 1.96 KB
/
monitor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import frida
import os
import sys
import serial
import subprocess
from time import sleep
TIMEOUT = 200 # timeout in seconds / 10
RESET_COUNTER = 10 # restart app after X actions
if (len(sys.argv) < 3):
print("Use: python monitor.py [bundleId of app] [serial device]")
sys.exit(1)
tmp = os.popen("ps -Af").read()
proccount = tmp.count('iproxy')
if (proccount == 0):
print("Need to run iproxy (and have 'open' installed on phone)!")
sys.exit(1)
bundleId = sys.argv[1] # e.g. com.epson.ESCPR01
dev = sys.argv[2] # e.g. /dev/cu.usbserial-0001
def open_app():
cmd = 'ssh -T -p 2222 root@localhost "open ' + bundleId + '"'
subprocess.run(cmd, shell=True)
def restart_app():
cmd = 'frida-ps -U -a | grep ' + bundleId + ' | cut -d " " -f1'
res = subprocess.run(cmd, shell=True, capture_output=True)
pid = int(res.stdout.decode())
cmd = 'frida-kill -U ' + str(pid)
subprocess.run(cmd, shell=True)
open_app()
def kill_all():
cmd = 'frida-ps -U -a | tail -n +4 | cut -d " " -f1'
res = subprocess.run(cmd, shell=True, capture_output=True)
pids = res.stdout.decode().split('\n')
for pid in pids:
if pid and int(pid) != 0:
cmd = 'frida-kill -U ' + pid
subprocess.run(cmd, shell=True)
device = frida.get_usb_device()
s = serial.Serial(dev, baudrate=115200, timeout=0.050)
i = 0 # timeout counter
j = 0 # restart counter
while True:
i += 1
if i > TIMEOUT:
print("Timeout reached. Exiting..")
sys.exit(2)
running_app = device.get_frontmost_application()
if running_app is None or running_app.identifier != bundleId:
print("App not running. Killing other apps and opening it..")
kill_all()
open_app()
sleep(1)
continue
data = s.readall().decode()
if "Waiting for commands.." in data:
j +=1
if (j > RESET_COUNTER):
print("Restarting app")
restart_app()
sleep(2)
j = 0
print("Sent commands.. " + str(j))
s.write("g".encode())
i = 0 # reset timeout counter
else:
if i % 10 == 0:
print("Waiting for serial device.. " + str(i))
sleep(0.1)