{{ message }}
Repository navigation
Expand file tree
/
Copy pathtest_elf.py
More file actions
947 lines (674 loc) · 34.8 KB
/
Copy pathtest_elf.py
File metadata and controls
947 lines (674 loc) · 34.8 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
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
#!/usr/bin/env python3
#
# Cross Platform and Multi Architecture Advanced Binary Emulation Framework
#
import unittest
import string
import random
import os
import io
import re
import socket
import sys
import time
import threading
from ctypes import *
sys.path.append("..")
from typing import Any, Sequence
from qiling import Qiling
from qiling.const import QL_ARCH, QL_OS, QL_INTERCEPT, QL_STOP, QL_VERBOSE
from qiling.exception import *
from qiling.extensions import pipe
from qiling.os.const import STRING
from qiling.os.posix import syscall
from qiling.os.mapper import QlFsMappedObject
class ELFTest(unittest.TestCase):
def test_libpatch_elf_linux_x8664(self):
ql = Qiling(["../examples/rootfs/x8664_linux/bin/patch_test.bin"], "../examples/rootfs/x8664_linux")
ql.patch(0x0000000000000575, b'qiling\x00', target='libpatch_test.so')
ql.run()
del ql
def test_elf_freebsd_x8664(self):
ql = Qiling(["../examples/rootfs/x8664_freebsd/bin/x8664_hello_asm"], "../examples/rootfs/x8664_freebsd", verbose=QL_VERBOSE.DUMP)
ql.run()
del ql
def test_elf_partial_linux_x8664(self):
snapshot_file = r'/tmp/snapshot.bin'
def dump(ql: Qiling, *args, **kw):
ql.save(reg=False, cpu_context=True, snapshot=snapshot_file)
ql.emu_stop()
ql = Qiling(["../examples/rootfs/x8664_linux/bin/sleep_hello"], "../examples/rootfs/x8664_linux", verbose=QL_VERBOSE.DEFAULT)
load_address = ql.profile.getint("OS64", "load_address")
ql.hook_address(dump, load_address + 0x1094)
ql.run()
ql = Qiling(["../examples/rootfs/x8664_linux/bin/sleep_hello"], "../examples/rootfs/x8664_linux", verbose=QL_VERBOSE.DEBUG)
load_address = ql.profile.getint("OS64", "load_address")
ql.restore(snapshot=snapshot_file)
begin_point = load_address + 0x109e
end_point = load_address + 0x10bc
ql.run(begin_point, end_point)
del ql
def test_elf_x_only_segment(self):
def stop(ql: Qiling):
ql.emu_stop()
ql = Qiling(["../examples/rootfs/x8664_linux/bin/sleep_hello_with_x_only_segment"], "../examples/rootfs/x8664_linux", verbose=QL_VERBOSE.DEBUG)
load_address = ql.profile.getint("OS64", "load_address")
ql.hook_address(stop, load_address + 0x1094)
ql.run()
del ql
def _test_elf_linux_x86_snapshot_restore_common(self, reg=False, ctx=False):
rootfs = "../examples/rootfs/x86_linux"
cmdline = ["../examples/rootfs/x86_linux/bin/x86_hello"]
snapshot = os.path.join(rootfs, 'snapshot_restore_reg_ctx.snapshot')
ql = Qiling(cmdline, rootfs, verbose=QL_VERBOSE.DEBUG)
load_address = ql.profile.getint("OS32", "load_address")
hook_address = load_address + 0x542 # call printf
def dump(ql: Qiling):
ql.save(reg=reg, cpu_context=ctx, os=True, loader=True, snapshot=snapshot)
ql.emu_stop()
ql.hook_address(dump, hook_address)
ql.run()
# make sure that the ending PC is the same as the hook address because dump stops the emulater
self.assertEqual(ql.arch.regs.arch_pc, hook_address)
del ql
ql = Qiling(cmdline, rootfs, verbose=QL_VERBOSE.DEBUG)
ql.restore(snapshot=snapshot)
# ensure that the starting PC is same as the PC we stopped on when taking the snapshot
self.assertEqual(ql.arch.regs.arch_pc, hook_address)
ql.run(begin=hook_address)
del ql
os.remove(snapshot)
def test_elf_linux_x86_snapshot_restore_reg(self):
self._test_elf_linux_x86_snapshot_restore_common(reg=True, ctx=False)
def test_elf_linux_x86_snapshot_restore_ctx(self):
self._test_elf_linux_x86_snapshot_restore_common(reg=False, ctx=True)
def test_elf_linux_x86_snapshot_restore_reg_ctx(self):
self._test_elf_linux_x86_snapshot_restore_common(reg=True, ctx=True)
PARAMS_PUTS = {'s': STRING}
def test_elf_linux_x8664(self):
checklist = {}
def my_puts(ql: Qiling):
params = ql.os.resolve_fcall_params(ELFTest.PARAMS_PUTS)
print(f'puts("{params["s"]}")')
reg = ql.arch.regs.rax
print(f'reg : {reg:#x}')
checklist['set_api'] = reg
def write_onEnter(ql: Qiling, fd: int, str_ptr: int, str_len: int):
checklist['set_syscall_onenter'] = True
print("enter write syscall!")
# override syscall pc (ignored) and set of params with our own
return None, (fd, str_ptr + 1, str_len - 1)
def write_onexit(ql: Qiling, fd: int, str_ptr: int, str_len: int, retval: int):
checklist['set_syscall_onexit'] = True
print("exit write syscall!")
# override syscall return value with our own
return str_len + 1
ql = Qiling(["../examples/rootfs/x8664_linux/bin/x8664_args", "1234test", "12345678", "bin/x8664_hello"], "../examples/rootfs/x8664_linux", verbose=QL_VERBOSE.DEBUG)
ql.os.set_syscall(1, write_onEnter, QL_INTERCEPT.ENTER)
ql.os.set_syscall(1, write_onexit, QL_INTERCEPT.EXIT)
ql.os.set_api('puts', my_puts)
blob = bytes.fromhex("ff fe fd fc fb fa fb fc fc fe fd")
ql.mem.map(0x1000, 0x1000)
ql.mem.write(0x1000, blob)
ql.mem.map(0x2000, 0x1000)
ql.mem.write(0x2000, blob)
ql.run()
self.assertListEqual([0x1000, 0x2000], ql.mem.search(blob))
self.assertEqual(0x5555555546ca, checklist['set_api'])
self.assertTrue(checklist['set_syscall_onenter'])
self.assertTrue(checklist['set_syscall_onexit'])
del ql
def test_elf_hijackapi_linux_x8664(self):
checklist = {}
def my_puts_enter(ql: Qiling):
params = ql.os.resolve_fcall_params(ELFTest.PARAMS_PUTS)
checklist['enter_str'] = params["s"]
def my_puts_exit(ql):
checklist['exit_rdi'] = ql.arch.regs.rdi
ql = Qiling(["../examples/rootfs/x8664_linux/bin/x8664_puts"], "../examples/rootfs/x8664_linux", verbose=QL_VERBOSE.DEBUG)
ql.os.set_api('puts', my_puts_enter, QL_INTERCEPT.ENTER)
ql.os.set_api('puts', my_puts_exit, QL_INTERCEPT.EXIT)
ql.run()
self.assertIn(checklist['exit_rdi'], (0x1, 0x7fffb81c2760))
self.assertEqual("CCCC", checklist['enter_str'])
del ql
def test_elf_linux_x8664_flex_api(self):
opened = []
def onenter_fopen(ql: Qiling):
params = ql.os.resolve_fcall_params({
'filename' : STRING,
'mode' : STRING
})
# log opened filenames
opened.append(params['filename'])
def hook_main(ql: Qiling):
# set up fopen hook when reaching main
ql.os.set_api('fopen', onenter_fopen, QL_INTERCEPT.ENTER)
ql = Qiling(["../examples/rootfs/x8664_linux/bin/x8664_fetch_urandom"], "../examples/rootfs/x8664_linux", verbose=QL_VERBOSE.DEFAULT)
ba = ql.loader.images[0].base
ql.hook_address(hook_main, ba + 0x10e0)
ql.run()
del ql
# test whether we interpected opening urandom
self.assertListEqual(opened, [r'/dev/urandom'])
def test_elf_linux_x8664_static(self):
ql = Qiling(["../examples/rootfs/x8664_linux/bin/x8664_hello_static"], "../examples/rootfs/x8664_linux", verbose=QL_VERBOSE.DEBUG)
ql.run()
del ql
def test_elf_linux_x86(self):
filename = 'test.qlog'
ql = Qiling(["../examples/rootfs/x86_linux/bin/x86_hello"], "../examples/rootfs/x86_linux", verbose=QL_VERBOSE.DEBUG, log_devices=[filename])
ql.run()
ql._log_file_fd.handlers[0].close() # prevent FD leak that causes downstream issues
os.remove(filename)
del ql
def test_elf_linux_x86_static(self):
ql = Qiling(["../examples/rootfs/x86_linux/bin/x86_hello_static"], "../examples/rootfs/x86_linux", verbose=QL_VERBOSE.DEBUG)
ql.run()
del ql
def posix_syscall_test(self, argv: str, rootfs: str, syscalls: Sequence[str]):
"""A generic method to test out POSIX system calls hooking.
"""
checklist = []
def test_syscall_read(ql: Qiling, fd: int, buf: int, count: int):
retval = syscall.ql_syscall_read(ql, fd, buf, count)
hpath = ql.os.fd[fd].name
if os.path.basename(hpath) == "test_syscall_read.txt":
mcontent = ql.mem.read(buf, count)
with open(hpath, 'rb') as infile:
fcontent = infile.read()
if ql.host.os is not QL_OS.WINDOWS:
os.remove(hpath)
self.assertEqual(mcontent, fcontent)
checklist.append('read')
return retval
def test_syscall_write(ql: Qiling, fd: int, buf: int, count: int):
retval = syscall.ql_syscall_write(ql, fd, buf, count)
hpath = ql.os.fd[fd].name
if os.path.basename(hpath) == "test_syscall_write.txt":
mcontent = ql.mem.read(buf, count)
with open(hpath, 'rb') as infile:
fcontent = infile.read()
if ql.host.os is not QL_OS.WINDOWS:
os.remove(hpath)
self.assertEqual(mcontent, fcontent)
checklist.append('write')
return retval
def test_syscall_open(ql: Qiling, path: int, flags: int, mode: int):
retval = syscall.ql_syscall_open(ql, path, flags, mode)
vpath = ql.os.utils.read_cstring(path)
if vpath == "test_syscall_open.txt":
hpath = ql.os.path.virtual_to_host_path(vpath)
self.assertTrue(os.path.isfile(hpath))
checklist.append('open')
if ql.host.os is not QL_OS.WINDOWS:
os.remove(hpath)
return retval
def test_syscall_openat(ql: Qiling, fd: int, path: int, flags: int, mode: int):
retval = syscall.ql_syscall_openat(ql, fd, path, flags, mode)
vpath = ql.os.utils.read_cstring(path)
if vpath == "test_syscall_open.txt":
hpath = ql.os.path.virtual_to_host_path(vpath)
self.assertTrue(os.path.isfile(hpath))
checklist.append('openat')
if ql.host.os is not QL_OS.WINDOWS:
os.remove(hpath)
return retval
def test_syscall_unlink(ql: Qiling, path: int):
retval = syscall.ql_syscall_unlink(ql, path)
vpath = ql.os.utils.read_cstring(path)
if vpath == "test_syscall_unlink.txt":
hpath = ql.os.path.virtual_to_host_path(vpath)
self.assertFalse(os.path.isfile(hpath))
checklist.append('unlink')
return retval
def test_syscall_unlinkat(ql: Qiling, fd: int, path: int, flags: int):
retval = syscall.ql_syscall_unlinkat(ql, fd, path, flags)
vpath = ql.os.utils.read_cstring(path)
if vpath == "test_syscall_unlink.txt":
hpath = ql.os.path.virtual_to_host_path(vpath)
self.assertFalse(os.path.isfile(hpath))
checklist.append('unlinkat')
return retval
def test_syscall_truncate(ql: Qiling, path: int, length: int):
retval = syscall.ql_syscall_truncate(ql, path, length)
vpath = ql.os.utils.read_cstring(path)
if vpath == "test_syscall_truncate.txt":
hpath = ql.os.path.virtual_to_host_path(vpath)
self.assertEqual(length, os.stat(hpath).st_size)
checklist.append('truncate')
if ql.host.os is not QL_OS.WINDOWS:
os.remove(hpath)
return retval
def test_syscall_ftruncate(ql: Qiling, fd: int, length: int):
retval = syscall.ql_syscall_ftruncate(ql, fd, length)
hpath = ql.os.fd[fd].name
if os.path.basename(hpath) == "test_syscall_ftruncate.txt":
self.assertEqual(length, os.stat(hpath).st_size)
checklist.append('ftruncate')
if ql.host.os is not QL_OS.WINDOWS:
os.remove(hpath)
return retval
hooks = {
'read' : test_syscall_read,
'write' : test_syscall_write,
'open' : test_syscall_open,
'openat' : test_syscall_openat,
'unlink' : test_syscall_unlink,
'unlinkat' : test_syscall_unlinkat,
'truncate' : test_syscall_truncate,
'ftruncate' : test_syscall_ftruncate,
}
ql = Qiling([f'{rootfs}{argv}'], rootfs, verbose=QL_VERBOSE.DEBUG)
# hook reuested system calls
for name in syscalls:
ql.os.set_syscall(name, hooks[name])
ql.run()
# make sure we visited them all
self.assertSequenceEqual(syscalls, checklist)
def test_elf_linux_x86_posix_syscall(self):
syscalls = ['openat', 'write', 'read', 'truncate', 'ftruncate', 'unlink']
self.posix_syscall_test(r'/bin/x86_posix_syscall', r'../examples/rootfs/x86_linux', syscalls)
def test_elf_linux_arm(self):
def my_puts(ql: Qiling):
params = ql.os.resolve_fcall_params(ELFTest.PARAMS_PUTS)
print(f'puts("{params["s"]}")')
all_mem = ql.mem.save()
ql.mem.restore(all_mem)
ql = Qiling(["../examples/rootfs/arm_linux/bin/arm_hello"], "../examples/rootfs/arm_linux", verbose=QL_VERBOSE.DEBUG)
ql.os.set_api('puts', my_puts)
ql.run()
del ql
def test_setsockopt_mips_so_rcvbuf(self):
# MIPS uses its own SO_* numbering (SO_RCVBUF = 0x1002, not the generic
# 0x08). The MIPS socket-option table had the wrong values, so e.g.
# busybox ping's setsockopt(SOL_SOCKET, SO_RCVBUF, ...) raised
# NotImplementedError ("Could not convert emulated socket option 4098").
from qiling.const import QL_ENDIAN
from qiling.os.posix.syscall.socket import ql_syscall_socket, ql_syscall_setsockopt
ql = Qiling(code=b"\x00\x00\x00\x00", archtype=QL_ARCH.MIPS, ostype=QL_OS.LINUX,
endian=QL_ENDIAN.EB, rootfs="../examples/rootfs/mips32_linux",
verbose=QL_VERBOSE.OFF)
AF_INET, SOCK_DGRAM = 2, 2
SOL_SOCKET, SO_RCVBUF = 0xffff, 0x1002 # MIPS values
fd = ql_syscall_socket(ql, AF_INET, SOCK_DGRAM, 0)
self.assertGreaterEqual(fd, 0)
base = 0x100000
ql.mem.map(base, 0x1000)
ql.mem.write_ptr(base, 16384, 4) # requested SO_RCVBUF size
# must map SO_RCVBUF (0x1002) to the host option and succeed, not raise
self.assertEqual(ql_syscall_setsockopt(ql, fd, SOL_SOCKET, SO_RCVBUF, base, 4), 0)
del ql
def test_elf_linux_arm_static(self):
ql = Qiling(["../examples/rootfs/arm_linux/bin/arm_hello_static"], "../examples/rootfs/arm_linux", verbose=QL_VERBOSE.DEFAULT)
all_mem = ql.mem.save()
ql.mem.restore(all_mem)
ql.run()
del ql
@unittest.skip('broken: ARM executable should be generated properly')
def test_elf_linux_arm_posix_syscall(self):
# TODO: check the list, it might be inacurate
syscalls = ['openat', 'write', 'read', 'truncate', 'ftruncate', 'unlink']
self.posix_syscall_test(r'/bin/arm_posix_syscall', r'../examples/rootfs/arm_linux', syscalls)
def test_elf_linux_arm64(self):
ql = Qiling(["../examples/rootfs/arm64_linux/bin/arm64_hello"], "../examples/rootfs/arm64_linux", verbose=QL_VERBOSE.DEBUG)
ql.run()
del ql
def test_elf_linux_arm64_static(self):
ql = Qiling(["../examples/rootfs/arm64_linux/bin/arm64_hello_static"], "../examples/rootfs/arm64_linux", verbose=QL_VERBOSE.DEFAULT)
ql.run()
del ql
def test_elf_linux_mips32eb_static(self):
ql = Qiling(["../examples/rootfs/mips32_linux/bin/mips32_hello_static"], "../examples/rootfs/mips32_linux")
ql.run()
del ql
@staticmethod
def random_generator(length: int):
chars = string.ascii_uppercase + string.digits
return ''.join(random.choices(chars, k=length))
def test_elf_linux_mips32eb(self):
ql = Qiling(["../examples/rootfs/mips32_linux/bin/mips32_hello", self.random_generator(64)], "../examples/rootfs/mips32_linux")
ql.run()
del ql
def test_mips32eb_fake_urandom(self):
class Fake_urandom(QlFsMappedObject):
def read(self, size: int):
return b'\x01' * size
def fstat(self):
return -1
def close(self):
return 0
ql = Qiling(["../examples/rootfs/mips32_linux/bin/mips32_fetch_urandom"], "../examples/rootfs/mips32_linux")
ql.add_fs_mapper("/dev/urandom", Fake_urandom())
ql.run()
del ql
def test_elf_onEnter_mips32el(self):
def my_puts_onenter(ql: Qiling):
params = ql.os.resolve_fcall_params(ELFTest.PARAMS_PUTS)
print(f'puts("{params["s"]}")')
params = ql.os.fcall.readParams(ELFTest.PARAMS_PUTS.values())
self.my_puts_onenter_addr = params[0]
return 2
ql = Qiling(["../examples/rootfs/mips32el_linux/bin/mips32el_double_hello"], "../examples/rootfs/mips32el_linux")
ql.os.set_api('puts', my_puts_onenter, QL_INTERCEPT.ENTER)
ql.run()
self.assertEqual(4196680, self.my_puts_onenter_addr)
del ql
def test_elf_linux_arm64_posix_syscall(self):
syscalls = ['openat', 'write', 'read', 'truncate', 'ftruncate', 'unlinkat']
self.posix_syscall_test(r'/bin/arm64_posix_syscall', r'../examples/rootfs/arm64_linux', syscalls)
def test_elf_linux_mips32el(self):
ql = Qiling(["../examples/rootfs/mips32el_linux/bin/mips32el_hello", self.random_generator(64)], "../examples/rootfs/mips32el_linux")
ql.run()
del ql
def test_elf_linux_mips32el_static(self):
ql = Qiling(["../examples/rootfs/mips32el_linux/bin/mips32el_hello_static", self.random_generator(64)], "../examples/rootfs/mips32el_linux")
ql.run()
del ql
def test_elf_linux_mips32el_posix_syscall(self):
syscalls = ['open', 'write', 'read', 'truncate', 'ftruncate', 'unlink']
self.posix_syscall_test(r'/bin/mips32el_posix_syscall', r'../examples/rootfs/mips32el_linux', syscalls)
def test_elf_linux_powerpc(self):
ql = Qiling(["../examples/rootfs/powerpc_linux/bin/powerpc_hello"], "../examples/rootfs/powerpc_linux", verbose=QL_VERBOSE.DEBUG)
ql.run()
del ql
def test_elf_linux_arm_custom_syscall(self):
checklist = {}
def my_syscall_write(ql: Qiling, fd: int, buf: int, count: int):
try:
data = ql.mem.read(buf, count)
ql.os.fd[fd].write(data)
except:
regreturn = -1
else:
regreturn = count
checklist['set_syscall'] = ql.arch.regs.r0
return regreturn
ql = Qiling(["../examples/rootfs/arm_linux/bin/arm_hello"], "../examples/rootfs/arm_linux")
ql.os.set_syscall('write', my_syscall_write)
ql.run()
self.assertEqual(1, checklist['set_syscall'])
del ql
def test_elf_linux_x86_crackme(self):
def instruction_count(ql: Qiling, address: int, size: int, user_data: Any):
user_data[0] += 1
def my__llseek(ql, *args, **kw):
pass
def run_one_round(payload):
ql = Qiling(["../examples/rootfs/x86_linux/bin/crackme_linux"], "../examples/rootfs/x86_linux", console=False)
ins_count = [0]
ql.hook_code(instruction_count, ins_count)
ql.os.set_syscall("_llseek", my__llseek)
ql.os.stdin = pipe.SimpleInStream(0)
ql.os.stdin.write(payload)
ql.run()
del ql
return ins_count[0]
def solve():
idx_list = [1, 4, 2, 0, 3]
flag = b'\x00\x00\x00\x00\x00\n'
old_count = run_one_round(flag)
for idx in idx_list:
for i in b'L1NUX\\n':
flag = flag[:idx] + chr(i).encode() + flag[idx + 1:]
tmp = run_one_round(flag)
if tmp > old_count:
old_count = tmp
break
print(flag)
print("Linux Simple Crackme Brute Force, This Will Take Some Time ...")
solve()
def test_x86_fake_urandom_multiple_times(self):
next_id = 0
def get_next_id() -> int:
nonlocal next_id
curr_id = next_id
next_id += 1
return curr_id
ids = []
class Fake_urandom(QlFsMappedObject):
def __init__(self):
ids.append(get_next_id())
def read(self, size: int):
return b'\x01' * size
def fstat(self):
return -1
def close(self):
return 0
ql = Qiling(["../examples/rootfs/x86_linux/bin/x86_fetch_urandom_multiple_times"], "../examples/rootfs/x86_linux")
ql.add_fs_mapper("/dev/urandom", Fake_urandom())
ql.run()
self.assertListEqual([0], ids)
del ql
def test_x86_fake_urandom(self):
class Fake_urandom(QlFsMappedObject):
def read(self, size: int):
return b"\x01" * size
def fstat(self):
return -1
def close(self):
return 0
ql = Qiling(["../examples/rootfs/x86_linux/bin/x86_fetch_urandom"], "../examples/rootfs/x86_linux", verbose=QL_VERBOSE.DEBUG)
ql.add_fs_mapper("/dev/urandom", Fake_urandom())
ql.run()
del ql
def test_x8664_map_urandom(self):
ql = Qiling(["../examples/rootfs/x8664_linux/bin/x8664_fetch_urandom"], "../examples/rootfs/x8664_linux", verbose=QL_VERBOSE.DEBUG)
ql.add_fs_mapper("/dev/urandom", "/dev/urandom")
ql.run()
del ql
def test_x8664_symlink(self):
ql = Qiling(["../examples/rootfs/x8664_linux_symlink/bin/x8664_hello"], "../examples/rootfs/x8664_linux_symlink", verbose=QL_VERBOSE.DEBUG)
ql.run()
del ql
def test_x8664_absolute_path(self):
ql = Qiling(["../examples/rootfs/x8664_linux/bin/absolutepath"], "../examples/rootfs/x8664_linux", verbose=QL_VERBOSE.DEBUG)
ql.os.stdout = pipe.SimpleOutStream(1)
ql.run()
self.assertEqual(ql.os.stdout.read(), b'test_complete\n\ntest_complete\n\n')
del ql
def test_x8664_getcwd(self):
ql = Qiling(["../examples/rootfs/x8664_linux/bin/testcwd"], "../examples/rootfs/x8664_linux", verbose=QL_VERBOSE.DEBUG)
ql.os.stdout = pipe.SimpleOutStream(1)
ql.run()
self.assertEqual(ql.os.stdout.read(), b'/\n/lib\n/bin\n/\n')
del ql
def test_elf_linux_x86_return_from_main_stackpointer(self):
ql = Qiling(["../examples/rootfs/x86_linux/bin/x86_return_main"], "../examples/rootfs/x86_linux", stop=QL_STOP.STACK_POINTER)
ql.run()
del ql
def test_elf_linux_x86_return_from_main_exit_trap(self):
ql = Qiling(["../examples/rootfs/x86_linux/bin/x86_return_main"], "../examples/rootfs/x86_linux", stop=QL_STOP.EXIT_TRAP)
ql.run()
del ql
def test_elf_linux_x8664_return_from_main_stackpointer(self):
ql = Qiling(["../examples/rootfs/x8664_linux/bin/x8664_return_main"], "../examples/rootfs/x8664_linux", stop=QL_STOP.STACK_POINTER)
ql.run()
del ql
def test_elf_linux_x8664_return_from_main_exit_trap(self):
ql = Qiling(["../examples/rootfs/x8664_linux/bin/x8664_return_main"], "../examples/rootfs/x8664_linux", stop=QL_STOP.EXIT_TRAP)
ql.run()
del ql
def test_arm_stat64(self):
ql = Qiling(["../examples/rootfs/arm_linux/bin/arm_stat64", "/bin/arm_stat64"], "../examples/rootfs/arm_linux", verbose=QL_VERBOSE.DEBUG)
ql.run()
del ql
def test_elf_linux_x8664_getdents(self):
ql = Qiling(["../examples/rootfs/x8664_linux/bin/x8664_getdents"], "../examples/rootfs/x8664_linux", verbose=QL_VERBOSE.DEBUG)
ql.os.stdout = io.BytesIO()
ql.run()
ql.os.stdout.seek(0)
self.assertIn("bin\n", ql.os.stdout.read().decode("utf-8"))
del ql
def test_elf_linux_armeb(self):
ql = Qiling(["../examples/rootfs/armeb_linux/bin/armeb_hello"], "../examples/rootfs/armeb_linux", verbose=QL_VERBOSE.DEBUG)
ql.run()
del ql
def test_elf_linux_armeb_static(self):
ql = Qiling(["../examples/rootfs/armeb_linux/bin/armeb_hello_static"], "../examples/rootfs/armeb_linux", verbose=QL_VERBOSE.DEFAULT)
ql.run()
del ql
# TODO: Disable for now
# def test_armoabi_eb_linux_syscall_elf_static(self):
# # src: https://github.com/qilingframework/qiling/blob/1f1e9bc756e59a0bfc112d32735f8882b1afc165/examples/src/linux/posix_syscall.c
# ql = Qiling(["../examples/rootfs/armeb_linux/bin/posix_syscall_msb.armoabi"], "../examples/rootfs/armeb_linux", verbose=QL_VERBOSE.DEBUG)
# ql.run()
def test_armoabi_le_linux_syscall_elf_static(self):
# src: https://github.com/qilingframework/qiling/blob/1f1e9bc756e59a0bfc112d32735f8882b1afc165/examples/src/linux/posix_syscall.c
ql = Qiling(["../examples/rootfs/arm_linux/bin/posix_syscall_lsb.armoabi"], "../examples/rootfs/arm_linux", verbose=QL_VERBOSE.DEBUG)
ql.run()
del ql
def test_elf_linux_x86_getdents64(self):
ql = Qiling(["../examples/rootfs/x86_linux/bin/x86_getdents64"], "../examples/rootfs/x86_linux", verbose=QL_VERBOSE.DEBUG)
ql.os.stdout = pipe.SimpleOutStream(1)
ql.run()
self.assertIn("bin\n", ql.os.stdout.read().decode("utf-8"))
del ql
# Regression for getdents64 record alignment. Each linux_dirent64 record
# must be padded so the next record's leading u64 d_ino stays 8-byte
# aligned; otherwise a strict-alignment guest (e.g. MIPS) faults with an
# unaligned load while walking the buffer. x86 tolerates the unaligned read,
# which is why it was never caught.
def test_linux_getdents64_alignment(self):
from qiling.const import QL_ENDIAN
from qiling.os.posix.syscall.fcntl import ql_syscall_open
from qiling.os.posix.syscall.unistd import ql_syscall_getdents64
ql = Qiling(code=b"\x00\x00\x00\x00", archtype=QL_ARCH.MIPS, ostype=QL_OS.LINUX,
endian=QL_ENDIAN.EB, rootfs="../examples/rootfs/mips32_linux",
verbose=QL_VERBOSE.OFF)
base = 0x100000
ql.mem.map(base, 0x4000)
path_ptr, buf_ptr = base, base + 0x100
ql.mem.write(path_ptr, b"/\x00")
fd = ql_syscall_open(ql, path_ptr, 0, 0) # O_RDONLY on a directory
self.assertGreaterEqual(fd, 0)
nbytes = ql_syscall_getdents64(ql, fd, buf_ptr, 0x2000)
self.assertGreater(nbytes, 0)
# walk the linux_dirent64 records; every record (hence its leading u64
# d_ino) must start at an 8-byte boundary. d_reclen is a u16 at offset 16.
buf = bytes(ql.mem.read(buf_ptr, nbytes))
off = 0
while off < nbytes:
self.assertEqual(off % 8, 0, f"dirent at offset {off} is not 8-byte aligned")
d_reclen = int.from_bytes(buf[off + 16:off + 18], "big")
self.assertNotEqual(d_reclen, 0)
off += d_reclen
del ql
def test_linux_getdents_alignment(self):
# the legacy getdents record was not rounded up to the alignment of its
# word-sized d_ino either, so a strict-alignment guest faulted walking
# the buffer. unlike getdents64, legacy linux_dirent keeps d_type in the
# record's last byte (offset d_reclen-1), so the padding must sit between
# d_name and d_type -- verify the records are aligned AND d_type survives.
from qiling.const import QL_ENDIAN
from qiling.os.posix.syscall.fcntl import ql_syscall_open
from qiling.os.posix.syscall.unistd import ql_syscall_getdents
ql = Qiling(code=b"\x00\x00\x00\x00", archtype=QL_ARCH.MIPS, ostype=QL_OS.LINUX,
endian=QL_ENDIAN.EB, rootfs="../examples/rootfs/mips32_linux",
verbose=QL_VERBOSE.OFF)
base = 0x100000
ql.mem.map(base, 0x4000)
path_ptr, buf_ptr = base, base + 0x100
ql.mem.write(path_ptr, b"/\x00")
fd = ql_syscall_open(ql, path_ptr, 0, 0) # O_RDONLY on a directory
self.assertGreaterEqual(fd, 0)
nbytes = ql_syscall_getdents(ql, fd, buf_ptr, 0x2000)
self.assertGreater(nbytes, 0)
# walk the linux_dirent records; on 32-bit MIPS d_ino is 4 bytes, so each
# record must start at a 4-byte boundary. d_reclen is a u16 at offset 8
# (after the 4-byte d_ino + 4-byte d_off); d_name starts at offset 10.
buf = bytes(ql.mem.read(buf_ptr, nbytes))
off = 0
while off < nbytes:
self.assertEqual(off % 4, 0, f"dirent at offset {off} is not 4-byte aligned")
d_reclen = int.from_bytes(buf[off + 8:off + 10], "big")
self.assertNotEqual(d_reclen, 0)
name = buf[off + 10:buf.index(b"\x00", off + 10)].decode()
# legacy getdents stores d_type in the record's last byte; '.' and
# '..' must still report DT_DIR (4) after the alignment padding
if name in (".", ".."):
self.assertEqual(buf[off + d_reclen - 1], 4, f"{name}: d_type not DT_DIR")
off += d_reclen
self.assertEqual(off, nbytes) # records tile the buffer exactly
del ql
def test_memory_search(self):
ql = Qiling(code=b"\xCC", archtype=QL_ARCH.X8664, ostype=QL_OS.LINUX, verbose=QL_VERBOSE.DEBUG)
ql.mem.map(0x1000, 0x1000)
ql.mem.map(0x2000, 0x1000)
ql.mem.map(0x3000, 0x1000)
ql.mem.write(0x1000, b"\x47\x06\x0d\x1e\x0d\x1a\x53\x0f\x07\x06\x06\x09\x53\x0f\x01\x1e\x0d\x53\x11\x07\x1d\x53\x1d\x18\x4f\x53\x06\x0d\x1e\x0d\x1a\x53\x0f\x07\x06\x06\x09\x53\x04\x0d\x1c\x53\x11\x07\x1d\x53\x0c\x07\x1f\x06\x45")
ql.mem.write(0x2000, b"\x47\x06\x0d\x1e\x0d\x1a\x53\x0f\x07\x06\x06\x09\x53\x1a\x1d\x06\x53\x09\x1a\x07\x1d\x06\x0c\x53\x09\x06\x0c\x53\x0c\x0d\x1b\x0d\x1a\x1c\x53\x11\x07\x1d\x4f\x53\x06\x0d\x1e\x0d\x1a\x53\x0f\x07\x06\x06\x09\x53\x05\x09\x03\x0d\x53\x11\x07\x1d\x53\x0b\x1a\x11\x45")
ql.mem.write(0x3000, b"\x47\x06\x0d\x1e\x0d\x1a\x53\x0f\x07\x06\x06\x09\x53\x1b\x09\x11\x53\x0f\x07\x07\x0c\x0a\x11\x0d\x4f\x53\x06\x0d\x1e\x0d\x1a\x53\x0f\x07\x06\x06\x09\x53\x1c\x0d\x04\x04\x53\x09\x53\x04\x01\x0d\x53\x09\x06\x0c\x53\x00\x1d\x1a\x1c\x53\x11\x07\x1d\x45")
ql.mem.write(0x1FFB, b"\x1f\x00\x07\x53\x03\x06\x07\x1f\x1b")
# Needle not in haystack
self.assertEqual([], ql.mem.search(b"\x3a\x01\x0b\x03\x53\x29\x1b\x1c\x04\x0d\x11"))
# Needle appears several times in haystack
self.assertEqual([0x1000 + 24, 0x2000 + 38, 0x3000 + 24], ql.mem.search(b"\x4f\x53\x06\x0d\x1e\x0d\x1a"))
# Needle inside haystack
self.assertEqual([0x1000 + 13], ql.mem.search(b"\x0f\x01\x1e\x0d\x53\x11\x07\x1d\x53\x1d\x18", begin=0x1000 + 10, end=0x1000 + 30))
# Needle before haystack
self.assertEqual([], ql.mem.search(b"\x04\x0d\x1c\x53\x11\x07\x1d\x53\x0c\x07\x1f\x06", begin=0x1337))
# Needle after haystack
self.assertEqual([], ql.mem.search(b"\x1b\x09\x11\x53\x0f\x07\x07\x0c\x0a\x11\x0d", end=0x3000 + 13))
# Needle exactly inside haystack
self.assertEqual([0x2000 + 13], ql.mem.search(b"\x1a\x1d\x06\x53\x09\x1a\x07\x1d\x06\x0c", begin=0x2000 + 13, end=0x2000 + 23))
# Needle 'tears' two mapped regions
self.assertEqual([], ql.mem.search(b"\x1f\x00\x07\x53\x03\x06\x07\x1f\x1b", begin=0x1F00, end=0x200F))
# Needle is a regex
self.assertEqual([0x1000 + 11, 0x2000 + 11, 0x3000 + 43], ql.mem.search(re.compile(b"\x09\x53(\x0f|\x1a|\x04)[^\x0d]")))
del ql
def test_elf_linux_x8664_path_traversion(self):
ql = Qiling(["../examples/rootfs/x8664_linux/bin/path_traverse_static"], "../examples/rootfs/x8664_linux", verbose=QL_VERBOSE.DEBUG)
ql.os.stdout = pipe.SimpleOutStream(1)
ql.run()
self.assertNotIn("root\n", ql.os.stdout.read().decode("utf-8"))
del ql
@unittest.skip("stdin hijacking doesn't work as expected")
def test_elf_linux_x8664_epoll_simple(self):
# This tests a sample binary that (e)polls on stdin and echos back the output. Upon
# receiving 'stop', it will exit.
#
# epoll-0 tkaen from: https://github.com/maxasm/epoll-c/blob/main/main.c
rootfs = "../examples/rootfs/x8664_linux"
argv = r"../examples/rootfs/x8664_linux/bin/x8664_linux_epoll_0".split()
ql = Qiling(argv, rootfs, verbose=QL_VERBOSE.DEBUG)
ql.os.stdin = pipe.SimpleInStream(0)
ql.os.stdin.write(b'echo\n')
ql.os.stdin.write(b'stop\n') # signal to exit gracefully
ql.run()
self.assertIn(b'echo\n', ql.os.stdout.read())
del ql
def test_elf_linux_x8664_epoll_server(self):
# This tests a simple server that uses epoll to wait for data, then prints it out. It has
# been modified to exit after data has been received; instead of a typical server operation
# that reads requests indefinitely.
#
# It listens on port 8000, and a separate thread is spawned in order to test how the server
# handles a 'hello world' input. The server prints out whatever it receives, so the assert
# statement checks the input is present as expected.
#
# onestraw server taken from: https://github.com/onestraw/epoll-example
# Note: Without a hook for this syscall, this error fires:
# TypeError: stat: path should be string, bytes, os.PathLike or integer, not NoneType
def hook_newfstatat(ql: Qiling, dirfd: int, pathname: int, statbuf: int, flags: int):
return 0
def client():
# give time for the server to listen
time.sleep(3)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("127.0.0.1", 8000))
s.send(b"hello world")
s.close()
rootfs = "../examples/rootfs/x8664_linux"
argv = r"../examples/rootfs/x8664_linux/bin/x8664_onestraw_server s".split() # s means 'server mode'
ql = Qiling(argv, rootfs, verbose=QL_VERBOSE.DEBUG)
ql.os.set_syscall("newfstatat", hook_newfstatat, QL_INTERCEPT.CALL)
ql.os.stdout = pipe.SimpleOutStream(1) # server prints data received to stdout
ql.filter = '^data:'
client_thread = threading.Thread(target=client, daemon=True)
client_thread.start()
ql.run()
self.assertIn(b'hello world', ql.os.stdout.read(200)) # 200 is arbitrary--"good enough" for this task
del ql
if __name__ == "__main__":
unittest.main()
You can’t perform that action at this time.
