This repository has been archived by the owner on May 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathandrotools.py
executable file
·431 lines (372 loc) · 11.3 KB
/
androtools.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import sys
import zipfile
import shutil
import commands
import os
import hashlib
import re
import traceback
import json
from lib.dexparser import Dexparser
from lib.CreateReport import HTMLReport
from lib.Certification import CERTParser
import lib.dynamic as dynamic
dexList = [] #dexfile list
#program usage
def usage():
print "androtools : no file specified"
print "./androtools <APK_FILE_PATH> <HTML_OUTPUT_FILENAME>"
#program information
def about(apkfile):
print "Androtools - Android APK Static & Dynamic Analyzer"
print "Developed by Kim Namjun (Sejong University, Department of Information Security)"
print "Target APK Path : %s" %apkfile
#filehash extractor
def filehash(apkfile, mode):
if mode == "md5":
with open(apkfile, 'rb') as f:
m = hashlib.md5()
while True:
data = f.read()
if not data:
break
m.update(data)
return m.hexdigest()
elif mode == "sha1":
with open(apkfile, 'rb') as f:
m = hashlib.sha1()
while True:
data = f.read()
if not data:
break
m.update(data)
return m.hexdigest()
elif mode == "sha256":
with open(apkfile, 'rb') as f:
m = hashlib.sha256()
while True:
data = f.read()
if not data:
break
m.update(data)
return m.hexdigest()
else:
return ""
#delete temp file directory
def delTemp():
commands.getoutput("rm -rf temp")
#check target file that this is vaild apk file
def is_android(zfile):
for fname in zfile.namelist():
if "AndroidManifest.xml" in fname:
return True
elif "resources.arsc" in fname:
return True
else:
pass
return False
#logging error to error_log.txt
def logError(error_msg):
f = open('error_log.txt', 'a+')
f.write('[*] ' + error_msg + '\n')
f.close()
#extract dex file to temp file
def extractDEX(zfile):
global dexList
for fname in zfile.namelist():
if fname[-4:] == ".dex": #if file extension is dex
zfile.extract(fname, "temp")
dexpath = os.path.join("temp", fname)
dexhash = filehash(dexpath, "md5")
shutil.move(dexpath, os.path.join("temp", dexhash + ".dex"))
dexList.append(dexhash + ".dex")
#file resource searching
def fileResource(zfile):
print "[*] Extracting File Resource Data..."
extension = {'.apk' : 0, '.png' : 0, '.jpg' : 0, '.xml' : 0, '.mp3' : 0, '.txt' : 0, '.ini' : 0, '.so' : 0}
keylist = extension.keys()
soEnvironment = []
for fname in zfile.namelist():
if fname[-4:] in keylist:
extension[fname[-4:]] += 1
if fname[:4] == "lib/":
soEnvironment.append(fname.split('/')[1])
extension[fname[-3:]] += 1
statistics = []
for ext in extension.keys():
if extension[ext] == 0:
pass
else:
tempArr = []
tempArr.append(ext)
tempArr.append(str(extension[ext]))
statistics.append(tempArr)
return statistics
#extract string from xml
def extractString(report, apkfile):
print "[*] Extracting All XML String..."
stringCmd = "./lib/aapt dump strings %s" %apkfile
strResult = commands.getoutput(stringCmd).split('\n')
extractedStr = []
for xmlstring in strResult:
if "res/" in xmlstring:
pass
else:
try:
if len(xmlstring.split(':')[1]) == 0:
pass
else:
extractedStr.append(xmlstring.split(': ')[1])
except:
extractedStr.append(xmlstring)
report.stringinfo(extractedStr)
#get method information from dex
def methodAnalysis(report, string, typeid, method):
methodArr = []
for i in range(len(method)):
(class_idx, proto_idx, name_idx) = method[i]
class_str = string[typeid[class_idx]]
name_str = string[name_idx]
data = '%s.%s()' % (class_str, name_str)
methodArr.append(data)
report.dexmethodinfo(methodArr)
#get dex class filename (.java)
def classExtract(report, string):
classArray = []
for dexstr in string:
if ".java" in dexstr:
classArray.append(dexstr)
report.dexclassinfo(classArray)
#get dex adler32 checksum
def checksum(dexmodule):
return dexmodule.checksum()
#check similarity using ssdeep
def simcheck(apkfile, fuzzyhash):
print "[*] Checking Similarity..."
simdata = []
match = []
if os.path.exists('sim.txt') == False: #if sim.txt not found?
print "[*] Creating similarity storage DB.."
f = open('sim.txt', 'a+')
f.write('ssdeep,1.1--blocksize:hash:hash,filename\n' + fuzzyhash + '\n')
else:
searchQuery = commands.getoutput("ssdeep -m sim.txt " + apkfile).split('\n')
#print searchQuery
for query in searchQuery:
tempArr = []
try:
persent = query.split(':')[1].split(' ')[1].replace(')', '%)')
filename = os.path.basename(query.split(':')[1].split(' ')[0])
tempArr.append(filename)
tempArr.append(persent)
match.append(tempArr)
except:
pass
f = open('sim.txt', 'a+')
f.write(fuzzyhash + '\n')
return match
#find suspicious string in dex and replace if highlight
def findSuspicious(report, stringlist):
dexstrlist = []
for i in range(len(stringlist)):
email = re.findall(r'([\w.-]+)@([\w.-]+)', stringlist[i])
url = re.findall(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', stringlist[i])
ip = re.findall(r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$', stringlist[i])
if email:
dexstrlist.append(str(email[0][0] + "@" + email[0][1]))
if url:
dexstrlist.append(str(url[0]))
if ip:
dexstrlist.append(str(ip[0]))
report.dexstringinfo(dexstrlist)
#parse information from DEX list
def parseDEX(report):
global dexList
report.dexinfoHeader()
for dexfile in dexList:
parse = Dexparser(os.path.join("temp", dexfile))
string = parse.string_list()
typeid = parse.typeid_list()
method = parse.method_list()
adler32 = checksum(parse)
report.dexBasicinfo(dexfile, adler32)
findSuspicious(report, string)
#classExtract(report, string)
#methodAnalysis(report, string, typeid, method)
#get permission information
def permission(report, apkfile):
print "[*] Extracting Permission Data..."
permlist = []
permcmd = "./lib/aapt dump permissions %s" %apkfile
getperm = commands.getoutput(permcmd).split('\n')
for perm in getperm:
if "uses-permission" in perm:
permlist.append(perm.split(': ')[1])
report.writePerminfo(permlist)
def nativeparser(solist, report):
filterList = []
for sofile in solist:
with open(os.path.join("temp", sofile[1] + ".so"), 'rb') as f:
data = f.read()
email = re.findall(r'([\w.-]+)@([\w.-]+)', data)
url = re.findall(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', data)
ip = re.findall(r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$', data)
if email:
if str(email[0][0] + "@" + email[0][1]) not in filterList:
filterList.append(str(email[0][0] + "@" + email[0][1]))
if url:
if str(url[0]) not in filterList:
filterList.append(str(url[0]))
if ip:
if str(ip[0]) not in filterList:
filterList.append(str(ip[0]))
report.nativeStringinfo(filterList)
#native file information
def nativefile(zfile, report):
print "[*] Extracting Native File Data..."
solist = []
for fname in zfile.namelist():
if fname[-3:] == ".so":
tempArr = []
sofile = os.path.basename(fname)
source = zfile.open(fname)
target = file(os.path.join("temp", sofile), "wb")
with source, target:
shutil.copyfileobj(source, target)
sohash = filehash(os.path.join("temp", sofile), "sha1")
shutil.move(os.path.join("temp", sofile), os.path.join("temp", sohash + ".so"))
tempArr.append(fname)
tempArr.append(sohash)
solist.append(tempArr)
report.nativeFileinfo(solist)
nativeparser(solist, report)
#get apk file basic information
def getbasic(apkfile, report):
print "[*] Extracting Basic APK File Data..."
filename = os.path.basename(apkfile)
md5hash = filehash(apkfile, "md5")
sha1hash = filehash(apkfile, "sha1")
sha256hash = filehash(apkfile, "sha256")
filesize = str(os.path.getsize(apkfile) / 1024)
try:
fuzzy = commands.getoutput("ssdeep -s " + apkfile).split('\n')[1]
except:
print "[*] Fuzzyhash Command not found. Please <brew install ssdeep> to install"
fuzzy = ""
report.writeBaseinfo(filename, md5hash, sha1hash, sha256hash, fuzzy.split(',')[0], filesize)
return fuzzy
#get Certification information
def getCert(zfile, report):
print "[*] Extracting Certification Data..."
certlist = []
certdata = []
for fname in zfile.namelist():
if fname[-4:] == ".RSA":
certfile = os.path.basename(fname)
source = zfile.open(fname)
target = file(os.path.join("temp", certfile), "wb")
with source, target:
shutil.copyfileobj(source, target)
certlist.append(certfile)
for cert in certlist:
tempArr = []
c = CERTParser(os.path.join("temp", cert))
tempArr.append(cert)
tempArr.append(c.fingerprint())
tempArr.append(c.issuer())
tempArr.append(c.starttime())
certdata.append(tempArr)
report.writeCERTinfo(certdata)
#get AndroidManifest.xml information
def getManifest(apkfile, report):
print "[*] Extracting AndroidManifest Data..."
infocmd = "./lib/aapt dump badging %s" %apkfile
getinfo = commands.getoutput(infocmd).split('\n')
apiver = ""
cputype = ""
entry = ""
targetver = ""
appname = ""
packname = ""
entry = ""
for info in getinfo:
data = info.split(':')
if data[0] == "sdkVersion":
apiver = data[1].replace('\'', '')
if data[0] == "targetSdkVersion":
targetver = data[1].replace('\'', '')
if data[0] == "application-label":
try:
appname = data[1].replace('\'', '')
except:
appname = data[1]
if data[0] == "package":
packname = data[1].split('\'')[1]
if data[0] == "launchable-activity":
entry = data[1].split('\'')[1]
if data[0] == "native-code":
for cpu in data[1].split('\''):
cputype += cpu + " "
report.writeManifestinfo(apiver, cputype, targetver, appname, packname, entry)
return [packname, entry]
#dynamic analysis
def dynamicAnalysis(report, apkfile, packname, entry):
print "[*] Dynamic Analysis start!"
anal_result = dynamic.main(apkfile, packname, entry)
result = json.loads(anal_result)
try:
report.datasectioninfo(result['filetag']['startCreate'], result['filetag']['endCreate'])
except:
pass
try:
report.logcatinfo(result['logtag'])
except:
pass
try:
report.packetinfo(result['packettag']['packet'], result['timeline']['ipList'])
except:
pass
print "[*] Dynamic Analysis end!"
#program entry point
def main(apkfile, output):
try:
about(apkfile) #program information
isVaild = zipfile.is_zipfile(apkfile) #check vaild zip container
if isVaild:
zfile = zipfile.ZipFile(apkfile)
isAndroid = is_android(zfile) #check vaild android apk file
if isAndroid:
print "[*] Analysis start!"
#setting HTML Report
report = HTMLReport(output)
report.header()
report.style()
report.bodystart()
fuzzy = getbasic(apkfile, report)
extractDEX(zfile) #extract dex file
filetype = fileResource(zfile) #analyze file resources
simresult = simcheck(apkfile, fuzzy) #similarity check
report.writeFileinfo(filetype, simresult)
xmlinfo = getManifest(apkfile, report)
permission(report, apkfile)
getCert(zfile, report)
parseDEX(report)
extractString(report, apkfile)
nativefile(zfile, report)
dynamicAnalysis(report, apkfile, xmlinfo[0], xmlinfo[1])
report.endbody()
del report
else:
print "[*] Sorry, We can\'t analyze this file"
else:
print "[*] Sorry, We can\'t analyze this file"
delTemp()
print "[*] Analysis complete!"
except Exception, e:
logError(str(traceback.format_exc()))
print "[*] Androtools Exception - Error logged!"
if __name__ == '__main__':
try:
main(sys.argv[1], sys.argv[2])
except:
usage()