-
Notifications
You must be signed in to change notification settings - Fork 2
/
monkey_traffic_old.py
73 lines (64 loc) · 2.3 KB
/
monkey_traffic_old.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
#!/usr/bin/env python2
#-*-encoding:utf-8-*-
import os
#from subprocess import check_output
import subprocess
import time
os.system('rm -r -f monkeylog')
list = os.listdir('.') # list files at current dir
series = '0123456789ABCDEF'
ISOTIMEFORMAT='%m%d-%H-%M-%S'
# 获取设备中的所有包名
def appName():
cmd = 'adb -s ' + series + ' shell pm list packages'
p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, shell = True)
#p = check_output(cmd, shell = True)
p.wait()
output=p.stdout.readlines()
output=set(x.split(':')[1].strip() for x in output)
return output
# open tcpdump in another terminal
def capture_pcap(flag):
if flag == True:
cmd = 'adb -s ' + series + ' shell /data/local/tcpdump -p -vv -s 0 -w /sdcard/' + x + current_time +'.pcap'
print cmd
pcap_process = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, shell = True)
else:
# pcap_process.terminate()
cmd = 'adb -s ' + series + ' shell ps | grep tcpdump | awk \'{print $2}\' | xargs adb shell kill'
print cmd
pcap_process = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, shell = True)
# pcap_process.terminate()
os.system('mkdir monkeylog')
for fline in list:
if fline == 'monkeylog' or fline == 'monkey_traffic.py':
continue
current_time = time.strftime( ISOTIMEFORMAT, time.localtime() )
# 获取安装应用前的所有包名
before = appName()
os.system('adb -s ' + series +' install ' + fline)
# 获取安装应用后的所有包名
after = appName()
# 集合运算,取差集
applist = after - before
if len(applist) != 1:
print fline
print applist
print 'error! not a single app selected!'
# break
continue
time.sleep(180) # to seperate traffic of diff app in a single pcap
for x in applist:
capture_pcap(True)
os.system('adb -s ' + series +' shell monkey -p ' + x + ' -vvv --throttle 5000 --ignore-crashes 480 > ./monkeylog/' + x + current_time+ '.txt')
print 'start monkey'
os.system('adb -s ' + series +' shell am force-stop ' + x)
print 'done monkey'
capture_pcap(False)
print 'done pcap'
os.system('adb -s ' + series +' uninstall ' + x)
print 'done uninstall'
os.system('cp ' + fline + ' ./monkeylog/' + x + current_time + '.apk') # rename the apk