forked from honeynet/apkinspector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetMethods.py
154 lines (117 loc) · 5.42 KB
/
GetMethods.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
import sys
if sys.path[0] == "":
sys.path.append(sys.path[1]+"/androguard/")
PATH_INSTALL = sys.path[1]+"/androguard"
else:
sys.path.append(sys.path[0]+"/androguard/")
PATH_INSTALL = sys.path[0]+"/androguard"
sys.path.append(PATH_INSTALL + "./")
sys.path.append(PATH_INSTALL + "/core")
sys.path.append(PATH_INSTALL + "/core/bytecodes")
sys.path.append(PATH_INSTALL + "/core/predicates")
sys.path.append(PATH_INSTALL + "/core/analysis")
sys.path.append(PATH_INSTALL + "/core/vm")
sys.path.append(PATH_INSTALL + "/core/wm")
sys.path.append(PATH_INSTALL + "/core/protection")
sys.path.append(PATH_INSTALL + "/classification")
import androguard, analysis, androlyze
import bytecode
from dvm import *
class CLASS:
apk = None
vm = None
vmx = None
def __init__(self, apk, vm, vmx):
self.apk = apk
self.vm = vm
self.vmx = vmx
def get_class(self):
return self.vm.get_classes()
def get_classname(self, classes):
return classes.get_name()
def get_methods(self, classes):
return classes.get_methods()
def get_methodname(self, method):
return method.get_name()
def get_code(self, method):
return method._code.show()
def get_classlist(self):
return self.vm.get_classes_names()
def get_methods_class(self, classes):
return self.vm.get_methods_class(classes)
def get_maxdepth(self):
classesnames = self.vm.get_classes_names()
maxdepth = 0
for i in classesnames:
l = len(i.split("/"))
if l > maxdepth:
maxdepth = l
return maxdepth
#get where a permission is used
def get_permission(self):
pathDict = {}
perms_access = self.vmx.tainted_packages.get_permissions([])
for perm in perms_access:
pathDict[perm] = self.show_path(perms_access[perm])
return pathDict
def show_path(self, paths):
accessPathList = []
for path in paths:
if isinstance(path,analysis.PathP):
if path.get_access_flag() == analysis.TAINTED_PACKAGE_CALL:
accessPath = ("%s %s %s (@%s-0x%x) ---> %s %s %s") % (path.get_method().get_class_name(), path.get_method().get_name(), \
path.get_method().get_descriptor(), path.get_bb().get_name(), path.get_bb().start + path.get_idx(), \
path.get_class_name(), path.get_name(), path.get_descriptor())
accessPathList.append(accessPath)
return accessPathList
# All Invoke Methods
def get_methodInvoke(self):
methodInvokeList = []
allMethods = self.vm.get_methods()
import Global
for m in allMethods:
#Yuan :build callinout tree
invokingMethod = m.get_class_name() + " " + m.get_descriptor() +"," + m.get_name()
if (Global.NAV_NO == 1):
print "name first method"
Global.FM = invokingMethod
code = m.get_code()
if code == None:
continue
else:
bc = code.get_bc()
idx = 0
lineNum = 1
for i in bc.get():
line = i.show_buff(idx)
if line.find("invoke-") >= 0:
index = line.index("[meth@")
method = str(line[index:])
method2 = method.split(" ")
# set the class
ClassStartIndex = index + len(method2[0]) + len(method2[1]) + 2
className = line[ClassStartIndex : ClassStartIndex + len(method2[2])]
# set the return type
ReturnStartIndex = index + method.rindex(")") + 2
returnType = line[ReturnStartIndex : ReturnStartIndex+len(method2[-2])]
# set the method name
NameStartIndex = index + method.rindex(" ") + 1
methodName = line[NameStartIndex : NameStartIndex + len(method2[-1]) - 1]
# set the parameter name
ParameterStartIndex = index + method.index("(")
ParameterEndIndex = index + method.rindex(")") + 1
parameterName = line[ParameterStartIndex : ParameterEndIndex]
# set the descriptor name
descriptorName = parameterName +returnType
invokedMethod = className + " " +descriptorName+ "," + methodName
methodInvokeList.append(invokingMethod +" ---> " + invokedMethod + "^Line:"+str(lineNum)+" Offset:"+"0x%x" % idx)
lineNum += 1
idx += i.get_length()
# print "methodinvoke list\n"
# print methodInvokeList
file = open('method.txt','a')
# file.write("%s\n" % method)
file.write("%s\n" % methodInvokeList )
file.close
Global.endmethod = invokedMethod
return methodInvokeList