-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathcsharp_module_maker.py
More file actions
643 lines (448 loc) · 21.1 KB
/
csharp_module_maker.py
File metadata and controls
643 lines (448 loc) · 21.1 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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
import sys
import argparse
import os
import json
import re
import base64
from antlr4 import *
from module_maker.CSharpLexer import CSharpLexer
from module_maker.CSharpParser import CSharpParser
from module_maker.CSharpParserListener import CSharpParserListener
from module_maker.symbol_ini import SymbolINI
from jinja2 import Environment, FileSystemLoader
from pathlib import Path
from core.dispatcher_cli import Dispatcher
OMODULE_TYPES = [
'decrypter',
'dkey',
'executor',
'postmodule',
'premodule',
'runner',
]
IMODULE_TYPES = [
'crypter',
'ekey',
]
MUTATOR_TYPES = [
'mutator',
]
MODULE_DIRECTIONS = [
'output',
'input',
]
def display_xmodule_choices(xmodule_choices):
print('[!] The following are valid choices:')
print()
for m in xmodule_choices:
print(f'{m}')
print()
def cli():
parser = argparse.ArgumentParser()
interface_choices = Dispatcher.get_choices('./modules/interfaces', 'MRunnerInterface')
decrypter_imodule_choices = Dispatcher.get_choices('./modules/input/crypters', 'MCrypter')
dkey_imodule_choices = Dispatcher.get_choices('./modules/input/ekeys', 'MEKey')
crypter_omodule_choices = Dispatcher.get_choices('./modules/output/decrypters', 'MDecrypter')
ekey_omodule_choices = Dispatcher.get_choices('./modules/output/dkeys', 'MDKey')
module_attributes_group = parser.add_argument_group('Module Attributes')
parser.add_argument('--debug',
dest='debug',
action='store_true',
help='Show debug output.')
parser.add_argument('--force',
dest='force',
action='store_true',
help='Overwrite existing modules')
parser.add_argument('--check-mods-exists',
dest='validate_compatibility',
action='store_true',
help='Ensure that compatible modules actually exist')
module_attributes_group.add_argument('--type',
dest='mtype',
type=str,
required=False,
default=None,
choices=IMODULE_TYPES+OMODULE_TYPES,
help='Specify the type of module you wish to create (--create mode only).')
module_attributes_group.add_argument('--compatible-interfaces',
dest='mcompatible_interfaces',
type=str,
required=False,
nargs='*',
default=[],
help='Specify the compatible interfaces for your module')
module_attributes_group.add_argument('--compatible-imodules',
dest='mcompatible_imodules',
type=str,
required=False,
nargs='*',
default=[],
help='Specify compatible input modules for new output module. ')
module_attributes_group.add_argument('--compatible-omodules',
dest='mcompatible_omodules',
type=str,
required=False,
nargs='*',
default=[],
help='Specify compatible output modules for new input module. ')
module_attributes_group.add_argument('--name',
dest='mname',
type=str,
required=False,
default=None,
help='Specify the name of your new module.')
module_attributes_group.add_argument('--author',
dest='mauthor',
type=str,
required=False,
default='',
help='Specify the author of your new module.')
module_attributes_group.add_argument('--description',
dest='mdescription',
type=str,
required=False,
default='',
help='Specify the description of your new module.')
template_vars_group = parser.add_argument_group('Template Vars')
template_vars_group.add_argument('--class-name',
dest='mclass_name',
type=str,
required=False,
default=None,
help='Specify the class name of your template code.')
template_vars_group.add_argument('--func-name',
dest='mfunc_name',
type=str,
required=False,
default=None,
help='Specify the entrypoint function to your code.')
template_vars_group.add_argument('--symbol-file',
dest='symbol_file',
type=str,
required=False,
help='Load vars, methods, params, delegates, and imports from INI file')
template_vars_group.add_argument('--source-file',
dest='source_file',
type=str,
required=False,
help='Raw CSharp source file from which to create template')
args = parser.parse_args()
if args.mtype:
if args.mtype in IMODULE_TYPES:
if args.mcompatible_imodules != []:
print()
print(f'[!] The --compatible-imodules flag should is '
'incompatible with module of type {args.mtype}. '
'Please use the --compatible-omodules flag '
'instead')
print()
sys.exit(1)
if args.mtype == 'crypter' and args.validate_compatibility:
for m in args.mcompatible_imodules:
if m not in crypter_omodule_choices:
print()
print(f'[!] Module "{m} specified as compatible '
f'output module but no module named "{m}" '
f'found.')
print()
display_xmodule_choices(crypter_omodule_choices)
sys.exit(1)
elif args.mtype == 'ekey' and args.validate_compatibility:
for m in args.mcompatible_omodules:
if m not in ekey_omodule_choices:
print()
print(f'[!] Module "{m} specified as compatible '
f'output module but no module named "{m}" '
f'found.')
print()
display_xmodule_choices(ekey_omodule_choices)
sys.exit(1)
args.mcompatible_xmodules = args.mcompatible_omodules
elif args.mtype in OMODULE_TYPES:
if args.mcompatible_omodules != []:
print()
print(f'[!] The --compatible-omodules flag should is '
f'incompatible with module of type {args.mtype}. '
f'Please use the --compatible-imodules flag '
f'instead')
print()
sys.exit(1)
if args.mclass_name is None:
print()
print(f'[!] The --class-name flag must be used with all output modules.')
print()
sys.exit(1)
if args.mfunc_name is None:
print()
print(f'[!] The --func-name flag must be used with all output modules.')
print()
sys.exit(1)
if args.mtype == 'decrypter' and args.validate_compatibility:
for m in args.mcompatible_imodules:
if m not in decrypter_imodule_choices:
print()
print(f'[!] Module "{m}" specified as compatible '
f'input module but no module named "{m}" '
f'found.')
print()
display_xmodule_choices(decrypter_imodule_choices)
sys.exit(1)
elif args.mtype == 'dkey' and args.validate_compatibility:
for m in args.mcompatible_imodules:
if m not in dkey_imodule_choices:
print()
print(f'[!] Module "{m} specified as compatible '
f'input module but no module named "{m}" '
f'found.')
print()
display_xmodule_choices(dkey_imodule_choices)
sys.exit(1)
args.mcompatible_xmodules = args.mcompatible_imodules
elif args.mtype in MUTATOR_TYPES:
if args.mcompatible_imodules != []:
print()
print(f'[!] Modules of type {args.mtype} should not have a '
f'corresponding input module, and as such the '
f'--compatible-imodules flag shuold not be used.')
print()
elif args.mcompatible_omodules != []:
print()
print(f'[!] Modules of type {args.mtype} should not have a '
f'corresponding output module, and as such the '
f'--compatible-omodules flag shuold not be used.')
print()
if args.validate_compatibility:
for i in args.mcompatible_interfaces:
if i not in interface_choices:
print()
print(f'[!] Interface "{i} specified as compatible '
f'interface but no interface named "{i}" '
f'found.')
print()
display_xmodule_choices(interface_choices)
# check to make sure module of name mname does not already exist
if not args.force:
if args.mtype == 'mutator':
if args.mname in Dispatcher.get_choices('./modules/mutators', 'MMutator'):
print(f'[!] Error: {args.mtype} module of name {args.mname} already exists')
sys.exit(1)
elif args.mtype == 'decrypter':
if args.mname in Dispatcher.get_choices('./modules/output/decrypters', 'MDecrypter'):
print(f'[!] Error: {args.mtype} module of name {args.mname} already exists')
sys.exit(1)
elif args.mtype == 'dkey':
if args.mname in Dispatcher.get_choices('./modules/output/dkeys', 'MDKey'):
print(f'[!] Error: {args.mtype} module of name {args.mname} already exists')
sys.exit(1)
elif args.mtype == 'executor':
if args.mname in Dispatcher.get_choices('./modules/output/executors', 'MExecutor'):
print(f'[!] Error: {args.mtype} module of name {args.mname} already exists')
sys.exit(1)
elif args.mtype == 'postmodules':
if args.mname in Dispatcher.get_choices('./modules/output/postmodules', 'MPostModule'):
print(f'[!] Error: {args.mtype} module of name {args.mname} already exists')
sys.exit(1)
elif args.mtype == 'premodule':
if args.mname in Dispatcher.get_choices('./modules/output/premodules', 'MPreModule'):
print(f'[!] Error: {args.mtype} module of name {args.mname} already exists')
sys.exit(1)
elif args.mtype == 'runners':
if args.mname in Dispatcher.get_choices('./modules/output/runners', 'MRunner'):
print(f'[!] Error: {args.mtype} module of name {args.mname} already exists')
sys.exit(1)
elif args.mtype == 'crypter':
if args.mname in Dispatcher.get_choices('./modules/input/crypters', 'MCrypter'):
print(f'[!] Error: {args.mtype} module of name {args.mname} already exists')
sys.exit(1)
elif args.mtype == 'ekey':
if args.mname in Dispatcher.get_choices('./modules/input/ekeys', 'MEKey'):
print(f'[!] Error: {args.mtype} module of name {args.mname} already exists')
sys.exit(1)
# ensure input files actually exist and are not directories
if args.mtype in OMODULE_TYPES:
if not os.path.exists(args.source_file):
print(f'[!] Error: source file {args.source_file} does not exist')
sys.exit(1)
if not os.path.isfile(args.source_file):
print(f'[!] Error: source file {args.source_file} is a directory')
sys.exit(1)
if not os.path.exists(args.symbol_file):
print(f'[!] Error: symbol file {args.symbol_file} does not exist')
sys.exit(1)
if not os.path.isfile(args.symbol_file):
print(f'[!] Error: symbol file {args.symbol_file} is a directory')
sys.exit(1)
return args
def replace_symbols_in_line(symbol, base64_strs, sub_val, line):
b64_sub_val = base64.b64encode(sub_val.encode()).decode()
base64_strs[sub_val] = b64_sub_val
# base64_strs will be updated, since all variables in python
# are passed by reference. however, symbol is a string, and
# as such is immutable, therefore we need to return it.
return line.replace(symbol, b64_sub_val)
def namespace_in_imports(line, imports):
namespace = line[len('using'):].strip().rstrip(';').strip()
return namespace in imports
def sub_file_symbols(input_handle,
func_name='',
class_name='',
_vars=[],
methods=[],
params=[],
class_decls=[],
delegates=[],
imports=[]):
base64_strs = {}
output_lines = []
for line in input_handle:
if line.startswith('using'):
if namespace_in_imports(line, imports):
continue
for v in _vars:
line = replace_symbols_in_line(v,
base64_strs,
'{{ v[\''+v+'\'] }}',
line)
for v in methods:
if v == func_name:
line = replace_symbols_in_line(v,
base64_strs,
'{{ func_name }}',
line)
else:
line = replace_symbols_in_line(v,
base64_strs,
'{{ v[\''+v+'\'] }}',
line)
for v in class_decls:
if v == class_name:
line = replace_symbols_in_line(v,
base64_strs,
'{{ special[\'class_name\'] }}',
line)
else:
line = replace_symbols_in_line(v,
base64_strs,
'{{ v[\''+v+'\'] }}',
line)
for v in params:
line = replace_symbols_in_line(v,
base64_strs,
'{{ v[\''+v+'\'] }}',
line)
for v in delegates:
line = replace_symbols_in_line(v,
base64_strs,
'{{ v[\''+v+'\'] }}',
line)
output_lines.append(line)
raw_output = ''.join(output_lines)
for sub_val,b64_sub_val in base64_strs.items():
raw_output = raw_output.replace(b64_sub_val, sub_val)
return raw_output
def create(args):
if args.debug:
print(json.dumps(args.__dict__, indent=4, sort_keys=True))
# set vars from args
mname = args.mname
mtype = args.mtype
mauthor = args.mauthor
mdescription = args.mdescription
compatible_interfaces = args.mcompatible_interfaces
compatible_xmodules = args.mcompatible_xmodules
class_name = args.mclass_name
func_name = args.mfunc_name
symbol_file = args.symbol_file
source_file = args.source_file
if mtype in OMODULE_TYPES:
module_direction = 'output'
if symbol_file is not None:
# set vars from symbol ini
s_ini = SymbolINI(symbol_file)
_vars = s_ini.vars()
methods = s_ini.method_names()
class_decls = s_ini.class_decls()
params = s_ini.formal_params()
delegates = s_ini.delegates()
imports = s_ini.imports()
_vars.sort(key=lambda item: (-len(item), item))
methods.sort(key=lambda item: (-len(item), item))
class_decls.sort(key=lambda item: (-len(item), item))
params.sort(key=lambda item: (-len(item), item))
delegates.sort(key=lambda item: (-len(item), item))
imports.sort(key=lambda item: (-len(item), item))
else:
_vars = []
methods = []
params = []
class_decls = []
delegates = []
imports = []
if args.debug:
print('_vars:', _vars)
print('methods:', methods)
print('params:', params)
print('delegates:', delegates)
print('imports:', imports)
# calculate template path
output_cs_template_path = os.path.join(f'{mtype}s', f'{mname}.cs')
input_py_template_path = os.path.join('module_maker', f'mm_{mtype}.py')
output_py_template_path = f'modules/output/{mtype}s/{mname}.py'
if args.debug:
print('output_cs_template_path :', output_cs_template_path)
print('input_py_template_path :', input_py_template_path)
print('output_py_template_path :', output_py_template_path)
env = Environment(loader=FileSystemLoader('templates'))
template = env.get_template(input_py_template_path)
rendered_template = template.render(name=mname,
author=mauthor,
description=mdescription,
compatible_interfaces=compatible_interfaces,
compatible_xmodules=compatible_xmodules,
_vars=_vars+methods+params+delegates,
imports=imports,
template_path=output_cs_template_path,
func_name=func_name,
class_name=class_name)
with open(output_py_template_path, 'w') as output_handle:
output_handle.write(rendered_template+'\n')
print(f'[*] Saved new module to path: {output_py_template_path}')
if source_file is not None:
with open(source_file) as input_handle:
raw_output = sub_file_symbols(input_handle,
func_name=func_name,
class_name=class_name,
_vars=_vars,
methods=methods,
params=params,
class_decls=class_decls,
delegates=delegates,
imports=imports)
output_file = os.path.join('templates', output_cs_template_path)
with open(output_file, 'w') as output_handle:
output_handle.write(raw_output)
print(f'[*] Saved new template to path: {output_file}')
else:
module_direction = 'input'
# calculate template path
input_py_template_path = os.path.join('module_maker', f'mm_{mtype}.py')
output_py_template_path = f'modules/input/{mtype}s/{mname}.py'
if args.debug:
print('input_py_template_path :', input_py_template_path)
print('output_py_template_path :', output_py_template_path)
env = Environment(loader=FileSystemLoader('templates'))
template = env.get_template(input_py_template_path)
rendered_template = template.render(name=mname,
author=mauthor,
description=mdescription,
compatible_interfaces=compatible_interfaces,
compatible_xmodules=compatible_xmodules)
with open(output_py_template_path, 'w') as output_handle:
output_handle.write(rendered_template+'\n')
print(f'[*] Saved new module to path: {output_py_template_path}')
def main():
args = cli()
create(args)
if __name__ == '__main__':
cli()
main()