总结一下CSAPP第三版的各个lab。
这里介绍的是Attack lab,主要考察code-injection、return-oriented-programming攻击的理解,和gdb,objdump的简单使用。
首先登陆网站http://csapp.cs.cmu.edu/3e/labs.html。Windows下点击Self-Study Handout获取压缩包。我的工作环境是ubuntu,64位操作系统,使用wget指令直接下载。(chrome点击F12选取元素获取下载地址)tar解压。
请务必阅读一下http://csapp.cs.cmu.edu/3e/attacklab.pdf的官方说明。
这个实验主要是针对第三版的二三章,关于缓冲区溢出攻击的练习。与第二版不同之处在于增加了ROP(Return-Oriented Programming)的攻击手段。单纯的缓冲区溢出攻击容易受到堆栈随机化,金丝雀防护的制约。而ROP攻击利用程序中已有的汇编代码片段组合出需要执行的指令。推荐http://drops.wooyun.org/tips/3071这篇文章,进一步讲述了Blind Return Oriented Programming (BROP) Attack。看完之后如果感觉理解有困难,可以浏览一遍http://www.scs.stanford.edu/brop/bittau-brop-slides.pdf。
这里假定已经大致浏览过说明,对各个文件的内容有了解。
简要 说明一下:
- cookie.txt:存放你攻击用的标识符
- rtarget:执行return-oriented-programming攻击的程序
- ctarget:执行code-injection攻击的程序
- farm.c:“gadget farm产生代码片段用的
- hex2raw:生成攻击字符串用的
实验分为5个部分。
Phase 1
首先反汇编可执行程序,生成汇编代码。
objdump -d ctarget > ctarget.d
这一关的任务是从test函数跳转到touch1,。
test如下:
void test() {
int val;
val = getbuf();
printf("No exploit. Getbuf returned 0x%x\n", val);
}
汇编如下:
test:
0000000000401968 <test>:
401968: 48 83 ec 08 sub $0x8,%rsp
40196c: b8 00 00 00 00 mov $0x0,%eax
401971: e8 32 fe ff ff callq 4017a8 <getbuf>
401976: 89 c2 mov %eax,%edx
401978: be 88 31 40 00 mov $0x403188,%esi
40197d: bf 01 00 00 00 mov $0x1,%edi
401982: b8 00 00 00 00 mov $0x0,%eax
401987: e8 64 f4 ff ff callq 400df0 <__printf_chk@plt>
40198c: 48 83 c4 08 add $0x8,%rsp
这里根据提示直接溢出覆盖getbuf的缓冲区,填写touch1的地址空间即可。
getbuf:
00000000004017a8 <getbuf>:
4017a8: 48 83 ec 28 sub $0x28,%rsp
4017ac: 48 89 e7 mov %rsp,%rdi
4017af: e8 8c 02 00 00 callq 401a40 <Gets>
4017b4: b8 01 00 00 00 mov $0x1,%eax
4017b9: 48 83 c4 28 add $0x28,%rsp
4017bd: c3 retq
4017be: 90 nop
4017bf: 90 nop
touch1:
00000000004017c0 <touch1>:
4017c0: 48 83 ec 08 sub $0x8,%rsp
4017c4: c7 05 0e 2d 20 00 01 movl $0x1,0x202d0e(%rip) # 6044dc <vlevel>
4017cb: 00 00 00
4017ce: bf c5 30