现在我们将构建启用 ASAN 的 tcpdump(和 libpcap),首先,我们要清理所有以前编译的目标文件和可执行文件:
1 2 3 4 5
cd $HOME/libpcap-1.8.0/ make clean
cd $HOME/tcpdump-tcpdump-4.9.2/ make clean
现在,我们在调用“configure”和“make”之前设置“AFL_USE_ASAN=1”:
1 2 3 4 5 6 7 8 9 10
cd $HOME/libpcap-1.8.0/ export LLVM_CONFIG="llvm-config-11" CC=afl-clang-lto ./configure --enable-shared=no --prefix="$HOME/fuzzing_tcpdump/install/" AFL_USE_ASAN=1 make AFL_USE_ASAN=1 make install
cd $HOME/tcpdump-tcpdump-4.9.2/ AFL_USE_ASAN=1 CC=afl-clang-lto ./configure --prefix="$HOME/fuzzing_tcpdump/install/" AFL_USE_ASAN=1 make AFL_USE_ASAN=1 make install
SMB PACKET: SMBtrans (REQUEST) SMB Command = 0x25 Error class = 0x0 Error code = 0 (0x0) Flags1 = 0x0 Flags2 = 0x0 Tree ID = 0 (0x0) Proc ID = 0 (0x0) UID = 0 (0x0) MID = 0 (0x0) Word Count = 17 (0x11) TotParamCnt=0 (0x0) TotDataCnt=11 (0xb) MaxParmCnt=0 (0x0) MaxDataCnt=0 (0x0) MaxSCnt=0 (0x0) TransFlags=0x0 Res1=0x3E9 Res2=0x0 Res3=0x0 ParamCnt=0 (0x0) ParamOff=0 (0x0) DataCnt=11 (0xb) DataOff=86 (0x56) SUCnt=3 (0x3) Data: (6 bytes) [000] 010001000200 \0x01\0x00\0x01\0x00\0x02\0x00 smb_bcc=28 ================================================================= ==2727==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x611000000107 at pc 0x0000003d0235 bp 0x7ffc22dc8210 sp 0x7ffc22dc79b8/* 对应代码的位置 */ READ of size 7 at 0x611000000107 thread T0 /* heap中的溢出点 */ #00x3d0234 in strcmp (/home/yhellow/fuzzing_tcpdump/install/sbin/tcpdump+0x3d0234) #10x712469 in print_trans /home/yhellow/tcpdump-tcpdump-4.9.2/./print-smb.c:375:6 #20x713f6a in print_smb /home/yhellow/tcpdump-tcpdump-4.9.2/./print-smb.c:863:6 #30x6ec5e1 in nbt_udp138_print /home/yhellow/tcpdump-tcpdump-4.9.2/./print-smb.c:1307:6 #40x6ec5e1 in udp_print /home/yhellow/tcpdump-tcpdump-4.9.2/./print-udp.c:608:4 #50x55dfe8 in ip_print_demux /home/yhellow/tcpdump-tcpdump-4.9.2/./print-ip.c:402:3 #60x5616e5 in ip_print /home/yhellow/tcpdump-tcpdump-4.9.2/./print-ip.c:673:3 #70x51ac27 in ethertype_print /home/yhellow/tcpdump-tcpdump-4.9.2/./print-ether.c:333:10 #80x51996a in ether_print /home/yhellow/tcpdump-tcpdump-4.9.2/./print-ether.c:236:7 #90x4791ab in pretty_print_packet /home/yhellow/tcpdump-tcpdump-4.9.2/./print.c:332:18 #100x4791ab in print_packet /home/yhellow/tcpdump-tcpdump-4.9.2/./tcpdump.c:2497:2 #110x83da5d in pcap_offline_read /home/yhellow/libpcap-1.8.0/./savefile.c:507:4 #120x470c0c in pcap_loop /home/yhellow/libpcap-1.8.0/./pcap.c:875:8 #130x470c0c in main /home/yhellow/tcpdump-tcpdump-4.9.2/./tcpdump.c:2000:12 #140x7fa3263bb082 in __libc_start_main /build/glibc-SzIz7B/glibc-2.31/csu/../csu/libc-start.c:308:16 #150x3bcedd in _start (/home/yhellow/fuzzing_tcpdump/install/sbin/tcpdump+0x3bcedd)
0x611000000107 is located 0 bytes to the right of 199-byte region [0x611000000040,0x611000000107) allocated by thread T0 here: #00x436f4d in malloc (/home/yhellow/fuzzing_tcpdump/install/sbin/tcpdump+0x436f4d) #10x83efd8 in pcap_check_header /home/yhellow/libpcap-1.8.0/./sf-pcap.c:401:14 #20x83ce1d in pcap_fopen_offline_with_tstamp_precision /home/yhellow/libpcap-1.8.0/./savefile.c:380:7 #30x83cb48 in pcap_open_offline_with_tstamp_precision /home/yhellow/libpcap-1.8.0/./savefile.c:287:6
SUMMARY: AddressSanitizer: heap-buffer-overflow (/home/yhellow/fuzzing_tcpdump/install/sbin/tcpdump+0x3d0234) in strcmp Shadow bytes around the buggy address: 0x0c227fff7fd0: 00000000000000000000000000000000 0x0c227fff7fe0: 00000000000000000000000000000000 0x0c227fff7ff0: 00000000000000000000000000000000 0x0c227fff8000: fa fa fa fa fa fa fa fa 0000000000000000 0x0c227fff8010: 00000000000000000000000000000000 =>0x0c227fff8020:[07]fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c227fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c227fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c227fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c227fff8060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c227fff8070: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa Shadow byte legend (one shadow byte represents 8 application bytes): Addressable: 00 Partially addressable: 01020304050607 Heap left redzone: fa Freed heap region: fd Stack left redzone: f1 Stack mid redzone: f2 Stack right redzone: f3 Stack after return: f5 Stack use after scope: f8 Global redzone: f9 Global init order: f6 Poisoned by user: f7 Container overflow: fc Array cookie: ac Intra object redzone: bb ASan internal: fe Left alloca redzone: ca Right alloca redzone: cb Shadow gap: cc ==2727==ABORTING
pwndbg> ni ================================================================= ==5418==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x611000000107 at pc 0x0000003d0235 bp 0x7fffffffbb30 sp 0x7fffffffb2d8 READ of size 7 at 0x611000000107 thread T0 [Attaching after Thread 0x7ffff7947800 (LWP 5418) fork to child process 5422] [New inferior 2 (process 5422)] [Detaching after fork from parent process 5418] [Inferior 1 (process 5418) detached] [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". process 5422 is executing new program: /usr/lib/llvm-11/bin/llvm-symbolizer Warning: Cannot insert breakpoint 1. Cannot access memory at address 0x712465
我第一时间不清楚 ASan 给出的报错信息和程序漏洞之间的关系,所以跟进 strcmp 继续调试
1
► 0x3d0252 <strcmp+482> call 0x43c5c0 <0x43c5c0>
一直跟进到 ASan 记录的地址:0x3d0235(我发现这个 strcmp 的地址有点怪,好像不在 libc 里面)
shadow memory 也是内存中的一块区域,但与 main memory 又不同,shadow memory 有中元数据的思想,其中的数据放映的是 main memory 的状态信息,因此,可以将 shadow memory 看做是 main memory 的元数据,而 main memory 中存储的才是程序真正的数据
/** * Copyright (c) 2021 junfu0903@aliyun.com. * * Unpublished copyright. All rights reserved. This material contains * proprietary information that should be used or copied only within * junfu0903@aliyun.com, except with written permission of junfu0903@aliyun.com. * * @file heap_buffer_overflow.c * @brief * @author junfu0903@aliyun.com * @version 1.0.0 * @date 2021-06-15 10:18:45 */
==6154==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x602000000021 at pc 0x00000040121b bp 0x7fff11080090 sp 0x7fff11080080/* 对应代码的位置 */ WRITE of size 1 at 0x602000000021 thread T0 /* heap中的溢出点 */ #00x40121a in main /home/yhellow/桌面/exp/shadow test/test.c:22 #10x7f9992b66082 in __libc_start_main ../csu/libc-start.c:308 #20x4010fd in _start (/home/yhellow/桌面/exp/shadow test/test+0x4010fd)
0x602000000021 is located 1 bytes to the right of 16-byte region [0x602000000010,0x602000000020) allocated by thread T0 here: #00x7f9992e41808 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cc:144 #10x4011db in main /home/yhellow/桌面/exp/shadow test/test.c:21 #20x7f9992b66082 in __libc_start_main ../csu/libc-start.c:308
SUMMARY: AddressSanitizer: heap-buffer-overflow /home/yhellow/桌面/exp/shadow test/test.c:22 in main Shadow bytes around the buggy address: 0x0c047fff7fb0: 00000000000000000000000000000000 0x0c047fff7fc0: 00000000000000000000000000000000 0x0c047fff7fd0: 00000000000000000000000000000000 0x0c047fff7fe0: 00000000000000000000000000000000 0x0c047fff7ff0: 00000000000000000000000000000000 =>0x0c047fff8000: fa fa 0000[fa]fa fa fa fa fa fa fa fa fa fa fa 0x0c047fff8010: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c047fff8020: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c047fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c047fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c047fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa Shadow byte legend (one shadow byte represents 8 application bytes): Addressable: 00 Partially addressable: 01020304050607 Heap left redzone: fa Freed heap region: fd Stack left redzone: f1 Stack mid redzone: f2 Stack right redzone: f3 Stack after return: f5 Stack use after scope: f8 Global redzone: f9 Global init order: f6 Poisoned by user: f7 Container overflow: fc Array cookie: ac Intra object redzone: bb ASan internal: fe Left alloca redzone: ca Right alloca redzone: cb Shadow gap: cc ==6154==ABORTING