- 简介
- 一、基础知识篇
- 二、工具篇
- 三、分类专题篇
- 四、技巧篇
- 五、高级篇
- 六、题解篇
- 6.1 Pwn
- 6.1.1 pwn HCTF2016 brop
- 6.1.2 pwn NJCTF2017 pingme
- 6.1.3 pwn XDCTF2015 pwn200
- 6.1.4 pwn BackdoorCTF2017 Fun-Signals
- 6.1.5 pwn GreHackCTF2017 beerfighter
- 6.1.6 pwn DefconCTF2015 fuckup
- 6.1.7 pwn 0CTF2015 freenote
- 6.1.8 pwn DCTF2017 Flex
- 6.1.9 pwn RHme3 Exploitation
- 6.1.10 pwn 0CTF2017 BabyHeap2017
- 6.1.11 pwn 9447CTF2015 Search-Engine
- 6.1.12 pwn N1CTF2018 vote
- 6.1.13 pwn 34C3CTF2017 readme_revenge
- 6.1.14 pwn 32C3CTF2015 readme
- 6.1.15 pwn 34C3CTF2017 SimpleGC
- 6.1.16 pwn HITBCTF2017 1000levels
- 6.1.17 pwn SECCONCTF2016 jmper
- 6.1.18 pwn HITBCTF2017 Sentosa
- 6.1.19 pwn HITBCTF2018 gundam
- 6.1.20 pwn 33C3CTF2016 babyfengshui
- 6.1.21 pwn HITCONCTF2016 Secret_Holder
- 6.1.22 pwn HITCONCTF2016 Sleepy_Holder
- 6.1.23 pwn BCTF2016 bcloud
- 6.1.24 pwn HITCONCTF2016 HouseofOrange
- 6.1.25 pwn HCTF2017 babyprintf
- 6.1.26 pwn 34C3CTF2017 300
- 6.1.27 pwn SECCONCTF2016 tinypad
- 6.1.28 pwn ASISCTF2016 b00ks
- 6.1.29 pwn Insomni'hackteaserCTF2017 TheGreatEscapepart-3
- 6.1.30 pwn HITCONCTF2017 Ghostinthe_heap
- 6.1.31 pwn HITBCTF2018 mutepig
- 6.1.32 pwn SECCONCTF2017 vmnofun
- 6.1.33 pwn 34C3CTF2017 LFA
- 6.1.34 pwn N1CTF2018 memsafety
- 6.1.35 pwn 0CTF2018 heapstorm2
- 6.1.36 pwn NJCTF2017 messager
- 6.1.37 pwn sixstarctf2018 babystack
- 6.1.38 pwn HITCONCMT2017 pwn200
- 6.1.39 pwn BCTF2018 houseofAtum
- 6.1.40 pwn LCTF2016 pwn200
- 6.1.41 pwn PlaidCTF2015 PlaidDB
- 6.1.42 pwn hacklu2015 bookstore
- 6.1.43 pwn 0CTF2018 babyheap
- 6.1.44 pwn ASIS2017 start_hard
- 6.1.45 pwn LCTF2016 pwn100
- 6.2 Reverse
- 6.3 Web
- 6.1 Pwn
- 七、实战篇
- 7.1 CVE
- 7.1.1 CVE-2017-11543 tcpdump sliplink_print 栈溢出漏洞
- 7.1.2 CVE-2015-0235 glibc _nsshostnamedigitsdots 堆溢出漏洞
- 7.1.3 CVE-2016-4971 wget 任意文件上传漏洞
- 7.1.4 CVE-2017-13089 wget skipshortbody 栈溢出漏洞
- 7.1.5 CVE–2018-1000001 glibc realpath 缓冲区下溢漏洞
- 7.1.6 CVE-2017-9430 DNSTracer 栈溢出漏洞
- 7.1.7 CVE-2018-6323 GNU binutils elfobjectp 整型溢出漏洞
- 7.1.8 CVE-2010-2883 Adobe CoolType SING 表栈溢出漏洞
- 7.1.9 CVE-2010-3333 Microsoft Word RTF pFragments 栈溢出漏洞
- 7.1 CVE
- 八、学术篇
- 8.1 The Geometry of Innocent Flesh on the Bone: Return-into-libc without Function Calls (on the x86)
- 8.2 Return-Oriented Programming without Returns
- 8.3 Return-Oriented Rootkits: Bypassing Kernel Code Integrity Protection Mechanisms
- 8.4 ROPdefender: A Detection Tool to Defend Against Return-Oriented Programming Attacks
- 8.5 Data-Oriented Programming: On the Expressiveness of Non-Control Data Attacks
- 8.7 What Cannot Be Read, Cannot Be Leveraged? Revisiting Assumptions of JIT-ROP Defenses
- 8.9 Symbolic Execution for Software Testing: Three Decades Later
- 8.10 AEG: Automatic Exploit Generation
- 8.11 Address Space Layout Permutation (ASLP): Towards Fine-Grained Randomization of Commodity Software
- 8.13 New Frontiers of Reverse Engineering
- 8.14 Who Allocated My Memory? Detecting Custom Memory Allocators in C Binaries
- 8.21 Micro-Virtualization Memory Tracing to Detect and Prevent Spraying Attacks
- 8.22 Practical Memory Checking With Dr. Memory
- 8.23 Evaluating the Effectiveness of Current Anti-ROP Defenses
- 8.24 How to Make ASLR Win the Clone Wars: Runtime Re-Randomization
- 8.25 (State of) The Art of War: Offensive Techniques in Binary Analysis
- 8.26 Driller: Augmenting Fuzzing Through Selective Symbolic Execution
- 8.27 Firmalice - Automatic Detection of Authentication Bypass Vulnerabilities in Binary Firmware
- 8.28 Cross-Architecture Bug Search in Binary Executables
- 8.29 Dynamic Hooks: Hiding Control Flow Changes within Non-Control Data
- 8.30 Preventing brute force attacks against stack canary protection on networking servers
- 8.33 Under-Constrained Symbolic Execution: Correctness Checking for Real Code
- 8.34 Enhancing Symbolic Execution with Veritesting
- 8.38 TaintEraser: Protecting Sensitive Data Leaks Using Application-Level Taint Tracking
- 8.39 DART: Directed Automated Random Testing
- 8.40 EXE: Automatically Generating Inputs of Death
- 8.41 IntPatch: Automatically Fix Integer-Overflow-to-Buffer-Overflow Vulnerability at Compile-Time
- 8.42 Dynamic Taint Analysis for Automatic Detection, Analysis, and Signature Generation of Exploits on Commodity Software
- 8.43 DTA++: Dynamic Taint Analysis with Targeted Control-Flow Propagation
- 8.44 Superset Disassembly: Statically Rewriting x86 Binaries Without Heuristics
- 8.45 Ramblr: Making Reassembly Great Again
- 8.46 FreeGuard: A Faster Secure Heap Allocator
- 8.48 Reassembleable Disassembling
- 九、附录
4.3 GCC 编译参数解析
GCC
$ wget -c http://www.mirrorservice.org/sites/sourceware.org/pub/gcc/releases/gcc-4.4.0/gcc-4.4.0.tar.bz2
$ tar -xjvf gcc-4.4.0.tar.bz2
$ ./configure
$ make && sudo make install
常用选项
使用 gcc -v
可以查看默认开启的选项:
$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.4.0-6ubuntu1~16.04.9' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.9)
控制标准版本的编译选项
-ansi
:告诉编译器遵守 C 语言的 ISO C90 标准。-std=
:通过使用一个参数来设置需要的标准。c89
:支持 C89 标准。iso9899:1999
:支持 ISO C90 标准。gnu89
:支持 C89 标准。
控制标准版本的常量
这些常量(#define)可以通过编译器的命令行选项来设置,或者通过源代码总的 #define
语句来定义。
__STRICT_ANSI__
:强制使用 C 语言的 ISO 标准。这个常量通过命令行选项-ansi
来定义。_POSIX_C_SOURCE=2
:启用由 IEEE Std1003.1 和 1003.2 标准定义的特性。_BSD_SOURCE
:启用 BSD 类型的特性。_GNU_SOURCE
:启用大量特性,其中包括 GNU 扩展。
编译器的警告选项
-pedantic
:除了启用用于检查代码是否遵守 C 语言标准的选项外,还关闭了一些不被标准允许的传统 C 语言结构,并且禁用所有的 GNU 扩展。-Wformat
:检查 printf 系列函数所使用的参数类型是否正确。Wparentheses
:检查是否总是提供了需要的圆括号。当想要检查一个复杂结构的初始化是否按照预期进行时,这个选项就很有用。Wswitch-default
:检查是否所有的 switch 语句都包含一个 default case。Wunused
:检查诸如声明静态函数但没有定义、未使用的参数和丢弃返回结果等情况。Wall
:启用绝大多数 gcc 的警告选项,包括所有以 -W 为前缀的选项。
Address sanitizer
Address sanitizer 是一种用于检测内存错误的技术,GCC 从 4.8 版本开始支持了这一技术。ASan 在编译时插入额外指令到内存访问操作中,同时通过 Shadow memory 来记录和检测内存的有效性。ASan 其实只是 Sanitizer 一系列工具中的一员,其他工具比如 memory leak 检测在 LeakSanitizer 中,uninitialized memory read 检测在 MemorySanitizer 中等等。
举个例子,很明显下面这个程序存在栈溢出:
#include<stdio.h>
void main() {
int a[10] = {0};
int b = a[11];
}
编译时加上参数 -fsanitize=address
,如果使用 Makefile,则将参数加入到 CFLAGS 中:
$ gcc -fsanitize=address santest.c
然后运行:
$ ./a.out
=================================================================
==9399==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffc03f4d64c at pc 0x565515082ad6 bp 0x7ffc03f4d5e0 sp 0x7ffc03f4d5d0
READ of size 4 at 0x7ffc03f4d64c thread T0
#0 0x565515082ad5 in main (/home/firmy/a.out+0xad5)
#1 0x7fb4c04c0f69 in __libc_start_main (/usr/lib/libc.so.6+0x20f69)
#2 0x565515082899 in _start (/home/firmy/a.out+0x899)
Address 0x7ffc03f4d64c is located in stack of thread T0 at offset 76 in frame
#0 0x565515082989 in main (/home/firmy/a.out+0x989)
This frame has 1 object(s):
[32, 72) 'a' <== Memory access at offset 76 overflows this variable
HINT: this may be a false positive if your program uses some custom stack unwind mechanism or swapcontext
(longjmp and C++ exceptions *are* supported)
SUMMARY: AddressSanitizer: stack-buffer-overflow (/home/firmy/a.out+0xad5) in main
Shadow bytes around the buggy address:
0x1000007e1a70: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x1000007e1a80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x1000007e1a90: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x1000007e1aa0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x1000007e1ab0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x1000007e1ac0: f1 f1 f1 f1 00 00 00 00 00[f2]f2 f2 00 00 00 00
0x1000007e1ad0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x1000007e1ae0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x1000007e1af0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x1000007e1b00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x1000007e1b10: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Shadow byte legend (one shadow byte represents 8 application bytes):
Addressable: 00
Partially addressable: 01 02 03 04 05 06 07
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
==9399==ABORTING
确实检测出了问题。在实战篇中,为了更好地分析软件漏洞,我们可能会经常用到这个选项。
参考:https://en.wikipedia.org/wiki/AddressSanitizer
mcheck
利用 mcheck 可以实现堆内存的一致性状态检查。其定义在 /usr/include/mcheck.h
,是一个 GNU 扩展函数,原型如下:
#include <mcheck.h>
int mcheck(void (*abortfunc)(enum mcheck_status mstatus));
可以看到参数是一个函数指针,但检查到堆内存异常时,通过该指针调用 abortfunc 函数,同时传入一个 mcheck_status 类型的参数。
举个例子,下面的程序存在 double-free 的问题:
#include <stdlib.h>
#include <stdio.h>
void main() {
char *p;
p = malloc(1000);
fprintf(stderr, "About to free\n");
free(p);
fprintf(stderr, "About to free a second time\n");
free(p);
fprintf(stderr, "Finish\n");
}
通过设置参数 -lmcheck
来链接 mcheck 函数:
$ gcc -lmcheck t_mcheck.c
$ ./a.out
About to free
About to free a second time
block freed twice
Aborted (core dumped)
还可以通过设置环境变量 MALLOC_CHECK_
来实现,这样就不需要重新编译程序。
$ gcc mcheck.c
$ #检查到错误时不作任何提示
$ MALLOC_CHECK_=0 ./a.out
About to free
About to free a second time
Finish
$ #检查到错误时打印一条信息到标准输出
$ MALLOC_CHECK_=1 ./a.out
About to free
About to free a second time
*** Error in `./a.out': free(): invalid pointer: 0x0000000001fb9010 ***
Finish
$ #检查到错误时直接中止程序
$ MALLOC_CHECK_=2 ./a.out
About to free
About to free a second time
Aborted (core dumped)
具体参考 man 3 mcheck
和 man 3 mallopt
。
glibc 还提供了 mtrace()
和 muntrace()
函数分别在程序中打开和关闭对内存分配调用进行跟踪的功能。这些函数需要与环境变量 MALLOC_TRACE
配合使用,该变量定义了写入跟踪信息的文件名。在被调用时,mtrace()
会检查是否定义了该文件,又是否可以读写该文件。如果一切正常,那么会在文件里跟踪和记录所有对 malloc 系列函数的调用。由于生成的文件不易于理解,还提供了脚本(mtrace
)用于分析文件,并生成易于理解的汇总报告。
将上面的例子修改一下:
#include <stdlib.h>
#include <stdio.h>
#include <mcheck.h>
void main() {
char *p;
mtrace();
calloc(16, 16);
fprintf(stderr, "calloc some chunks that will not be freed\n");
p = malloc(1000);
fprintf(stderr, "About to free\n");
free(p);
fprintf(stderr, "About to free a second time\n");
free(p);
fprintf(stderr, "Finish\n");
muntrace();
}
$ gcc t_mtrace.c
$ export MALLOC_TRACE=/tmp/t
$ ./a.out
calloc some chunks that will not be freed
About to free
About to free a second time
Finish
$ mtrace /tmp/t
- 0x000055e427cde7b0 Free 5 was never alloc'd 0x55e425da287c
Memory not freed:
-----------------
Address Size Caller
0x000055e427cde6a0 0x100 at 0x55e425da27f6
于是 double-free 和内存泄漏被检测出来了。
参考资料
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论