/
findPending.py
472 lines (447 loc) · 18.3 KB
/
findPending.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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
'''Copyright 2015 LinkedIn Corp. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.'''
import lib.plyj.parser as plyj
from modules import common, report
import lib.plyj.model as m
import re,sys
import logging
from report import ReportIssue
from modules.common import Severity, ReportIssue
from createExploit import ExploitType
from lib.progressbar import *
from lib.pubsub import pub
from common import terminalPrint
common.logger = logging.getLogger()
tree=''
current_file=''
parser = plyj.Parser()
method_list=[]
#These methods create instances of Pending Intents
method_list.append('getActivities')
method_list.append('getService')
method_list.append('getActivity')
method_list.append('getBroadcast')
def start(queue,height):
"""
Start finding pending intents
"""
results = []
global tree
global current_file
count = 0
#TODO - Look for use of fillIn method which can make this a much more exploitable condition
for j in common.java_files:
count = count + 1
pub.sendMessage('progress', count1=None, count2=round(count*100/common.java_files.__len__()), count3=None)
current_file=j
tree=parser.parse_file(j)
#TODO - Need to add scanning of the imports, to see if Intent or PendingIntent is extended, was working on it,
#but the one issue where it arose was non-trivial, so I gave up for now
if hasattr(tree,'type_declarations'):
for type_decl in tree.type_declarations:
if type(type_decl) is m.ClassDeclaration:
for t in type_decl.body:
for f in t._fields:
#dynamically parse each token where is f is the field and t is the token
try:
recurse(f,t,results)
except Exception as e:
common.logger.debug("Problem in recurse function of findPending.py: " + str(e))
common.parsingerrors.add(str(current_file))
else:
common.logger.debug("No type declarations: " + str(j))
report.write("parsingerror-issues-list", str(current_file), "strong")
queue.put(results)
return
def recurse(f,t,results):
"""
Recurse
"""
#Looking for pending intents
global method_list
current=getattr(t,f)
if type(current) is str:
for e in method_list:
if str(type(current)) == str(e):
#TODO - Remove this (never seems to be hit)
report.write("parsingerror-issues-list", str(current_file), "strong")
common.logger.debug("Please report the following error, if you see this")
common.logger.debug("INCOMPLETE CODE BRANCH REACHED findPending.py #0 (results may be incomplete)")
elif type(current) is list:
for l in current:
#TODO - There might be a way to get rid of this block and just use recurse
if type(l) is m.MethodInvocation:
for i in method_list:
if str(l.name) == str(i):
if hasattr(current,'target'):
if hasattr(current.target,'value'):
if str(current.target.value) == "PendingIntent":
#TODO - the interesting ones seem to be of type Name only
#The intent is always the third parameter, for getActivities it is an array
try:
parseArgs(current.arguments[2],results)
except Exception as e:
report.write("parsingerror-issues-list", str(current_file), "strong")
common.logger.debug("Problem in parseArgs function of findPending.py: " + str(e))
elif l is None:
pass
else:
if type(l) is not str:
for n in l._fields:
try:
recurse(n,l,results)
except Exception as e:
report.write("parsingerror-issues-list", str(current_file), "strong")
common.logger.debug("Problem in recurse function of findPending.py: " + str(e))
else:
for g in method_list:
if str(g) == str(l):
#TODO - Remove this (never seems to be hit)
report.write("parsingerror-issues-list", str(current_file), "strong")
common.logger.debug("Please report the following error, if you see this")
common.logger.debug("INCOMPLETE CODE BRANCH REACHED findPending.py #2 - Press any key to continue (results may be incomplete)")
elif type(current) is m.MethodInvocation:
for j in method_list:
if str(current.name) == str(j):
if hasattr(current,'target'):
if hasattr(current.target,'value'):
if str(current.target.value) == "PendingIntent":
#TODO - the interesting ones seem to be of type Name only
#The intent is always the third parameter, for getActivities it is an array
try:
parseArgs(current.arguments[2],results)
except Exception as e:
report.write("parsingerror-issues-list", str(current_file), "strong")
common.logger.debug("Problem in recurse function of findPending.py: " + str(e))
elif type(current) is m.VariableDeclaration:
if hasattr(current,'type'):
if type(current.type) is m.Type:
if hasattr(current.type,'name'):
if type(current.type.name) is not str:
if hasattr(current.type.name,'value'):
if str(current.type.name.value) == "PendingIntent":
report.write("parsingerror-issues-list", str(current_file), "strong")
common.logger.debug("findPending - recurse, found PendingIntent, but not handled: " + str(current))
else:
report.write("parsingerror-issues-list", str(current_file), "strong")
common.logger.debug("findPending - recurse, can't find PendingIntent: " + str(current))
elif hasattr(current,'_fields'):
for q in current._fields:
try:
recurse(q,current,results)
except Exception as e:
report.write("parsingerror-issues-list", str(current_file), "strong")
common.logger.debug("Problem in recurse function of findPending.py: " + str(e))
return
def parseArgs(arg,results):
"""
Checking to see whether the setClass method is called on the arguments of a pending intent, if it is a new instance of an Intent
"""
if type(arg) is m.MethodInvocation:
if type(arg.target) is m.InstanceCreation:
#If the intent only has one argument, it must be implicit
if len(arg.target.arguments) > 1:
if type(arg.target.arguments[1]) is m.Name:
#At this point, I need to go through the tree and see if this arg is a class, making this explicit and safe
#If the argument is not a name, what is it?
if hasattr(arg.target.arguments[1],'value'):
try:
if findClass(arg.target.arguments[1].value,results):
return
else:
report.write("parsingerror-issues-list", str(current_file), "strong")
common.logger.debug("ERROR: CAN'T FIND THE CLASS in parseArgs")
except Exception as e:
report.write("parsingerror-issues-list", str(current_file), "strong")
common.logger.debug("Problem in findClass function of findPending.py: " + str(e))
else:
report.write("parsingerror-issues-list", str(current_file), "strong")
common.logger.debug("ERROR: UNEXPECTED Type " + str(type(arg.target.arguments[1])))
else:
#TODO - need to add details here
issue = ReportIssue()
issue.setCategory(ExploitType.INTENT)
issue.setDetails("PendingIntent created with implicit intent. File: " + str(current_file))
issue.setFile(current_file)
issue.setSeverity(Severity.VULNERABILITY)
results.append(issue)
issue = terminalPrint()
issue.setLevel(Severity.VULNERABILITY)
issue.setData("PendingIntent created with implicit intent. File: " + str(current_file))
results.append(issue)
else:
#TODO - Remove this (never seems to be hit)
report.write("parsingerror-issues-list", str(current_file), "strong")
common.logger.debug("Please report the following error, if you see this")
common.logger.debug("ERROR: INCOMPLETE CODE BRANCH REACHED findPending.py #4 - Press any key to continue (results may be incomplete)")
elif type(arg) is m.Name:
if hasattr(arg,'value'):
try:
findIntent(arg.value,results)
except Exception as e:
report.write("parsingerror-issues-list", str(current_file), "strong")
common.logger.debug("Problem in findIntent function of findPending.py: " + str(e))
else:
#TODO - Remove this (never seems to be hit)
report.write("parsingerror-issues-list", str(current_file), "strong")
common.logger.debug("Please report the following error, if you see this")
common.logger.debug("ERROR: INCOMPLETE CODE BRANCH REACHED findPending.py #5 - Press any key to continue (results may be incomplete)")
return
def findClass(classname,results):
found=False
"""
Find the class name
"""
if hasattr(tree,'type_declarations'):
for type_decl in tree.type_declarations:
if type(type_decl) is m.ClassDeclaration:
if str(type_decl.name) == str(classname):
found=True
return found
else:
if recurseClass(classname,type_decl,results):
return True
elif type(type_decl) is m.VariableDeclaration:
if hasattr(type_decl,'type'):
if hasattr(type_decl.type,'name'):
if hasattr(type_decl.type.name,'value'):
if str(type_decl.type.name.value) == str(classname):
return True
else:
if recurseClass(classname,type_decl,results):
return True
return found
def recurseClass(classname,treeObject,results):
found=False
if hasattr(treeObject,'_fields'):
for x in treeObject._fields:
if type(getattr(treeObject,x)) is list:
for y in getattr(treeObject,x):
if type(y) is m.ClassDeclaration:
if str(y.name) == str(classname):
found=True
return found
elif type(y) is m.VariableDeclaration:
if hasattr(y,'type'):
if hasattr(y.type,'name'):
if hasattr(y.type.name,'value'):
if str(y.type.name.value) == "Class":
for v in y.variable_declarators:
if str(v.variable.name) == str(classname):
found=True
return found
else:
if recurseClass(classname,y,results):
return True
elif type(getattr(treeObject,x)) is m.ClassDeclaration:
if x.name == str(classname):
found=True
return found
else:
if recurseClass(classname,x,results):
return True
elif type(getattr(treeObject,x)) is m.VariableDeclaration:
if hasattr(getattr(treeObject,x),'type'):
if hasattr(getattr(treeObject,x).type,'name'):
if hasattr(getattr(treeObject,x).type.name,'value'):
if str(getattr(treeObject,x).type.name.value) == str(classname):
found=True
return found
elif hasattr(getattr(treeObject,x),'_fields'):
for z in getattr(treeObject,x)._fields:
if recurseClass(classname,getattr(getattr(treeObject,x),z),results):
return True
elif type(treeObject) is list:
for a in treeObject:
if recurseClass(classname,a,results):
return True
return found
def findIntent(intent,results):
"""
Find Intent
"""
#TODO - This section is ugly and needs to be refactored
#Looking for the intent used in the pending intent
global tree
found=False
explicit=False
if hasattr(tree,'type_declarations'):
for type_decl in tree.type_declarations:
if type(type_decl) is m.ClassDeclaration:
for t in type_decl.body:
if not found:
if type(t) is m.VariableDeclaration:
if hasattr(t,'type'):
if type(t.type) is not str:
report.write("parsingerror-issues-list", str(current_file), "strong")
common.logger.debug("Please report the following error, if you see this")
common.logger.debug("ERROR: INCOMPLETE CODE BRANCH REACHED findPending.py #6 - Press any key to continue (results may be incomplete)")
if str(t.type) == "Intent":
common.logger.error("FOUND Intent Variable 0: " + str(t))
else:
if hasattr(t.type,'name'):
if hasattr(t.type.name,'value'):
if str(t.type.name.value) == "Intent":
common.logger.debug("FOUND Intent Variable 1: " + str(t))
else:
try:
temp=intent_digger(t,intent)
except Exception as e:
common.logger.debug("Error in intent_digger function of findPending.py: " + str(e))
found=temp[0]
explicit=temp[1]
if found:
if not explicit:
#See if the intent is explicit
if type(t) is m.MethodInvocation:
if str(t.name) == "setClass":
explicit=True
elif str(t.name) == "setPackage":
explicit=True
elif type(t) is m.MethodDeclaration:
for f in t._fields:
if type(getattr(t,f)) is list:
for x in getattr(t,f):
if type(x) is m.MethodInvocation:
if str(x.name) == "setClass":
explicit=True
elif str(x.name) == "setPackage":
explicit=True
else:
if hasattr(x,'_fields'):
for y in x._fields:
if type(getattr(x,y)) is m.MethodInvocation:
if str(getattr(x,y).name) == "setClass":
if hasattr(getattr(x,y).target,'value'):
if str(getattr(x,y).target.value) == str(intent):
explicit=True
elif str(getattr(x,y).name) == "setPackage":
if str(getattr(x,y).target.value) == str(intent):
explicit=True
else:
common.logger.debug("Something went wrong in findPending, when determining if the Intent was explicit")
elif type(x) is list:
common.logger.debug("ERROR: UNEXPECTED CODE PATH in findIntent - 1")
elif type(getattr(t,f)) is m.MethodInvocation:
common.logger.debug("ERROR: UNEXPECTED CODE PATH in findIntent - 2")
else:
for f in t._fields:
if type(getattr(t,f)) is list:
for x in getattr(t,f):
if type(x) is m.MethodInvocation:
if str(x.name) =="setClass":
explicit=True
elif str(x.name) =="setPackage":
explicit=True
else:
break
else:
common.logger.debug("NO TYPE DECLARATIONS")
if not explicit:
if found:
issue = ReportIssue()
issue.setCategory(ExploitType.INTENT)
issue.setDetails("Implicit Intent: " + str(intent) + " used to create instance of PendingIntent. A malicious application could potentially intercept, redirect and/or modify (in a limited manner) this Intent. Pending Intents retain the UID of your application and all related permissions, allowing another application to act as yours. File: " + str(current_file) + " More details: https://www.securecoding.cert.org/confluence/display/android/DRD21-J.+Always+pass+explicit+intents+to+a+PendingIntent")
issue.setFile(current_file)
issue.setSeverity(Severity.VULNERABILITY)
results.append(issue)
issue = terminalPrint()
issue.setLevel(Severity.VULNERABILITY)
issue.setData("Implicit Intent: " + str(intent) + " used to create instance of PendingIntent. A malicious application could potentially intercept, redirect and/or modify (in a limited manner) this Intent. Pending Intents retain the UID of your application and all related permissions, allowing another application to act as yours. File: " + str(current_file) + " More details: https://www.securecoding.cert.org/confluence/display/android/DRD21-J.+Always+pass+explicit+intents+to+a+PendingIntent")
results.append(issue)
else:
#TO DO - ensure we can resolve custom intents to avoid this error
common.logger.debug("ERROR: COULDN'T FIND THE INTENT: " + str(intent) + " This may either be due to a custom Intent class or other error")
return
def intent_digger(t,intent):
"""
Traverse the intent declaration hierarchy
"""
results=[False,False]
if hasattr(t,"_fields"):
for f in t._fields:
current=getattr(t,f)
if type(current) is list:
for c in current:
if type(c) is m.VariableDeclaration:
if hasattr(c,'type'):
if type(c.type) is m.Type:
if hasattr(c.type,'name'):
if hasattr(c.type.name,'value'):
if c.type.name.value == "Intent":
for v in c.variable_declarators:
if str(v.variable.name) == str(intent):
found=True
if hasattr(v.initializer,'arguments'):
for a in v.initializer.arguments:
if type(a) is m.ClassLiteral:
explicit=True
results=[found,explicit]
return results
results=[found,False]
return results
elif type(c) is m.VariableDeclarator:
if hasattr(c.initializer,'type'):
if hasattr(c.initializer.type.name,'value'):
if c.initializer.type.name.value == "Intent":
if str(c.variable.name) == str(intent):
found=True
if hasattr(c.initializer,'arguments'):
for a in c.initializer.arguments:
if type(a) is m.ClassLiteral:
explicit=True
results=[found,explicit]
return results
explicit=False
results=[found,explicit]
return results
else:
results=intent_digger(c.initializer,intent)
if results[0] == True:
return results
else:
results=intent_digger(c,intent)
if results[0] == True:
return results
elif type(current) is m.VariableDeclaration:
if hasattr(current,'type'):
if type(current.type) is m.Type:
if hasattr(current.type.name,'value'):
if current.type.name.value == "Intent":
for v in current.variable_declarators:
if str(v.variable.name) == str(intent):
found=True
for a in v.initializer.arguments:
if type(a) is m.ClassLiteral:
explicit=True
results=[found,explicit]
return results
elif type(current) is m.VariableDeclarator:
if hasattr(c.initializer,'type'):
if hasattr(c.initializer.type.name,'value'):
if c.initializer.type.name.value == "Intent":
if str(c.variable.name) == str(intent):
found=True
for a in c.initializer.arguments:
if type(a) is m.ClassLiteral:
explicit=True
results=[found,explicit]
return results
explicit=False
results=[found,explicit]
return results
else:
if hasattr(current,'_fields'):
for r in current._fields:
results=intent_digger(getattr(current,r),intent)
if results[0] == True:
return results
elif type(t) is list:
for y in t:
results=intent_digger(y,intent)
if results[0] == True:
return results
return results