-
-
Notifications
You must be signed in to change notification settings - Fork 716
/
runtests.py
executable file
·1156 lines (1006 loc) · 45.1 KB
/
runtests.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
#!/usr/bin/env python3
#
# Run tests by spawning a gdb instance for every command.
# A test is always executed against all architectures, unless the
# decorators `include_for_architectures` and `exclude_for_architectures`
# are used.
#
import re
import subprocess
import tempfile
import unittest
import warnings
from pathlib import Path
from helpers import (
gdb_run_cmd,
gdb_run_silent_cmd,
gdb_start_silent_cmd,
gdb_start_silent_cmd_last_line,
gdb_test_python_method,
include_for_architectures,
ARCH,
is_64b,
_target,
start_gdbserver, stop_gdbserver,
)
BIN_LS = Path("/bin/ls")
BIN_SH = Path("/bin/sh")
TMPDIR = Path(tempfile.gettempdir())
GEF_DEFAULT_PROMPT = "gef➤ "
GEF_DEFAULT_TEMPDIR = "/tmp/gef"
class GdbAssertionError(AssertionError):
pass
class GefUnitTestGeneric(unittest.TestCase):
"""Generic class for command testing, that defines all helpers"""
@staticmethod
def assertException(buf):
"""Assert that GEF raised an Exception."""
if not ("Python Exception <" in buf
or "Traceback" in buf
or "'gdb.error'" in buf
or "Exception raised" in buf
or "failed to execute properly, reason:" in buf):
raise GdbAssertionError("GDB Exception expected, not raised")
@staticmethod
def assertNoException(buf):
"""Assert that no Exception was raised from GEF."""
if ("Python Exception <" in buf
or "Traceback" in buf
or "'gdb.error'" in buf
or "Exception raised" in buf
or "failed to execute properly, reason:" in buf):
raise GdbAssertionError(f"Unexpected GDB Exception raised in {buf}")
if "is deprecated and will be removed in a feature release." in buf:
lines = [l for l in buf.splitlines()
if "is deprecated and will be removed in a feature release." in l]
deprecated_api_names = [x[0] for x in lines]
warnings.warn(
UserWarning(f"Use of deprecated API(s): {', '.join(deprecated_api_names)}")
)
@staticmethod
def assertFailIfInactiveSession(buf):
if "No debugging session active" not in buf:
raise AssertionError("No debugging session inactive warning")
class TestGefCommandsUnit(GefUnitTestGeneric):
"""Tests GEF GDB commands."""
def test_cmd_canary(self):
self.assertFailIfInactiveSession(gdb_run_cmd("canary"))
res = gdb_start_silent_cmd("canary", target=_target("canary"))
self.assertNoException(res)
self.assertIn("The canary of process", res)
return
def test_cmd_capstone_disassemble(self):
self.assertNotIn("capstone", gdb_run_silent_cmd("gef missing"))
self.assertFailIfInactiveSession(gdb_run_cmd("capstone-disassemble"))
res = gdb_start_silent_cmd("capstone-disassemble")
self.assertNoException(res)
self.assertTrue(len(res.splitlines()) > 1)
self.assertFailIfInactiveSession(gdb_run_cmd("cs --show-opcodes"))
res = gdb_start_silent_cmd("cs --show-opcodes --length 5 $pc")
self.assertNoException(res)
self.assertTrue(len(res.splitlines()) >= 5)
res = res[res.find("→ "):] # jump to the output buffer
addr, opcode, symbol, *_ = [x.strip() for x in res.splitlines()[2].strip().split()]
# match the correct output format: <addr> <opcode> [<symbol>] mnemonic [operands,]
# gef➤ cs --show-opcodes --length 5 $pc
# → 0xaaaaaaaaa840 80000090 <main+20> adrp x0, #0xaaaaaaaba000
# 0xaaaaaaaaa844 00f047f9 <main+24> ldr x0, [x0, #0xfe0]
# 0xaaaaaaaaa848 010040f9 <main+28> ldr x1, [x0]
# 0xaaaaaaaaa84c e11f00f9 <main+32> str x1, [sp, #0x38]
# 0xaaaaaaaaa850 010080d2 <main+36> movz x1, #0
self.assertTrue(addr.startswith("0x"))
self.assertTrue(int(addr, 16))
self.assertTrue(int(opcode, 16))
self.assertTrue(symbol.startswith("<") and symbol.endswith(">"))
res = gdb_start_silent_cmd("cs --show-opcodes main")
self.assertNoException(res)
self.assertTrue(len(res.splitlines()) > 1)
return
def test_cmd_checksec(self):
cmd = "checksec"
res = gdb_run_cmd(cmd)
self.assertNoException(res)
target = _target("checksec-no-canary")
res = gdb_run_cmd(cmd, target=target)
self.assertIn("Canary : ✘", res)
target = _target("checksec-no-nx")
res = gdb_run_cmd(cmd, target=target)
self.assertIn("NX : ✘", res)
target = _target("checksec-no-pie")
res = gdb_run_cmd(cmd, target=target)
self.assertIn("PIE : ✘", res)
return
def test_cmd_dereference(self):
self.assertFailIfInactiveSession(gdb_run_cmd("dereference"))
res = gdb_start_silent_cmd("dereference $sp")
self.assertNoException(res)
self.assertTrue(len(res.splitlines()) > 2)
res = gdb_start_silent_cmd("dereference 0x0")
self.assertNoException(res)
self.assertIn("Unmapped address", res)
return
@include_for_architectures(["i686", "amd64", "armv7l", "aarch64"])
def test_cmd_edit_flags(self):
# force enable flag
res = gdb_start_silent_cmd_last_line("edit-flags +carry")
self.assertNoException(res)
self.assertIn("CARRY ", res)
# force disable flag
res = gdb_start_silent_cmd_last_line("edit-flags -carry")
self.assertNoException(res)
self.assertIn("carry ", res)
# toggle flag
res = gdb_start_silent_cmd_last_line("edit-flags")
flag_set = "CARRY " in res
res = gdb_start_silent_cmd_last_line("edit-flags ~carry")
self.assertNoException(res)
if flag_set:
self.assertIn("carry ", res)
else:
self.assertIn("CARRY ", res)
return
def test_cmd_elf_info(self):
res = gdb_run_cmd("elf-info")
self.assertNoException(res)
self.assertIn("7f 45 4c 46", res)
return
def test_cmd_entry_break(self):
res = gdb_run_cmd("entry-break", before=["gef config gef.disable_color 1",])
self.assertNoException(res)
return
def test_cmd_format_string_helper(self):
cmd = "format-string-helper"
target = _target("format-string-helper")
res = gdb_run_cmd(cmd,
after=["set args testtest",
"run",],
target=target)
self.assertNoException(res)
self.assertIn("Possible insecure format string:", res)
return
def test_cmd_functions(self):
cmd = "functions"
res = gdb_run_cmd(cmd)
self.assertNoException(res)
self.assertIn("$_heap", res)
return
def test_cmd_got(self):
cmd = "got"
target = _target("format-string-helper")
self.assertFailIfInactiveSession(gdb_run_cmd(cmd, target=target))
res = gdb_start_silent_cmd(cmd, target=target)
self.assertIn("printf", res)
self.assertIn("strcpy", res)
res = gdb_start_silent_cmd("got printf", target=target)
self.assertIn("printf", res)
self.assertNotIn("strcpy", res)
return
def test_cmd_gef_remote(self):
before = ["gef-remote :1234"]
gdbserver = start_gdbserver()
res = gdb_start_silent_cmd("vmmap", before=before)
self.assertNoException(res)
stop_gdbserver(gdbserver)
return
def test_cmd_heap_arenas(self):
cmd = "heap arenas"
target = _target("heap")
self.assertFailIfInactiveSession(gdb_run_cmd(cmd, target=target))
res = gdb_start_silent_cmd(cmd, target=target)
self.assertNoException(res)
self.assertIn("Arena(base=", res)
return
def test_cmd_heap_set_arena(self):
cmd = "heap set-arena &main_arena"
target = _target("heap")
self.assertFailIfInactiveSession(gdb_run_cmd(cmd, target=target))
res = gdb_run_silent_cmd(cmd, target=target, after=["heap arenas"])
self.assertNoException(res)
self.assertIn("Arena(base=", res)
return
def test_cmd_heap_chunk(self):
cmd = "heap chunk p1"
target = _target("heap")
self.assertFailIfInactiveSession(gdb_run_cmd(cmd, target=target))
res = gdb_run_silent_cmd(cmd, target=target)
self.assertNoException(res)
self.assertIn("NON_MAIN_ARENA flag: ", res)
cmd = "heap chunk --number 2 p1"
self.assertFailIfInactiveSession(gdb_run_cmd(cmd, target=target))
res = gdb_run_silent_cmd(cmd, target=target)
self.assertNoException(res)
heap_lines = [x for x in res.splitlines() if x.startswith("Chunk(addr=")]
self.assertTrue(len(heap_lines) == 2)
return
def test_cmd_heap_chunks(self):
cmd = "heap chunks"
target = _target("heap")
self.assertFailIfInactiveSession(gdb_run_cmd(cmd, target=target))
res = gdb_run_silent_cmd(cmd, target=target)
self.assertNoException(res)
self.assertIn("Chunk(addr=", res)
self.assertIn("top chunk", res)
cmd = "python gdb.execute(f'heap chunks {int(list(gef.heap.arenas)[1]):#x}')"
target = _target("heap-non-main")
res = gdb_run_silent_cmd(cmd, target=target)
self.assertNoException(res)
self.assertNotIn("using '&main_arena' instead", res)
self.assertIn("Chunk(addr=", res)
self.assertIn("top chunk", res)
return
def test_cmd_heap_chunks_mult_heaps(self):
py_cmd = 'gdb.execute(f"heap set-arena {int(list(gef.heap.arenas)[1]):#x}")'
before = ['run', 'python ' + py_cmd]
cmd = "heap chunks"
target = _target("heap-multiple-heaps")
res = gdb_run_silent_cmd(cmd, before=before, target=target)
self.assertNoException(res)
self.assertIn("Chunk(addr=", res)
self.assertIn("top chunk", res)
return
def test_cmd_heap_bins_fast(self):
cmd = "heap bins fast"
before = ["set environment GLIBC_TUNABLES glibc.malloc.tcache_count=0"]
target = _target("heap-fastbins")
self.assertFailIfInactiveSession(gdb_run_cmd(cmd, before=before, target=target))
res = gdb_run_silent_cmd(cmd, before=before, target=target)
self.assertNoException(res)
# ensure fastbins is populated
self.assertIn("Fastbins[idx=0, size=", res)
self.assertIn("Chunk(addr=", res)
return
def test_cmd_heap_bins_large(self):
cmd = "heap bins large"
target = _target("heap-bins")
res = gdb_run_silent_cmd(cmd, target=target)
self.assertNoException(res)
self.assertIn("Found 1 chunks in 1 large non-empty bins", res)
self.assertIn("Chunk(addr=", res)
self.assertIn("size=0x420", res)
def test_cmd_heap_bins_non_main(self):
cmd = "python gdb.execute(f'heap bins fast {gef.heap.main_arena.addr}')"
before = ["set environment GLIBC_TUNABLES glibc.malloc.tcache_count=0"]
target = _target("heap-non-main")
res = gdb_run_silent_cmd(cmd, before=before, target=target)
self.assertNoException(res)
self.assertIn("size=0x20", res)
return
def test_cmd_heap_bins_small(self):
cmd = "heap bins small"
before = ["set environment GLIBC_TUNABLES glibc.malloc.tcache_count=0"]
target = _target("heap-bins")
res = gdb_run_silent_cmd(cmd, before=before, target=target)
self.assertNoException(res)
self.assertIn("Found 1 chunks in 1 small non-empty bins", res)
self.assertIn("Chunk(addr=", res)
self.assertIn("size=0x20", res)
def test_cmd_heap_bins_tcache(self):
cmd = "heap bins tcache"
target = _target("heap-non-main")
res = gdb_run_silent_cmd(cmd, target=target)
self.assertNoException(res)
# ensure tcachebins is populated
self.assertIn("Tcachebins[idx=", res)
return
def test_cmd_heap_bins_tcache_all(self):
cmd = "heap bins tcache all"
target = _target("heap-tcache")
res = gdb_run_silent_cmd(cmd, target=target)
self.assertNoException(res)
# ensure there's 2 tcachebins
tcachebins_lines = [x for x in res.splitlines() if x.startswith("Tcachebins[idx=")]
self.assertTrue(len(tcachebins_lines) == 2)
return
def test_cmd_heap_bins_unsorted(self):
cmd = "heap bins unsorted"
target = _target("heap-bins")
res = gdb_run_silent_cmd(cmd, target=target)
self.assertNoException(res)
self.assertIn("Found 1 chunks in unsorted bin", res)
self.assertIn("Chunk(addr=", res)
self.assertIn("size=0x430", res)
def test_cmd_heap_analysis(self):
cmd = "heap-analysis-helper"
target = _target("heap-analysis")
self.assertFailIfInactiveSession(gdb_run_cmd(cmd))
res = gdb_start_silent_cmd(cmd, after=["continue"], target=target)
self.assertNoException(res)
self.assertIn("Tracking", res)
self.assertIn("correctly setup", res)
self.assertIn("malloc(16)=", res)
self.assertIn("calloc(32)=", res)
addr = int(res.split("calloc(32)=")[1].split("\n")[0], 0)
self.assertRegex(res, r"realloc\(.+, 48")
self.assertIn(f"free({addr:#x}", res)
return
def test_cmd_hexdump(self):
self.assertFailIfInactiveSession(gdb_run_cmd("hexdump $pc"))
res = gdb_start_silent_cmd("hexdump qword $pc")
self.assertNoException(res)
res = gdb_start_silent_cmd("hexdump dword $pc -s 1")
self.assertNoException(res)
res = gdb_start_silent_cmd("hexdump word $pc -s 5 -r")
self.assertNoException(res)
res = gdb_start_silent_cmd("hexdump byte $sp -s 32")
self.assertNoException(res)
return
def test_cmd_memory_watch(self):
self.assertFailIfInactiveSession(gdb_run_cmd("memory watch $pc"))
res = gdb_start_silent_cmd("memory watch $pc 0x100 byte")
self.assertNoException(res)
res = gdb_start_silent_cmd("memory watch $pc 0x40 word")
self.assertNoException(res)
res = gdb_start_silent_cmd("memory watch $pc 0x30 dword")
self.assertNoException(res)
res = gdb_start_silent_cmd("memory watch $pc 0x20 qword")
self.assertNoException(res)
res = gdb_start_silent_cmd("memory watch $pc 0x8 pointers")
self.assertNoException(res)
res = gdb_start_silent_cmd("memory watch $pc")
self.assertNoException(res)
target = _target("memwatch")
res = gdb_start_silent_cmd("memory watch &myglobal",
before=["set args 0xdeadbeef"],
after=["continue"],
target=target,
context='memory')
self.assertIn("deadbeef", res)
self.assertNotIn("cafebabe", res)
res = gdb_start_silent_cmd("memory watch &myglobal",
before=["set args 0xcafebabe"],
after=["continue"],
target=target,
context="memory")
self.assertIn("cafebabe", res)
self.assertNotIn("deadbeef", res)
def test_cmd_memory_unwatch(self):
self.assertFailIfInactiveSession(gdb_run_cmd("memory unwatch $pc"))
res = gdb_start_silent_cmd("memory unwatch $pc")
self.assertNoException(res)
def test_cmd_memory_list(self):
self.assertFailIfInactiveSession(gdb_run_cmd("memory list"))
res = gdb_start_silent_cmd("memory list")
self.assertNoException(res)
def test_cmd_memory_reset(self):
self.assertFailIfInactiveSession(gdb_run_cmd("memory reset"))
res = gdb_start_silent_cmd("memory reset")
self.assertNoException(res)
def test_cmd_name_break(self):
res = gdb_run_cmd("nb foobar *main+10")
self.assertNoException(res)
res = gdb_run_cmd("nb foobar *0xcafebabe")
self.assertNoException(res)
self.assertIn("at 0xcafebabe", res)
res = gdb_start_silent_cmd("nb foobar")
self.assertNoException(res)
return
def test_cmd_keystone_assemble(self):
self.assertNotIn("keystone", gdb_run_silent_cmd("gef missing"))
cmds = [
"assemble --arch arm --mode arm add r0, r1, r2",
"assemble --arch arm --mode arm --endian big add r0, r1, r2",
"assemble --arch arm --mode thumb add r0, r1, r2",
"assemble --arch arm --mode thumb --endian big add r0, r1, r2",
"assemble --arch arm --mode armv8 add r0, r1, r2",
"assemble --arch arm --mode armv8 --endian big add r0, r1, r2",
"assemble --arch arm --mode thumbv8 add r0, r1, r2",
"assemble --arch arm --mode thumbv8 --endian big add r0, r1, r2",
"assemble --arch arm64 --mode 0 add x29, sp, 0; mov w0, 0; ret",
"assemble --arch mips --mode mips32 add $v0, 1",
"assemble --arch mips --mode mips32 --endian big add $v0, 1",
"assemble --arch mips --mode mips64 add $v0, 1",
"assemble --arch mips --mode mips64 --endian big add $v0, 1",
"assemble --arch ppc --mode ppc32 --endian big ori 0, 0, 0",
"assemble --arch ppc --mode ppc64 ori 0, 0, 0",
"assemble --arch ppc --mode ppc64 --endian big ori 0, 0, 0",
"assemble --arch sparc --mode sparc32 set 0, %o0",
"assemble --arch sparc --mode sparc32 --endian big set 0, %o0",
"assemble --arch sparc --mode sparc64 --endian big set 0, %o0",
"assemble --arch x86 --mode 16 mov ax, 0x42",
"assemble --arch x86 --mode 32 mov eax, 0x42",
"assemble --arch x86 --mode 64 mov rax, 0x42",
]
for cmd in cmds:
res = gdb_start_silent_cmd(cmd)
self.assertNoException(res)
self.assertTrue(len(res.splitlines()) > 1)
return
def test_cmd_patch(self):
self.assertFailIfInactiveSession(gdb_run_cmd("patch"))
return
def test_cmd_patch_byte(self):
res = gdb_start_silent_cmd_last_line("patch byte $pc 0xcc", after=["display/8bx $pc",])
self.assertNoException(res)
self.assertRegex(res, r"0xcc\s*0x[^c]{2}")
return
def test_cmd_patch_word(self):
res = gdb_start_silent_cmd_last_line("patch word $pc 0xcccc", after=["display/8bx $pc",])
self.assertNoException(res)
self.assertRegex(res, r"(0xcc\s*)(\1)0x[^c]{2}")
return
def test_cmd_patch_dword(self):
res = gdb_start_silent_cmd_last_line("patch dword $pc 0xcccccccc",
after=["display/8bx $pc",])
self.assertNoException(res)
self.assertRegex(res, r"(0xcc\s*)(\1\1\1)0x[^c]{2}")
return
def test_cmd_patch_qword(self):
res = gdb_start_silent_cmd_last_line("patch qword $pc 0xcccccccccccccccc",
after=["display/8bx $pc",])
self.assertNoException(res)
self.assertRegex(res, r"(0xcc\s*)(\1\1\1\1\1\1)0xcc")
return
def test_cmd_patch_qword_symbol(self):
target = _target("bss")
before = gdb_run_silent_cmd("deref -l 1 $sp", target=target)
after = gdb_run_silent_cmd("patch qword $sp &msg", after=["deref -l 1 $sp"], target=target)
self.assertNoException(before)
self.assertNoException(after)
self.assertNotIn("Hello world!", before)
self.assertIn("Hello world!", after)
return
def test_cmd_patch_string(self):
res = gdb_start_silent_cmd_last_line("patch string $sp \"Gef!Gef!Gef!Gef!\"",
after=["grep Gef!Gef!Gef!Gef!",])
self.assertNoException(res)
self.assertIn("Gef!Gef!Gef!Gef!", res)
return
def test_cmd_pattern_create(self):
cmd = "pattern create -n 4 32"
res = gdb_run_cmd(cmd)
self.assertNoException(res)
self.assertIn("aaaabaaacaaadaaaeaaaf", res)
cmd = "pattern create -n 8 32"
res = gdb_run_cmd(cmd)
self.assertNoException(res)
self.assertIn("aaaaaaaabaaaaaaacaaaaaaadaaaaaaa", res)
return
@include_for_architectures(["x86_64", "aarch64"])
def test_cmd_pattern_search(self):
target = _target("pattern")
if ARCH == "aarch64":
r = "$x30"
elif ARCH == "x86_64":
r = "$rbp"
else:
raise ValueError("Invalid architecture")
cmd = f"pattern search -n 4 {r}"
before = ["set args aaaabaaacaaadaaaeaaafaaagaaahaaa", "run"]
res = gdb_run_cmd(cmd, before=before, target=target)
self.assertNoException(res)
self.assertIn("Found at offset", res)
cmd = f"pattern search -n 8 {r}"
before = ["set args aaaaaaaabaaaaaaacaaaaaaadaaaaaaa", "run"]
res = gdb_run_cmd(cmd, before=before, target=target)
self.assertNoException(res)
self.assertIn("Found at offset", res)
res = gdb_start_silent_cmd("pattern search -n 4 caaaaaaa")
self.assertNoException(res)
self.assertNotIn("Found at offset", res)
res = gdb_start_silent_cmd("pattern search -n 8 caaaaaaa")
self.assertNoException(res)
self.assertIn("Found at offset", res)
res = gdb_start_silent_cmd("pattern search -n 8 0x6261616161616161")
self.assertNoException(res)
self.assertIn("Found at offset", res)
return
def test_cmd_print_format(self):
self.assertFailIfInactiveSession(gdb_run_cmd("print-format"))
res = gdb_start_silent_cmd("print-format $sp")
self.assertNoException(res)
self.assertTrue("buf = [" in res)
res = gdb_start_silent_cmd("print-format --lang js $sp")
self.assertNoException(res)
self.assertTrue("var buf = [" in res)
res = gdb_start_silent_cmd("set *((int*)$sp) = 0x41414141",
after=["print-format --lang hex $sp"])
self.assertNoException(res)
self.assertTrue("41414141" in res, f"{res}")
res = gdb_start_silent_cmd("print-format --lang iDontExist $sp")
self.assertNoException(res)
self.assertTrue("Language must be in:" in res)
return
def test_cmd_process_status(self):
self.assertFailIfInactiveSession(gdb_run_cmd("process-status"))
res = gdb_start_silent_cmd("process-status")
self.assertNoException(res)
self.assertIn("Process Information", res)
self.assertIn("No child process", res)
self.assertIn("No open connections", res)
return
def test_cmd_process_search(self):
target = _target("pattern")
res = gdb_start_silent_cmd("process-search", target=target,
before=["set args w00tw00t"])
self.assertNoException(res)
self.assertIn(str(target), res)
res = gdb_start_silent_cmd("process-search gdb.*fakefake",
target=target, before=["set args w00tw00t"])
self.assertNoException(res)
self.assertIn("gdb", res)
res = gdb_start_silent_cmd("process-search --smart-scan gdb.*fakefake",
target=target, before=["set args w00tw00t"])
self.assertNoException(res)
self.assertNotIn("gdb", res)
return
@include_for_architectures(["aarch64", "armv7l", "x86_64", "i686"])
def test_cmd_registers(self):
self.assertFailIfInactiveSession(gdb_run_cmd("registers"))
res = gdb_start_silent_cmd("registers")
self.assertNoException(res)
if ARCH in ("aarch64",):
self.assertIn("$x0", res)
self.assertIn("$cpsr", res)
elif ARCH in ("armv7l", ):
self.assertIn("$r0", res)
self.assertIn("$lr", res)
self.assertIn("$cpsr", res)
elif ARCH in ("x86_64", ):
self.assertIn("$rax", res)
self.assertIn("$eflags", res)
elif ARCH in ("i686", ):
self.assertIn("$eax", res)
self.assertIn("$eflags", res)
return
def test_cmd_reset_cache(self):
res = gdb_start_silent_cmd("reset-cache")
self.assertNoException(res)
return
@include_for_architectures(["x86_64", "i686"])
def test_cmd_ropper(self):
cmd = "ropper"
self.assertFailIfInactiveSession(gdb_run_cmd(cmd))
cmd = "ropper --search \"pop %; pop %; ret\""
res = gdb_run_silent_cmd(cmd)
self.assertNoException(res)
self.assertNotIn(": error:", res)
self.assertTrue(len(res.splitlines()) > 2)
return
def test_cmd_scan(self):
cmd = "scan libc stack"
target = _target("checksec-no-pie")
self.assertFailIfInactiveSession(gdb_run_cmd(cmd))
res = gdb_start_silent_cmd(cmd, target=target)
self.assertNoException(res)
self.assertIn(str(target), res)
res = gdb_start_silent_cmd("scan binary libc")
self.assertNoException(res)
self.assertIn("__libc_start_main", res)
return
def test_cmd_search_pattern(self):
self.assertFailIfInactiveSession(gdb_run_cmd(f"grep {BIN_SH}"))
res = gdb_start_silent_cmd(f"grep {BIN_SH}")
self.assertNoException(res)
self.assertIn("0x", res)
return
def test_cmd_set_permission(self):
self.assertFailIfInactiveSession(gdb_run_cmd("set-permission"))
target = _target("set-permission")
# get the initial stack address
res = gdb_start_silent_cmd("vmmap", target=target)
self.assertNoException(res)
stack_line = [l.strip() for l in res.splitlines() if "[stack]" in l][0]
stack_address = int(stack_line.split()[0], 0)
# compare the new permissions
res = gdb_start_silent_cmd(f"set-permission {stack_address:#x}",
after=[f"xinfo {stack_address:#x}"], target=target)
self.assertNoException(res)
line = [l.strip() for l in res.splitlines() if l.startswith("Permissions: ")][0]
self.assertEqual(line.split()[1], "rwx")
res = gdb_start_silent_cmd("set-permission 0x1338000", target=target)
self.assertNoException(res)
self.assertIn("Unmapped address", res)
# Make sure set-permission command doesn't clobber any register
before = [
"gef config context.clear_screen False",
"gef config context.layout '-code -stack'",
"entry-break",
"printf \"match_before\\n\"",
"info registers all",
"printf \"match_before\\n\""
]
after = [
"printf \"match_after\\n\"",
"info registers all",
"printf \"match_after\\n\""
]
res = gdb_run_cmd("set-permission $sp", before=before, after=after, target=target)
regs_before = re.match(r"(?:.*match_before)(.+)(?:match_before.*)", res, flags=re.DOTALL)[1]
regs_after = re.match(r"(?:.*match_after)(.+)(?:match_after.*)", res, flags=re.DOTALL)[1]
self.assertEqual(regs_before, regs_after)
return
def test_cmd_shellcode(self):
res = gdb_start_silent_cmd("shellcode")
self.assertNoException(res)
self.assertIn("Missing sub-command (search|get)", res)
return
def test_cmd_shellcode_search(self):
cmd = f"shellcode search execve {BIN_SH}"
res = gdb_start_silent_cmd(cmd)
self.assertNoException(res)
self.assertIn(f"setuid(0) + execve({BIN_SH}) 49 bytes", res)
return
def test_cmd_shellcode_get(self):
res = gdb_start_silent_cmd("shellcode get 77")
self.assertNoException(res)
self.assertIn("Shellcode written to ", res)
return
def test_cmd_stub(self):
# due to compiler optimizations printf might be converted to puts
cmds = ["stub printf", "stub puts"]
self.assertFailIfInactiveSession(gdb_run_cmd(cmds))
res = gdb_start_silent_cmd("continue")
self.assertNoException(res)
self.assertIn("Hello World!", res)
res = gdb_start_silent_cmd(cmds, after=["continue"])
self.assertNoException(res)
self.assertNotIn("Hello World!", res)
return
def test_cmd_theme(self):
res = gdb_run_cmd("theme")
self.assertNoException(res)
possible_themes = [
"context_title_line"
"dereference_base_address"
"context_title_message"
"disable_color"
"dereference_code"
"dereference_string"
"default_title_message",
"default_title_line"
"dereference_register_value",
"xinfo_title_message",
]
for t in possible_themes:
# testing command viewing
res = gdb_run_cmd(f"theme {t}")
self.assertNoException(res)
# testing command setting
v = "blue blah 10 -1 0xfff bold"
res = gdb_run_cmd(f"theme {t} {v}")
self.assertNoException(res)
return
def test_cmd_trace_run(self):
cmd = "trace-run"
res = gdb_run_cmd(cmd)
self.assertFailIfInactiveSession(res)
cmd = "trace-run $pc+1"
res = gdb_start_silent_cmd(
cmd, before=[f"gef config trace-run.tracefile_prefix {TMPDIR / 'gef-trace-'}"])
self.assertNoException(res)
self.assertIn("Tracing from", res)
return
@include_for_architectures(["x86_64"])
def test_cmd_unicorn_emulate(self):
nb_insn = 4
cmd = f"emu {nb_insn}"
res = gdb_run_silent_cmd(cmd)
self.assertFailIfInactiveSession(res)
target = _target("unicorn")
before = ["break function1"]
after = ["si"]
start_marker = "= Starting emulation ="
end_marker = "Final registers"
res = gdb_run_silent_cmd(cmd, target=target, before=before, after=after)
self.assertNoException(res)
self.assertNotIn("Emulation failed", res)
self.assertIn(start_marker, res)
self.assertIn(end_marker, res)
insn_executed = len(res[res.find(start_marker):res.find(end_marker)].splitlines()[1:-1])
self.assertTrue(insn_executed >= nb_insn)
return
def test_cmd_vmmap(self):
self.assertFailIfInactiveSession(gdb_run_cmd("vmmap"))
res = gdb_start_silent_cmd("vmmap")
self.assertNoException(res)
self.assertTrue(len(res.splitlines()) > 1)
res = gdb_start_silent_cmd("vmmap stack")
self.assertNoException(res)
self.assertTrue(len(res.splitlines()) > 1)
return
def test_cmd_xfiles(self):
self.assertFailIfInactiveSession(gdb_run_cmd("xfiles"))
res = gdb_start_silent_cmd("xfiles")
self.assertNoException(res)
self.assertTrue(len(res.splitlines()) >= 3)
return
def test_cmd_xinfo(self):
self.assertFailIfInactiveSession(gdb_run_cmd("xinfo $sp"))
res = gdb_start_silent_cmd("xinfo")
self.assertIn("At least one valid address must be specified", res)
res = gdb_start_silent_cmd("xinfo $sp")
self.assertNoException(res)
self.assertTrue(len(res.splitlines()) >= 7)
return
def test_cmd_xor_memory(self):
cmd = "xor-memory display $sp 0x10 0x41"
self.assertFailIfInactiveSession(gdb_run_cmd(cmd))
res = gdb_start_silent_cmd(cmd)
self.assertNoException(res)
self.assertIn("Original block", res)
self.assertIn("XOR-ed block", res)
cmd = "xor-memory patch $sp 0x10 0x41"
res = gdb_start_silent_cmd(cmd)
self.assertNoException(res)
self.assertIn("Patching XOR-ing ", res)
return
@include_for_architectures(["x86_64", "aarch64"])
def test_cmd_highlight_x86(self):
cmds = [
"highlight add 41414141 yellow",
"highlight add 42424242 blue",
"highlight add 43434343 green",
"highlight add 44444444 pink",
'patch string $sp "AAAABBBBCCCCDDDD"',
"hexdump qword $sp -s 2"
]
res = gdb_start_silent_cmd('', after=cmds, strip_ansi=False)
self.assertNoException(res)
self.assertIn("\x1b[33m41414141\x1b[0m", res)
self.assertIn("\x1b[34m42424242\x1b[0m", res)
self.assertIn("\x1b[32m43434343\x1b[0m", res)
self.assertIn("\x1b[35m44444444\x1b[0m", res)
return
def test_cmd_aliases(self):
# test add functionality
add_res = gdb_start_silent_cmd("aliases add alias_function_test example")
self.assertNoException(add_res)
# test list functionality
list_res = gdb_start_silent_cmd("aliases ls",
before=["aliases add alias_function_test example"])
self.assertNoException(list_res)
self.assertIn("alias_function_test", list_res)
# test rm functionality
rm_res = gdb_start_silent_cmd("aliases ls",
before=["aliases add alias_function_test example",
"aliases rm alias_function_test"])
self.assertNoException(rm_res)
self.assertNotIn("alias_function_test", rm_res)
return
def test_cmd_pcustom(self):
with tempfile.TemporaryDirectory(prefix=GEF_DEFAULT_TEMPDIR) as dd:
dirpath = Path(dd).absolute()
with tempfile.NamedTemporaryFile(dir = dirpath, suffix=".py") as fd:
fd.write(b"""from ctypes import *
class foo_t(Structure):
_fields_ = [("a", c_int32),("b", c_int32),]
class goo_t(Structure):
_fields_ = [("a", c_int32), ("b", c_int32), ("c", POINTER(foo_t)), ("d", c_int32), ("e", c_int32),]
""")
fd.seek(0)
fd.flush()
res = gdb_run_cmd("gef config pcustom.struct_path",
before=[f"gef config pcustom.struct_path {dirpath}",])
self.assertNoException(res)
self.assertIn(f"pcustom.struct_path (str) = \"{dirpath}\"", res)
res = gdb_run_cmd("pcustom", before=[f"gef config pcustom.struct_path {dirpath}",])
self.assertNoException(res)
structline = [x for x in res.splitlines() if x.startswith(f" → {dirpath}") ][0]
self.assertIn("goo_t", structline)
self.assertIn("foo_t", structline)
# bad structure name with address
res = gdb_run_cmd("pcustom meh_t 0x1337100",
before=[f"gef config pcustom.struct_path {dirpath}",])
self.assertNoException(res)
self.assertIn("Session is not active", res)
return
def test_cmd_pcustom_show(self):
with tempfile.TemporaryDirectory(prefix=GEF_DEFAULT_TEMPDIR) as dd:
dirpath = Path(dd).absolute()
with tempfile.NamedTemporaryFile(dir = dirpath, suffix=".py") as fd:
fd.write(b"""from ctypes import *
class foo_t(Structure):
_fields_ = [("a", c_int32),("b", c_int32),]
def __repr__(self): return "foo_t"
def __str__(self): return "foo_t"
class goo_t(Structure):
_fields_ = [("a", c_int32), ("b", c_int32), ("c", POINTER(foo_t)), ("d", c_int32), ("e", c_int32),]
def __repr__(self): return "goo_t"
""")
fd.seek(0)
fd.flush()
# no address
res = gdb_run_cmd("pcustom foo_t",
before=[f"gef config pcustom.struct_path {dirpath}",])
self.assertNoException(res)
self.assertIn("0000 a c_int /* size=0x4 */", res)
self.assertIn("0004 b c_int /* size=0x4 */", res)
# with address
res = gdb_run_silent_cmd("pcustom goo_t 0x1337100", target=_target("pcustom"),
before=[f"gef config pcustom.struct_path {dirpath}",])
self.assertNoException(res)
if is_64b():
self.assertIn(f"""0x1337100+0x00 a : 3 (c_int)
0x1337100+0x04 b : 4 (c_int)
0x1337100+0x08 c : """, res)
self.assertIn(f""" 0x1337000+0x00 a : 1 (c_int)
0x1337000+0x04 b : 2 (c_int)
0x1337100+0x10 d : 12 (c_int)
0x1337100+0x14 e : 13 (c_int)""", res)
else:
self.assertIn(f"""0x1337100+0x00 a : 3 (c_int)
0x1337100+0x04 b : 4 (c_int)
0x1337100+0x08 c : """, res)
self.assertIn(f""" 0x1337000+0x00 a : 1 (c_int)
0x1337000+0x04 b : 2 (c_int)
0x1337100+0x0c d : 12 (c_int)
0x1337100+0x10 e : 13 (c_int)""", res)
# bad structure name
res = gdb_run_cmd("pcustom meh_t",
before=[f"gef config pcustom.struct_path {dirpath}",])
self.assertNoException(res)
self.assertIn("No structure named 'meh_t' found", res)
# bad structure name with address
res = gdb_run_silent_cmd("pcustom meh_t 0x1337100", target=_target("pcustom"),
before=[f"gef config pcustom.struct_path {dirpath}",])
self.assertNoException(res)
self.assertIn("No structure named 'meh_t' found", res)
return
class TestGefFunctionsUnit(GefUnitTestGeneric):
"""Tests GEF internal functions."""
def test_func_get_memory_alignment(self):
res = gdb_test_python_method("gef.arch.ptrsize")
self.assertIn(res.splitlines()[-1], ("4", "8"))
return
@include_for_architectures(["x86_64", "i686"])
def test_func_reset_architecture(self):
res = gdb_test_python_method("gef.arch.arch, gef.arch.mode", before="reset_architecture()")
res = (res.splitlines()[-1])
self.assertIn("X86", res)
return
def test_func_which(self):
res = gdb_test_python_method("which('gdb')")
lines = res.splitlines()
self.assertIn("/gdb", lines[-1])
res = gdb_test_python_method("which('__IDontExist__')")
self.assertIn("Missing file `__IDontExist__`", res)
return
def test_func_get_filepath(self):
res = gdb_test_python_method("gef.session.file", target=BIN_LS)
self.assertNoException(res)
target = TMPDIR / "foo bar"
subprocess.call(["cp", BIN_LS, target])
res = gdb_test_python_method("gef.session.file", target=target)
self.assertNoException(res)
subprocess.call(["rm", target])
return
def test_func_get_pid(self):
res = gdb_test_python_method("gef.session.pid", target=BIN_LS)
self.assertNoException(res)
self.assertTrue(int(res.splitlines()[-1]))
return
def test_func_auxiliary_vector(self):
func = "gef.session.auxiliary_vector"
res = gdb_test_python_method(func, target=BIN_LS)