-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunSentinel.py
More file actions
271 lines (253 loc) · 9.16 KB
/
runSentinel.py
File metadata and controls
271 lines (253 loc) · 9.16 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import os, sys
import json, subprocess, glob
import tempfile, shutil
import threading
GLOBAL_DECODE_LOCK = threading.Lock()
class GlobalConfigs:
def __init__(self):
self.APK_NAME=""
self.GATOR_ROOT=""
self.ADK_ROOT=""
self.APKTOOL_PATH=""
self.GATOR_OPTIONS=["-client", "SensorRegistrationAnalysisClient", "-clientParam", "WTPK4", "-clientParam", "testGen"]
self.KEEP_DECODE=False
def fatalError(str):
print(str)
sys.exit(1)
pass
def extractLibsFromPath(pathName):
if not pathExists(pathName):
return ""
pass
fileList = glob.glob(pathName+"/*.jar")
if len(fileList) == 0:
return ""
ret = ""
for item in fileList:
ret += ':' + item
return ret;
def pathExists(pathName):
if os.access(pathName, os.F_OK):
return True
return False
pass
def invokeGatorOnAPK(\
apkPath,
resPath,
manifestPath,
apiLevel,
sdkLocation,
benchmarkName,
options,
configs,
output = None,
timeout = 0
):
''''''
SootAndroidLocation = configs.GATOR_ROOT + "/SootAndroid"
bGoogleAPI = False
if len(apiLevel) > 6:
if apiLevel[:6] == "google":
bGoogleAPI = True
sLevelNum = apiLevel[apiLevel.find('-') + 1:]
try:
iLevelNum = int(sLevelNum)
except:
fatalError("FATALERROR: API Level not valid")
if (bGoogleAPI):
GoogleAPIDir = sdkLocation + \
"/add-ons/addon-google_apis-google-" + sLevelNum
if not pathExists(GoogleAPIDir) :
print("Google API Level:" + sLevelNum + "Not installed!")
sys.exit(-1);
GoogleAPI = "{0}/libs/maps.jar:{0}/libs/usb.jar:{0}/libs/effects.jar".format(GoogleAPIDir)
PlatformAPIDir = sdkLocation + \
"/platforms/" + "android-" + str(iLevelNum)
ClassPathJar = extractLibsFromPath("{0}/lib".format(SootAndroidLocation));
ClassPathJar = ":{0}/bin".format(SootAndroidLocation) + ClassPathJar
PlatformJar = "{0}/platforms/android-".format(sdkLocation) + sLevelNum +"/android.jar"
PlatformJar+=":" + "{0}/deps/android-support-annotations.jar:{0}/deps/android-support-v4.jar:{0}/deps/android-support-v7-appcompat.jar:{0}/deps/android-support-v7-cardview.jar:{0}/deps/android-support-v7-gridlayout.jar:{0}/deps/android-support-v7-mediarouter.jar:{0}/deps/android-support-v7-palette.jar:{0}/deps/android-support-v7-preference.jar::{0}/deps/android-support-v7-recyclerview.jar".format(SootAndroidLocation)
if iLevelNum >= 23:
#include the apache library
apacheLib = "{0}/platforms/android-".format(sdkLocation) + sLevelNum +"/optional/org.apache.http.legacy.jar"
if pathExists(apacheLib):
PlatformJar += ":" + apacheLib
#Finished computing platform libraries
callList = [\
'java', \
'-Xmx12G', \
'-classpath', ClassPathJar, \
'presto.android.Main', \
'-project', apkPath,\
'-android', PlatformJar,\
'-sdkDir', sdkLocation,\
'-classFiles', apkPath, \
'-resourcePath', resPath, \
'-manifestFile', manifestPath,\
'-apiLevel', "android-" + sLevelNum,\
'-benchmarkName', benchmarkName,\
'-guiAnalysis',
'-listenerSpecFile', SootAndroidLocation + "/listeners.xml",
'-wtgSpecFile', SootAndroidLocation + '/wtg.xml']
callList.extend(options);
#print(callList)
if timeout == 0:
return subprocess.call(callList, stdout = output, stderr = output)
else:
try:
retval = subprocess.call(callList, stdout = output, stderr = output, timeout = timeout)
return retval
except subprocess.TimeoutExpired:
return -50
pass
def decodeAPK(apkPath, apktoolPath, decodeLocation, output = None):
global GLOBAL_DECODE_LOCK
GLOBAL_DECODE_LOCK.acquire()
callList = ['java',\
'-jar',\
apktoolPath,\
'd', apkPath,\
'-o', decodeLocation, \
'-f']
ret = subprocess.call(callList, stdout = output, stderr = None)
GLOBAL_DECODE_LOCK.release()
if ret != 0:
fatalError("APK Decode Failed!")
pass
def parseMainParam():
params = sys.argv
configs=GlobalConfigs()
determinGatorRootAndSDKPath(configs)
i = 0
while i < len(params) - 1:
i += 1
var = params[i]
if (var[-4:] == ".apk") and (configs.APK_NAME == ""):
configs.APK_NAME = var
continue
if var == "--keep-decoded-apk-dir":
configs.KEEP_DECODE = True
continue
configs.GATOR_OPTIONS.append(var)
pass
return configs
def determinAPILevel(dirName, configs):
targetLevel = 0;
if pathExists(dirName + "/apktool.yml"):
infoFile = open(dirName + "/apktool.yml", 'r')
lines = infoFile.readlines()
infoFile.close()
for i in range(len(lines)):
if "targetSdkVersion" in lines[i]:
targetLevel = extractAPILevelFromLine(lines[i])
elif "minSdkVersion" in lines[i]:
minLevel = extractAPILevelFromLine(lines[i])
if minLevel > targetLevel:
targetLevel = minLevel
pass
pass
pass
if (targetLevel != 0):
if pathExists(configs.ADK_ROOT + "platforms/android-" + str(targetLevel)):
return targetLevel
else:
return 23
else:
return 23
else:
return 23
def extractAPILevelFromLine(curLine):
i = curLine.find("'")
curLine = curLine[i+1:]
i = curLine.find("'")
curLine = curLine[:i]
numLevel = int(curLine)
return numLevel
def determinGatorRootAndSDKPath(configs):
print("Determine Root called")
gatorRoot = os.environ.get("GatorRoot")
if gatorRoot != None:
configs.GATOR_ROOT = gatorRoot
else:
configs.GATOR_ROOT = os.getcwd()
os.environ['GatorRoot'] = configs.GATOR_ROOT
adkRoot = os.environ.get("ADK")
if adkRoot == None:
adkRoot = os.environ.get("ANDROID_SDK")
if adkRoot != None:
configs.ADK_ROOT = adkRoot
else:
homeDir = os.environ.get("HOME")
if homeDir == None:
fatalError("ADK environment variable is not defined")
if sys.platform == "linux2":
if pathExists(homeDir + "/Android/Sdk"):
configs.ADK_ROOT = homeDir+"/Android/Sdk"
else:
fatalError("ADK environment variable is not defined")
elif sys.platform == "darwin":
if pathExists(homeDir + "/Library/Android/sdk"):
configs.ADK_ROOT = homeDir + "/Library/Android/sdk"
else:
fatalError("ADK environment variable is not defined")
pass
pass
def getParentDir(pathName):
pathName = pathName.strip()
if pathName[-1] == '/':
pathName = pathName[:len(pathName) - 1]
index = 0
for i in reversed(range(len(pathName))):
if pathName[i] == '/':
index = i
break
pass
if index != 0:
return pathName[:index]
else:
fatalError("GatorRoot environment variable not defined")
pass
def runGatorOnAPKDirect(apkFileName, GatorOptions, keepdecodedDir, output = None, configs = None, timeout = 0):
if configs == None:
configs = GlobalConfigs()
if configs.GATOR_ROOT == "" or configs.ADK_ROOT == "":
determinGatorRootAndSDKPath(configs)
decodeDir = tempfile.mkdtemp()
if output == None:
print("Extract APK at: " + decodeDir)
else:
output.write("Extract APK at: " + decodeDir + "\n")
configs.KEEP_DECODE = keepdecodedDir
configs.APK_NAME = apkFileName
configs.GATOR_OPTIONS = GatorOptions
decodeAPK(configs.APK_NAME, configs.GATOR_ROOT + "/SootAndroid/apktool.jar", decodeDir, output = output)
numAPILevel = determinAPILevel(decodeDir, configs)
pathElements = apkFileName.split("/")
apkFileName = pathElements[-1]
manifestPath = decodeDir + "/AndroidManifest.xml"
resPath = decodeDir + "/res"
retval = invokeGatorOnAPK(\
apkPath = configs.APK_NAME,\
resPath = resPath, \
manifestPath = manifestPath,\
apiLevel = "android-{0}".format(numAPILevel), \
sdkLocation = configs.ADK_ROOT, \
benchmarkName = apkFileName,\
options = configs.GATOR_OPTIONS,
configs = configs,
output = output,
timeout = timeout)
if not configs.KEEP_DECODE:
shutil.rmtree(decodeDir)
if output == None:
print("Extracted APK resources removed!")
else:
output.write("Extracted APK resources removed!")
return retval
def main():
configs = parseMainParam();
return runGatorOnAPKDirect(configs.APK_NAME,\
configs.GATOR_OPTIONS,\
configs.KEEP_DECODE, configs = configs)
if __name__ == '__main__':
main()