-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPath.py
More file actions
165 lines (141 loc) · 5.63 KB
/
Path.py
File metadata and controls
165 lines (141 loc) · 5.63 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
#!/usr/bin/python
#coding=utf-8
from Method import Method
import re
from Node import Node
class Path:
def __init__(self):
self.__sink = Node()
self.__source = Node()
self.__nodes = {}
'''
处理一条泄露路径
lines : 一条完整的泄露路径
'''
def handlePathNode(self,lines=[]):
self.__makeCallGraph(lines)
self.__handleStartActivity()
self.__handleStartService()
self.__handleBroadcast()
self.__handlehanler()
self.__printTree()
'''
如果出现修改beforeonCreate的情况
'''
def __handleStartActivity(self):
for key in self.__nodes.keys():
if self.__nodes[key].getMethodName() == 'beforeonCreate':
nnode = self.__nodes[key]
nnode.getParent().addChildList(nnode.getChildList())
nnode.getParent().deleteChild(nnode)
self.__nodes.pop(key)
break
def __handleStartService(self):
pass
def __handleBroadcast(self):
pass
def __handlehanler(self):
pass
def __makeCallGraph(self,lines):
for index in range(len(lines)):
if lines[index].startswith('===>'):
classname ,methodname = self.__patternMethod(lines[index][4:])
# print classname
# print methodname
parentNode = Node(classname,methodname)
if parentNode.getHash() not in self.__nodes:
self.__nodes[parentNode.getHash()] = parentNode
# for key in self.__nodes.keys():
# print self.__nodes[key].getClassName()
# print self.__nodes[key].getMethodName()
# print self.__nodes[key].getChildList()
else:
parentNode = self.__nodes[parentNode.getHash()]
childstate = lines[index+1]
if 'invoke' not in childstate:#没有invoke认为赋值语句
if 'return' in childstate:#如果是return 说明return的上一句是return下一句的父节点
# print childstate
childchildstate = lines[index + 2]
classname, methodname = self.__patternMethod(childchildstate[4:])
gradepaNode = Node(classname, methodname)
if gradepaNode.getHash() not in self.__nodes:
self.__nodes[gradepaNode.getHash()] = gradepaNode
gradepaNode.addChild(parentNode)
else:
gradepaNode = self.__nodes[gradepaNode.getHash()]
gradepaNode.addChild(parentNode)
else:
temp = self.__patternNode(childstate[7:])
if temp is None:
continue
classname ,methodname = self.__splitNodestr(temp)
# print classname
# print methodname
childNode = Node(classname,methodname)
self.__nodes[childNode.getHash()] = childNode
parentNode.addChild(childNode)
if lines[index].startswith('------>'):
pass
def __printTree(self):
for key in self.__nodes.keys():
print self.__nodes[key].getClassName(),self.__nodes[key].getMethodName()
for nnode in self.__nodes[key].getChildList():
print 'child----',nnode.getClassName(),nnode.getMethodName()
def setSink(self,sink):
temp = self.__patternNode( sink)
classname , methodname = self.__splitNodestr(temp)
self.__sink.setClassName(classname)
self.__sink.setMethodName(methodname)
# self.__sink = sink
def getSink(self):
return self.__sink
def setSource(self,source):
temp = self.__patternNode(source)
classname, methodname = self.__splitNodestr(temp)
self.__source.setClassName(classname)
self.__source.setMethodName(methodname)
# self.__source = source
def getSource(self):
return self.__source
def getNodeList(self):
return self.__nodeList
'''
针对method做正则
如:
<com.aaa.bbb: void func(args1,args2)>
这里的method是Node类型
'''
def __patternMethod(self,str = ''):
matchs = re.split('^<(.*):\s(.*)\s(.*)\\((.*)\\)>$', str)
# print matchs[1]
# print matchs[2]
# print matchs[3]
# print matchs[4]
# method.setClassName(matchs[1])
# method.setMethodName(matchs[3])
return matchs[1],matchs[3]
'''
针对语句进行正则
'''
def __patternNode(self,str):
if str.index('<') > -1:
matchs = re.split('<(.*)>',str)
return matchs[1]
else:
return None
def __splitNodestr(self,str = ''):
classname = str[:str.index(':')]
# print classname
if '(' in str:
methodname = str[self.__find_last(str,' ')+1:str.index('(')]
else:
methodname = str[self.__find_last(str,' ')+1:]
# print methodname
return classname,methodname
def __find_last(self,string,str):
last_position = -1
while True:
position = string.find(str, last_position + 1)
if position == -1:
return last_position
last_position = position