name); } void menu() { printf("1. Human\n"); printf("2. Robot\n"); printf("3. Custom\n"); printf("> "); } void human_func() {//malloc->멤버변수받기->free하는 함수 int sel; human = (struct Human *)malloc(sizeof(struct Human)); //malloc strcpy(human->name, "Human"); //human->name="Human" printf("Human Weight: "); scanf("%d", &human->weight); //weight 사용자 입력 printf("Human Age: "); scanf("%ld", &human->age); //age 사용자 입력 free(human); //free } void robot_func() { //malloc->멤버"> name); } void menu() { printf("1. Human\n"); printf("2. Robot\n"); printf("3. Custom\n"); printf("> "); } void human_func() {//malloc->멤버변수받기->free하는 함수 int sel; human = (struct Human *)malloc(sizeof(struct Human)); //malloc strcpy(human->name, "Human"); //human->name="Human" printf("Human Weight: "); scanf("%d", &human->weight); //weight 사용자 입력 printf("Human Age: "); scanf("%ld", &human->age); //age 사용자 입력 free(human); //free } void robot_func() { //malloc->멤버"> name); } void menu() { printf("1. Human\n"); printf("2. Robot\n"); printf("3. Custom\n"); printf("> "); } void human_func() {//malloc->멤버변수받기->free하는 함수 int sel; human = (struct Human *)malloc(sizeof(struct Human)); //malloc strcpy(human->name, "Human"); //human->name="Human" printf("Human Weight: "); scanf("%d", &human->weight); //weight 사용자 입력 printf("Human Age: "); scanf("%ld", &human->age); //age 사용자 입력 free(human); //free } void robot_func() { //malloc->멤버">
// Name: uaf_overwrite.c
// Compile: gcc -o uaf_overwrite uaf_overwrite.c

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

struct Human {
  char name[16];
  int weight;
  long age;
};

struct Robot {
  char name[16];
  int weight;
  void (*fptr)();
};
//Human 구조체와 Robot 구조체 크기 동일

//전역변수 human,romot,custom,c_idx (BSS에 저장)
struct Human *human;
struct Robot *robot;
char *custom[10]; //포인터 배열
int c_idx;

//로봇 이름 출력하는 함수
void print_name() { printf("Name: %s\\n", robot->name); }

void menu() {
  printf("1. Human\\n");
  printf("2. Robot\\n");
  printf("3. Custom\\n");
  printf("> ");
}

void human_func() {//malloc->멤버변수받기->free하는 함수
  int sel;
  human = (struct Human *)malloc(sizeof(struct Human)); //malloc

  strcpy(human->name, "Human"); //human->name="Human"

  printf("Human Weight: "); 
  scanf("%d", &human->weight); //weight 사용자 입력

  printf("Human Age: ");
  scanf("%ld", &human->age); //age 사용자 입력

  free(human); //free
}

void robot_func() { //malloc->멤버 변수 입력->fptr 실행->free
  int sel;
  robot = (struct Robot *)malloc(sizeof(struct Robot)); //malloc

  strcpy(robot->name, "Robot"); //robot->name="Robot"
  printf("Robot Weight: ");
  scanf("%d", &robot->weight); //weight 사용자 입력

  if (robot->fptr) //fptr가 NULL이 아니면 fptr 가리키는 함수 넣음
    robot->fptr();
  else
    robot->fptr = print_name; //fptr가 NULL이면 fptr에 print_name(로봇이름 출력함수) 대입
  
	robot->fptr(robot); //fptr(robot) 실행
  free(robot); //free
}

int custom_func() {
  unsigned int size;
  unsigned int idx;

  if (c_idx > 9) {
    printf("Custom FULL!!\\n");
    return 0;
  }

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

  if (size >= 0x100) { //256byte이상일 때
    custom[c_idx] = malloc(size); 
    printf("Data: ");
    read(0, custom[c_idx], size - 1); //size-1만큼 custom[c_idx]에 stdin에서 쓰기
    printf("Data: %s\\n", custom[c_idx]); //데이터출력
    printf("Free idx: ");
    scanf("%d", &idx); //idx 입력받기

    if (idx < 10 && custom[idx]) { 
			//custom[idx] != NULL =>할당받았는데 아직 free아닌애들
      free(custom[idx]);
      custom[idx] = NULL; //포인터 초기
    }
  }
  c_idx++; //총 10번할 수 있는 듯 
}

int main() {
  int idx;
  char *ptr;
  
  setvbuf(stdin, 0, 2, 0);
  setvbuf(stdout, 0, 2, 0);
  
  while (1) {
    menu();
    scanf("%d", &idx);

    switch (idx) {
      case 1:
        human_func();
        break;
      case 2:
        robot_func();
        break;
      case 3:
        custom_func();
        break;
    }
  }
}

목표 : 셸 획득 ← overwrite필요, 실행권한 필요

human_func() 호출 후 age 부분에 one_gadget 삽입 + robot->fptr(); 이용

robot->fptr(); 에 셸 실행함수 넣기

<aside> 💡 **원가젯은 라이브러리 내에 매핑되어있음!!!!!!!**→ 라이브러리 주소 필요

</aside>

청크가 해제되었을 때 fastbin에 들어가가지 않는 모든 청크들은 ‘크기 구분하지 않고!!!’ unsorted bin에 저장된다