-
Notifications
You must be signed in to change notification settings - Fork 44
/
super_adb
executable file
·138 lines (110 loc) · 3.76 KB
/
super_adb
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#! /usr/bin/env python
import subprocess
import tempfile
import urllib
import sys
packageName = None
packageDir = None
def echo(msg):
sys.stdout.write(msg)
sys.stdout.flush()
def call(cmd):
try:
return subprocess.check_output(
'adb -s %s %s %s' % (
call.device, "shell" if packageName is None else ("exec-out run-as " + packageName), cmd),
shell=True)
except Exception as e:
return e.output
def get_tmp_path():
return '/data/local/tmp' if packageName is None else packageDir + "/tmp"
def _get_devices():
try:
devices = subprocess.check_output('adb devices', shell=True)
except:
return None
devices = devices.strip().split('\n')
devices = devices[1:]
return [line.split()[0] for line in devices]
def select_device():
devices = _get_devices()
if not devices:
sys.exit('No devices found, Have you connect to any devices?')
length = len(devices)
if length > 1:
echo('multiple devices found:\n')
for index, d in enumerate(devices):
echo('%s. %s\n' % (index, d))
select = raw_input('Select (0~%s):\n' % (length - 1))
while (select.strip() not in [str(i) for i in range(length)]):
select = raw_input('Please enter the number 0~%s:\n' % (length - 1))
return devices[int(select)]
else:
return devices[0]
def detect_busybox():
tmp_path = get_tmp_path()
output = call('ls -al %s/busybox' % tmp_path)
if not output:
return False
output = output.lower()
if "no such file" in output:
return False
permission = output.split()[0]
if permission.endswith('x'):
return True
else:
# no permission
permission_ret = call('chmod +x %s/busybox' % tmp_path)
if not permission_ret:
return True
return False
def install():
tmp_path = get_tmp_path()
def report(count, size, total):
percent = int(count * size * 100 / total)
sys.stdout.write("\r %d%%" % percent + ' complete')
sys.stdout.flush()
binary = tempfile.NamedTemporaryFile()
echo("download busybox binary...\n")
urllib.urlretrieve('https://github.com/tiann/super-adb/raw/master/busybox-armv6l', binary.name, reporthook=report)
echo('install busybox...\n')
out = subprocess.check_output(
'adb -s %s push %s /data/local/tmp/busybox' % (
call.device, binary.name), shell=True)
call('mkdir tmp')
call('cp /data/local/tmp/busybox %s/busybox' % tmp_path)
if "error" in out.lower():
raise Error('push busybox failed.')
out = call('chmod 777 %s/busybox' % tmp_path)
echo('change permission : %s\n' % out)
out = call('%s/busybox --install -s %s/' % (tmp_path, tmp_path))
echo("install busybox : %s\n" % out)
def interact():
try:
pexpect = __import__('pexpect')
except ImportError:
raise Exception('Please install the pexpect module first!! \nYou can do it by "pip install pexpect"')
shell = pexpect.spawn('adb -s %s shell' % call.device)
shell.expect('$')
if packageName is not None:
shell.sendline('run-as %s' % packageName)
shell.sendline('export PATH=%s/:$PATH' % get_tmp_path())
shell.sendline('clear')
shell.interact()
def main():
call.device = select_device()
global packageDir
packageDir = call('pwd').strip()
if not detect_busybox():
echo("install busybox...\n")
install()
interact()
if __name__ == '__main__':
if len(sys.argv) == 1:
main()
elif len(sys.argv) == 3 and sys.argv[1] == "run-as":
packageName = sys.argv[2]
print("run-as " + sys.argv[2])
main()
else:
print("usage:\n super_adb\n or\n super_adb run-as packageName")