Skip to content

Commit 1a44704

Browse files
author
Kevin Ku
committed
Add main_pdev.py for single machine analysis
1 parent 99e8b4f commit 1a44704

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

main_LargeVM.py

+4
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ def analyze((apkEntry, OUT)):
6363
return ""
6464

6565
if __name__ == '__main__':
66+
if len(sys.argv) < 3:
67+
print "Usage: python main_LargeVM.py log_file_dir apk_list_file"
68+
sys.exit(1)
69+
6670
OUT = sys.argv[1]
6771
isParallel = False
6872
#for parallel running on multiple instances

main_pdev.py

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
'''
2+
Created on Sep 9, 2012
3+
4+
@author: psachdev
5+
'''
6+
#import manager
7+
import namespaceanalyzer
8+
import permission
9+
import SearchIntents
10+
import DbManager
11+
import logging
12+
import sys
13+
14+
from androguard.core.bytecodes import apk
15+
from androguard.core.bytecodes import dvm
16+
from androguard.core.analysis.analysis import *
17+
18+
from multiprocessing import Pool, get_logger
19+
20+
def handler (signum, sigframe):
21+
raise Exception ("Killed");
22+
23+
24+
def analyze((apkEntry, OUT)):
25+
try:
26+
OUT = OUT + '/'
27+
fileName = apkEntry['packageName'] + '.apk'
28+
path = apkEntry['fileDir']
29+
print "FileName Analyzed :" + fileName
30+
tokens = namespaceanalyzer.NameSpaceMgr.GetTokensStatic (path, '/')
31+
category = tokens [len (tokens) - 1]
32+
#print category
33+
filename = path + '/' + fileName
34+
outFileName = '/package.txt'
35+
outFileName = OUT + outFileName
36+
instance = namespaceanalyzer.NameSpaceMgr()
37+
38+
try:
39+
a = apk.APK(filename, zipmodule=1)
40+
except:
41+
a = apk.APK(filename, zipmodule=2)
42+
d = dvm.DalvikVMFormat (a.get_dex())
43+
dx = uVMAnalysis (d)
44+
#remove old db entry in static analysis db
45+
dbMgr.deleteEntry(apkEntry['packageName'])
46+
47+
packages = instance.execute (filename, outFileName, dbMgr, fileName, category, a, d, dx)
48+
49+
outfile_perm = '/permissions.txt'
50+
outfile_perm = OUT + outfile_perm
51+
permission.StaticAnalyzer (filename, outfile_perm, packages, dbMgr, fileName, a, d, dx)
52+
53+
outfile_links = '/links.txt'
54+
outfile_links = OUT + outfile_links
55+
SearchIntents.Intents(filename, outfile_links, packages, dbMgr, fileName, a, d, dx);
56+
dbMgr.androidAppDB.apkInfo.update({'packageName':apkEntry['packageName']}, {'$set': {'isApkUpdated': False}})
57+
return apkEntry['packageName']
58+
except:
59+
logger.error("\n")
60+
logger.error("=======================================================================")
61+
logger.error("\n")
62+
logger.exception("Main : Exception occured for " + apkEntry['packageName'])
63+
return ""
64+
65+
if __name__ == '__main__':
66+
if len(sys.argv) < 3:
67+
print "Usage: python main_pdev.py log_file_dir apk_list_file"
68+
print "apk list format: [package name] [directory containing the apk]"
69+
sys.exit(1)
70+
71+
OUT = sys.argv[1]
72+
apkListFile = sys.argv[2]
73+
74+
#in case the crawler breaks, append to the list.
75+
analyzedApkFile = open(OUT + '/' + 'filelist.txt', 'a+')
76+
'''
77+
Database Handle used to insert fields
78+
'''
79+
dbMgr = DbManager.DBManagerClass()
80+
81+
'''
82+
Example of how the various entrie are made into the database
83+
dbMgr.insert3rdPartyPackageInfo("testpackage", "testfilename", "testexternalpackage")
84+
dbMgr.insertPermissionInfo('testpackage', 'testfilename', 'testpermission', True, 'testdest', 'testexternalpackagename', 'testsrc')
85+
dbMgr.insertLinkInfo('testpackage', 'testfilename', 'testlink', True, 'testdest', 'testexternalpackagename')
86+
'''
87+
logger = get_logger()
88+
logFileHandler = logging.FileHandler(OUT + '/exceptions.log')
89+
logFormat = logging.Formatter("%(levelname)s %(asctime)s %(funcName)s %(lineno)d %(message)s")
90+
logFileHandler.setLevel(logging.DEBUG)
91+
logFileHandler.setFormatter(logFormat)
92+
logger.addHandler(logFileHandler)
93+
94+
apkList = []
95+
apkList_f = open(apkListFile)
96+
for line in apkList_f:
97+
pair = line.rstrip('\n').split(' ')
98+
apkList.append({'packageName': pair[0], "fileDir": pair[1]})
99+
apkList_f.close()
100+
101+
apkList = [(entry, OUT) for entry in apkList]
102+
103+
numberOfProcess = 4
104+
pool = Pool(numberOfProcess)
105+
for packageName in pool.imap(analyze, apkList):
106+
if packageName != "":
107+
analyzedApkFile.write(packageName + '\n')
108+
analyzedApkFile.flush()
109+

0 commit comments

Comments
 (0)