-
Notifications
You must be signed in to change notification settings - Fork 4
/
androsim.py
executable file
·264 lines (227 loc) · 8.41 KB
/
androsim.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
#!/usr/bin/env python
# This file is part of Androguard.
#
# Copyright (C) 2012, Anthony Desnos <desnos at t0t0.fr>
# All rights reserved.
#
# Androguard is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Androguard is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Androguard. If not, see <http://www.gnu.org/licenses/>.
import sys, os
from optparse import OptionParser
from androguard.core import androconf
from androguard.core.bytecodes import apk, dvm
from androguard.core.analysis import analysis
from androguard.util import read
sys.path.append("./elsim")
from elsim import elsim
from elsim.elsim_dalvik import ProxyDalvik, FILTERS_DALVIK_SIM
from elsim.elsim_dalvik import ProxyDalvikStringMultiple, ProxyDalvikStringOne, FILTERS_DALVIK_SIM_STRING
option_0 = {
'name': ('-i', '--input'),
'help': 'file : use these filenames',
'nargs': 2
}
option_1 = {
'name': ('-t', '--threshold'),
'help':
'specify the threshold (0.0 to 1.0) to know if a method is similar. This option will impact on the filtering method. Because if you specify a higher value of the threshold, you will have more associations',
'nargs': 1
}
option_2 = {
'name': ('-c', '--compressor'),
'help':
'specify the compressor (BZ2, ZLIB, SNAPPY, LZMA, XZ). The final result depends directly of the type of compressor. But if you use LZMA for example, the final result will be better, but it take more time',
'nargs': 1
}
option_4 = {
'name': ('-d', '--display'),
'help': 'display all information about methods',
'action': 'count'
}
option_5 = {
'name': ('-n', '--new'),
'help':
'calculate the final score only by using the ratio of included methods',
'action': 'count'
}
option_6 = {
'name': ('-e', '--exclude'),
'help': 'exclude specific class name (python regexp)',
'nargs': 1
}
option_7 = {
'name': ('-s', '--size'),
'help':
'exclude specific method below the specific size (specify the minimum size of a method to be used (it is the length (bytes) of the dalvik method)',
'nargs': 1
}
option_8 = {
'name': ('-x', '--xstrings'),
'help': 'display similarities of strings',
'action': 'count'
}
option_9 = {
'name': ('-v', '--version'),
'help': 'version of the API',
'action': 'count'
}
option_10 = {
'name': ('-l', '--library'),
'help':
'use python library (python) or specify the path of the shared library)',
'nargs': 1
}
options = [option_0, option_1, option_2, option_4, option_5, option_6, option_7,
option_8, option_9, option_10]
def check_one_file(a,
d1,
dx1,
FS,
threshold,
file_input,
view_strings=False,
new=True,
library=True):
d2 = None
ret_type = androconf.is_android(file_input)
if ret_type == "APK":
a = apk.APK(file_input)
d2 = dvm.DalvikVMFormat(a.get_dex())
elif ret_type == "DEX":
d2 = dvm.DalvikVMFormat(read(file_input))
if d2 == None:
return
dx2 = analysis.VMAnalysis(d2)
el = elsim.Elsim(ProxyDalvik(d1, dx1),
ProxyDalvik(d2, dx2),
FS,
threshold,
options.compressor,
libnative=library)
el.show()
print "\t--> methods: %f%% of similarities" % el.get_similarity_value(new)
if options.display:
print "SIMILAR methods:"
diff_methods = el.get_similar_elements()
for i in diff_methods:
el.show_element(i)
print "IDENTICAL methods:"
new_methods = el.get_identical_elements()
for i in new_methods:
el.show_element(i)
print "NEW methods:"
new_methods = el.get_new_elements()
for i in new_methods:
el.show_element(i, False)
print "DELETED methods:"
del_methods = el.get_deleted_elements()
for i in del_methods:
el.show_element(i)
print "SKIPPED methods:"
skipped_methods = el.get_skipped_elements()
for i in skipped_methods:
el.show_element(i)
if view_strings:
els = elsim.Elsim(ProxyDalvikStringMultiple(d1, dx1),
ProxyDalvikStringMultiple(d2, dx2),
FILTERS_DALVIK_SIM_STRING,
threshold,
options.compressor,
libnative=library)
#els = elsim.Elsim( ProxyDalvikStringOne(d1, dx1),
# ProxyDalvikStringOne(d2, dx2), FILTERS_DALVIK_SIM_STRING, threshold, options.compressor, libnative=library )
els.show()
print "\t--> strings: %f%% of similarities" % els.get_similarity_value(
new)
if options.display:
print "SIMILAR strings:"
diff_strings = els.get_similar_elements()
for i in diff_strings:
els.show_element(i)
print "IDENTICAL strings:"
new_strings = els.get_identical_elements()
for i in new_strings:
els.show_element(i)
print "NEW strings:"
new_strings = els.get_new_elements()
for i in new_strings:
els.show_element(i, False)
print "DELETED strings:"
del_strings = els.get_deleted_elements()
for i in del_strings:
els.show_element(i)
print "SKIPPED strings:"
skipped_strings = els.get_skipped_elements()
for i in skipped_strings:
els.show_element(i)
def check_one_directory(a,
d1,
dx1,
FS,
threshold,
directory,
view_strings=False,
new=True,
library=True):
for root, dirs, files in os.walk(directory, followlinks=True):
if files != []:
for f in files:
real_filename = root
if real_filename[-1] != "/":
real_filename += "/"
real_filename += f
print "filename: %s ..." % real_filename
check_one_file(a, d1, dx1, FS, threshold, real_filename,
view_strings, new, library)
############################################################
def main(options, arguments):
if options.input != None:
a = None
ret_type = androconf.is_android(options.input[0])
if ret_type == "APK":
a = apk.APK(options.input[0])
d1 = dvm.DalvikVMFormat(a.get_dex())
elif ret_type == "DEX":
d1 = dvm.DalvikVMFormat(read(options.input[0]))
dx1 = analysis.VMAnalysis(d1)
threshold = None
if options.threshold != None:
threshold = float(options.threshold)
FS = FILTERS_DALVIK_SIM
FS[elsim.FILTER_SKIPPED_METH].set_regexp(options.exclude)
FS[elsim.FILTER_SKIPPED_METH].set_size(options.size)
new = True
if options.new != None:
new = False
library = True
if options.library != None:
library = options.library
if options.library == "python":
library = False
if os.path.isdir(options.input[1]) == False:
check_one_file(a, d1, dx1, FS, threshold, options.input[1],
options.xstrings, new, library)
else:
check_one_directory(a, d1, dx1, FS, threshold, options.input[1],
options.xstrings, new, library)
elif options.version != None:
print "Androsim version %s" % androconf.ANDROGUARD_VERSION
if __name__ == "__main__":
parser = OptionParser()
for option in options:
param = option['name']
del option['name']
parser.add_option(*param, **option)
options, arguments = parser.parse_args()
sys.argv[:] = arguments
main(options, arguments)