본문 바로가기

Heap 2번 본문

wargame/Protostar

Heap 2번

Seongjun_You 2021. 11. 27. 18:41
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <stdio.h>

struct auth {
  char name[32];
  int auth;
};
struct auth *auth;
char *service;

int main(int argc, char **argv)
{
  char line[128];
  while(1) {
      printf("[ auth = %p, service = %p ]\n", auth, service);
      if(fgets(line, sizeof(line), stdin) == NULL) break;
      if(strncmp(line, "auth ", 5) == 0) {
          auth = malloc(sizeof(auth));
          memset(auth, 0, sizeof(auth));
          if(strlen(line + 5) < 31) {
              strcpy(auth->name, line + 5);
          }
      }
      if(strncmp(line, "reset", 5) == 0) {
          free(auth);
      }
      if(strncmp(line, "service", 6) == 0) {
          service = strdup(line + 7);
      }
      if(strncmp(line, "login", 5) == 0) {
          if(auth->auth) {
              printf("you have logged in already!\n");
          } else {
              printf("please enter your password\n");
          }
      }
  }
}

UAF 취약점을 이용한다.

할당된 힙 영역을 free 하면 데이터가 완전히 사라지지 않으며,

다음에 같은 크기가 동적할당되면 같은 자리에 들어온다.

 

사실 heap overflow로도 간단하게 풀 수 있는데 문제 취지랑 맞지 않아 정석대로 풀기로 한다.

 

 

auth->auth에 값을 넣어야 한다.

구조체 auth를 free하고 service를 동적 할당받는다.

그럼 auth->auth를 덮을 수 있다.

 

auth -> reset -> service -> login 순서로 실행했다.

'wargame > Protostar' 카테고리의 다른 글

NET 0번  (0) 2021.11.28
Heap 3번  (0) 2021.11.28
Heap 1번  (0) 2021.11.27
Heap 0번  (0) 2021.11.27
Format 4번  (0) 2021.11.25
Comments