This repository has been archived by the owner on Mar 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
metaengine.py
349 lines (333 loc) · 16.3 KB
/
metaengine.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
# SPDX-License-Identifier: GPL-3.0-or-later
# metangine - metamorphic engine.
# Copyright (C) 2021-2023 Sergio Chica Manjarrez.
# This file is part of pymetangine.
# pymetangine is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# pymetangine 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 General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
from itertools import combinations
import keystone as ks
import random
import re
class MetaEngine:
def __init__(self, bits):
self.ks = ks.Ks(ks.KS_ARCH_X86,
ks.KS_MODE_32 if bits == 32 else
ks.KS_MODE_64)
self.bits = bits
self._ins = 0
self.gen_regexp()
@property
def ins(self):
return self._ins
def ins_inc(self):
self._ins = self._ins + 1
def gen_bytes(self, code):
asm, _ = self.ks.asm(code)
return ''.join(['{:02x}'.format(ins) for ins in asm])
def gen_nops(self, size):
reg = random.choice(self.regs)
if size == 1:
return 'nop'
if self.bits == 32:
if size == 2:
choice = random.randint(1, 4)
if choice == 1:
return 'pushfd; popfd'
elif choice == 2:
return 'pushad; popad'
elif choice == 3:
return 'push {}; pop {}'.format(reg, reg)
else:
return '{}; {}'.format(self.gen_nops(1),
self.gen_nops(1))
elif size == 3:
choice = random.randint(1, 3)
rnd_ins_1B = random.choice(
['pushfd', 'popfd', 'pushad', 'popad',
'push {}', 'pop {}', 'inc {}', 'dec {}'])
if choice == 1:
return 'jmp {}; {}'.format(3, rnd_ins_1B.format(reg))
elif choice == 2:
return '{}; {}'.format(self.gen_nops(1),
self.gen_nops(2))
else:
return '{}; {}'.format(self.gen_nops(2),
self.gen_nops(1))
elif self.bits == 64:
if size == 2:
choice = random.randint(1, 3)
if choice == 1:
return 'pushfq; popfq'
elif choice == 2:
return 'push {}; pop {}'.format(reg, reg)
else:
return '{}; {}'.format(self.gen_nops(1),
self.gen_nops(1))
elif size == 3:
choice = random.randint(1, 4)
# nop; push; pop already in case no-op_1B; no-op_2B
# push; pop; nop already in case no-op_2B; no-op_1B
# nop; pushfq; popfq already in case no-op_1B; no-op_2B
# pushfq; popfq; nop already in case no-op_2B; no-op_1B
if choice == 1:
return 'push {0}; {1}; pop {0}'.format(
reg, self.gen_nops(1))
elif choice == 2:
return 'pushfq; {}; popfq'.format(self.gen_nops(1))
elif choice == 3:
return '{}; {}'.format(self.gen_nops(1), self.gen_nops(2))
else:
return '{}; {}'.format(self.gen_nops(2), self.gen_nops(1))
elif size == 4:
choice = random.randint(1, 2)
if choice == 1:
rnd_ins_1B_x2 = random.sample(
['push {}', 'pop {}', 'pushfq', 'popfq'], 2)
rnd_ins_2B = random.choice(
['mov {}, {}', 'test {}, {}', 'cmp {}, {}',
'or {}, {}', 'sub {}, {}', 'inc {}',
'xor {}, {}', 'and {}, {}', 'dec {}'])
rnd = random.randint(1, 2)
if rnd == 1:
reg2 = random.choice(self.regs)
rnd_ins = rnd_ins_1B_x2.format(reg, reg2)
else:
reg_32b = ['eax', 'ebx', 'ecx', 'edx', 'esi', 'edi']
rnd_ins = rnd_ins_2B.format(random.choice(reg_32b))
return 'jmp {}; {}'.format(4, rnd_ins)
else:
return '{}; {}'.format(self.gen_nops(2),
self.gen_nops(2))
def gen_regexp(self):
self.mutable_ins = frozenset(
['nop', 'upush', 'mov', 'acmp', 'or', 'xor', 'sub']
) if self.bits == 32 else frozenset(
['nop', 'mov', 'acmp', 'or', 'xor', 'sub'])
# upush -> push reg | acmp -> test reg, reg
self.regs = (['eax', 'ebx', 'ecx', 'edx', 'esi', 'edi']
if self.bits == 32 else
['rax', 'rbx', 'rcx', 'rdx', 'rsi', 'rdi'])
# for some reason these registers crash the program:
# r8, r9, r10, r11, r12, r13, r14, r15
self.nop_1B = re.compile(r'nop$')
self.push_1B = re.compile(r'push (e..)$')
self.pop_1B = re.compile(r'pop (e..)$')
self.mov_2B = re.compile(r'mov (e..), (e..)$')
self.test_2B = re.compile(r'test (e..), ((?=\1)...)$')
self.or_2B = re.compile(r'or (e..), ((?=\1)...)$')
self.xor_2B = re.compile(r'xor (e..), ((?=\1)...)$')
self.sub_2B = re.compile(r'sub (e..), ((?=\1)...)$')
self.mov_5B = re.compile(
r'mov (e..), (0?x?(?:[0-7][\dA-Fa-f]|[\dA-Fa-f]))$')
if self.bits == 64:
self.mov_3B = re.compile(r'mov (r.[ixp]), (r.[ixp])$')
self.test_3B = re.compile(
r'test (r[a-ds189][ixp0-5]), ((?=\1)...)$')
self.or_3B = re.compile(r'or (r[a-ds189][ixp0-5]), ((?=\1)...)$')
self.xor_3B = re.compile(r'xor (r[a-ds189][ixp0-5]), ((?=\1)...)$')
self.sub_3B = re.compile(r'sub (r[a-ds189][ixp0-5]), ((?=\1)...)$')
def gen_permutation(self, ins1, ins2):
res = []
for locations in combinations(range(len(ins1) + len(ins2)), len(ins2)):
out = ins1[:]
for location, element in zip(locations, ins2):
out.insert(location, element)
res.append('; '.join(map(str, out)))
return random.choice(res)
def gen_mutations(self, ins_list, idx):
if ins_list[idx]['size'] == 1:
m1 = self.nop_1B.match(ins_list[idx]['opcode'])
if m1 is not None:
if (idx + 1 < len(ins_list) and
ins_list[idx + 1]['type'] != 'invalid'): # noqa
m2 = self.nop_1B.match(ins_list[idx + 1]['opcode'])
if m2 is not None:
if (idx + 2 < len(ins_list) and
ins_list[idx + 2]['type'] != 'invalid'): # noqa
m3 = self.nop_1B.match(ins_list[idx + 2]['opcode'])
if m3 is not None: # nop; nop; nop
# flip a coin to change ins
rnd = random.randint(1, 2)
if rnd == 1:
# equivalent instruction
return '{}'.format(self.gen_nops(3)), 3
else:
# original instruction
return '', 3
else: # nop; nop
rnd = random.randint(1, 2)
if rnd == 1:
return '{}'.format(self.gen_nops(2)), 2
else:
return '', 2
m1 = self.push_1B.match(ins_list[idx]['opcode'])
if m1 is not None:
if (idx + 1 < len(ins_list) and
ins_list[idx + 1]['type'] != 'invalid'): # noqa
m2 = self.pop_1B.match(ins_list[idx + 1]['opcode'])
if m2 is not None: # push reg1; pop reg2
rnd = random.randint(1, 2)
if rnd == 1: # mov reg2, reg1
return 'mov {}, {}'.format(
m2.group(1), m1.group(1)), 2
else:
return '', 2
return None
if ins_list[idx]['size'] == 2:
m1 = self.mov_2B.match(ins_list[idx]['opcode'])
if m1 is not None:
if m1.group(1) == m1.group(2): # mov reg1, reg1
rnd = random.randint(1, 2)
if rnd == 1: # no-op; no-op
return '{}'.format(self.gen_nops(2)), 2
else: # mov reg1, reg1
return '', 2
elif (m1.group(1) != m1.group(2) and
self.bits == 32): # mov reg2, reg1
rnd = random.randint(1, 2)
if rnd == 1: # push reg1; pop reg2
return 'push {}; pop {}'.format(
m1.group(2), m1.group(1)), 2
else: # mov reg2, reg1
return '', 2
m1 = self.test_2B.match(ins_list[idx]['opcode'])
if m1 is not None: # test reg1, reg1
rnd = random.randint(1, 2)
if rnd == 1: # or reg1, reg1
return 'or {0}, {0}'.format(m1.group(1)), 2
else: # test reg1, reg1
return '', 2
m1 = self.or_2B.match(ins_list[idx]['opcode'])
if m1 is not None: # or reg1, reg1
rnd = random.randint(1, 2)
if rnd == 1: # test reg1, reg1
return 'test {0}, {0}'.format(m1.group(1)), 2
else: # or reg1, reg1
return '', 2
m1 = self.xor_2B.match(ins_list[idx]['opcode'])
if m1 is not None: # xor reg1, reg1
rnd = random.randint(1, 2)
if rnd == 1: # sub reg1, reg1
return 'sub {0}, {0}'.format(m1.group(1)), 2
else: # xor reg1, reg1
return '', 2
m1 = self.sub_2B.match(ins_list[idx]['opcode'])
if m1 is not None: # sub reg1, reg1
rnd = random.randint(1, 2)
if rnd == 1: # xor reg1, reg1
return 'xor {0}, {0}'.format(m1.group(1)), 2
else: # sub reg1, reg1
return '', 2
return None
if self.bits == 64:
if ins_list[idx]['size'] == 3:
m1 = self.mov_3B.match(ins_list[idx]['opcode'])
if m1 is not None: # mov reg1, reg2
rnd = random.randint(1, 2)
if rnd == 1: # no-op; push reg2; pop reg1
ins_pair1 = ['{0}']
ins_pair2 = ['push {1}', 'pop {2}']
return (self.gen_permutation(ins_pair1, ins_pair2)
.format(self.gen_nops(1),
m1.group(2), m1.group(1)), 3)
else:
return '', 3
m1 = self.test_3B.match(ins_list[idx]['opcode'])
if m1 is not None: # test reg1, reg1
rnd = random.randint(1, 2)
if rnd == 1: # or reg1, reg1
return 'or {0}, {0}'.format(m1.group(1)), 3
else: # test reg1, reg1
return '', 3
m1 = self.or_3B.match(ins_list[idx]['opcode'])
if m1 is not None: # or reg1, reg1
rnd = random.randint(1, 2)
if rnd == 1: # test reg1, reg1
return 'test {0}, {0}'.format(m1.group(1)), 3
else: # or reg1, reg1
return '', 3
m1 = self.xor_3B.match(ins_list[idx]['opcode'])
if m1 is not None: # xor reg1, reg1
rnd = random.randint(1, 2)
if rnd == 1: # sub reg1, reg1
return 'sub {0}, {0}'.format(m1.group(1)), 3
else: # xor reg1, reg1
return '', 3
m1 = self.sub_3B.match(ins_list[idx]['opcode'])
if m1 is not None: # sub reg1, reg1
rnd = random.randint(1, 2)
if rnd == 1: # xor reg1, reg1
return 'xor {0}, {0}'.format(m1.group(1)), 3
else: # sub reg1, reg1
return '', 3
return None
if self.bits == 32:
if ins_list[idx]['size'] == 5:
m1 = self.mov_5B.match(ins_list[idx]['opcode'])
if m1 is not None:
if m1.group(2) in ['0x1', '1']: # mov reg1, 1
rnd = random.randint(0, 3)
elif m1.group(2) in ['0x0', '0']: # mov reg1, 0
rnd = random.randint(3, 6)
else: # mov reg1, imm
rnd = random.randint(1, 3)
if rnd == 0:
# permutations, keeping order
# (push before pop, xor before inc)
# of --> pushfd; xor reg1, reg1; inc reg1; popfd
ins_pair1 = ['pushfd', 'popfd']
ins_pair2 = ['xor {0}, {0}', 'inc {0}']
return (self.gen_permutation(ins_pair1, ins_pair2)
.format(m1.group(1)), 5)
elif rnd == 1:
# permutations, keeping order
# (push before pop)
# of --> push imm; pop reg; no-op
ins_pair1 = ['{0}']
ins_pair2 = ['push {1}', 'pop {2}']
return (self.gen_permutation(ins_pair1, ins_pair2)
.format(self.gen_nops(2),
m1.group(2), m1.group(1)), 5)
elif rnd == 2:
# permutations, keeping order
# (push before pop)
# of --> push imm; pop reg; no-op; no-op
ins_pair1 = ['{0}', '{1}']
ins_pair2 = ['push {2}', 'pop {3}']
return (self.gen_permutation(ins_pair1, ins_pair2)
.format(self.gen_nops(1),
self.gen_nops(1),
m1.group(2), m1.group(1)), 5)
elif rnd == 3:
return '', 5
elif rnd == 4:
# permutations, keeping order
# (push before xor before pop,
# so flags are not modified)
# of --> pushfd; xor reg1, reg1; popfd; no-op
ins_pair1 = ['pushfd', 'xor {0}, {0}', 'popfd']
ins_pair2 = ['{1}']
return (self.gen_permutation(ins_pair1, ins_pair2)
.format(m1.group(1), self.gen_nops(1)), 5)
elif rnd == 5:
# permutations, keeping order
# (push before sub before pop,
# so flags are not modified)
# of --> pushfd; sub reg1, reg1; popfd; no-op
ins_pair1 = ['pushfd', 'sub {0}, {0}', 'popfd']
ins_pair2 = ['{1}']
return (self.gen_permutation(ins_pair1, ins_pair2)
.format(m1.group(1), self.gen_nops(1)), 5)
else:
# pushfd; and reg1, 0; popfd
return ('pushfd; and {}, 0; popfd'
.format(m1.group(1)), 5)
return None