-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest_CrimsonUroboros.py
2894 lines (2288 loc) · 106 KB
/
test_CrimsonUroboros.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
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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import sys
import lief
import plistlib
from uuid import UUID
from io import StringIO
from CrimsonUroboros import *
# HOW TO TEST
'''
Each TestSnake class should have a method for each option in the Snake class being tested.
During setup phase for each TestSnake class, we prepare tests samples and assert that they exists.
Then, we run the testing methods and assert the outputs for each CrimsonUroboros option.
Finally, we purge the test samples.
We do it for each TestSnake class.
.vscode/settings.json:
{
"python.testing.pytestArgs": [
".",
"--disable-warnings",
"-vv",
"--rootdir=.",
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true,
"python.testing.cwd": "${workspaceFolder}/tests",
"python.REPL.enableREPLSmartSend": false,
"python.testing.autoTestDiscoverOnSaveEnabled": true
}
'''
snake_class = SnakeX
class Compiler:
"""
A class for compiling C code using clang.
Usage:
compiler = Compiler()
compiler.compileIt("../I. Mach-O/custom/hello.c", "hello", ["-arch", "arm64"])
compiler.purgeCompiledFiles()
"""
def __init__(self):
self.compiled_files = [] # Stores the paths to the compiled files.
def compile(self, cmd):
"""
Compile C code using the given compile command.
Args:
cmd (str): The compile command to run.
Returns:
int: Return code of the compilation process.
"""
result = os.system(cmd)
def buildClangCommand(self, source, output, flags=None):
"""
Build a clang compile command string based on the source file, output file, and optional flags.
Args:
source (str): Path to the source file.
output (str): Path to the output file.
flags (list, optional): List of additional flags. Defaults to None.
Returns:
str: Compiled clang command string.
"""
cmd_parts = ["clang"]
cmd_parts.append(source)
cmd_parts.extend(["-o", output])
if flags:
cmd_parts.extend(flags)
return ' '.join(cmd_parts)
def compileIt(self, source, output, flags=None):
"""
Compile the given source file using clang.
Args:
source (str): The path to the source file.
output (str): The path to the output file.
flags (list, optional): Additional compilation flags. Defaults to None.
Returns:
int: The exit code of the compilation process.
"""
cmd = self.buildClangCommand(source, output, flags)
self.compile(cmd)
self.compiled_files.append(output)
def purgeCompiledFiles(self):
"""
Remove all compiled files.
"""
for file in self.compiled_files:
os.remove(file)
class CodeSigner:
"""
# Example usage:
code_signer = CodeSigner()
entitlements = {
"com.apple.security.app-sandbox": "true",
"com.apple.security.cs.allow-jit": "true"
}
certificate = '-'
code_signer.signBinary("../../../snake_apple/simple/hello", certificate, entitlements)
"""
def __init__(self):
self.signed_files = []
def writeEntitlementsToFile(self, entitlements, filename):
"""Write entitlements dictionary to a plist file."""
with open(filename, 'wb') as f:
plistlib.dump(entitlements, f)
def signBinary(self, binary_path, certificate_name=None, entitlements=None):
"""
Sign binary using codesign.
(optional) Add entitlements to a binary using codesign. Example:
entitlements = {
"com.apple.security.app-sandbox": "true",
"com.apple.security.cs.allow-jit": "true"
}
Parameters:
- binary_path: Path to the binary file.
- certificate_name (optional): Name of the certificate to sign the binary.
- entitlements (optional): Dictionary of entitlements to add to the binary.
"""
# Write entitlements to a plist file
if entitlements:
entitlements_file = "unpredictable_entitlements_1029384756.plist"
self.writeEntitlementsToFile(entitlements, entitlements_file)
else:
entitlements_file = None
# Construct the codesign command
command = " ".join([
"codesign",
"-f",
"--entitlements",
entitlements_file,
"-s",
certificate_name,
binary_path
])
# Execute the codesign command
os.system(command)
def argumentWrapper(args_list):
"""
Wrapper function to parse command line arguments.
Args:
args_list (list): List of command line arguments.
Returns:
tuple: A tuple containing the parsed arguments and the absolute file path.
"""
sys.argv[1:] = args_list # Example: ['-p', 'hello', '--file_type']
arg_parser = ArgumentParser()
args = arg_parser.parseArgs()
return args
def executeCodeBlock(func):
"""
Executes the provided function and captures its output.
Args:
func: The function to be executed.
Returns:
The captured output of the function.
"""
# Redirect stdout and stderr to capture the output
captured_output = StringIO()
sys.stdout = sys.stderr = captured_output
# Execute the provided function
func()
# Restore stdout and stderr
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
# Get the captured output
output = captured_output.getvalue().strip()
return output
def decompressKernelcache():
command = 'ipsw kernel dec $(ls /System/Volumes/Preboot/*/boot/*/System/Library/Caches/com.apple.kernelcaches/kernelcache) -o kernelcache'
return os.system(command)
def run_and_get_stdout(command):
command_with_stdout = f"{command} 2>&1"
# Run the command and capture the output in bytes
result = subprocess.run(command_with_stdout, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
# Decode with utf-8, ignoring invalid characters or replacing them
return result.stdout.decode('utf-8', errors='replace').strip()
class TestSnakeAppBundleExtension():
'''Testing App Bundle Extension'''
@classmethod
def setup_class(cls):
# Prepare bare_bone.app for test_bundle_structure
os.system("../App\ Bundle\ Extension/custom/make_bundle.sh")
assert os.path.exists("bare_bone.app")
@classmethod
def teardown_class(cls):
# Remove bare_bone.app for test_bundle_structure
os.system("rm -rf bare_bone.app")
assert not os.path.exists("bare_bone.app")
def test_bundle_structure(self):
'''Test the --bundle_structure flag of SnakeI.'''
args_list = ['-b', 'bare_bone.app', '--bundle_structure']
args = argumentWrapper(args_list)
def code_block():
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
uroboros_output = executeCodeBlock(code_block)
expected_output_1 = 'bare_bone.app'
expected_output_2 = 'red_icon.icns'
assert expected_output_1 in uroboros_output
assert expected_output_2 in uroboros_output
def test_bundle_info(self):
'''Test the --bundle_structure flag of SnakeI.'''
args_list = ['-b', 'bare_bone.app', '--bundle_info']
args = argumentWrapper(args_list)
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
def code_block():
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
uroboros_output = executeCodeBlock(code_block)
expected_output = '"CFBundleExecutable": "bare_bone_exe",'
assert expected_output in uroboros_output
def test_bundle_info_syntax_check(self):
'''Test the --bundle_info_syntax_check flag of SnakeI.'''
args_list = ['-b', 'bare_bone.app', '--bundle_info_syntax_check']
args = argumentWrapper(args_list)
def code_block():
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
uroboros_output = executeCodeBlock(code_block)
expected_output = 'Valid Bundle Info.plist syntax'
assert expected_output in uroboros_output
def test_bundle_frameworks(self):
'''Test the --bundle_frameworks flag of SnakeI.'''
args_list = ['-b', 'bare_bone.app', '--bundle_frameworks']
args = argumentWrapper(args_list)
def code_block():
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
uroboros_output = executeCodeBlock(code_block)
expected_output = 'ClockOpen.framework'
assert expected_output in uroboros_output
def test_bundle_plugins(self):
'''Test the --bundle_plugins flag of SnakeI.'''
args_list = ['-b', 'bare_bone.app', '--bundle_plugins']
args = argumentWrapper(args_list)
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
def code_block():
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
uroboros_output = executeCodeBlock(code_block)
expected_output = 'No plugins found.'
assert expected_output in uroboros_output
class TestSnakeI():
'''Testing I. MACH-O'''
@classmethod
def setup_class(cls):
# Set up the compilation process
cls.compiler = Compiler()
cls.compiler.compileIt("../I.\ Mach-O/custom/hello.c", "hello_1", ["-arch", "arm64"])
assert os.path.exists("hello_1") # Check if the file exists after compilation
@classmethod
def teardown_class(cls):
# Purge the compiled files
cls.compiler.purgeCompiledFiles()
assert not os.path.exists("hello_1") # Check if the file is removed after purging
os.remove("test_bin")
assert not os.path.exists("test_bin") # Check if the file is removed after purging
def test_MachOProcessor(self):
'''Test the initialization of MachOProcessor.
This test checks if the snake_instance is created by using the MachOProcessor class.
It sets up arguments for testing, defines the code block to be executed, executes the code block,
and asserts that there are no errors by checking the output.
'''
# Set up arguments for testing
args_list = ['-p', 'hello_1']
args = argumentWrapper(args_list)
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
# Define the code block to be executed
def code_block():
macho_processor = MachOProcessor()
macho_processor.process(args)
# Execute the code block using the wrapper function and capture the output
uroboros_output = executeCodeBlock(code_block)
# Assert there are no errors by checking the output - when using only --path there is no output if file exists and is valid arm64 Mach-O file
expected_output = ''
assert uroboros_output == expected_output
def test_file_type(self):
'''Test the --file_type flag of SnakeI.'''
args_list = ['-p', 'hello_1', '--file_type']
args = argumentWrapper(args_list)
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
def code_block():
macho_processor = MachOProcessor()
macho_processor.process(args)
uroboros_output = executeCodeBlock(code_block)
expected_output = 'File type: EXECUTE'
assert uroboros_output == expected_output
def test_header_flags(self):
'''Test the --header_flags flag of SnakeI.'''
args_list = ['-p', 'hello_1', '--header_flags']
args = argumentWrapper(args_list)
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
def code_block():
macho_processor = MachOProcessor()
macho_processor.process(args)
uroboros_output = executeCodeBlock(code_block)
expected_output = 'Header flags: NOUNDEFS DYLDLINK TWOLEVEL PIE'
assert uroboros_output == expected_output
def test_endian(self):
'''Test the --endian flag of SnakeI.'''
args_list = ['-p', 'hello_1', '--endian']
args = argumentWrapper(args_list)
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
def code_block():
macho_processor = MachOProcessor()
macho_processor.process(args)
uroboros_output = executeCodeBlock(code_block)
expected_output = 'Endianess: little'
assert uroboros_output == expected_output
def test_header(self):
'''Test the --header flag of SnakeI.'''
args_list = ['-p', 'hello_1', '--header']
args = argumentWrapper(args_list)
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
def code_block():
macho_processor = MachOProcessor()
macho_processor.process(args)
uroboros_output = executeCodeBlock(code_block)
expected_output_1 = 'ARM64'
expected_output_2 = 'EXECUTE'
expected_output_3 = 'Flags: 2097285'
assert expected_output_1 in uroboros_output
assert expected_output_2 in uroboros_output
assert expected_output_3 in uroboros_output
def test_load_commands(self):
'''Test the --load_commands flag of SnakeI.'''
args_list = ['-p', 'hello_1', '--load_commands']
args = argumentWrapper(args_list)
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
def code_block():
macho_processor = MachOProcessor()
macho_processor.process(args)
uroboros_output = executeCodeBlock(code_block)
expected_output = 'Load Commands: SEGMENT_64 SEGMENT_64 SEGMENT_64 SEGMENT_64 DYLD_CHAINED_FIXUPS DYLD_EXPORTS_TRIE SYMTAB DYSYMTAB LOAD_DYLINKER UUID BUILD_VERSION SOURCE_VERSION MAIN LOAD_DYLIB FUNCTION_STARTS DATA_IN_CODE CODE_SIGNATURE'
assert expected_output in uroboros_output
def test_has_cmd(self):
'''Test the --has_cmd flag of SnakeI.'''
args_list = ['-p', 'hello_1', '--has_cmd', 'LC_MAIN']
args = argumentWrapper(args_list)
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
def code_block():
macho_processor = MachOProcessor()
macho_processor.process(args)
uroboros_output = executeCodeBlock(code_block)
expected_output = 'hello_1 has LC_MAIN'
assert expected_output in uroboros_output
def test_segments(self):
'''Test the --segments flag of SnakeI.'''
args_list = ['-p', 'hello_1', '--segments']
args = argumentWrapper(args_list)
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
def code_block():
macho_processor = MachOProcessor()
macho_processor.process(args)
uroboros_output = executeCodeBlock(code_block)
expected_output_1 = '__PAGEZERO ---/--- VM: 0x0000000000000000-0x0000000100000000 FILE: 0x0-0x0'
expected_output_2 = '__TEXT r-x/r-x VM: 0x0000000100000000-0x0000000100004000 FILE: 0x4000-0x8000'
expected_output_3 = '__DATA_CONST rw-/rw- VM: 0x0000000100004000-0x0000000100008000 FILE: 0x4000-0x8000 (SG_READ_ONLY)'
expected_output_4 = '__LINKEDIT r--/r-- VM: 0x0000000100008000-0x000000010000c000 FILE: 0x298-0x530'
assert expected_output_1 in uroboros_output
assert expected_output_2 in uroboros_output
assert expected_output_3 in uroboros_output
assert expected_output_4 in uroboros_output
def test_has_segment(self):
'''Test the --has_segment flag of SnakeI.'''
args_list = ['-p', 'hello_1', '--has_segment', '__TEXT']
args = argumentWrapper(args_list)
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
def code_block():
macho_processor = MachOProcessor()
macho_processor.process(args)
uroboros_output = executeCodeBlock(code_block)
expected_output = 'hello_1 has __TEXT'
assert expected_output in uroboros_output
def test_sections(self):
'''Test the --sections flag of SnakeI.'''
args_list = ['-p', 'hello_1', '--sections']
args = argumentWrapper(args_list)
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
def code_block():
macho_processor = MachOProcessor()
macho_processor.process(args)
uroboros_output = executeCodeBlock(code_block)
expected_output_1 = '__TEXT __text'
expected_output_2 = '__TEXT __stubs'
expected_output_3 = '__TEXT __cstring'
expected_output_4 = '__TEXT __unwind_info'
expected_output_5 = '__DATA_CONST __got'
assert expected_output_1 in uroboros_output
assert expected_output_2 in uroboros_output
assert expected_output_3 in uroboros_output
assert expected_output_4 in uroboros_output
assert expected_output_5 in uroboros_output
def test_has_section(self):
'''Test the --has_section flag of SnakeI.'''
args_list = ['-p', 'hello_1', '--has_section', '__TEXT,__text']
args = argumentWrapper(args_list)
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
def code_block():
macho_processor = MachOProcessor()
macho_processor.process(args)
uroboros_output = executeCodeBlock(code_block)
expected_output = 'hello_1 has __TEXT,__text'
assert expected_output in uroboros_output
def test_symbols(self):
'''Test the --symbols flag of SnakeI.'''
args_list = ['-p', 'hello_1', '--symbols']
args = argumentWrapper(args_list)
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
def code_block():
macho_processor = MachOProcessor()
macho_processor.process(args)
uroboros_output = executeCodeBlock(code_block)
expected_output_1 = '__mh_execute_header'
expected_output_2 = '_main'
expected_output_3 = '_printf'
assert expected_output_1 in uroboros_output
assert expected_output_2 in uroboros_output
assert expected_output_3 in uroboros_output
def test_imported_symbols(self):
'''Test the --imported_symbols flag of SnakeI.'''
args_list = ['-p', 'hello_1', '--imported_symbols']
args = argumentWrapper(args_list)
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
def code_block():
macho_processor = MachOProcessor()
macho_processor.process(args)
uroboros_output = executeCodeBlock(code_block)
expected_output = '_printf : /usr/lib/libSystem.B.dylib'
assert uroboros_output == expected_output
def test_chained_fixups(self):
'''Test the --chained_fixups flag of SnakeI.'''
args_list = ['-p', 'hello_1', '--chained_fixups']
args = argumentWrapper(args_list)
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
def code_block():
macho_processor = MachOProcessor()
macho_processor.process(args)
uroboros_output = executeCodeBlock(code_block)
expected_output = '_DATA_CONST0x100004000: _printf (libSystem.B.dylib) addend: 0x0'
assert expected_output in uroboros_output
def test_exports_trie(self):
'''Test the --exports_trie flag of SnakeI.'''
args_list = ['-p', 'hello_1', '--exports_trie']
args = argumentWrapper(args_list)
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
def code_block():
macho_processor = MachOProcessor()
macho_processor.process(args)
uroboros_output = executeCodeBlock(code_block)
expected_output = '_main{addr: 0x3f58, flags: 0}'
assert expected_output in uroboros_output
def test_uuid(self):
'''Test the --uuid flag of SnakeI.'''
args_list = ['-p', 'hello_1', '--uuid']
args = argumentWrapper(args_list)
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
def code_block():
macho_processor = MachOProcessor()
macho_processor.process(args)
uroboros_output = executeCodeBlock(code_block)
try:
is_valid = UUID(uroboros_output.split('UUID: ')[-1], version=4) # this returns the UUID string if it is valid and stores it in is_valid
except ValueError:
is_valid = False
assert is_valid
def test_main(self):
'''Test the --main flag of SnakeI.'''
args_list = ['-p', 'hello_1', '--main']
args = argumentWrapper(args_list)
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
def code_block():
macho_processor = MachOProcessor()
macho_processor.process(args)
uroboros_output = executeCodeBlock(code_block)
expected_output_1 = 'Entry point: 0x3f58'
expected_output_2 = 'Stack size: 0x0'
assert expected_output_1 in uroboros_output
assert expected_output_2 in uroboros_output
def test_encryption_info(self):
'''Test the --encryption_info flag of SnakeI.'''
args_list = ['-p', 'hello_1', '--encryption_info']
args = argumentWrapper(args_list)
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
def code_block():
macho_processor = MachOProcessor()
macho_processor.process(args)
uroboros_output = executeCodeBlock(code_block)
expected_output = 'hello_1 binary does not have encryption info.'
assert expected_output in uroboros_output
def test_strings_section(self):
'''Test the --strings_section flag of SnakeI.'''
args_list = ['-p', 'hello_1', '--strings_section']
args = argumentWrapper(args_list)
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
def code_block():
macho_processor = MachOProcessor()
macho_processor.process(args)
uroboros_output = executeCodeBlock(code_block)
expected_output_1 = 'Hello, World!'
expected_output_2 = '__cstring'
assert expected_output_1 in uroboros_output
assert expected_output_2 in uroboros_output
def test_all_strings(self):
'''Test the --all_strings flag of SnakeI.'''
args_list = ['-p', 'hello_1', '--all_strings']
args = argumentWrapper(args_list)
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
def code_block():
macho_processor = MachOProcessor()
macho_processor.process(args)
uroboros_output = executeCodeBlock(code_block)
expected_output_1 = 'Hello, World!'
assert expected_output_1 in uroboros_output
def test_save_strings(self):
'''Test the --save_strings flag of SnakeI.'''
args_list = ['-p', 'hello_1', '--save_strings', 'testing_strings.txt']
args = argumentWrapper(args_list)
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
def code_block():
macho_processor = MachOProcessor()
macho_processor.process(args)
uroboros_output = executeCodeBlock(code_block)
assert os.path.exists('testing_strings.txt')
with open('testing_strings.txt', 'r') as file:
file_output = file.read()
expected_output = 'Hello, World'
assert expected_output in file_output
os.remove('testing_strings.txt')
def test_info(self):
'''Test the --info flag of SnakeI.'''
args_list = ['-p', 'hello_1', '--info']
args = argumentWrapper(args_list)
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
def code_block():
macho_processor = MachOProcessor()
macho_processor.process(args)
uroboros_output = executeCodeBlock(code_block)
expected_output_1 = 'Entry point: 0x3f58'
expected_output_2 = '__mh_execute_header'
expected_output_3 = '__PAGEZERO'
expected_output_4 = '__DATA_CONST0x100004000'
expected_output_5 = 'Command: SEGMENT_64'
assert expected_output_1 in uroboros_output
assert expected_output_2 in uroboros_output
assert expected_output_3 in uroboros_output
assert expected_output_4 in uroboros_output
assert expected_output_5 in uroboros_output
def test_dump_data(self):
'''Test the --dump_data flag of SnakeI.'''
args_list = ['-p', 'hello_1', '--dump_data', '0x00,0x08,hello_1_header_dump.bin']
args = argumentWrapper(args_list)
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
def code_block():
macho_processor = MachOProcessor()
macho_processor.process(args)
executeCodeBlock(code_block)
assert os.path.exists('hello_1_header_dump.bin')
with open('hello_1_header_dump.bin', 'rb') as file:
file_output = file.read()
expected_output = b'\xcf\xfa\xed\xfe\x0c\x00\x00\x01'
assert expected_output in file_output
os.remove('hello_1_header_dump.bin')
def test_calc_offset(self):
'''Test the --calc_offset flag of SnakeI.'''
args_list = ['-p', 'hello_1', '--calc_offset', "0x0000000100003f20"]
args = argumentWrapper(args_list)
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
def code_block():
macho_processor = MachOProcessor()
macho_processor.process(args)
uroboros_output = executeCodeBlock(code_block)
expected_output = "0x0000000100003f20 : 0x3f20"
assert expected_output in uroboros_output
def test_constructors(self):
'''Test the --constructors flag of SnakeI.'''
args_list = ['-p', 'hello_1', '--constructors']
args = argumentWrapper(args_list)
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
def code_block():
macho_processor = MachOProcessor()
macho_processor.process(args)
uroboros_output = executeCodeBlock(code_block)
expected_output = ""
# todo - this is only negative test, I should also check the file with valid constructors.
assert expected_output in uroboros_output
def test_dump_section(self):
'''Test the --dump_section flag of SnakeI.'''
uroboros_output = run_and_get_stdout('python3 CrimsonUroboros.py -p hello_1 --dump_section "__TEXT,__cstring"')
expected_output = 'Hello, World!'
assert expected_output in uroboros_output
def test_dump_binary(self):
'''Test the --dump_binary flag of SnakeI.'''
args_list = ['-p', 'hello_1', '--dump_binary', 'test_bin']
args = argumentWrapper(args_list)
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
def code_block():
macho_processor = MachOProcessor()
macho_processor.process(args)
uroboros_output = executeCodeBlock(code_block)
expected_output = ''
assert expected_output in uroboros_output
assert os.path.exists('test_bin')
assert run_and_get_stdout('file test_bin') == 'test_bin: Mach-O 64-bit executable arm64'
class TestSnakeII():
'''Testing II. CODE SIGNING'''
@classmethod
def setup_class(cls):
# Set up the compilation process
cls.compiler = Compiler()
cls.compiler.compileIt("../III.\ Checksec/custom/hello.c", "hello_2", ["-arch", "arm64"])
assert os.path.exists("hello_2") # Check if the file exists after compilation
# Prepare signed binary for test_remove_sig
cls.compiler.compileIt("../III.\ Checksec/custom/hello.c", "hello_2_test_remove_sig", ["-arch", "arm64"])
assert os.path.exists("hello_2_test_remove_sig") # Check if the file exists after compilation
# Prepare unsigned binary for test_sign_binary
binary2 = lief.parse('hello_2')
binary2.remove_signature()
binary2.write('hello_2_unsigned_binary')
cls.compiler.compiled_files.append("hello_2_unsigned_binary")
assert os.path.exists("hello_2_unsigned_binary")
# Code sign and set entitlements
cls.code_signer = CodeSigner()
cls.entitlements = {
"com.apple.security.app-sandbox": "true",
"com.apple.security.cs.allow-jit": "true"
}
cls.certificate = '-'
cls.code_signer.signBinary("hello_2", cls.certificate, cls.entitlements)
# Prepare bare_bone.app for test_bundle_structure
os.system("../App\ Bundle\ Extension/custom/make_bundle.sh")
assert os.path.exists("bare_bone.app")
@classmethod
def teardown_class(cls):
# Purge the compiled files
cls.compiler.purgeCompiledFiles()
assert not os.path.exists("hello_2")
assert not os.path.exists("hello_2_unsigned_binary")
# Purge entitlements file
os.remove('unpredictable_entitlements_1029384756.plist')
assert not os.path.exists('unpredictable_entitlements_1029384756.plist')
# Remove bare_bone.app for test_bundle_structure
os.system("rm -rf bare_bone.app")
assert not os.path.exists("bare_bone.app")
def test_verify_signature(self):
'''Test the --verify_signature flag of SnakeII.'''
args_list = ['-p', 'hello_2', '--verify_signature']
args = argumentWrapper(args_list)
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
def code_block():
macho_processor = MachOProcessor()
macho_processor.process(args)
code_signing_processor = CodeSigningProcessor()
code_signing_processor.process(args)
uroboros_output = executeCodeBlock(code_block)
expected_output = 'Valid Code Signature (matches the content)'
assert uroboros_output == expected_output
args_list = ['-p', 'hello_2_unsigned_binary', '--verify_signature']
args = argumentWrapper(args_list)
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
def code_block():
macho_processor = MachOProcessor()
macho_processor.process(args)
code_signing_processor = CodeSigningProcessor()
code_signing_processor.process(args)
uroboros_output = executeCodeBlock(code_block)
expected_output = 'Invalid Code Signature (does not match the content)'
assert uroboros_output == expected_output
def test_cd_info(self):
'''Test the --cd_info flag of SnakeII.'''
args_list = ['-p', 'hello_2', '--cd_info']
args = argumentWrapper(args_list)
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
def code_block():
macho_processor = MachOProcessor()
macho_processor.process(args)
code_signing_processor = CodeSigningProcessor()
code_signing_processor.process(args)
uroboros_output = executeCodeBlock(code_block)
expected_output = 'flags=0x2(adhoc)'
assert expected_output in uroboros_output
def test_cd_requirements(self):
'''Test the --cd_requirements flag of SnakeII.'''
args_list = ['-p', 'hello_2', '--cd_requirements']
args = argumentWrapper(args_list)
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
def code_block():
macho_processor = MachOProcessor()
macho_processor.process(args)
code_signing_processor = CodeSigningProcessor()
code_signing_processor.process(args)
uroboros_output = executeCodeBlock(code_block)
expected_output = 'designated'
assert expected_output in uroboros_output
def test_entitlements(self):
'''Test the --entitlements flag of SnakeII.'''
args_list = ['-p', 'hello_2', '--entitlements']
args = argumentWrapper(args_list)
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
def code_block():
macho_processor = MachOProcessor()
macho_processor.process(args)
code_signing_processor = CodeSigningProcessor()
code_signing_processor.process(args)
uroboros_output = executeCodeBlock(code_block)
expected_output_1 = 'com.apple.security.app-sandbox'
expected_output_2 = 'com.apple.security.cs.allow-jit'
assert expected_output_1 in uroboros_output
assert expected_output_2 in uroboros_output
def test_extract_cms(self):
'''Test the --extract_cms flag of SnakeII.''' # TODO - this test can be improved, but we need to create a security-identity for tests
args_list = ['-p', 'hello_2', '--extract_cms', 'hello_2_unpredictable_name.cms']
args = argumentWrapper(args_list)
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
def code_block():
macho_processor = MachOProcessor()
macho_processor.process(args)
code_signing_processor = CodeSigningProcessor()
code_signing_processor.process(args)
uroboros_output = executeCodeBlock(code_block)
assert os.path.exists('hello_2_unpredictable_name.cms')
os.remove('hello_2_unpredictable_name.cms')
def test_extract_certificates(self):
'''Test the --extract_certificates flag of SnakeII.'''
args_list = ['-p', '/usr/lib/dyld', '--extract_certificates', 'hello_2_unpredictable_name']
args = argumentWrapper(args_list)
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
def code_block():
macho_processor = MachOProcessor()
macho_processor.process(args)
code_signing_processor = CodeSigningProcessor()
code_signing_processor.process(args)
executeCodeBlock(code_block)
for i in range(3):
assert os.path.exists(f'hello_2_unpredictable_name_{i}')
os.remove(f'hello_2_unpredictable_name_{i}')
def test_remove_sig(self):
'''Test the --remove_code_signature flag of SnakeII.'''
args_list = ['-p', 'hello_2', '--remove_sig', 'hello_2_test_remove_sig']
args = argumentWrapper(args_list)
snake_hatchery = SnakeHatchery(args, snake_class)
snake_hatchery.hatch()
def code_block():
macho_processor = MachOProcessor()
macho_processor.process(args)
code_signing_processor = CodeSigningProcessor()
code_signing_processor.process(args)
executeCodeBlock(code_block)
assert os.path.exists('hello_2_test_remove_sig')
binary1 = lief.parse('hello_2_test_remove_sig')
assert not binary1.has_code_signature
"""# sign_binary is problematic, because of race condition on codesign tool, but we do not need to test that since we are testing verify_signature. The test can be reimplemented when I rewrite codesigning utility in python.