-
Notifications
You must be signed in to change notification settings - Fork 0
/
namespaceanalyzer.py
330 lines (275 loc) · 12.2 KB
/
namespaceanalyzer.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
'''
Created on Sep 21, 2012
@author: psachdev
'''
from androguard.core.bytecodes import apk
from androguard.core.bytecodes import dvm
from androguard.core.analysis.analysis import *
import DirStructHandler
import PackageRules
import re
class NameSpaceMgr:
'''
1. This class creates a n-tree of the directories present in apk
2. After creation of the tree it creates a set of static rules for printing external packages
3. Iterates over the tree structure and prints the directories according to rules
4. Returns all the packages encountered as it will be used in printing links and permissions
'''
def __init__(self):
'''
1. Packages contains all the packages encountered while traversing the tree
'''
self.packages = []
'''
2. Contains alreadyprinted. To take of the noise produced by decompilation
'''
self.alreadyPrinted = []
'''
3. Tokenized items of the main package name
'''
self.main_package_tokens = []
@staticmethod
def GetTokensStatic (string, delimiter):
tokens = []
for name in string.split (delimiter):
tokens.append(name)
return tokens
def GetTokens (self, string):
tokens = []
for name in string.split ('.'):
tokens.append(name)
return tokens
'''
Song
Special Handling for Google SDK
'''
def specialHandlingForGoogleSDK(self, package_name):
if "googleAnalytics" not in self.alreadyPrinted and (package_name.startswith('Lcom/google/analytics/') or package_name.startswith('Lcom/google/android/apps/analytics/')):
#should do something or not for tracking
pass
if "admob" not in self.alreadyPrinted and package_name.startswith('Lcom/google/ads'):
self.dbMgr.insert3rdPartyPackageInfo (self.main_package_name, self.fileName, "admob", self.category)
self.alreadyPrinted.append ("admob")
self.packages.append ("admob")
def execute (self, fileName, outFileName, dbMgr, noprefixfilename, category, a, d, dx):
###a = apk.APK(fileName)
###d = dvm.DalvikVMFormat (a.get_dex())
###dx = uVMAnalysis (d)
#self.outHandle = open (outFileName, 'a+')
'''
Handle to DataBase
'''
self.dbMgr = dbMgr
if len(category) > 250 :
self.category = (category[:200] + '..')
else:
self.category = category
self.dirEntries = []
self.main_package_name = ""
######self.outHandle.write ("---Package Name---\n")
'''
Keeping main package name within range of mysql datatype
'''
mpn = a.get_package()
if len(mpn) > 250 :
self.main_package_name = (mpn[:200] + '..')
else:
self.main_package_name = mpn
'''
Keeping main package name within range of mysql datatype
'''
if len(noprefixfilename) > 250 :
self.fileName = (noprefixfilename[:200] + '..')
else:
self.fileName = noprefixfilename
#print self.main_package_name
###self.outHandle.write(fileName)
###self.outHandle.write ('\n')
self.main_package_tokens = self.GetTokens(self.main_package_name)
packages = dx.get_tainted_packages()
#Filtering out internal packages used for app creation
ex1 = re.compile ("Ljava/*")
ex2 = re.compile ("Landroid/*")
ex3 = re.compile (self.GetDecompiledPackageName (self.main_package_name))
ex4 = re.compile("/google/")
package_names = []
for _, package_name in packages.get_packages():
self.specialHandlingForGoogleSDK(package_name)
if ex3.search (package_name) == None and ex1.search (package_name) == None and ex2.search (package_name) == None and ex4.search (package_name) == None:
package_names.append (self.GetDirectoryName (package_name))
#print package_name
###self.outHandle.write ("--External Packages---\n")
self.PopulateDirEntries(package_names)
self.GetPackages ()
#self.outHandle.close()
return self.alreadyPrinted
'''
Converting names with dots as separator with names with slashes
'''
def GetDecompiledPackageName (self, main_package_name):
tokens = []
for string in main_package_name.split ('.'):
tokens.append(string)
decompiledName = 'L'
for string in tokens:
decompiledName = decompiledName + string
decompiledName = decompiledName + '/'
#print decompiledName
return decompiledName
'''
Song
New method for Removing noisy characters from names
'''
def _GetDirectoryName(self, package_name):
package_name = package_name.replace('/', '.')
#remove not java characters
package_name = package_name.translate(re.sub('[^_a-zA-Z$0-9\s\.]', '', package_name))
return package_name
'''
Removing noisy characters from the names
'''
def GetDirectoryName (self, package_name):
tokens = []
i=0
for string in package_name.split ('/'):
if i==0:
string = string[1:]
tokens.append(string)
i = i + 1
lastToken = tokens.pop()
lastToken = lastToken[:-1]
tokens.append(lastToken)
new_package_name = ""
for string in tokens:
new_package_name += string
new_package_name += "."
#print new_package_name
return new_package_name
def GetTokenizedEntries (self, string):
tokens = []
for name in string.split ('.'):
dirEntry = DirStructHandler.DirEntry ()
dirEntry.AddDirName (name)
tokens.append(dirEntry)
return tokens
def GetRootDirIfPresent (self, dirEntry):
for entry in self.dirEntries:
if entry.IsEqual (dirEntry) == True:
return entry
return None
def InsertIfNotPresent (self, parent, newchild):
children = parent.ChildRef
if len (children) == 0:
parent.AddChildRef (newchild)
return True
else:
found = False
for child in children :
if child.DirName == newchild.DirName:
found = True
break
if found == False:
parent.AddChildRef (newchild)
'''
Creating tree structure for the directories present
example of structure
com (parent) dir1 (child-1) dir2 (child-2)
org (parent) dir1 (child-1) dir2 (child-2) dir3 (child-3)
google (parent) ...
'''
def PopulateDirEntries (self, package_names):
for entry in package_names:
#print entry
tokens = []
tokens = self.GetTokenizedEntries(entry)
i = 0
for dirEntryToken in tokens:
if (len (dirEntryToken.DirName) == 0):
continue
root = self.GetRootDirIfPresent (dirEntryToken)
if i == 0 and (len(self.dirEntries) == 0 or root == None):
self.dirEntries.append(dirEntryToken)
parentDirEntry = dirEntryToken
elif i == 0 and root != None:
parentDirEntry = root
else:
self.InsertIfNotPresent (parentDirEntry, dirEntryToken)
parentDirEntry = dirEntryToken
i = i + 1
'''
Recursive function to print the package name / directory name based on rules (packageLevel)
'''
def PrintPackageNameAtLevel (self, rootEntry, packageLevel):
if packageLevel == 1:
self.packages.append (rootEntry.DirName)
if (len(rootEntry.DirName) <= 3) or rootEntry.DirName.find('$')!=-1 or ('w3c' in rootEntry.DirName) or ('apache' in rootEntry.DirName) or ('xml' in rootEntry.DirName) or ('junit' in rootEntry.DirName) or ('sun' in rootEntry.DirName) or ('android' in rootEntry.DirName) or ('dalvik' in rootEntry.DirName) or ('json' in rootEntry.DirName) :
return
if (rootEntry.DirName in self.alreadyPrinted):
return
ex = re.compile (rootEntry.DirName)
for maintokens in self.main_package_tokens:
if (ex.search (maintokens) != None):
return
'''
Printing into file as well as writing into Database
'''
###self.outHandle.write (str (rootEntry.DirName))
if len(rootEntry.DirName) > 250 :
name = (rootEntry.DirName[:200] + '..')
else:
name = rootEntry.DirName
self.dbMgr.insert3rdPartyPackageInfo(self.main_package_name, self.fileName, name, self.category)
###self.outHandle.write ("\n")
self.alreadyPrinted.append (rootEntry.DirName)
return
packageLevel = packageLevel - 1
childRef = rootEntry.ChildRef
for child in childRef:
self.PrintPackageNameAtLevel(child, packageLevel)
'''
Before iterating through the directory structure print the top level directories based on static rules
'''
def PrintCommonAncestor (self, rootEntry, rules):
ancestorLevel = rules.packageLevel;
if ancestorLevel > 1:
#Descend down that many levels
self.PrintPackageNameAtLevel (rootEntry, ancestorLevel)
elif ancestorLevel == -2: #Ignore noise
return;
elif ancestorLevel == 3:
###self.outHandle.write (str ("titanium"))
self.dbMgr.insert3rdPartyPackageInfo (self.main_package_name, self.fileName, "titanium", self.category)
self.alreadyPrinted.append ("titanium")
elif ancestorLevel == 1:
self.packages.append (rootEntry.DirName)
'''Rule added to PackageRules'''
# if (len(rootEntry.DirName) <= 2) or ('w3c' in rootEntry.DirName) or ('apache' in rootEntry.DirName) or ('xml' in rootEntry.DirName) or ('L' in rootEntry.DirName) or ('junit' in rootEntry.DirName) or ('sun' in rootEntry.DirName) or ('android' in rootEntry.DirName) or ('dalvik' in rootEntry.DirName) or ('json' in rootEntry.DirName) :
# return
###self.outHandle.write (str (rootEntry.DirName))
if len(rootEntry.DirName) > 250 :
name = (rootEntry.DirName[:200] + '..')
else:
name = rootEntry.DirName
self.dbMgr.insert3rdPartyPackageInfo (self.main_package_name, self.fileName, name, self.category)
###self.outHandle.write ("\n")
self.alreadyPrinted.append (rootEntry.DirName)
else:
#google package
self.packages.append (self.main_package_name)
'''Rule added to PackageRules'''
# if (len(rootEntry.DirName) <= 2) or ('w3c' in rootEntry.DirName) or ('apache' in rootEntry.DirName) or ('xml' in rootEntry.DirName) or ('L' in rootEntry.DirName) or ('junit' in rootEntry.DirName) or ('sun' in rootEntry.DirName) or ('android' in rootEntry.DirName) or ('dalvik' in rootEntry.DirName) or ('json' in rootEntry.DirName) :
# return
###self.outHandle.write (str (self.main_package_name))
###self.outHandle.write ("\n")
self.PrintPackageNameAtLevel (rootEntry, 2)
def SetRules (self, rootEntry):
rules = PackageRules.Rules (rootEntry, self.main_package_name);
return rules
'''
Iterate through all the top level directories and print the package names
'''
def GetPackages (self):
i = 0
for rootEntry in self.dirEntries:
rules = self.SetRules (rootEntry)
self.PrintCommonAncestor (rootEntry, rules)